string.js
, simply S
is a lightweight (< 2k Gzipped) JavaScript library for the browser or for Node.js that provides extra String methods. Originally, it would modify your String prototype. But I quickly learned that in JavaScript this is considered bad practice.
Motivation
Personally, I prefer the cleanliness of the way code looks when you modify native Javascript prototypes. However, if any app dependency required string.js
then the app's string prototype would modified as well. This could be troublesome.
Here's a list of alternative frameworks:
Why wasn't I happy with any of them? They are all static methods that don't seem to support chaining in a clean way.
Installation
npm install --production string
Usage
Node.js
var S = require('string');
Originally, I was using $s
but glancing over the code, it was easy to confuse $s
for string.js with $
for jQuery. Feel free to use the most convenient variable for you.
Browsers (IE/Chrome/Safari/Firefox)
<script src="https://raw.github.com/jprichardson/string.js/master/lib/string.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/jprichardson/lib/string.js/master/string.min.js"></script>
A global variable window.S
or simply S
is created.
Both
var doesIt = S('my cool string').left(2).endsWith('y');
Access the wrapped string using s
variable or toString()
var name = S('Your name is JP').right(2).s;
is the same as…
var name = S('Your name is JP').right(2).toString();
Still like the clean look of calling these methods directly on native Strings? No problem. Call cloberPrototype()
. Make sure to not call this at the module level, at it'll effect the entire application lifecycle. You should really only use this at the method level. The one exception being if your application will not be a dependency of another application.
S.cloberPrototype();
var name = 'Your name is JP'.right(2);
S.restorePrototype();
Methods
See test file for more details.
I use the same nomenclature as Objective-C regarding methods. + means static
or class
method. - means non-static
or instance
method.
+ cloberPrototype()
Modifies String.prototype
to have all of the methods found in string.js.
Example:
S.cloberPrototype();
- collapseWhitespace()
Converts all adjacent whitespace characters to a single space.
Example:
var str = S(' String \t libraries are \n\n\t fun\n! ').collapseWhitespace().s;
- contains(substring) Aliases: include/includes
Returns true if the string contains the substring.
Example:
S('JavaScript is one of the best languages!').contains('one');
- endsWith(substring)
Returns true if the string ends with the substring.
Example:
S("hello jon").endsWith('jon');
- isAlpha()
Return true if the string contains only letters.
Example:
S("afaf").isAlpha();
S('fdafaf3').isAlpha();
S('dfdf--dfd').isAlpha();
- isAlphaNumeric()
Return true if the string contains only letters and numbers
Example:
S("afaf35353afaf").isAlphaNumeric();
S("FFFF99fff").isAlphaNumeric();
S("99").isAlphaNumeric();
S("afff").isAlphaNumeric();
S("Infinity").isAlphaNumeric();
S("-Infinity").isAlphaNumeric();
S("-33").isAlphaNumeric();
S("aaff..").isAlphaNumeric();
- isEmpty()
Return true if the string is solely composed of whitespace
Example:
S(' ').isEmpty();
S('\t\t\t ').isEmpty();
S('\n\n ').isEmpty();
- isNumeric()
Return true if the string only contains digits
Example:
S("3").isNumeric();
S("34.22").isNumeric();
S("-22.33").isNumeric();
S("NaN").isNumeric();
S("Infinity").isNumeric();
S("-Infinity").isNumeric();
S("JP").isNumeric();
S("-5").isNumeric();
S("000992424242").isNumeric();
- ltrim()
Return the string with leading and trailing whitespace removed
Example:
S(' How are you?').ltrim().s;
- left(N)
Return the substring denoted by N positive left-most characters.
Example:
S('My name is JP').left(2).s;
S('Hi').left(0).s;
S('My name is JP').left(-2).s;
- replaceAll(substring, replacement)
Return the new string with all occurrences of substring replaced with the replacement string
Example:
S(' does IT work? ').replaceAll(' ', '_').s;
S('Yes it does!').replaceAll(' ', '').s;
+ restorePrototype()
Restore the original String prototype.
Example:
S.restorePrototype();
- right(N)
Return the substring denoted by N positive right-most characters
Example:
S('I AM CRAZY').right(2).s;
S('Does it work? ').right(4).s;
S('Hi').right(0).s;
S('My name is JP').right(-2).s;
- startsWith(prefix)
Return true if the string starts with the input string
Example:
S("JP is a software engineer").startsWith("JP");
S('wants to change the world').startsWith("politicians");
- trim()
Return the string with leading and trailing whitespace removed. Reverts to native trim()
if it exists.
Example:
S('hello ').trim().s;
S(' hello ').trim().s;
S('\nhello').trim().s;
S('\nhello\r\n').trim().s;
S('\thello\t').trim().s;
I will definitely add more methods, I'll be adding them on as-needed basis.
License
Triple licensed under MIT/X11, Apache v2, and LGPL. If you use this, pick which one works for you and your software. Attribution is always nice.
As noted, some of these methods were plucked from Google.
Copyright (c) 2012 JP Richardson