Interface UnderscoreStringStaticExports

Functions exported for mixing with underscore object.

Usage: .mixin(.string.exports()); interface UnderscoreStatic extends UnderscoreStringStaticExports { }

Hierarchy

Index

Methods

Methods

public camelize(str: string): string

Converts underscored or dasherized string to a camelized one. ('-moz-transform') => 'MozTransform'

Parameters

  • str: string

Returns

string

public capitalize(str: string): string

Converts first letter of the string to uppercase. ('foo Bar') => 'Foo Bar'

Parameters

  • str: string

Returns

string

public center(str: string, length: number, padStr?: string): string

Left/right-pad a string. Alias for pad(str, length, padStr, 'both') ('1', 8, '0') => '00001000'

Parameters

  • str: string
  • length: number
  • padStr?: string optional

Returns

string

public chars(str: string): Array<any>

Convert string to an array of characters. ('Hello') => ['H','e','l','l','o']

Parameters

  • str: string

Returns

Array<any>

public chop(str: string, step: number): Array<any>

Chop a string into pieces. ('whitespace', 3) => ['whi','tes','pac','e']

Parameters

  • str: string

    String to chop

  • step: number

    Size of the pieces

Returns

Array<any>

public classify(str: string): string

Converts string to camelized class name. ('some_class_name') => 'SomeClassName'

Parameters

  • str: string

Returns

string

public clean(str: string): string

Compress some whitespaces to one. (' foo bar ') => 'foo bar'

Parameters

  • str: string

Returns

string

public count(str: string, substr: string): number

Count occurences of a sub string. ('Hello world', 'l') => 3

Parameters

  • str: string
  • substr: string

Returns

number

public dasherize(str: string): string

Converts a underscored or camelized string into an dasherized one. ('MozTransform') => '-moz-transform'

Parameters

  • str: string

Returns

string

public endsWith(value: string, starts: string): boolean

Checks if string ends with another string. ('image.gif', 'gif') => true

Parameters

  • value: string
  • starts: string

Returns

boolean

public escapeHTML(str: string): string

Converts HTML special characters to their entity equivalents. ('

Blah blah blah
') => '<div>Blah blah blah</div>'

Parameters

  • str: string

Returns

string

public escapeRegExp(str: string): string

Escape a string for use in a regular expression.

Parameters

  • str: string

Returns

string

public exports(): UnderscoreStringStaticExports

Returns

UnderscoreStringStaticExports

public humanize(str: string): string

Converts an underscored, camelized, or dasherized string into a humanized one. Also removes beginning and ending whitespace, and removes the postfix '_id'. (' capitalize dash-CamelCase_underscore trim ') => 'Capitalize dash camel case underscore trim'

Parameters

  • str: string

Returns

string

public insert(str: string, i: number, substr: string): string

Insert a string at index.

Parameters

  • str: string
  • i: number
  • substr: string

Returns

string

public isBlank(str: string): boolean

Determine if a string is 'blank.'

Parameters

  • str: string

Returns

boolean

public join(separator: string, args?: Array<string>): string

Joins strings together with given separator. (' ', 'foo', 'bar') => 'foo bar'

Parameters

  • separator: string
  • args?: Array<string> optional

Returns

string

public levenshtein(str1: string, str2: string): number

Calculates Levenshtein distance between two strings. ('kitten', 'kittah') => 2

Parameters

  • str1: string
  • str2: string

Returns

number

public lines(str: string): Array<any>

Split string by newlines character. ('Hello\nWorld') => ['Hello', 'World']

Parameters

  • str: string

Returns

Array<any>

public ljust(str: string, length: number, padStr?: string): string

Right-pad a string. Alias for pad(str, length, padStr, 'right') ('1', 8, '0') => '10000000'

Parameters

  • str: string
  • length: number
  • padStr?: string optional

Returns

string

public lpad(str: string, length: number, padStr?: string): string

Left-pad a string. Alias for pad(str, length, padStr, 'left') ('1', 8, '0') => '00000001'

Parameters

  • str: string
  • length: number
  • padStr?: string optional

Returns

string

public lrpad(str: string, length: number, padStr?: string): string

Left/right-pad a string. Alias for pad(str, length, padStr, 'both') ('1', 8, '0') => '00001000'

Parameters

  • str: string
  • length: number
  • padStr?: string optional

Returns

string

public lstrip(str: string, characters?: string): string

Left trim. Similar to trim, but only for left side.

Parameters

  • str: string
  • characters?: string optional

Returns

string

public ltrim(str: string, characters?: string): string

Left trim. Similar to trim, but only for left side.

Parameters

  • str: string
  • characters?: string optional

Returns

string

public naturalCmp(str1: string, str2: string): number

Naturally sort strings like humans would do. Caution: this function is charset dependent.

Parameters

  • str1: string
  • str2: string

Returns

number

public numberFormat(number: number, dec?: number, dsep?: string, tsep?: string): string

Formats the numbers. (1000, 2) => '1,000.00' (123456789.123, 5, '.', ',') => '123,456,789.12300'

Parameters

  • number: number
  • dec?: number optional
  • dsep?: string optional
  • tsep?: string optional

Returns

string

public pad(str: string, length: number, padStr?: string, type?: string): string

Pads a string with characters until the total string length is equal to the passed length parameter. By default, pads on the left with the space char (' '). padStr is truncated to a single character if necessary. ('1', 8) => ' 1' ('1', 8, '0') => '00000001' ('1', 8, '0', 'right') => '10000000' ('1', 8, '0', 'both') => '00001000' ('1', 8, 'bleepblorp', 'both') => 'bbbb1bbb'

Parameters

  • str: string
  • length: number
  • padStr?: string optional
  • type?: string optional

Returns

string

public prune(str: string, length: number, pruneStr?: string): string

Elegant version of truncate. Makes sure the pruned string does not exceed the original length. Avoid half-chopped words when truncating. ('Hello, cruel world', 15) => 'Hello, cruel...'

Parameters

  • str: string
  • length: number
  • pruneStr?: string optional

Returns

string

public q(str: string, quoteChar?: string): string

Quotes a string. quoteChar defaults to " ('foo') => '"foo"'

Parameters

  • str: string
  • quoteChar?: string optional

Returns

string

public quote(str: string, quoteChar?: string): string

Quotes a string. quoteChar defaults to " ('foo') => '"foo"'

Parameters

  • str: string
  • quoteChar?: string optional

Returns

string

public repeat(value: string, count: number, separator?: string): string

Repeat a string with an optional separator. ('foo', 3) => 'foofoofoo' ('foo', 3, 'bar') => 'foobarfoobarfoo'

Parameters

  • value: string
  • count: number
  • separator?: string optional

Returns

string

public rjust(str: string, length: number, padStr?: string): string

Left-pad a string. Alias for pad(str, length, padStr, 'left') ('1', 8, '0') => '00000001'

Parameters

  • str: string
  • length: number
  • padStr?: string optional

Returns

string

public rpad(str: string, length: number, padStr?: string): string

Right-pad a string. Alias for pad(str, length, padStr, 'right') ('1', 8, '0') => '10000000'

Parameters

  • str: string
  • length: number
  • padStr?: string optional

Returns

string

public rstrip(str: string, characters?: string): string

Right trim. Similar to trim, but only for right side.

Parameters

  • str: string
  • characters?: string optional

Returns

string

public rtrim(str: string, characters?: string): string

Right trim. Similar to trim, but only for right side.

Parameters

  • str: string
  • characters?: string optional

Returns

string

public slugify(str: string): string

Transform text into a URL slug. Replaces whitespaces, accentuated, and special characters with a dash. ('Un éléphant à l'orée du bois') => 'un-elephant-a-loree-du-bois'

Parameters

  • str: string

Returns

string

public splice(str: string, i: number, howmany: number, substr?: string): string

Splice a string like an array.

Parameters

  • str: string
  • i: number
  • howmany: number
  • substr?: string optional

Returns

string

public sprintf(format: string, args?: Array<any>): string

C like string formatting. _.sprintf('%.1f', 1.17) => '1.2'

Parameters

  • format: string
  • args?: Array<any> optional

Returns

string

public startsWith(str: string, starts: string): boolean

Checks if string starts with another string. ('image.gif', 'image') => true

Parameters

  • str: string
  • starts: string

Returns

boolean

public strLeft(str: string, sep: string): string

Searches a string from left to right for a pattern. Returns a substring consisting of the characters in the string that are to the left of the pattern. If no match found, returns entire string. ('Thisis_a_test_string').strLeft('') => 'This'

Parameters

  • str: string
  • sep: string

Returns

string

public strLeftBack(str: string, sep: string): string

Searches a string from right to left for a pattern. Returns a substring consisting of the characters in the string that are to the left of the pattern. If no match found, returns entire string. ('Thisis_a_test_string').strLeftBack('') => 'This_is_a_test'

Parameters

  • str: string
  • sep: string

Returns

string

public strRight(str: string, sep: string): string

Searches a string from left to right for a pattern. Returns a substring consisting of the characters in the string that are to the right of the pattern. If no match found, returns entire string. ('Thisis_a_test_string').strRight('') => 'is_a_test_string'

Parameters

  • str: string
  • sep: string

Returns

string

public strRightBack(str: string, sep: string): string

Searches a string from right to left for a pattern. Returns a substring consisting of the characters in the string that are to the right of the pattern. If no match found, returns entire string. ('Thisis_a_test_string').strRightBack('') => 'string'

Parameters

  • str: string
  • sep: string

Returns

string

public strip(str: string, characters?: string): string

Trims defined characters from begining and ending of the string. Defaults to whitespace characters. (' foobar ') => 'foobar' ('-foobar-', '_-') => 'foobar'

Parameters

  • str: string
  • characters?: string optional

Returns

string

public stripTags(str: string): string

Removes all html tags from string.

Parameters

  • str: string

Returns

string

public succ(str: string): string

Returns the successor to passed string. ('a') => 'b'

Parameters

  • str: string

Returns

string

public surround(str: string, wrapper: string): string

Surround a string with another string. ('foo', 'ab') => 'abfooab'

Parameters

  • str: string
  • wrapper: string

Returns

string

public swapCase(str: string): string

Returns a copy of the string in which all the case-based characters have had their case swapped. ('hELLO') => 'Hello'

Parameters

  • str: string

Returns

string

public titleize(str: string): string

Capitalize first letter of every word in the string. ('my name is epeli') => 'My Name Is Epeli'

Parameters

  • str: string

Returns

string

public toBoolean(str: string, trueValues?: Array<any>, falseValues?: Array<any>): boolean

Turn strings that can be commonly considered as booleans to real booleans. Such as "true", "false", "1" and "0". This function is case insensitive. ('true') => true ('FALSE') => false ('random') => undefined ('truthy', ['truthy'], ['falsy']) => true ('true only at start', [/^true/]) => true

Parameters

  • str: string
  • trueValues?: Array<any> optional
  • falseValues?: Array<any> optional

Returns

boolean

public toNumber(str: string, decimals?: number): number

Parse string to number. Returns NaN if string can't be parsed to number. ('2.556').toNumber() => 3 ('2.556').toNumber(1) => 2.6

Parameters

  • str: string
  • decimals?: number optional

Returns

number

public toSentence(array: Array<any>, separator?: string, lastSeparator?: string, serial?: boolean): string

Join an array into a human readable sentence. (['jQuery', 'Mootools', 'Prototype']) => 'jQuery, Mootools and Prototype' (['jQuery', 'Mootools', 'Prototype'], ', ', ' unt ') => 'jQuery, Mootools unt Prototype'

Parameters

  • array: Array<any>
  • separator?: string optional
  • lastSeparator?: string optional
  • serial?: boolean optional

Returns

string

public toSentenceSerial(array: Array<any>, separator?: string, lastSeparator?: string): string

The same as toSentence, but uses ', ' as default for lastSeparator.

Parameters

  • array: Array<any>
  • separator?: string optional
  • lastSeparator?: string optional

Returns

string

public trim(str: string, characters?: string): string

Trims defined characters from begining and ending of the string. Defaults to whitespace characters. (' foobar ') => 'foobar' ('-foobar-', '_-') => 'foobar'

Parameters

  • str: string
  • characters?: string optional

Returns

string

public truncate(str: string, length: number, truncateStr?: string): string

Truncate string to specified length. ('Hello world').truncate(5) => 'Hello...' ('Hello').truncate(10) => 'Hello'

Parameters

  • str: string
  • length: number
  • truncateStr?: string optional

Returns

string

public underscored(str: string): string

Converts a camelized or dasherized string into an underscored one. ('MozTransform') => 'moz_transform'

Parameters

  • str: string

Returns

string

public unescapeHTML(str: string): string

Converts entity characters to HTML equivalents. ('<div>Blah blah blah</div>') => '

Blah blah blah
'

Parameters

  • str: string

Returns

string

public unquote(str: string, quoteChar?: string): string

Unquotes a string. quoteChar defaults to " ('"foo"') => 'foo' ("'foo'", "'") => 'foo'

Parameters

  • str: string
  • quoteChar?: string optional

Returns

string

public words(str: string): Array<string>

Split string by delimiter (String or RegExp). /\s+/ by default. (' I love you ') => ['I','love','you'] ('Ilove_you', '') => ['I','love','you']

Parameters

  • str: string

Returns

Array<string>

public words(str: string, delimiter: string): Array<string>

Split string by delimiter (String or RegExp). /\s+/ by default. (' I love you ') => ['I','love','you'] ('Ilove_you', '') => ['I','love','you']

Parameters

  • str: string
  • delimiter: string

Returns

Array<string>

public words(str: string, delimiter: RegExp): Array<string>

Split string by delimiter (String or RegExp). /\s+/ by default. (' I love you ') => ['I','love','you'] ('Ilove_you', '') => ['I','love','you']

Parameters

  • str: string
  • delimiter: RegExp

Returns

Array<string>