strangler.js
It's a string wrangler, and not nearly as dangerous as it sounds. A set of string utilities which expand those in string-tools with additional features. ES module native
Usage
Often I do string parsing and I like some convenience functions to help out.
you can either retain an instance and use it that way:
import * as stringTool from 'strangler';
// or: const stringTool = require('strangler');
stringTool.contains(string, substring);
or you can just attach to the prototype (this can be OK in an app, but is a bad idea in a library):
require('string-tools').proto();
string.contains(substring);
- string-tools + strangler
- .proto()
- .contains(str, target) ⇒
boolean
- .beginsWith(str, target) ⇒
boolean
- .endsWith(str, target) ⇒
boolean
- .splitHonoringQuotes(str, [delimiter], [quotes]) ⇒
Array
- .decompose(str, [delimiter], [quotes]) ⇒
Array
- .multiLineAppend(str, appendStr) ⇒
string
proto()
assign these utilities to String.prototype and throw caution to the wind...
Kind: static property of strangler
boolean
.contains(str, candidate) ⇒ Tests whether the string contains a particular substring or set of substrings
Kind: static method of strangler
Param | Type | Description |
---|---|---|
input | string |
input string to test |
candidate | string or Array |
the substring to test |
Example
'elongated'.contains('gate'); //returns true;
'elongated'.contains(['long', 'gate']); //returns true;
'elongated'.contains(['wall']); //returns false;
boolean
.beginsWith(str, candidate) ⇒ Tests whether the string begins with a particular substring
Kind: static method of strangler
Param | Type | Description |
---|---|---|
input | string |
input string to test |
candidate | string |
the substring to test |
Example
'max'.beginsWith('m'); //return true;
boolean
.endsWith(str, candidate) ⇒ Tests whether the string ends with a particular substring
Kind: static method of strangler
Param | Type | Description |
---|---|---|
input | string |
input string to test |
candidate | string |
the substring to test |
Example
'max'.endsWith('x'); //return true;
Array
.splitHonoringQuotes(str, [delimiter], [escape], [quotes], [terminator]) ⇒ like String.split(), but it will honor the opening and closing of quotes, so a delimiter inside a quote won't blow it up.
Kind: static method of strangler
Param | Type | Default | Description |
---|---|---|---|
input | string |
input string to split | |
delimiter | string |
',' | the pattern to split on |
escape | char |
the character to use as an escape value | |
quotes | Array |
["'", '""'] | the quotes to respect |
terminator | char |
the character to split groups |
Example
'a, b, c="r, u, d", d'.splitHonoringQuotes(',');
returns
['a', ' b', ' c="r, u, d"', ' d']
Array
.decompose(str, [delimit], [quotes]) ⇒ like String.split(), but it will honor the opening and closing of quotes, so a delimiter inside a quote won't blow it up, rather than using a fixed delimiter, you can provide a RegExp to split on. Not as fast as splitHonoringQuotes
, but much more flexible.
Kind: static method of strangler
Param | Type | Default | Description |
---|---|---|---|
input | string |
input string to split | |
delimiter | RegExp |
',' | the pattern to split on |
quotes | Array |
["'", '""'] | the quotes to respect |
string
.multiLineAppend(str, appendStr, [joinStr]) ⇒ returns the two strings which are appended together in a line by line fashion.
Kind: static method of strangler
Param | Type | Default | Description |
---|---|---|---|
str | string |
multiline string prefix | |
appendStr | string |
multiline string suffix | |
joinStr | string |
the chars to stick between them |
Example
var firsts = 'this \
attaches \
the ';
var seconds = 'one \
to \
other'
firsts.multiLineAppend(seconds);
returns
'this one\
attaches to\
the other'
strangler.StreamDecomposer
Class
.StreamDecomposer([options]) ⇒ This class allows you to parse a large string as and to generate events during parse to prevent storing the results in memory. It is an EventEmitter and will generate token
events for each token it finds and if options.terminator
is set it will generate cell
and row
events.
Kind: constructor of strangler.StreamDecomposer
Param | Type | Default | Description |
---|---|---|---|
options.delimiter | char |
the character to split individual pieces of data on | |
options.terminator | char |
the character to split groups of data on | |
options.escape | char |
the character to escape on | |
options.quotes | Array |
list of characters to quote with |
stream.Writable
.StreamDecomposer.writable ⇒ Generate a writable stream to pipe a readable stream into in order to parse.
Kind: method of strangler
returns stream.Writable
Testing
just run
mocha
Enjoy,
-Abbey Hawk Sparrow