split-string-or-buffer-once-pmb

1.0.0 • Public • Published

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 the sep above.

Optional properties:

  • last: If set to a truthy value, use lastIndexOf to search for sep.

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

Readme

Keywords

Package Sidebar

Install

npm i split-string-or-buffer-once-pmb

Weekly Downloads

140

Version

1.0.0

License

ISC

Unpacked Size

4.92 kB

Total Files

5

Last publish

Collaborators

  • mk-pmb