npm.io
1.0.2 • Published 1 year ago

split-string-or-buffer-once-pmb

Licence
ISC
Version
1.0.2
Deps
0
Size
5 kB
Vulns
0
Weekly
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 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.

simpleLast = splitOnce.last(sep, input)

A convenience alais for splitOnce({ sep, last: true }, input).

Usage

see test/usage.mjs.

Known issues

  • Needs more/better tests and docs.

 

License

ISC

Keywords