str-utils

JavaScript String Utilities based on Apache Commons StringUtils

Usage no npm install needed!

<script type="module">
  import strUtils from 'https://cdn.skypack.dev/str-utils';
</script>

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 suffixes. 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, '

);
   // '$$Hello';

normalizeSpace(string) : string

Returns a string with all whitespace, carriage return, and line feed characters removed.

   var string = strUtils.normalizeSpace('This is a normal sentence.');
   // 'This is a normal sentence.'
   
   var string = strUtils.normalizeSpace('This     is     a poorly-formatted        sentence.');
   // 'This is a poorly-formatted sentence.'
   
   var string = strUtils.normalizeSpace('This is    a poorly-formatted \r\n sentence     with a carriage      return and a line feed character.');
   // 'This is a poorly-formatted sentence with a carriage return and a line feed character.'

removeEnd(string, remove) : string, null

Removes a substring only if it occurs at the end of string, otherwise returns string. Case-sensitive. Returns null if string is null.

   var string = strUtils.removeEnd('String', 'ing');
   // 'Str'
   
   var string = strUtils.removeEnd('String', 'Str');
   // 'String'
   
   var string = strUtils.removeEnd('String', 'ING');
   // 'String'

removeEndIgnoreCase(string, remove) : string, null

Removes a substring only if it occurs at the end of string, otherwise returns string. Case-insensitive. Returns null if string is null.

   var string = strUtils.removeEnd('String', 'ing');
   // 'Str'
   
   var string = strUtils.removeEnd('String', 'Str');
   // 'String'
   
   var string = strUtils.removeEnd('String', 'ING');
   // 'Str'

removeStart(string, remove) : string, null

Removes a substring only if it occurs at the beginning of string, otherwise returns string. Case-sensitive. Returns null if string is null.

   var string = strUtils.removeStart('String', 'ing');
   // 'String'
   
   var string = strUtils.removeStart('String', 'Str');
   // 'ing'
   
   var string = strUtils.removeStart('String', 'STR');
   // 'String'
   
   var string = strUtils.removeStart('String', 'rin');
   // 'String'

removeStartIgnoreCase(string, remove) : string, null

Removes a substring only if it occurs at the beginning of string, otherwise returns string. Case-insensitive. Returns null if string is null.

   var string = strUtils.removeStart('String', 'ing');
   // 'String'
   
   var string = strUtils.removeStart('String', 'Str');
   // 'ing'
   
   var string = strUtils.removeStart('String', 'STR');
   // 'ing'
   
   var string = strUtils.removeStart('String', 'rin');
   // 'String'

rightPad(string) : string

Pads the string with a char the amount of times defined by length on the right. If char is not defined, pads with a whitespace (' ');

   var string = strUtils.rightPad('Hello', 4);
   // 'Hello    '
   
   var string = strUtils.rightPad('Hello', 4, '

);
   // 'Hello$$';

startsWith(string, prefix) : boolean

Returns true if a string ends with the defined prefix. Otherwise returns false.

   var string = strUtils.startsWith('Hello', 'He');
   // true
   
   var string = strUtils.startsWith('Hello', 'he');
   // false

startsWithIgnoreCase(string, prefix) : boolean

Returns true if a string starts with the defined case-insensitive prefix. Otherwise returns false.

   var string = strUtils.startsWith('Hello', 'He');
   // true
   
   var string = strUtils.startsWith('Hello', 'he');
   // true

startsWithAny(string, prefixArray) : boolean

Returns true if a string starts with any of the defined prefixes. Otherwise returns false. Case sensitive.

   var string = strUtils.startsWithAny('abc', ['ab', 'ba']);
   // true
   
   var string = strUtils.startsWithAny('abc', ['bc', 'bb']);
   // false

swapCase(string) : string, null

Returns string with lowercase characters as uppercase and uppercase characters as lowercase. Returns null if string is null.

   var string = strUtils.swapCase('this Is a sTriNg');
   // 'THIS iS A StRInG'

uncapitalize(string) : string, null

Returns string with first letter to lowercase. Returns null if string is null.

   var string = strUtils.uncapitalize('cAT');
   // 'cAT'
   
   var string = strUtils.uncapitalize('Cat');
   // 'cat'