2.0.1 • Published 8 months ago

strangler v2.0.1

Weekly downloads
865
License
MIT
Repository
-
Last release
8 months ago

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

ParamTypeDescription
inputstringinput string to test
candidatestring or Arraythe 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

ParamTypeDescription
inputstringinput string to test
candidatestringthe 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

ParamTypeDescription
inputstringinput string to test
candidatestringthe 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

ParamTypeDefaultDescription
inputstringinput string to split
delimiterstring','the pattern to split on
escapecharthe character to use as an escape value
quotesArray"'", '""'the quotes to respect
terminatorcharthe 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

ParamTypeDefaultDescription
inputstringinput string to split
delimiterRegExp','the pattern to split on
quotesArray"'", '""'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

ParamTypeDefaultDescription
strstringmultiline string prefix
appendStrstringmultiline string suffix
joinStrstringthe 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

ParamTypeDefaultDescription
options.delimitercharthe character to split individual pieces of data on
options.terminatorcharthe character to split groups of data on
options.escapecharthe character to escape on
options.quotesArraylist 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