divider logo@nyaomaru/divider
divider logo

Divide strings and arrays with ease

A lightweight utility library for dividing strings and arrays in multiple ways

Installation

npm

npm install @nyaomaru/divider

Usage

basic

import { divider } from '@nyaomaru/divider';
// divide a string by index position
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']
// split string using character delimiter
const divideWithString = divider('hello', 'e');
// ['h', 'llo']
const divideWithMultipleString = divider('hello', 'l');
// ['he', 'o']
// divide the 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']
const dividedWithTrimOption = divider([' hello ', ' world '], 2, { trim: true });
// ['h', 'ello', 'w', 'orld']

Features

String Division

Divide strings by index positions or character delimiters with a simple API.

Array Processing

Process arrays of strings with the same intuitive interface.

Nested Array

Handle complex data structures with support for nested arrays.

Flexible Output

Get results as arrays or use destructuring for individual variables.

Flattening Option

Simplify results with the flatten option to get a single-level array.

Mixed Delimiters

Combine index positions and character delimiters in the same operation.

API Reference

divider(input, ...dividers, options?)

Returns an string array or nested string array of divided strings or arrays.

Parameters:

  • input : String or array to divide
  • dividers : Numbers (index positions) or strings (character delimiters)
  • options : Optional configuration object

Options:

  • flatten : Boolean - When true, flattens the result into a single array
  • trim : Boolean - When true, trims whitespace from each divided segment
  • excludeEmpty : Boolean - When true, excludes empty segments from the result

dividerFirst(input, ...dividers)

Returns the first part of the divided string.

Parameters:

  • input : String or array to divide
  • dividers : Numbers (index positions) or strings (character delimiters)

dividerLast(input, ...dividers)

Returns the last part of the divided string.

Parameters:

  • input : String or array to divide
  • dividers : Numbers (index positions) or strings (character delimiters)

dividerLoop(input, size, options?)

Divides the input into equal-sized chunks by the given number. Useful for fixed-width slicing.

Parameters:

  • input : String or array to divide
  • size : The size (number of characters) for each chunk. Must be a positive integer.
  • options : Optional configuration object

Options:

  • flatten : Boolean - When true, flattens the result into a single array
  • trim : Boolean - When true, trims whitespace from each divided segment
  • excludeEmpty : Boolean - When true, excludes empty segments from the result

dividerNumberString(input, options?)

Divides inputs number and string.

Parameters:

  • input : String or array to divide
  • options : Optional configuration object

Options:

  • flatten : Boolean - When true, flattens the result into a single array
  • trim : Boolean - When true, trims whitespace from each divided segment
  • excludeEmpty : Boolean - When true, excludes empty segments from the result