1.8.7 β€’ Published 7 months ago

@nyaomaru/divider v1.8.7

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Divider

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

πŸš€ Installation

You can install @nyaomaru/divider using your favorite package manager:

# Using pnpm (recommended)
pnpm install @nyaomaru/divider

# Using npm
npm install @nyaomaru/divider

# Using bun
bun add @nyaomaru/divider

# Using yarn
yarn add @nyaomaru/divider

πŸ“– Usage

πŸ‘‰ Check out the full documentation here!

divider allows you to divide a string or an array of strings using index positions or delimiters.

πŸ“Œ Basic Usage

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 Usage

// 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']

πŸ“Œ dividerFirst() Usage

dividerFirst() returns only the first divided element from the result.

import { dividerFirst } from '@nyaomaru/divider';

const firstElement = dividerFirst('hello world', ' ');
// 'hello'

const firstArrayElement = dividerFirst(['hello', 'world'], 2);
// 'he'

πŸ“Œ dividerLast() Usage

dividerLast() returns only the last divided element from the result.

import { dividerLast } from '@nyaomaru/divider';

const firstElement = dividerLast('hello world', ' ');
// 'world'

const firstArrayElement = dividerLast(['hello', 'world'], 2);
// 'rld'

πŸ“Œ dividerLoop() Usage

import { dividerLoop } from '@nyaomaru/divider';

// Divide string into chunks of given size
const result = dividerLoop('abcdefghij', 3);
// ['abc', 'def', 'ghi', 'j']

// Supports flatten option for string[]
const result2 = dividerLoop(['hello', 'world'], 2, { flatten: true });
// ['he', 'll', 'ow', 'or', 'ld']

// You can also control where to start dividing using `startOffset`
const result3 = dividerLoop('abcdefghij', 3, { startOffset: 1 });
// ['abcd', 'efg', 'hij']

// Combine with flatten and trim
const result4 = dividerLoop(['  hello ', 'world  '], 2, {
  flatten: true,
  trim: true,
  startOffset: 1,
});
// ['h', 'el', 'lo', 'wor', 'ld']

// Limit the number of chunks using maxChunks
const result5 = dividerLoop('abcdefghij', 3, { maxChunks: 2 });
// ['abc', 'defghij']

πŸ“Œ dividerNumberString() Usage

import { dividerNumberString } from '@nyaomaru/divider';

// Split numbers and letters from a string
const result = dividerNumberString('abc123def456');
// ['abc', '123', 'def', '456']

// Split each string in a string[]
const result2 = dividerNumberString(['abc123', '45z']);
// [['abc', '123'], ['45', 'z']]

// Flatten option
const result3 = dividerNumberString(['abc123', '45z'], { flatten: true });
// ['abc', '123', '45', 'z']

🎯 General Options

OptionTypeDefaultDescription
flattenbooleanfalseIf true, the resulting nested arrays are flattened into a single array.
trimbooleanfalseIf true, trims whitespace from each divided segment.
exclude'none' / 'empty' / 'whitespace''none'See detailed explanation below

flatten (default: false)

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

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

trim (default: false)

const result = divider('  hello world  ', 7, { trim: true });
// ['hello', 'world']

const result = divider(['  a  ', ' b  c '], ' ', {
  flatten: true,
  trim: true,
});
// ['a', 'b', 'c']

exclude (default: 'none')

OptionDescription
'none'Do not exclude any segments (all results are kept).
'empty'Exclude empty strings ('').
'whitespace'Exclude strings that contain only whitespace characters (e.g., ' ').

Control how segments like empty strings ('') or whitespace-only strings (' ') are handled.

// Remove truly empty strings
const result = divider('a,,b', ',', { exclude: 'empty' });
// ['a', 'b']

// Remove both empty and whitespace-only strings
const result = divider('a, ,b', ',', { exclude: 'whitespace' });
// ['a', 'b']

// You can combine with `trim` for clearer results
const result = divider('a, ,b', ',', {
  trim: true,
  exclude: 'whitespace',
});
// ['a', 'b']

Special Options

OptionTypeDefaultDescription
startOffsetnumber0Starting index offset when dividing into chunks (only for dividerLoop)
maxChunksnumber∞Maximum number of chunks allowed. Extra chunks are joined into the last chunk. (only for dividerLoop)

πŸ’‘ Features

  • 🧩 Flexible Division: Index-based and string-based separators
  • 🧡 Handles Nested Input: Supports both string and string[]
  • πŸŽ›οΈ Optional Behaviors: flatten, trim, excludeEmpty
  • 🎯 Targeted Extractors: dividerFirst(), dividerLast()
  • πŸ” Loop Support: dividerLoop() for chunked division
  • πŸ”’ Digit-Letter Splitter: dividerNumberString()

πŸ›  Contributing

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

Setup

pnpm install

Test

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. 😺
1.8.7

7 months ago

1.8.6

7 months ago

1.8.5

8 months ago

1.8.4

8 months ago

1.8.3

8 months ago

1.8.2

8 months ago

1.8.1

8 months ago

1.8.0

8 months ago

1.7.4

8 months ago

1.7.3

8 months ago

1.7.2

8 months ago

1.7.1

9 months ago

1.7.0

9 months ago

1.6.2

9 months ago

1.6.1

9 months ago

1.6.0

9 months ago

1.5.3

9 months ago

1.5.2

9 months ago

1.5.1

9 months ago

1.5.0

9 months ago

1.4.1

9 months ago

1.4.0

9 months ago

1.3.7

10 months ago

1.3.6

10 months ago

1.3.5

10 months ago

1.3.4

10 months ago

1.3.3

10 months ago

1.3.2

10 months ago

1.3.1

10 months ago

1.3.0

10 months ago

1.2.3

10 months ago

1.2.2

10 months ago

1.2.1

10 months ago

1.2.0

10 months ago

1.1.0

10 months ago

1.0.21

10 months ago

1.0.20

10 months ago

1.0.19

10 months ago

1.0.18

10 months ago

1.0.17

10 months ago

1.0.16

10 months ago

1.0.15

10 months ago

1.0.14

10 months ago

1.0.13

10 months ago

1.0.12

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago