/strangler

An NPM for string utility functions

Primary LanguageJavaScriptMIT LicenseMIT

strangler.js

NPM version npm Travis

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);

proto()

assign these utilities to String.prototype and throw caution to the wind...

Kind: static property of strangler

.contains(str, candidate) ⇒ boolean

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;

.beginsWith(str, candidate) ⇒ boolean

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;

.endsWith(str, candidate) ⇒ boolean

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;

.splitHonoringQuotes(str, [delimiter], [escape], [quotes], [terminator]) ⇒ Array

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']

.decompose(str, [delimit], [quotes]) ⇒ Array

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

.multiLineAppend(str, appendStr, [joinStr]) ⇒ string

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

.StreamDecomposer([options]) ⇒ Class

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

.StreamDecomposer.writable ⇒ stream.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