An open API service indexing awesome lists of open source software.

https://github.com/nyaomaru/divider

Divide string or string[]
https://github.com/nyaomaru/divider

array-split array-splitter javascript lightweight nodejs npm-package open-source string-manipulation string-split string-splitter typescript utility-library

Last synced: 3 months ago
JSON representation

Divide string or string[]

Awesome Lists containing this project

README

        

# Divider


Divider logo



npm version


License


npm downloads


Build Status

A simple utility to divide a `string` or `string[]` based on given indexes or delimiters.

## ๐Ÿš€ Installation

```sh
pnpm install @nyaomaru/divider
```

## ๐Ÿ“– Usage

### Basic Examples

```ts
import { divider } from '@nyaomaru/divider';

// Divide a string by index positions
const helloArray = divider('hello', 1, 3);
// ['h', 'el', 'lo']

const [hello1, hello2, ...restHello] = divider('hello', 1, 3, 4);
// hello1 = 'h'
// hello2 = 'el'
// restHello = ['l', 'o']

// Divide a string using a character separator
const divideWithString = divider('hello', 'e');
// ['h', 'llo']

const divideWithMultipleString = divider('hello', 'l');
// ['he', 'o']

// Divide an array of strings
const words = ['hello', 'world'];
const dividedWords = divider(words, 2);
// [['he', 'llo'], ['wo', 'rld']]
const dividedWordsWithFlattenOption = divider(words, 2, { flatten: true });
// ['he', 'llo', 'wo', 'rld']
```

### Advanced Examples

```ts
// Mixed usage of indexes and characters
const complexDivide = divider('hello world', 3, 'o');
// ['hel', 'l', ' w', 'rld']

// Nested array handling
const nestedArray = divider(['hello', 'new world'], ' ', 2);
// [['he', 'llo'], ['ne', 'w wor', 'ld']]

// Flatten option to get a single array
const flatArray = divider(['hello', 'new world'], ' ', 2, { flatten: true });
// ['he', 'llo', 'ne', 'w', 'wor', 'ld']
```

## ๐ŸŽฏ Options

### `flatten` (default: `false`)

If `true`, the resulting nested arrays are flattened into a single array.

```ts
const words = ['hello', 'world'];
const result1 = divider(words, 2);
// [['he', 'llo'], ['wo', 'rld']]

const result2 = divider(words, 2, { flatten: true });
// ['he', 'llo', 'wo', 'rld']
```

## ๐Ÿ’ก Features

- Supports both `index-based` and `string-based` division
- Works with both `strings` and `arrays of strings`
- Supports `multiple separators` (mixing indexes and characters).
- Provides an `optional flattening` feature for array results.

## ๐Ÿ›  Contributing

Welcome your contributions! If you want to add features or fix issues, feel free to submit a PR!

### Setup

```sh
pnpm install
```

### Test

```sh
pnpm test
```

### Contribution Guidelines

- If you add new functions, please add corresponding tests in the `tests` directory.
- No strict rulesโ€”just keep it clean and readable!
- Thank you for your contribution. ๐Ÿ˜บ