Extra JavaScript string methods.

Related tags

String string.js
Overview

string.js

build status CDNJS

Sauce Test Status

string.js, or simply S is a lightweight (< 5 kb minified and gzipped) JavaScript library for the browser or for Node.js that provides extra String methods. Originally, it modified the String prototype. But I quickly learned that in JavaScript, this is considered poor practice.

Why?

Personally, I prefer the cleanliness of the way code looks when it appears to be native methods. i.e. when you modify native JavaScript prototypes. However, if any app dependency required string.js, then the app's string prototype would be modified in every module. This could be troublesome. So I settled on creating a wrapper a la jQuery style. For those of you prototype hatin' fools, there is the method extendPrototype().

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 'OR' they have an odd dependency. Sugar is the notable exception.

Installation

  1. If you want to use this library, you first need to install the [Node.js] (https://nodejs.org/en/).

  2. When you install node.js, will also be installed [npm] (https://www.npmjs.com/).

  3. Please run the following command.

npm install --save string

Experiment with String.js Now

Assuming you're on http://stringjs.com, just simply open up the Webkit inspector in either Chrome or Safari, or the web console in Firefox and you'll notice that string.js is included in this page so that you can start experimenting with it right away.

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.

Rails

Checkout this gem to easily use string.js on the asset pipeline: https://github.com/jesjos/stringjs-rails

Browsers

<!-- HTML5 -->
<script src="https://cdn.rawgit.com/jprichardson/string.js/master/dist/string.min.js"></script>

<!-- Note that in the mime type for Javascript is now officially 'application/javascript'. If you
set the type to application/javascript in IE browsers, your Javacript will fail. Just don't set a
type via the script tag and set the mime type from your server. Most browsers look at the server mime
type anyway -->

<!-- For HTML4/IE -->
<script type="text/javascript" src="https://cdn.rawgit.com/jprichardson/string.js/master/dist/string.min.js"></script>

A global variable window.S or simply S is created.

AMD Support

It now has AMD support. See require.js on how to use with AMD modules.

Both

var doesIt = S('my cool string').left(2).endsWith('y'); //true

Access the wrapped string using s variable or toString()

var name = S('Your name is JP').right(2).s; //'JP'

is the same as…

var name = S('Your name is JP').right(2).toString(); //'JP'

Still like the clean look of calling these methods directly on native Strings? No problem. Call extendPrototype(). 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.extendPrototype();
var name = 'Your name is JP'.right(2); //'JP'
S.restorePrototype(); //be a good citizen and clean up

Browser Compatibility

string.js has been designed to be compatible with Node.js and with IE6+, Firefox 3+, Safari 2+, Chrome 3+. Please click here to run the tests in your browser. Report any browser issues here: https://github.com/jprichardson/string.js/issues

Extending string.js

See: https://github.com/jprichardson/string.js/pull/57

Native JavaScript Methods

string.js imports all of the native JavaScript methods. This is for convenience. The only difference is that the imported methods return string.js objects instead of native JavaScript strings. The one exception to this is the method charAt(index). This is because charAt() only returns a string of length one. This is typically done for comparisons and a string.js object will have little to no value here.

All of the native methods support chaining with the string.js methods.

Example:

var S = require('string');

var phrase = S('JavaScript is the best scripting language ever!');
var sub = 'best scripting';
var pos = phrase.indexOf(sub);
console.log(phrase.substr(pos, sub.length).truncate(8)); //best...

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.

- constructor(nativeJsString)

This creates a new string.js object. The parameter can be anything. The toString() method will be called on any objects. Some native objects are used in some functions such as toCSV().

Example:

S('hello').s //"hello"
S(['a,b']).s //"a,b"
S({hi: 'jp'}).s //"[object Object]""

- between(left, right)

Extracts a string between left and right strings.

Example:

S('<a>foo</a>').between('<a>', '</a>').s // => 'foo'
S('<a>foo</a></a>').between('<a>', '</a>').s // => 'foo'
S('<a><a>foo</a></a>').between('<a>', '</a>').s // => '<a>foo'
S('<a>foo').between('<a>', '</a>').s // => ''
S('Some strings } are very {weird}, dont you think?').between('{', '}').s // => 'weird'
S('This is a test string').between('test').s // => ' string'
S('This is a test string').between('', 'test').s // => 'This is a '

- camelize()

Remove any underscores or dashes and convert a string into camel casing.

Example:

S('data_rate').camelize().s; //'dataRate'
S('background-color').camelize().s; //'backgroundColor'
S('-moz-something').camelize().s; //'MozSomething'
S('_car_speed_').camelize().s; //'CarSpeed'
S('yes_we_can').camelize().s; //'yesWeCan'

- capitalize()

Capitalizes the first character of a string.

Example:

S('jon').capitalize().s; //'Jon'
S('JP').capitalize().s; //'Jp'

- chompLeft(prefix)

Removes prefix from start of string.

Example:

S('foobar').chompLeft('foo').s; //'bar'
S('foobar').chompLeft('bar').s; //'foobar'

- chompRight(suffix)

Removes suffix from end of string.

Example:

S('foobar').chompRight('bar').s; //'foo'
S('foobar').chompRight('foo').s; //'foobar'

- 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; //'String libraries are fun !'

- contains(ss)

Returns true if the string contains ss.

Alias: include()

Example:

S('JavaScript is one of the best languages!').contains('one'); //true

- count(substring)

Returns the count of the number of occurrences of the substring.

Example:

S('JP likes to program. JP does not play in the NBA.').count("JP")// 2
S('Does not exist.').count("Flying Spaghetti Monster") //0
S('Does not exist.').count("Bigfoot") //0
S('JavaScript is fun, therefore Node.js is fun').count("fun") //2
S('funfunfun').count("fun") //3

- dasherize()

Returns a converted camel cased string into a string delimited by dashes.

Examples:

S('dataRate').dasherize().s; //'data-rate'
S('CarSpeed').dasherize().s; //'-car-speed'
S('yesWeCan').dasherize().s; //'yes-we-can'
S('backgroundColor').dasherize().s; //'background-color'

- decodeHTMLEntities()

Decodes HTML entities into their string representation.

S('Ken Thompson &amp; Dennis Ritchie').decodeHTMLEntities().s; //'Ken Thompson & Dennis Ritchie'
S('3 &lt; 4').decodeHTMLEntities().s; //'3 < 4'

- endsWith(ss)

Returns true if the string ends with ss.

Example:

S("hello jon").endsWith('jon'); //true

- escapeHTML()

Escapes the html.

Example:

S('<div>hi</div>').escapeHTML().s; //&lt;div&gt;hi&lt;/div&gt;

+ extendPrototype()

Modifies String.prototype to have all of the methods found in string.js.

Example:

S.extendPrototype();

- ensureLeft(prefix)

Ensures string starts with prefix.

Example:

S('subdir').ensureLeft('/').s; //'/subdir'
S('/subdir').ensureLeft('/').s; //'/subdir'

- ensureRight(suffix)

Ensures string ends with suffix.

Example:

S('dir').ensureRight('/').s; //'dir/'
S('dir/').ensureRight('/').s; //'dir/'

- humanize()

Transforms the input into a human friendly form.

Example:

S('the_humanize_string_method').humanize().s  //'The humanize string method'
S('ThehumanizeStringMethod').humanize().s //'Thehumanize string method'
S('the humanize string method').humanize().s  //'The humanize string method'
S('the humanize_id string method_id').humanize().s //'The humanize id string method'
S('the  humanize string method  ').humanize().s //'The humanize string method'
S('   capitalize dash-CamelCase_underscore trim  ').humanize().s //'Capitalize dash camel case underscore trim'

- include(ss)

Returns true if the string contains the ss.

Alias: contains()

Example:

S('JavaScript is one of the best languages!').include('one'); //true

- isAlpha()

Return true if the string contains only letters.

Example:

S("afaf").isAlpha(); //true
S('fdafaf3').isAlpha(); //false
S('dfdf--dfd').isAlpha(); //false

- isAlphaNumeric()

Return true if the string contains only letters and numbers

Example:

S("afaf35353afaf").isAlphaNumeric(); //true
S("FFFF99fff").isAlphaNumeric(); //true
S("99").isAlphaNumeric(); //true
S("afff").isAlphaNumeric(); //true
S("Infinity").isAlphaNumeric(); //true
S("-Infinity").isAlphaNumeric(); //false
S("-33").isAlphaNumeric(); //false
S("aaff..").isAlphaNumeric(); //false

- isEmpty()

Return true if the string is solely composed of whitespace or is null/undefined.

Example:

S(' ').isEmpty(); //true
S('\t\t\t    ').isEmpty(); //true
S('\n\n ').isEmpty(); //true
S('helo').isEmpty(); //false
S(null).isEmpty(); //true
S(undefined).isEmpty(); //true

- isLower()

Return true if the character or string is lowercase

Example:

S('a').isLower(); //true
S('z').isLower(); //true
S('B').isLower(); //false
S('hijp').isLower(); //true
S('hi jp').isLower(); //false
S('HelLO').isLower(); //false

- isNumeric()

Return true if the string only contains digits

Example:

S("3").isNumeric(); //true
S("34.22").isNumeric(); //false
S("-22.33").isNumeric(); //false
S("NaN").isNumeric(); //false
S("Infinity").isNumeric(); //false
S("-Infinity").isNumeric(); //false
S("JP").isNumeric(); //false
S("-5").isNumeric(); //false
S("000992424242").isNumeric(); //true

- isUpper()

Returns true if the character or string is uppercase

Example:

S('a').isUpper() //false
S('z').isUpper()  //false
S('B').isUpper() //true
S('HIJP').isUpper() //true
S('HI JP').isUpper() //false
S('HelLO').isUpper() //true

- latinise()

Removes accents from Latin characters.

S('crème brûlée').latinise().s // 'creme brulee'

- left(n)

Return the substring denoted by n positive left-most characters.

Example:

S('My name is JP').left(2).s; //'My'
S('Hi').left(0).s; //''
S('My name is JP').left(-2).s; //'JP', same as right(2)

- length

Property to return the length of the string object.

Example:

S('hi').length; //2

- lines()

Returns an array with the lines. Cross-platform compatible.

Example:

var stuff = "My name is JP\nJavaScript is my fav language\r\nWhat is your fav language?"
var lines = S(stuff).lines()

console.dir(lines)
/*
[ 'My name is JP',
  'JavaScript is my fav language',
  'What is your fav language?' ]
*/

- pad(len, [char])

Pads the string in the center with specified character. char may be a string or a number, defaults is a space.

Example:

S('hello').pad(5).s //'hello'
S('hello').pad(10).s //'   hello  '
S('hey').pad(7).s //'  hey  '
S('hey').pad(5).s //' hey '
S('hey').pad(4).s //' hey'
S('hey').pad(7, '-').s//'--hey--'

- padLeft(len, [char])

Left pads the string.

Example:

S('hello').padLeft(5).s //'hello'
S('hello').padLeft(10).s //'     hello'
S('hello').padLeft(7).s //'  hello'
S('hello').padLeft(6).s //' hello'
S('hello').padLeft(10, '.').s //'.....hello'

- padRight(len, [char])

Right pads the string.

Example:

S('hello').padRight(5).s //'hello'
S('hello').padRight(10).s //'hello     '
S('hello').padRight(7).s //'hello  '
S('hello').padRight(6).s //'hello '
S('hello').padRight(10, '.').s //'hello.....'

- parseCSV()

Parses a CSV line into an array.

Arguments:

  • delimiter: The character that is separates or delimits fields. Default: ,
  • qualifier: The character that encloses fields. Default: "
  • escape: The character that represents the escape character. Default: \
  • lineDelimiter: The character that represents the end of a line. When a lineDelimiter is passed the result will be a multidimensional array. Default: undefined

Example:

S("'a','b','c'").parseCSV(',', "'") //['a', 'b', 'c'])
S('"a","b","c"').parseCSV() // ['a', 'b', 'c'])
S('a,b,c').parseCSV(',', null)  //['a', 'b', 'c'])
S("'a,','b','c'").parseCSV(',', "'") //['a,', 'b', 'c'])
S('"a","b",4,"c"').parseCSV(',', null) //['"a"', '"b"', '4', '"c"'])
S('"a","b","4","c"').parseCSV() //['a', 'b', '4', 'c'])
S('"a","b",       "4","c"').parseCSV() //['a', 'b', '4', 'c'])
S('"a","b",       4,"c"').parseCSV(",", null) //[ '"a"', '"b"', '       4', '"c"' ])
S('"a","b\\"","d","c"').parseCSV() //['a', 'b"', 'd', 'c'])
S('"a","b\\"","d","c"').parseCSV() //['a', 'b"', 'd', 'c'])
S('"a\na","b","c"\n"a", """b\nb", "a"').parseCSV(',', '"', '"', '\n')) // [ [ 'a\na', 'b', 'c' ], [ 'a', '"b\nb', 'a' ] ]

- repeat(n)

Returns a string repeated n times.

Alias: times()

Example:

S(' ').repeat(5).s; //'     '
S('*').repeat(3).s; //'***'

- replaceAll(ss, newstr)

Return the new string with all occurrences of ss replaced with newstr.

Example:

S(' does IT work? ').replaceAll(' ', '_').s; //'_does_IT_work?_'
S('Yes it does!').replaceAll(' ', '').s; //'Yesitdoes!'

+ restorePrototype()

Restore the original String prototype. Typically used in conjunction with extendPrototype().

Example:

S.restorePrototype();

- right(n)

Return the substring denoted by n positive right-most characters.

Example:

S('I AM CRAZY').right(2).s; //'ZY'
S('Does it work?  ').right(4).s; //'k?  '
S('Hi').right(0).s; //''
S('My name is JP').right(-2).s; //'My', same as left(2)

- s

Alias: toString()

The encapsulated native string representation of an S object.

Example:

S('my name is JP.').capitalize().s; //My name is JP.
var a = "Hello " + S('joe!'); //a = "Hello joe!"
S("Hello").toString() === S("Hello").s; //true

- setValue(value)

Sets the string to a value.

var myString = S('War');
myString.setValue('Peace').s; // 'Peace'

- slugify()

Converts the text into a valid url slug. Removes accents from Latin characters.

S('Global Thermonuclear Warfare').slugify().s // 'global-thermonuclear-warfare'
S('Crème brûlée').slugify().s // 'creme-brulee'

- splitLeft(sep, [maxSplit = -1, [limit]])

Returns an array of strings, split from the left at sep. Performs at most maxSplit splits, and slices the result into an array with at most limit elements.

Example:

S('We built this city').splitLeft(' '); // ['We', 'built', 'this', 'city'];
S('We built this city').splitLeft(' ', 1); // ['We', 'built this city'];
S('On Rock N Roll and other Stuff').splitLeft(' ', -1, 4); // ['On', 'Rock', 'N', 'Roll'];
S('On Rock N Roll and other Stuff').splitLeft(' ', 5, -2); // ['and', 'other Stuff'];

- splitRight(sep, [maxSplit = -1, [limit]])

Returns an array of strings, split from the left at sep. Performs at most maxSplit splits, and slices the result into an array with at most limit elements.

Example:

S('This is all very fun').splitRight(' '); // ['This', 'is', 'all', 'very', 'fun'];
S('and I could do it forever').splitRight(' ', 1); // ['and I could do it', 'forever'];
S('but nothing matters in the end.').splitRight(' ', -1, 2); // ['the', 'end.'];
S('but nothing matters in the end.').splitRight(' ', 4, -2); // ['but nothing', 'matters'];

- startsWith(prefix)

Return true if the string starts with prefix.

Example:

S('JP is a software engineer').startsWith('JP'); //true
S('wants to change the world').startsWith('politicians'); //false

- strip([string1],[string2],...)

Returns a new string with all occurrences of [string1],[string2],... removed.

Example:

S(' 1 2 3--__--4 5 6-7__8__9--0').strip(' ', '_', '-').s; //'1234567890'
S('can words also be stripped out?').strip('words', 'also', 'be').s; //'can    stripped out?'

- stripLeft([chars])

Returns a new string in which all chars have been stripped from the beginning of the string (default whitespace characters).

Example:

S('  hello ').stripLeft().s; //'hello '
S('abcz').stripLeft('a-z').s; //'bcz'
S('www.example.com').stripLeft('w.').s; //'example.com'

- stripRight([chars])

Returns a new string in which all chars have been stripped from the end of the string (default whitespace characters).

Example:

S('  hello ').stripRight().s; //'  hello'
S('abcz').stripRight('a-z').s; //'abc'

- stripPunctuation()

Strip all of the punctuation.

Example:

S('My, st[ring] *full* of %punct)').stripPunctuation().s; //My string full of punct

- stripTags([tag1],[tag2],...)

Strip all of the HTML tags or tags specified by the parameters.

Example:

S('<p>just <b>some</b> text</p>').stripTags().s //'just some text'
S('<p>just <b>some</b> text</p>').stripTags('p').s //'just <b>some</b> text'

- template(values, [open], [close])

Takes a string and interpolates the values. Defaults to {{ and }} for Mustache compatible templates. However, you can change this default by modifying S.TMPL_OPEN and S.TMPL_CLOSE.

Example:

var str = "Hello {{name}}! How are you doing during the year of {{date-year}}?"
var values = {name: 'JP', 'date-year': 2013}
console.log(S(str).template(values).s) //'Hello JP! How are you doing during the year of 2013?'

str = "Hello #{name}! How are you doing during the year of #{date-year}?"
console.log(S(str).template(values, '#{', '}').s) //'Hello JP! How are you doing during the year of 2013?'

S.TMPL_OPEN = '{'
S.TMPL_CLOSE = '}'
str = "Hello {name}! How are you doing during the year of {date-year}?"
console.log(S(str).template(values).s) //'Hello JP! How are you doing during the year of 2013?'

- times(n)

Returns a string repeated n times.

Alias: repeat()

Example:

S(' ').times(5).s //'     '
S('*').times(3).s //'***'

- titleCase()

Returns a string with the first letter of each word uppercased, including hyphenated words

Example:

S('Like ice in the sunshine').titleCase().s // 'Like Ice In The Sunshine'
S('data_rate').titleCase().s // 'Data_Rate'
S('background-color').titleCase().s // 'Background-Color'
S('-moz-something').titleCase().s // '-Moz-Something'
S('_car_speed_').titleCase().s // '_Car_Speed_'
S('yes_we_can').titleCase().s // 'Yes_We_Can

S('   capitalize dash-CamelCase_underscore trim  ').humanize().titleCase().s // 'Capitalize Dash Camel Case Underscore Trim'

- toBoolean() / toBool()

Converts a a logical truth string to boolean. That is: true, 1, 'true', 'on', or 'yes'.

JavaScript Note: You can easily convert truthy values to booleans by prefixing them with !!. e.g. !!'hi' === true or !!'' === false or !!{} === true.

Example:

S('true').toBoolean() //true
S('false').toBoolean() //false
S('hello').toBoolean() //false
S(true).toBoolean() //true
S('on').toBoolean() //true
S('yes').toBoolean() //true
S('TRUE').toBoolean() //true
S('TrUe').toBoolean() //true
S('YES').toBoolean() //true
S('ON').toBoolean() //true
S('').toBoolean() //false
S(undefined).toBoolean() //false
S('undefined').toBoolean() //false
S(null).toBoolean() //false
S(false).toBoolean() //false
S({}).toBoolean() //false
S(1).toBoolean() //true
S(-1).toBoolean() //false
S(0).toBoolean() //false

- toCSV(options)

Converts an array or object to a CSV line.

You can either optionally pass in two string arguments or pass in a configuration object.

String Arguments:

  • delimiter: The character that is separates or delimits fields. Default: ,
  • qualifier: The character that encloses fields. Default: "

Object Configuration:

  • delimiter: The character that is separates or delimits fields. Default: ,
  • qualifier: The character that encloses fields. Default: "
  • escape: The character that escapes any incline qualifier characters. Default: \, in JS this is \\
  • encloseNumbers: Enclose number objects with the qualifier character. Default: true
  • keys: If the input isn't an array, but an object, then if this is set to true, the keys will be output to the CSV line, otherwise it's the object's values. Default: false.

Example:

S(['a', 'b', 'c']).toCSV().s //'"a","b","c"'
S(['a', 'b', 'c']).toCSV(':').s //'"a":"b":"c"'
S(['a', 'b', 'c']).toCSV(':', null).s //'a:b:c')
S(['a', 'b', 'c']).toCSV('*', "'").s //"'a'*'b'*'c'"
S(['a"', 'b', 4, 'c']).toCSV({delimiter: ',', qualifier: '"', escape: '\\',  encloseNumbers: false}).s //'"a\\"","b",4,"c"'
S({firstName: 'JP', lastName: 'Richardson'}).toCSV({keys: true}).s //'"firstName","lastName"'
S({firstName: 'JP', lastName: 'Richardson'}).toCSV().s //'"JP","Richardson"'

- toFloat([precision])

Return the float value, wraps parseFloat.

Example:

S('5').toFloat() // 5
S('5.3').toFloat()  //5.3
S(5.3).toFloat()  //5.3
S('-10').toFloat()  //-10
S('55.3 adfafaf').toFloat() // 55.3
S('afff 44').toFloat()  //NaN
S(3.45522222333232).toFloat(2) // 3.46

- toInt() / toInteger()

Return the number value in integer form. Wrapper for parseInt(). Can also parse hex values.

Example:

S('5').toInt(); //5
S('5.3').toInt(); //5;
S(5.3).toInt(); //5;
S('-10').toInt(); //-10
S('55 adfafaf').toInt(); //55
S('afff 44').toInt(); //NaN
S('0xff').toInt() //255

- toString()

Alias: s

Return the string representation of an S object. Not really necessary to use. However, JS engines will look at an object and display its toString() result.

Example:

S('my name is JP.').capitalize().toString(); //My name is JP.
var a = "Hello " + S('joe!'); //a = "Hello joe!"
S("Hello").toString() === S("Hello").s; //true

- trim()

Return the string with leading and trailing whitespace removed. Reverts to native trim() if it exists.

Example:

S('hello ').trim().s; //'hello'
S(' hello ').trim().s; //'hello'
S('\nhello').trim().s; //'hello'
S('\nhello\r\n').trim().s; //'hello'
S('\thello\t').trim().s; //'hello'

- trimLeft()

Return the string with leading and whitespace removed

Example:

S('  How are you?').trimLeft().s; //'How are you?';

- trimRight()

Return the string with trailing whitespace removed.

Example:

S('How are you?   ').trimRight().s; //'How are you?';

- truncate(length, [chars])

Truncates the string, accounting for word placement and character count.

Example:

S('this is some long text').truncate(3).s //'...'
S('this is some long text').truncate(7).s //'this is...'
S('this is some long text').truncate(11).s //'this is...'
S('this is some long text').truncate(12).s //'this is some...'
S('this is some long text').truncate(11).s //'this is...'
S('this is some long text').truncate(14, ' read more').s //'this is some read more'

- underscore()

Returns converted camel cased string into a string delimited by underscores.

Example:

S('dataRate').underscore().s; //'data_rate'
S('CarSpeed').underscore().s; //'car_speed'
S('yesWeCan').underscore().s; //'yes_we_can'

- unescapeHTML()

Unescapes the html.

Example:

S('&lt;div&gt;hi&lt;/div&gt;').unescapeHTML().s; //<div>hi</div>

- wrapHTML()

wrapHTML helps to avoid concatenation of element with string. the string will be wrapped with HTML Element and their attributes.

Example:

S('Venkat').wrapHTML().s //<span>Venkat</span>
S('Venkat').wrapHTML('div').s //<div>Venkat</div>
S('Venkat').wrapHTML('div', {
    "class": "left bullet"
}).s //<div class="left bullet">Venkat</div>
S('Venkat').wrapHTML('div', {
    "id": "content",
    "class": "left bullet"
}).s // <div id="content" class="left bullet">Venkat</div>

+ VERSION

Returns native JavaScript string containing the version of string.js.

Example:

S.VERSION; //1.0.0

Quirks

decodeHtmlEntities() converts &nbsp; to 0xa0 (160) and not 0x10 (20). Most browsers consider 0xa0 to be whitespace characters, Internet Explorer does not despite it being part of the ECMA standard. Google Closure does a good job of normalizing this behavior. This may need to be fixed in string.js at some point in time.

Testing

Node.js

Install the dev dependencies:

$ npm install string --development

Install mocha globally:

$ npm install -g mocha

Then navigate to the installed directory:

$ cd node_modules/string/

Run test package:

$ mocha test

Browser

Click here to run the tests in your web browser.

Credits

I have looked at the code by the creators in the libraries mentioned in Motivation. As noted in the source code, I've specifically used code from Google Closure (Google Inc), Underscore String Esa-Matti Suuronen, and php.js (http://phpjs.org/authors/index), Substack and TJ Holowaychuk.

Contributions

If you contribute to this library, just modify string.js, string.test.js, and update README.md. I'll update the website docs and generate the new string.min.js, changelog and version.

Contributors

(You can add your name, or I'll add it if you forget)

Roadmap to v2.0

License

Licensed under MIT.

Copyright (C) 2012-2016 JP Richardson [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Comments
  • use gulp to build

    use gulp to build

    @jprichardson I think this needs a good look.

    • moved to gulp for building it
    • uses browserify #134
    • changes main to dist/* from lib/*

    You might want to make sure bower/component/npm works with this and the test/browser.test.html is ok.

    opened by az7arul 14
  • prevent  'to: wrong argument' output caused by shelljs

    prevent 'to: wrong argument' output caused by shelljs

    `to` method in shelljs expects two arguments and throws an error if there is no second argument, so passing an extra string to keep it happy
    

    This fixes #127 although I am not entirely sure if we should do it or not.

    @jprichardson thoughts ?

    opened by az7arul 10
  • Let trim* method accept arguments

    Let trim* method accept arguments

    I am requesting a new feature. This is about to let trim* method (trim, trimLeft, trimRight) to accept arguments.

    The arguments can be:

    • A string (char), indicates the string to be trimmed
    • A list of strings,indicates the strings to be trimmed Or, make the whole argument list as target strings, just like this proposal

    How do you think about this?

    opened by lijunle 8
  • Add Ability to Easily Extend string.js

    Add Ability to Easily Extend string.js

    Making string.js More Extensible

    I love string.js! However, I'd like to have the ability to easily extend it in a safe, consistent and simple manner.

    Why?

    Simply stated, I have requirements that string.js does not meet on its own. I'm sure that others do too. Now someone in my position could simply modify string.js and submit a pull request. But what if some of the changes really don't belong in a generic library like string.js? What if they're too specific to a particular application domain? The author, as they should, would nix those changes.

    So extending string.js, just like the author has extended the native Javascript String object, is the way to go.

    Why Not Just Modify the String.js Prototype?

    What if you're working on a large project with several developers or using other libraries which depend on string.js and you modify string.js? Now you're in the same boat as you would be if you modified the String Object prototype. Other modules may use string.js and expect certain behavior that you have modified. Bugs galore!

    For an example, one of my requirements is case insensitivity with the ability to use case sensitivity on demand. If I modify the string.js replaceAll method to make it default to case insensitive matches, other code that expects string.js to be case sensitive will break.

    How?

    While attempting to create such a string.js extension, I found that string.js needed to be modified in four simple ways. The good news is, none of the changes affect the module's interface. Everything works just like before. The changes just make it more generic and extensible.

    1. Add an initialze function containing the current S constructor's code.

    The S constructor and the new setValue method (see below) both call this new function.

    2. Add a setValue method.

    This method allows the constructor in a module which extends string.js to set the string value just like the S constructor without knowing how to set the necessary values (s and orig) itself.

    The constructor in this other module would look something like this:

    ExtendedString.prototype = S('');
    
    ExtendedString.prototype.constructor = ExtendedString;
    
    function ExtendedString (value) {
        this.setValue(value);
    }
    

    3. Change returned values from methods.

    Instead of returning a new S object, methods must return a new object using the given object's constructor. For string.js objects, the constructor will be S. For extended objects, it will be the object's constructor (ExtendedString in the example above). This way, string.js methods will always return the same kind of object that they were given.

    Doing this prevents each extended module from having to jump through hoops to pull in the string.js methods and force them to return its own kind of object. This is what string.js has to do to get the native String object methods to return string.js objects (in the Attach Native JavaScript String Properties section).

    So each method that returns a new string, now does this:

    return new this.constructor(s);
    

    Instead of this:

    return new S(s);
    

    4. Set the constructor to S after setting the prototype.

    When setting the prototype of S to the object containing the methods, the constructor is obliterated. So string.js objects are instanceof Object but they're not instanceof S when they should be.

    Extension Example

    The following code shows a string.js extension module which creates and manipulates ExtendedStrings. It modifies the contains string.js method to default to case-insensitive searches and provides callers with a way to force the search to be case sensitive.

    var CASE_OPTIONS;
    var parentPrototype;
    var stringJSObject;
    
    //-------------------------------------------------------------------------------------
    // caseOptions
    //-------------------------------------------------------------------------------------
    CASE_OPTIONS =
        Object.freeze({
            CASE_INSENSITIVE : 'CASE_INSENSITIVE',
            CASE_SENSITIVE   : 'CASE_SENSITIVE'
        });
    
    //-------------------------------------------------------------------------------------
    // ExtendedStrings constructor
    //-------------------------------------------------------------------------------------
    stringJSObject = S('');
    
    parentPrototype = Object.getPrototypeOf(stringJSObject);
    
    ExtendedStrings.prototype = stringJSObject;
    
    ExtendedStrings.prototype.constructor = ExtendedStrings;
    
    function ExtendedStrings (value) {
        this.setValue(value);
    }
    
    //-------------------------------------------------------------------------------------
    // extendedStringMaker
    //-------------------------------------------------------------------------------------
    function extendedStringMaker (value) {
        if (value instanceof ExtendedStrings) {
            return value;
        }
    
        return new ExtendedStrings(value);
    };
    
    //-------------------------------------------------------------------------------------
    // contains
    //-------------------------------------------------------------------------------------
    ExtendedStrings.prototype.contains =
        function contains (value, caseOption) {
            if (caseOption === CASE_OPTIONS.CASE_SENSITIVE) {
                return parentPrototype.contains.call(this, value);
            }
            else {
                return parentPrototype.contains.call(this.toUpperCase(), value.toUpperCase());
            }
        };
    
    //-------------------------------------------------------------------------------------
    // Set this module's public interface.
    //-------------------------------------------------------------------------------------
    extendedStringMaker.CASE_OPTIONS = CASE_OPTIONS;
    
    return extendedStringMaker;
    

    Example usage:

    extendedStringMaker('This is a test').contains('this'); // true
    
    opened by jeffgrann 8
  • Introducing equalsIgnoreCase

    Introducing equalsIgnoreCase

    @jprichardson Pull request introducing equalsIgnoreCase function, I've been using String and I really missed this function.

    usage:

    S('StringJS').equalsIgnoreCase('stringjs')

    opened by marcelocure 7
  • Add rsplit

    Add rsplit

    For a project I just needed something like Python's rsplit, but I was sad to see that it isn't available in string.js.

    Would it be possible to add it?

    Example code from this StackOverflow question:

    String.prototype.rsplit = function(sep, maxsplit) {
        var split = this.split(sep);
        return maxsplit ? [ split.slice(0, -maxsplit).join(sep) ].concat(split.slice(-maxsplit)) : split;
    }
    
    opened by danieldiekmeier 7
  • Add support for format or sprintf

    Add support for format or sprintf

    A simple replace function for things like "Hello {1} {2}".format("Peter", "Rottmann");

    or with names "Hello {firstname} {lastname}".format({ firstname: "Peter", lastname: "Rottmann" });

    will be a nice features.

    opened by rottmann 7
  • Add .titleCase()

    Add .titleCase()

    Capitalizes first character of each word, including hyphenated words. Is not smart enough to recognize articles, however.

    Here's a test case: http://jsfiddle.net/edelman/DJvUj/ And with the minified file: http://jsfiddle.net/edelman/DJvUj/1/

    opened by ultimatedelman 6
  • Request new feature

    Request new feature ".toTitleCase"

    Wish to has a function to convert string to title case.

    for example: 'a simple CCTV test' -> 'A Simple CCTV Test'

    It's good only convert words except 'a, and, the... etc' And do not lower user's input upper case

    enhancement 4 
    opened by chongwang87 6
  • toBoolean()

    toBoolean()

    this is always a bitch.

    "true" and "false" => true and false

    something like this:

    return val === 'true' ? true : (val === 'false' ? false : val);

    interfaced like so:

    S(myVariable).toBoolean()

    would be awesome to have this functionality. i can see how this could suck though if the value isn't "true" or "false" to begin with, but if it just returns the original value, that's probably good enough.

    opened by ultimatedelman 6
  • capitalize() works as unexpected

    capitalize() works as unexpected

    S('this post').capitalize().camelize().s; // returns ThisPost But S('this post').camelize().capitalize().s; // returns Thispost

    capitalize() IMO should only change the first character and not worry about the rest.

    opened by ghost 5
  • Who to contact for security issues

    Who to contact for security issues

    Hello 👋

    I run a security community that finds and fixes vulnerabilities in OSS. A researcher (@sno2) has found a potential issue, which I would be eager to share with you.

    Could you add a SECURITY.md file with an e-mail address for me to send further details to? GitHub recommends a security policy to ensure issues are responsibly disclosed, and it would help direct researchers in the future.

    Looking forward to hearing from you 👍

    (cc @huntr-helper)

    opened by benharvie 0
  • Bump uglify-js from 1.3.5 to 3.6.3

    Bump uglify-js from 1.3.5 to 3.6.3

    Bumps uglify-js from 1.3.5 to 3.6.3.

    Release notes

    Sourced from uglify-js's releases.

    v3.6.3

     

    v3.6.2

     

    v3.6.1

     

    v3.6.0

     

    v3.5.15

     

    v3.5.14

     

    v3.5.13

     

    v3.5.12

     

    v3.5.11

     

    v3.5.10

     

    v3.5.9

     

    v3.5.8

     

    v3.5.7

     

    v3.5.6

     

    v3.5.5

     

    v3.5.4

     

    v3.5.3

     

    ... (truncated)
    Commits
    Maintainer changes

    This version was pushed to npm by alexlamsl, a new releaser for uglify-js since your current version.


    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot ignore this [patch|minor|major] version will close this PR and stop Dependabot creating any more for this minor/major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 0
  • S('foo<br>bar').stripTags() == 'foobar'

    S('foo
    bar').stripTags() == 'foobar'

    IMO should be 'foo bar', no? More relevant use case:

    let body = 'Hello {name}!<br>Please verify your email at url <a href="https://example.com/verify/12345">https://example.com/verify/1234</a><br>See you soon'
    let text = S(body).stripTags().s
    // Hello {name}!Please verify your email at url https://example.com/verify/12345See you soon
    //              ^                                                               ^
    
    opened by HxxxxxS 1
  • [WIP] v4-refactor

    [WIP] v4-refactor

    This is an Initial POC/WIP refactoring work (v4) to modernize the codebase and separate out the functions into individual files.

    I've been looking into using https://github.com/lerna/lerna, but imagine this project will also need to be converted into an org so we can scope these individual functions into standalone packages.

    Converted

    For these functions to be considered "converted", each one requires it to be extracted from lib/string.js and moved to the following file structure:

    src/{name}/test/fixtures.js
    src/{name}/index.d.ts
    src/{name}/index.js
    
    • [x] between
    • [ ] camelize
    • [ ] capitalize
    • [ ] chompLeft
    • [ ] chompRight
    • [ ] collapseWhitespace
    • [ ] contains
    • [ ] count
    • [ ] dasherize
    • [ ] equalsIgnoreCase
    • [ ] latinise - Remove in favor of another npm package
    • [ ] decodeHtmlEntities - Remove in favor of another npm package
    • [ ] escapeHTML - Remove in favor of another npm package
    • [ ] ensureLeft
    • [ ] ensureRight
    • [ ] humanize
    • [ ] isAlpha
    • [ ] isAlphaNumeric
    • [ ] isEmpty
    • [ ] isLower
    • [ ] isNumeric
    • [ ] isUpper
    • [ ] left
    • [ ] lines
    • [ ] pad
    • [ ] padLeft
    • [ ] padRight
    • [ ] parseCSV - Remove in favor of another npm package
    • [ ] replaceAll
    • [ ] splitLeft
    • [ ] splitRight
    • [ ] strip
    • [ ] stripLeft
    • [ ] stripRight
    • [ ] right
    • [ ] setValue - Remove in favor of StringJs.create()
    • [ ] slugify
    • [ ] stripPunctuation
    • [ ] stripTags
    • [x] template
    • [ ] times
    • [ ] titleCase
    • [ ] toBoolean
    • [ ] toFloat
    • [ ] toInt
    • [ ] truncate
    • [ ] toCSV - Remove in favor of another npm package
    • [ ] underscore
    • [ ] unescapeHTML - Remove in favor of another npm package
    • [ ] wrapHTML

    Todo

    • [ ] Merge the proxying of native String methods back into StringJs
    • [ ] Add back coverage support
    • [ ] Add back browser testing
    • [ ] Deprecate methods removed above in v3 and create a new release to let people know.

    x-ref: #183

    opened by markhalliwell 0
  • High Severity Vulnerability Alert

    High Severity Vulnerability Alert

    CVE-2017-16116 high severity Vulnerable versions: <= 3.3.3

    The string module is vulnerable to regular expression denial of service when specifically crafted untrusted user input is passed into the underscore or unescapeHTML methods.

    opened by joarwilk 7
Owner
JP Richardson
The Times 03/Jan/2009 Chancellor on brink of second bailout for banks.
JP Richardson
String manipulation helpers for javascript

The stable release documentation can be found here https://epeli.github.io/underscore.string/ Underscore.string Javascript lacks complete string manip

Esa-Matti Suuronen 3.4k Dec 25, 2022
easier than regex string matching patterns for urls and other strings. turn strings into data or data into strings.

url-pattern easier than regex string matching patterns for urls and other strings. turn strings into data or data into strings. This is a great little

null 562 Jan 5, 2023
Lo-fi, powerful, community-driven string manipulation library.

Lo-fi, powerful, community-driven string manipulation library. This is the main monorepo codebase of Plexis.js a production-ready string manipulation

Plexis 145 Sep 24, 2022
A robust HTML entity encoder/decoder written in JavaScript.

he he (for “HTML entities”) is a robust HTML entity encoder/decoder written in JavaScript. It supports all standardized named character references as

Mathias Bynens 3.2k Dec 27, 2022
Multiline strings in JavaScript

multiline Multiline strings in JavaScript No more string concatenation or array join! Use ES2015 template literals instead whenever possible. Before c

Sindre Sorhus 1.4k Dec 30, 2022
Javascript URL mutation library

URI.js About Understanding URIs Documentation jQuery URI Plugin Author Changelog IMPORTANT: You may not need URI.js anymore! Modern browsers provide t

Medialize 6.2k Dec 30, 2022
Lightweight URL manipulation with JavaScript

domurl 2.x (former jsurl) Lightweight URL manipulation with JavaScript for both DOM and server JavaScript. Goal To have a convenient way working with

Mykhailo Stadnyk 511 Dec 28, 2022
sprintf.js is a complete open source JavaScript sprintf implementation

sprintf-js sprintf-js is a complete open source JavaScript sprintf implementation for the browser and Node.js. Note: as of v1.1.1 you might need some

Alexandru Mărășteanu 2k Jan 4, 2023
Helps to encode a string to base64 and decode a base64 string to a normal string.

@prashoonb/base64-encoder-decoder Installation npm install @prashoonb/base64-encoder-decoder API base64.encode(input) This function takes a byte strin

PrashoonB 4 Mar 29, 2022
This extensions adds blocks to help you create your own carnival games in MakeCode Arcade using throwable balls, extra timer functions, and extra game-over options.

Usage This extensions adds blocks to help you create your own carnival games in MakeCode Arcade using throwable balls, extra timer functions, and extr

Microsoft 6 Nov 16, 2022
Extended magic-string with extra utilities

DEPRECATED. It has been ported back to magic-string >= 0.26.0 magic-string-extra Extended Rich-Harris/magic-string with extra utilities. Install npm i

Anthony Fu 130 Sep 8, 2022
:ledger: Minimal lightweight logging for JavaScript, adding reliable log level methods to wrap any available console.log methods

loglevel Don't debug with logs alone - check out HTTP Toolkit: beautiful, powerful & open-source tools for building, testing & debugging HTTP(S) Minim

Tim Perry 2.3k Jan 6, 2023
Provides simple and the most useful methods to string operations in JavaScript / Node.js

?? Strops (String Operations) Provides simple methods for the most useful operations with substrings: - remove, replace, get from A to B, get from A t

Max Shane 3 May 20, 2022
Convert some JavaScript/TypeScript code string into a .d.ts TypeScript Declaration code string

convert-to-dts Converts the source code for any .js or .ts file into the equivalent .d.ts code TypeScript would generate. Usage import { convertToDecl

Lily Scott 11 Mar 3, 2022
Seamless and lightweight parallax scrolling library implemented in pure JavaScript utilizing Hardware acceleration for extra performance.

parallax-vanilla.js Seamless and lightweight parallax scrolling library implemented in pure JavaScript utilizing Hardware acceleration for extra perfo

Erik Engervall 91 Dec 16, 2022
An Easy to use and advanced working multiguild Waitingroom Bot written in discord.js v13 without any extra modules.

Multiguild-Waitingroom-v13 An Easy to use and advanced working multiguild Waitingroom Bot written in discord.js v13 without any extra modules. It is m

Tomato6966 17 Dec 11, 2022
Markdown Plus is a markdown editor with extra features

Markdown Plus Markdown Plus ("M+" or "mdp" for short) is a markdown editor with extra features. Online demo: mdp.tylingsoft.com Apps We currently don'

null 2.1k Dec 29, 2022
DevArms - a collection of developer utils that gives you extra arms to reach more in your tasks

DevArms is a collection of developer utils that gives you extra arms to reach more in your tasks. It runs completely offline, and cross-platform across Windows, Mac and Linux. Written in Rust, React. Powered by Tauri.

Qiushi Pan 82 Dec 18, 2022
Phone for QB-Core Framework. Edited for a NP-Style look with a few extra things

Phone for QB-Core Framework. Edited for a NP-Style look with a few extra things, This file has been edited with the changes noted

Booya 50 Jan 7, 2023
Lavalink client with lots of extra functionality, easy to use and well optimized.

?? nSysLava Lavalink client with lots of extra functionality, easy to use and well optimized! พัฒนาโดยคนไทย ?? Many utility functions - มีฟังก์ชันอรรถ

nicenathapong 3 Apr 12, 2022