1.0.0 • Published 7 years ago
split-string-or-buffer-once-pmb v1.0.0
split-string-or-buffer-once-pmb
Generic splitOnce, using indexOf and slice so it can work on strings and buffers alike.
API
This module exports one function:
simple = splitOnce(sep, input)
input is the String, Buffer, or other compatible value you want to split.
sep is whatever input.indexOf() shall search for.
However, sep must have a number as its length property.
Returns an array with two slices of input if sep was found,
or false otherwise.
advanced = splitOnce(opt, input)
Similar as above, but with an options object opt.
Required properties on opt:
sep: Same as thesepabove.
Optional properties:
last: If set to a truthy value, uselastIndexOfto search forsep.
Confusing properties: If for some exotic reason your opt could
happen to have a length property, make sure it's set to undefined,
or the opt might be mistaken for sep.
Usage
from test/usage.js:
import splitOnce from 'split-string-or-buffer-once-pmb';
const hsw = 'hello string world';
equal(splitOnce(' ', hsw),
['hello', 'string world']);
function buf(x) { return Buffer.from(x); }
const hbw = buf('hello buffer world');
equal(splitOnce(' ', hbw),
[buf('hello'), buf('buffer world')]);
equal(splitOnce('', hbw),
[buf(''), buf('hello buffer world')]);
let opt = { sep: ' ', last: true };
equal(splitOnce(opt, hsw),
['hello string', 'world']);
equal(splitOnce(opt, hbw),
[buf('hello buffer'), buf('world')]);
opt = { sep: '', last: true };
equal(splitOnce(opt, hsw),
['hello string world', '']);Known issues
- Needs more/better tests and docs.
License
ISC
1.0.0
7 years ago