string-utilz v1.4.0
String Utilz
String management, tools and tricks for JavaScript.
The Fun Stuff
Why Another String formatting tool?
Several npms utilize multiple dependencies, and lead to code bloat. There are also several modules on the market that are very opinionated (e.g. force you to do certain things) or are focused on a single form of string management. This tool aims to be a lightweight, flexible solution that allows for infinitely customizable options to suit user needs.
Getting Started
- Install the npm in your project:
npm install --save string-utilz - Require the library where needed:
const stringz = require('string-utilz'); - Manage strings in a much more seamless way.
Default behavior
By default, you will need to use the object returned from the require statement to manage strings. As an example:
const stringz = require('string-utilz');
stringz.startsWith('Bob','B'); // true
stringz.endsWith('Bob','o'); // false#addStringPrototypes()
If you are so inclined to let string-utilz try to prototype the String object, feel free to call the stringz.addStringPrototypes() method. This will non-destructively add each of the functions to the String object, if there is no existing definition. This can safely be called multiple times, without concern.
String helpers
Depending on the version of Node.js (or where your application is running), you might or might not have access to String#startsWith(str) and String#endsWith(str). This module adds that functionality, as well as methods for String#containsIgnoreCase(str) and String#replaceAll(oldStr,newStr). These are documented below:
string#startsWith(str)=> returnstrueifstrexactly matches the first part ofString,falseotherwise.string#endsWith(str)=> returnstrueifstrexactly matches the last part ofString,falseotherwise.string#containsIgnoreCase(str)=> returnstrueifstrmatches any part ofString, no matter the case,falseotherwise.string#replaceAll(oldStr,newStr)=> replaces all instances ofoldStrwithnewStrand returns a newString. It is important to note that this will replace all instances, the existingString#replace(oldStr,newStr)only replaces the first instance.string#replaceAllIgnoreCase(oldStr,newStr)=> replaces all instances ofoldStrwithnewStrand returns a newString. It is important to note that this will replace all instances - without case sensitivity - the existingString#replace(oldStr,newStr)only replaces the first instance.string#escapeRegEx()=> escapes all special RegEx charactersstring#times(size)=> returns theStringmultipled bysize, multiplying by 0 =>nulland muliplying by 1 or any negative number returns theStringstring#pad(size,char)=> pads the givenStringsizetimes using the givenchar.charcan be one or more characters, and is optional (default value is an empty space (' ')). Negativesizepads left, postivesizepads right, and0assizedoesn't pad.string#chop(size)=> chops the givenStringsizecharacters from the end (size> 0) or from the beginnning (size< 0).string#fixSize(size, char)=> pads or chops the givenStringas needed, to makestring.length == Math.abs(size). Positive values ofsizechop from the right or pad on the right, negative values ofsizechop from the left or pad to the leftstring#fmt(args...)=> extends the string object to support formatting. This formatting can be done using simple%{s}replacements (in order of theargs...) or theargs...can be referenced using their 0-based indicies (e.g.%{0}or%{2})String.fmt(fmtString, args...)=> same functionality asstring#fmt(args...), just an extension of theStringclass, rather than object.
Usage examples:
// startsWith
'Bob'.startsWith('B'); // true
'Bob'.startsWith('b'); // false
'Franklin'.startsWith('Frank'); // true
// endsWith
'Bob'.endsWith('B'); // false
'Bob'.endsWith('b'); // true
'Franklin'.endsWith('lin'); // true
// containsIgnoreCase
'The quick brown fox'.containsIgnoreCase('quick'); // true
'The quick brown fox'.containsIgnoreCase('QUICK'); // true
'The quick brown fox'.containsIgnoreCase('ck BR'); // true
'The quick brown fox'.containsIgnoreCase('lazy'); // false
// replaceAll
'Bobby'.replaceAll('b','d'); // 'Boddy'
'Bobby'.replaceAll('B','d'); // 'dobby'
'Bobby'.replaceAll('n','d'); // 'Bobby'
'Meow meow meow, said the cat'.replaceAll('meow','woof') // 'Meow woof woof, said the cat'
// replaceAllIgnoreCase
'Bobby'.replaceAll('b','d'); // 'doddy'
'Bobby'.replaceAll('B','d'); // 'doddy'
'Bobby'.replaceAll('n','d'); // 'Bobby'
'Meow meow meow, said the cat'.replaceAll('meow','woof') // 'woof woof woof, said the cat'
// escapeRegEx
'-'.escapeRegEx(); // '\-'
'{'.escapeRegEx(); // '\{'
// times
'*'.times(2); // '**'
'bob'.times(3); // 'bobbobbob'
'frank'.times(0); // null
'frank'.times(-2); // 'frank'
// pad
'-'.pad(1); // '- '
'frank'.pad(2, '-'); // 'frank--'
'frank'.pad(-2,'-'); // '--frank'
'bob'.pad(0); // 'bob'
'bob'.pad(1 'frank'); // 'bobfrank'
// chop
'testing'.chop(3); // 'test'
'testing'.chop(-4); // 'ing'
// fixSize
'testing'.fixSize(4); // 'test'
'testing'.fixSize(-3); // 'ing'
'testing'.fixSize(10, '-'); // 'testing---'
'testing'.fixSize(-10, '-'); // '---testing'
'testing'.fixSize(8); // 'testing '
// fmt
'The quick brown fox'.fmt('bob', 'frank') // 'The quick brown fox'
'The %{2} %{0} %{1}'.fmt('quick', 'brown', 'fox'); // 'The fox quick brown'
'The %{0} %{0} %{0}'.fmt('quick', 'brown', 'fox'); // 'The quick quick quick'
'The %{s} %{s} %{s}'.fmt('quick', 'brown', 'fox'); // 'The quick brown fox'
// String.fmt
String.fmt('The quick brown fox', 'bob', 'frank'); // 'The quick brown fox'
String.fmt('The %{2} %{0} %{1}', 'quick', 'brown', 'fox'); // 'The fox quick brown'
String.fmt('The %{0} %{0} %{0}', 'quick', 'brown', 'fox'); // 'The quick quick quick'
String.fmt('The %{s} %{s} %{s}', 'quick', 'brown', 'fox'); // 'The quick brown fox'Slack
This is one of several projects that are in the works, so feel free to reach out on Slack. Please email slack at maddhacker dot com for an invite.
Issues
Please use the Issues tab to report any problems or feature requests.
Change Log
All change history can be found in the CHANGELOG.md file.