string-chunking

Chunk Unicode strings with a fast JavaScript module.


Keywords
algorithm, array, best, bitwise, browser, chunk, delimiter, emoji, fast, format, library, module, nodejs, npm, object, segment, simple, string, substring, unicode, parts, utf-16, utf-8
License
MIT
Install
npm install string-chunking@1.0.1

Documentation

About

Description

This is a fast module for segmenting Unicode strings.

It's written in JavaScript with extreme efficiency and simplicity.

A complete line-by-line code algorithm explanation is coming soon.

License

The Unlicense

Installation

Browser

git clone https://github.com/liamloads/javascript-string-segmentation-module.git
<script src="string-segmentation.js" type="text/javascript"></script>
<script type="text/javascript">console.log(stringSegmentation('🕹123456789', 2));</script>

Unix

cd
npm install string-segmentation
const stringSegmentation = require('string-segmentation');
console.log(stringSegmentation('🕹123456789', 2));

Usage

Details

The module function accepts five arguments.

The first argument is the string to segment.

The second argument is the numeric size of each segment with a limit of 99999999.

The third optional argument is the string delimiter with a length limit of 99999999.

The fourth optional argument is the numeric limit of segments with a limit of 99999999.

The fifth optional argument is a boolean that includes the remaining string without segmentation beyond the defined numeric limit.

The return value is a string if the third optional argument is a string delimiter, otherwise the return value is an object array.

If the length of the segment size multiplied by the delimiter length exceeds 99999999, the return value is false.

Examples

console.log(stringSegmentation('🕹123456789', 2));
// ['🕹1', '23', '45', '67', '89']

console.log(stringSegmentation('🕹12345', 3, '_'));
// '🕹12_345'

console.log(stringSegmentation('🕹123456789', 2, '_', 3));
// '🕹1_23_45'

console.log(stringSegmentation('🕹123456789', 2, '_', 3, true));
// '🕹1_23_45_6789'

console.log(stringSegmentation('🕹123456789', '0001', false, 3));
// ['🕹', '1', '2']

console.log(stringSegmentation('🕹123456789', 1, false, 2, true));
// ['🕹', '1', '23456789']

console.log(stringSegmentation('🕹123456789', 11));
// ['🕹123456789']

console.log(stringSegmentation('🕹123456789', 11, '_'));
// '🕹123456789'

console.log(stringSegmentation('🕹123456789', 11, '_', 0));
// ''

console.log(stringSegmentation('🕹123456789', 0));
// []