README
str-utils
JavaScript String Utilities based on Apache Commons StringUtils
Version 1.1.0
usage
npm install str-utils
var strUtils = require('str-utils');
methods
abbreviate(string, maxWidth, offset) : string, null
If offset is not set, abbreviates the maxWidth
of a string with an ellipsis, otherwise abbreviates the beginning and end of a string, beginning at offset
and ending at maxWidth
.
var string = strUtils.abbreviate('Java is to JavaScript as Car is to Carpet.', 20, 5);
// '...is to JavaScript...'
abbreviateMiddle(string, middle, length) : string, null
Abbreviates the middle of a string starting at length
, with characters defined in middle
. Returns null
if string
is null.
var string = strUtils.abbreviateMiddle('Hello', '..', 3);
// 'Hel..lo'
difference(string, comparison) : string, null
Returns the difference between two strings. Returns null
if string
is null
.
var string = strUtils.difference('I am a human', 'I am a machine');
// 'machine'
capitalize(string) : string, null
Returns string with first letter to uppercase. Returns null
if string
is null
.
var string = strUtils.capitalize('cAT');
// 'CAT'
var string = strUtils.capitalize('Cat');
// 'Cat'
chomp(string) : string, null
Returns string with a single line-feed or carriage return character removed. Returns null
if string
is null
.
var string = strUtils.chomp('foo\n');
// 'foo'
var string = strUtils.chomp('foo\r\n');
// 'foo\r'
var string = strUtils.chomp('foo\r\n\r');
// 'foo\r\n';
var string = strUtils.chomp(null);
// null
endsWith(string, suffix) : boolean
Returns true
if a string ends with the defined suffix
. Otherwise returns false
.
var string = strUtils.endsWith('Hello', 'llo');
// true
var string = strUtils.endsWith('Hello', 'LLO');
// false
endsWithIgnoreCase(string, suffix) : boolean
Returns true
if a string ends with the defined case-insensitive suffix
. Otherwise returns false
.
var string = strUtils.endsWith('Hello', 'LlO');
// true
var string = strUtils.endsWith('Hello', 'llo');
// true
endsWithAny(string, suffixArray) : boolean
Returns true
if a string ends with any of the the defined suffix
es. Otherwise returns false
. Case sensitive.
var string = strUtils.endsWith('abc', ['ab', 'ba']);
// false
var string = strUtils.endsWith('abc', ['bc', 'bb']);
// true
indexOfDifference(string, comparison) : int
Returns the index at which two strings begin diverging.
var string = strUtils.indexOfDifference(null, null);
// -1
var string = strUtils.indexOfDifference('hello', 'hello');
// 0
var string = strUtils.indexOfDifference('abcde', 'abxyz');
// 2
isAllLowercase(string) : boolean
Returns true
if all of the characters in a string are lowercase. Otherwise returns false
.
Null and empty strings return false
var string = strUtils.isAllLowercase('asdf');
// true
var string = strUtils.isAllLowercase('ASdf');
// false
isAllUppercase(string) : boolean
Returns true
if all of the characters in a string are uppercase. Otherwise returns false
.
Null and empty strings return false
var string = strUtils.isAllLowercase('ASDF');
// true
var string = strUtils.isAllLowercase('ASdf');
// false
isAnyEmpty(string) : boolean
Returns true
if any of the strings are empty (''). Otherwise returns false
.
var string = strUtils.isAnyEmpty('', ' ');
// true
var string = strUtils.isAnyEmpty('asdf', ' ', 'Hello', ' ', 123);
// false
isEmpty(string) : boolean
Returns true
if the string is empty (''). Otherwise returns false
.
var string = strUtils.isEmpty('');
// true
var string = strUtils.isEmpty('Nope');
// false
isNoneEmpty(string) : boolean
Returns true
if any of the strings are not empty (''). Otherwise returns false
.
var string = strUtils.isNoneEmpty('', 'foo', 'bar', 'baz');
// false
var string = strUtils.isNoneEmpty('Help', 'i\'m', 'trapped', 'in', 'a', 'computer');
// true
isNotEmpty(string) : boolean
Returns true
if string is empty (''). Otherwise returns false
.
var string = strUtils.isNotEmpty('asfas');
// true
var string = strUtils.isNotEmpty('');
// false
leftPad(string, length, char) : string
Pads the string with a char
the amount of times defined by length
on the left. If char
is not defined, pads with a whitespace (' ');
var string = strUtils.leftPad('Hello', 4);
// ' Hello'
var string = strUtils.leftPad('Hello', 4, '