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[]
- Host: GitHub
- URL: https://github.com/nyaomaru/divider
- Owner: nyaomaru
- License: mit
- Created: 2025-02-20T12:15:04.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-02-26T13:15:12.000Z (3 months ago)
- Last Synced: 2025-02-26T13:24:38.058Z (3 months ago)
- Topics: array-split, array-splitter, javascript, lightweight, nodejs, npm-package, open-source, string-manipulation, string-split, string-splitter, typescript, utility-library
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@nyaomaru/divider
- Size: 70.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Divider
![]()
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. ๐บ