A Javascript string manipulation library
npm install strings.jsstrings.js
==========
A Javascript string manipulation library. No cryptic names, no methods returning undefined when you expect a string, etc.. Contains the usual suspects and some handy additions.
key features:
- most methods are available both static and dynamic
- most methods are chainable for the dynamic part
- Strings is 1 based; 1 is the first character of the string, -1 the last
- can use negative numbers in almost all methods to target from the end of the string
- can target with indexes and substring arguments mixed in some methods
- all methods are dynamically type checked if needed
- methods should always return the expected type
______________________________________________
javascript
// discover strings:
var str= new Strings( 'asagcnaicrbtas' )
.reverse().remove('a','b','c').upper(1).append('!');
console.log( str.$ );
// Strings!
str.lower('s').remove('!').append( '.js' );
console.log( str.$ );
// strings.js// 33 character password from all printable ascii:
console.log( Strings.random(33) );
// 9&@w=Q+|Gxe
NzL='Q8?4IxAg0dUyq}]s (pseudo)// or from numbers only:
console.log( Strings.random(33, Strings.ASCII_RANGE_NUMBERS) );
// 326579354237121359463402643861378
console.log( Strings.sort('326579354237121359463402643861378') );
// '011122223333333444455566667778899'
var sparse= ' \t\t max. 1 \t consecutive \t \tspace! \t ';
console.log( Strings.oneSpaceAndTrim(sparse) );
// max 1 consecutive space!
// there is so much more, see below
``
___
Made for browser and/or node.js. You can use npm install strings.js at the base directory of your project, after that:`javascript`
var Strings= require( 'strings.js' );
javascript
require.config({
paths: {
'strings', [ 'path/to/strings.min.js' ]
}
});require( ['strings'], function( Strings ){
console.log( Strings.times(':)', 3) );
// :):):)
});
`
___
$3
Because strings.js is build upon types.js it is very robust. Almost any String type argument accepts a Number type and viceversa.
All input and output is checked for type validity. If you expect a
typeof 'string', you won't get undefined or
any other type that can break following code.All string indexes in strings.js are 1 based and translated to 0 based internally. Negative indexes can be used in most functions. -1 references
the last character in the string, 1 references the first character in the string.
The
new Strings() object is made for chaining operations on strings, most of it's methods return their own context.
To return the actual value of the string, one can use .get() or .$ or .string.Almost all functionality of Strings prototypes, and some extra functions, are available from Strings static
function library.
$3
types.js essential type-checker/enforcer is included in strings.js. It is the fundament for strings.js and can be found after loading strings.js.
`javascript
var types= Strings.Types;
`The types.js API can be found at: https://github.com/phazelift/types.js.
$3
`javascriptvar Strings= require("strings.js"); // with node.js
// all comments reflect the value of s.string, except for .wrap
var s= new Strings( 'HELLO STRINGS!' );
s.lower('ello', 'trings').insert(' library', -1); // Hello Strings library!
s.lower('h').toCamel(' ').remove('!'); // helloStringsLibrary
s.unCamel(' ').upper(1, 7).push('!'); // Hello Strings library!
s.remove('Hello', 'library', ' '); // Strings!
s.prepend('reversed ').reverse(); // !sgnirtS desrever
s.shuffle(); // getriever!nrss dS (random on every run)
s.set(); // (.set with no usable argument wipes the string)
// wrap 'Strings' .wrap returns the wrapped string
s.set('Strings').setWrap('<3 ', '!').wrap; // <3 Strings!
// apply the wrap to s while wrapping it once again
s.applyWrap('I ', '!!')
s.get(); // I <3 Strings!!!
// some static Methods
// all comments reflect the value of s
s= ' spaces or tabs in here? ' ;
s= Strings.replace(s, 'not in s', 'ignored..'); // spaces or tabs in here?
Strings.split(s+ '\t\t \t '); // [ 'spaces', 'or', 'tabs', 'in', 'here?' ]
s= Strings.oneSpaceAndTrim(s); // spaces or tabs in here?
s= Strings.remove(s, ' in here?'); // spaces or tabs
s= Strings.xs(s, function(char, index){ // SpAcEs oR TaBs
if (index %2 === 0)
return char.toUpperCase();
return true;
});
Strings.toCamel('a,comma,seperated,string?', ','); // aCommaSeperatedString?
Strings.unCamel('aUnderscoredString', '_'); // a_underscored_string
Strings.times('A', 3); // AAA
s= Strings.random(20); // j#4-s,t0]
bRd86!,>=Z (create password/random string)
___
API
---In this API, the type of a dynamic Strings object is denoted with
. Therefore, all methods returning
can be used for chaining.this.string represents the actual state of the internal dynamic string.
____
Strings constructor
>
> Calls .set internally, so .set rules apply, see below.
`javascript
var string= new Strings( 'All those characters..' );
`Strings.prototype.string
>
> Internal/contextual string, do not set directly, use .set() instead to prevent bugs. You can of course use
> .string as a getter to fetch the string.
`javascript
var myString= new Strings('The actual string');
console.log( myString.string );
// The actual string
`Strings.prototype.set
>
> Sets this.string to string arguments, or resets this.string to '' if no argument is given. Arguments that are not of
> type String or Number will not be set.
`javascript
var string= new Strings();
string.set('James ', 'Bond ', 'is ', 0, 0, 7);
// James Bond is 007
`
Strings.prototype.sort
> > Returns this.string's characters sorted by their ordinal value.
`javascript
var string= new Strings( 'sort', 'charcters', 'and', 5, 2, 9, 1 );
console.log( string.sort().$ );
// 1259aaccdehnorrrsstt
`Strings.prototype.random
>
> Applies a random string with amount characters within asciiRange. asciiRange is an Array with two indices; [min, max].
`javascript
// 10 random special characters
console.log( new Strings().random(10, Strings.ASCII_RANGE_SPECIAL_1) );
// &!! %.,./*
`Strings.prototype.xs
>
> Access every index of this.string and apply the result of the callback to it.
> If the callback returns true, char is applied. If the callback returns false or undefined, char will be skipped.
> Any character, String or Number returned by callback will be applied to index in string.
`javascript
var string= new Strings('It is easy to change characters in any way!');
string.xs( function(ch){
return (ch === ' ') ? ' * ' : true;
});
console.log( string.$ );
// It is easy to change characters in any way!
`Strings.prototype.times
>
> Duplicates this.string by amount, or leaves this.string unchanged if no amount is given.
`javascript
console.log( new Strings('<3 ').times(3).$ );
// <3 <3 <3
`Strings.prototype.get
>
> Returns one or more indexes in a new string, without affecting this.string.
> Without arguments get() returns the full this.string.
`javascript
var string= new Strings('sdblaem');
console.log( string.get(5, 1, 1, -2, -1, 3, 4, -2, 2) );
// assembled
`Strings.prototype.$
>
(getter)> Returns this.get()
Strings.prototype.copy
>
> Returns a substring of this.string from offset to offset+amount.
> If amount is not given, all characters from offset to end of this.string are returned.
> If no arguments are given, a full copy of this.string is returned.
`javascript
var string= new Strings('copy a part');
console.log( string.copy(-4, 4) );
// part
`Strings.prototype.empty
>
> Returns true if this.string.length is < 1.
`javascript
console.log( new Strings().empty() );
// true
`Strings.isAlpha
>
> Returns true if this.string is in the range ['a'..'z'] and/or ['A'..'Z']
`javascript
console.log( new Strings('abcIsAlpha').isAlpha() );
// true
`Strings.isNumeric
>
> Returns true if this.string is in the range ['0'..'9']
`javascript
console.log( new Strings('123').isNumeric() );
// true
`Strings.isAlphaNumeric
>
> Returns true if this.string is in the range ['a'..'z'] and/or ['A'..'Z'] and/or ['0'..'9']
`javascript
console.log( new Strings('abc123').isAlphaNumeric() );
// true
`Strings.isSpecial
>
> Returns true if this.string is NOT in the range ['a'..'z'] and/or ['A'..'Z'] and/or ['0'..'9'], but in the
> range of all printable ascii characters.
`javascript
console.log( new Strings('!@ #$').isSpecial() );
// true
// note that space is a special character!
`Strings.prototype.isSpace
>
> Returns true if this.string contains no characters other than spaces and/or horizontal tabs.
`javascript
console.log( new Strings(' \t ').isSpace() );
// true
`Strings.prototype.isUpper
>
> Returns true if this.string contains only uppercase characters.
`javascript
console.log( new Strings('ABC').isUpper() );
// true
`Strings.prototype.hasUpper
>
> Returns true if this.string contains at least one uppercase character.
`javascript
console.log( new Strings('aBc').hasUpper() );
// true
`Strings.prototype.isLower
>
> Returns true if this.string contains only lowercase characters.
`javascript
console.log( new Strings('abc').isLower() );
// true
`Strings.prototype.push
>
> Append string(s) to this.string.
`javascript
var string= new Strings('add to this ').push('string', '?');
console.log( string.$ );
// add to this string?
`Strings.prototype.pop
>
> Removes amount characters starting from the end of this.string going backwards, no arguments pops only one
> character.
`javascript
var string= new Strings('remove characters from the end').pop(13);
console.log( string.$ );
// remove characters
`Strings.prototype.prepend
>
> Prepend this.string with string(s).
`javascript
var string= new Strings('to prepend').prepend(1, '. some', ' strings ');
console.log( string.$ );
// 1. some strings to prependStrings.prototype.insert
>
> Insert insertion at pos in this.string, the insertion will be inserted before the character at pos. If insertion is invalid,
> or index is less than the negative length of the string, insertion will be prepended to the string. If pos is greater than
> the length of the string, insertion will be appended to the string.
> Multiple positions are allowed, but duplicate positions ignored. Positions are relative to the string before insertion,
> so, if our string is
'123' and we insert '-' at position 2 and 3, we will get '1-2-3'.
`javascript
var string= new Strings('wherearethespaces?').insert(' ', 6, 9, 12 );
console.log( string.$ );
// where are the spaces?Strings.prototype.trim
>
> Removes white space characters, including spaces, tabs, form feeds, line feeds and other Unicode spaces, from the
> beginning and the end of the string.
`javascript
var string= new Strings(' \t remove leading and trailing tabs and spaces \t').trim();
console.log( string.$ );
// remove leading and trailing tabs and spaces
`Strings.prototype.trimLeft
>
> Removes white space characters, including spaces, tabs, form feeds, line feeds and other Unicode spaces
> from the start of this.string.
Strings.prototype.trimRight
>
> Removes white space characters, including spaces, tabs, form feeds, line feeds and other Unicode spaces
> from the end of this.string.
Strings.prototype.oneSpace
>
> Reduces all consecutive horizontal tabs and/or spaces found in this.string to a maximum of one.
`javascript
var string= new Strings('sparse strings \t cleaned up!').oneSpace();
console.log( string.$ );
// sparse strings cleaned up!Strings.prototype.oneSpaceAndTrim
>
> Applies this.trim() and this.oneSpace() on this.string.
`javascript
var string= new Strings(' \t sparse strings \t cleaned up! \t ').oneSpaceAndTrim();
console.log( string.$ );
// sparse strings cleaned up!Strings.prototype.find
>
> Returns an array containing all indexes where substring is found, or an empty array if there is no match.
> If flags is set to an empty string, only the first occurance of the found substring will be pushed into the array.
> find internally uses RegExp, so flags is 100% compatible with RegExp flags.
`javascript
console.log( new Strings('find character positions').find(' ') );
// [ 5, 15 ]
`Strings.prototype.count
>
> Returns the amount of times substring is found in this.string.
`javascript
console.log( new Strings('now count the spaces in this string').count(' ') );
// 6
console.log( new Strings('count substrings in this string').count('string') );
// 2
`Strings.prototype.contains
>
> Returns true if string is a substring of this.string, false if not.
`javascript
console.log( new Strings('any spaces in here?').contains('spaces') );
// true
`Strings.prototype.between
>
> Returns the string between before and after. The first occurance of before and the last occurance of
> after are matched. An empty string is returned in case of no match.
`javascript
console.log( new Strings('what is (between) the braces?').between('(', ')') );
// between
`Strings.prototype.slice
>
> Crop this.string from offset with amount.
`javascript
console.log( new Strings('fetch a slice of this').slice(9, 5).$ );
// slice
`Strings.prototype.crop
> An alias for slice.
Strings.prototype.truncate
>
> Removes all characters after offset from this.string, and optionally add a suffix.
`javascript
var string= new Strings('is truncate pop with a suffix?')
.truncate(15, '? No, it counts from the start, and you can add a suffix.');
console.log( string.$ );
// is truncate pop? No, it counts from the start and you can add a suffix.
`Strings.prototype.remove
>
> - Arguments are substrings - Remove all found/matching strings given as arguments from this.string.
`javascript
var string= new Strings('what is the lifetime of a string?');
console.log( string.remove( 'what', 'is ', '?').$ );
// the lifetime of a string
`Strings.prototype.removeRange
>
> Removes amount character(s) from this.string, starting from index.
`javascript
var string= new Strings('what is the lifetime of a string?');
console.log( string.removeRange(8, 16).$ );
// what is a string?
`Strings.prototype.removePos
>
> - Arguments are indices - Remove all (one character) positions given as arguments, from this.string.
`javascript
var string= new Strings('remove single characters from this string?');
console.log( string.removePos(-1, 1, 2) );
// move single characters from this string
`Strings.prototype.replace
>
> Replace the first or every occurence of subString in this.string with replacement depending on flags.
> As Strings.replace internally uses RegExp you can set flags to your liking. flags defaults to 'g' (global)
`javascript
console.log( new Strings('almost standard..').replace('almost', 'not so').$ );
// not so standard
`Strings.prototype.reverse
>
> Reverses this.string.
`javascript
console.log( new Strings('desrever').reverse().$ );
// reversed
`Strings.prototype.upper
>
> If arg(s) are number(s), the character(s) in this.string at index or indexes are changed to uppercase.
> If arg(s) are character, all matching characters in this.string are changed to uppercase.
> Multiple character strings are matched as well.
`javascript
console.log( new Strings('change case').upper('c').$ );
// Change Case
console.log( new Strings('change case').upper(1, 3, 5, -2, -4).$ );
// ChAnGe CaSe
`Strings.prototype.lower
>
> Same as .upper, it only changes uppercase characters to lowercase.
Strings.prototype.shuffle
>
> Randomizes(pseudo) the position of each character in this.string.
`javascript
console.log( new Strings('shuffle').shuffle().$ );
// fsfluhe (pseudo random)
`Strings.prototype.toCamel
>
> Converts every following character matching char in this.string to uppercase, and removes char.
`javascript
console.log( new Strings('underscores_to_camels').toCamel('_').$ );
underscoresToCamels
`Strings.prototype.unCamel
>
> Converts this.string camels to lower-case with insertion prepended. Insertion defaults to dashes, but can be set
> to any character of your liking.
`javascript
console.log( new Strings('underscoresFromCamels').unCamel('_').$ );
underscores_from_camels
`Strings.prototype.startsWith
>
> Returns true if this.string starts with start, false if not.
`javascript
console.log( new Strings('abc 123').startsWith('ab') );
// true
`Strings.prototype.endsWith
>
> Returns true if this.string ends with ending, false if not.
`javascript
console.log( new Strings('abc 123').endsWith('23') );
// true
`Strings.prototype.charactersMatch
>
> Returns true if the count for each specific character in this.string is equal to the string given
`javascript
console.log( new Strings('abc').charactersMatch('cba') );
// true
`Strings.prototype.setWrap
>
> Sets a wrapper that wraps this.string between prepend and append.
> Output of .get() or .$ is not affected by setWrap. Fetch .wrap to return the wrapped this.string
> You can add to prepend and append (outwards) by calling .setWrap again.
`javascript
var string= new Strings('<3').setWrap( 'she ', ' me');
// string is unchanged
console.log( string.$ );
// <3// but .wrap shows the string wrapped
console.log( string.wrap+ '!' );
// she <3 me!
// and why not wrap once more:
string.setWrap('Will ', ' forever?');
console.log( string.wrap );
// Will she <3 me forever?
// still not applied to string
console.log( string.$+ '..' );
// <3..
// use .setWrap to apply the wrap to string
string.applyWrap();
console.log( string.$ );
// Will she <3 me forever?
`Strings.prototype.removeWrap
>
> Removes the wrapper.
Strings.prototype.applyWrap
>
> Calls setWrap and wraps the wrapper with prepend and append if set already. Then the total wrap is applied
> to this.string. Finally the wrapper method will be reset with removeWrap.
`javascript
var string= new Strings('<3').applyWrap( 'She ', '\'s me!');
console.log( string.$ );
// She <3's me!
`Strings.prototype.wrap
>
(getter)> Returns this.string wrapped by the text set with setWrap(). If setWrap() has not been called yet, only this.string
> will return.
________________
Static functions
----------------
See descriptions for similar functions above.
______________________________________________
Strings.create
>
> Returns an assembled string from given arguments of type String. Non String arguments are omitted.
> If no valid arguments are given, an empty string will be returned.
Strings.get
>
> Returns a string containing every position from string given, in the order they were given.
> Invalid positions are ignored and won't disrupt the process.
Strings.sort
>
Strings.random
>
Strings.times
>
Strings.regEscape
>
> Returns string with all found special regular expression characters in string escaped.
Strings.empty
>
Strings.isAlpha
>
Strings.isNumeric
>
Strings.isAlphaNumeric
>
Strings.isSpecial
>
> Returns true if string contains only special ascii characters.
Strings.isSpace
>
Strings.isUpper
>
Strings.hasUpper
>
Strings.isLower
>
Strings.xs
>
Strings.copy
>
Strings.replace
>
Strings.trim
>
Strings.trimLeft
>
Strings.trimRight
>
Strings.oneSpace
>
Strings.oneSpaceAndTrim
>
Strings.toCamel
>
Strings.unCamel
>
Strings.shuffle
>
Strings.find
>
Strings.count
>
Strings.contains
>