/** * @license * Copyright The Closure Library Authors. * SPDX-License-Identifier: Apache-2.0 */ /** * @fileoverview Utilities for string manipulation. */ /** * Namespace for string utilities */ /** * @type {boolean} Enables HTML escaping of lowercase letter "e" which helps * with detection of double-escaping as this letter is frequently used. */ export const DETECT_DOUBLE_ESCAPING: boolean; /** * @type {boolean} Whether to force non-dom html unescaping. */ export const FORCE_NON_DOM_HTML_UNESCAPING: boolean; /** * Common Unicode string characters. */ export type Unicode = string; export namespace Unicode { const NBSP: string; } /** * Parse a string in decimal or hexidecimal ('0xFFFF') form. * * To parse a particular radix, please use parseInt(string, radix) directly. See * https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt * * This is a wrapper for the built-in parseInt function that will only parse * numbers as base 10 or base 16. Some JS implementations assume strings * starting with "0" are intended to be octal. ES3 allowed but discouraged * this behavior. ES5 forbids it. This function emulates the ES5 behavior. * * For more information, see Mozilla JS Reference: http://goo.gl/8RiFj * * @param {string|number|null|undefined} value The value to be parsed. * @return {number} The number, parsed. If the string failed to parse, this * will be NaN. */ declare function _parseInt(value: string | number | null | undefined): number; /** * Replaces Windows and Mac new lines with unix style: \r or \r\n with \n. * @param {string} str The string to in which to canonicalize newlines. * @return {string} `str` A copy of {@code} with canonicalized newlines. */ export function canonicalizeNewlines(str: string): string; /** * Capitalizes a string, i.e. converts the first letter to uppercase * and all other letters to lowercase, e.g.: * * capitalize('one') => 'One' * capitalize('ONE') => 'One' * capitalize('one two') => 'One two' * * Note that this function does not trim initial whitespace. * * @param {string} str String value to capitalize. * @return {string} String value with first letter in uppercase. */ export function capitalize(str: string): string; /** * A string comparator that ignores case. * -1 = str1 less than str2 * 0 = str1 equals str2 * 1 = str1 greater than str2 * * @param {string} str1 The string to compare. * @param {string} str2 The string to compare `str1` to. * @return {number} The comparator result, as described above. */ export function caseInsensitiveCompare(str1: string, str2: string): number; /** * Determines whether a string contains a substring, ignoring case. * @param {string} str The string to search. * @param {string} subString The substring to search for. * @return {boolean} Whether `str` contains `subString`. */ export function caseInsensitiveContains(str: string, subString: string): boolean; /** * Case-insensitive suffix-checker. * @param {string} str The string to check. * @param {string} suffix A string to look for at the end of `str`. * @return {boolean} True if `str` ends with `suffix` (ignoring * case). */ export function caseInsensitiveEndsWith(str: string, suffix: string): boolean; /** * Case-insensitive equality checker. * @param {string} str1 First string to check. * @param {string} str2 Second string to check. * @return {boolean} True if `str1` and `str2` are the same string, * ignoring case. */ export function caseInsensitiveEquals(str1: string, str2: string): boolean; /** * Case-insensitive prefix-checker. * @param {string} str The string to check. * @param {string} prefix A string to look for at the end of `str`. * @return {boolean} True if `str` begins with `prefix` (ignoring * case). */ export function caseInsensitiveStartsWith(str: string, prefix: string): boolean; /** * Removes the breaking spaces from the left and right of the string and * collapses the sequences of breaking spaces in the middle into single spaces. * The original and the result strings render the same way in HTML. * @param {string} str A string in which to collapse spaces. * @return {string} Copy of the string with normalized breaking spaces. */ export function collapseBreakingSpaces(str: string): string; /** * Converts multiple whitespace chars (spaces, non-breaking-spaces, new lines * and tabs) to a single space, and strips leading and trailing whitespace. * @param {string} str Input string. * @return {string} A copy of `str` with collapsed whitespace. */ export function collapseWhitespace(str: string): string; /** * Compares two version numbers. * * @param {string|number} version1 Version of first item. * @param {string|number} version2 Version of second item. * * @return {number} 1 if `version1` is higher. * 0 if arguments are equal. * -1 if `version2` is higher. */ export function compareVersions(version1: string | number, version2: string | number): number; /** * Determines whether a string contains a substring. * @param {string} str The string to search. * @param {string} subString The substring to search for. * @return {boolean} Whether `str` contains `subString`. */ export function contains(str: string, subString: string): boolean; /** * Returns the non-overlapping occurrences of ss in s. * If either s or ss evalutes to false, then returns zero. * @param {string} s The string to look in. * @param {string} ss The string to look for. * @return {number} Number of occurrences of ss in s. */ export function countOf(s: string, ss: string): number; /** * Generates and returns a string which is unique in the current document. * This is useful, for example, to create unique IDs for DOM elements. * @return {string} A unique id. */ export function createUniqueString(): string; /** * Computes the Levenshtein edit distance between two strings. * @param {string} a * @param {string} b * @return {number} The edit distance between the two strings. */ export function editDistance(a: string, b: string): number; /** * Fast suffix-checker. * @param {string} str The string to check. * @param {string} suffix A string to look for at the end of `str`. * @return {boolean} True if `str` ends with `suffix`. * * @deprecated use str.endsWith(suffix) directly */ export function endsWith(str: string, suffix: string): boolean; /** * Takes a character and returns the escaped string for that character. For * example escapeChar(String.fromCharCode(15)) -> "\\x0E". * @param {string} c The character to escape. * @return {string} An escaped string representing `c`. */ export function escapeChar(c: string): string; /** * Takes a string and returns the escaped string for that input string. * @param {string} str The string to escape. * @return {string} An escaped string representing `str`. */ export function escapeString(str: string): string; /** * String comparison function that handles non-negative integer and fractional * numbers in a way humans might expect. Using this function, the string * 'File 2.jpg' sorts before 'File 10.jpg', and '3.14' before '3.2'. Equivalent * to {@link intAwareCompare} apart from the way how it interprets * dots. * * @param {string} str1 The string to compare in a numerically sensitive way. * @param {string} str2 The string to compare `str1` to. * @return {number} less than 0 if str1 < str2, 0 if str1 == str2, greater than * 0 if str1 > str2. */ export function floatAwareCompare(str1: string, str2: string): number; /** * Returns a string with at least 64-bits of randomness. * * Doesn't trust JavaScript's random function entirely. Uses a combination of * random and current timestamp, and then encodes the string in base-36 to * make it shorter. * * @return {string} A random string, e.g. sn1s7vb4gcic. */ export function getRandomString(): string; /** * String hash function similar to java.lang.String.hashCode(). * The hash code for a string is computed as * s[0] * 31 ^ (n - 1) + s[1] * 31 ^ (n - 2) + ... + s[n - 1], * where s[i] is the ith character of the string and n is the length of * the string. We mod the result to make it between 0 (inclusive) and 2^32 * (exclusive). * @param {string} str A string. * @return {number} Hash value for `str`, between 0 (inclusive) and 2^32 * (exclusive). The empty string returns 0. */ export function hashCode(str: string): number; /** * Escapes double quote '"' and single quote '\'' characters in addition to * '&', '<', and '>' so that a string can be included in an HTML tag attribute * value within double or single quotes. * * It should be noted that > doesn't need to be escaped for the HTML or XML to * be valid, but it has been decided to escape it for consistency with other * implementations. * * With DETECT_DOUBLE_ESCAPING, this function escapes also the * lowercase letter "e". * * NOTE(user): * HtmlEscape is often called during the generation of large blocks of HTML. * Using statics for the regular expressions and strings is an optimization * that can more than half the amount of time IE spends in this function for * large apps, since strings and regexes both contribute to GC allocations. * * Testing for the presence of a character before escaping increases the number * of function calls, but actually provides a speed increase for the average * case -- since the average case often doesn't require the escaping of all 4 * characters and indexOf() is much cheaper than replace(). * The worst case does suffer slightly from the additional calls, therefore the * opt_isLikelyToContainHtmlChars option has been included for situations * where all 4 HTML entities are very likely to be present and need escaping. * * Some benchmarks (times tended to fluctuate +-0.05ms): * FireFox IE6 * (no chars / average (mix of cases) / all 4 chars) * no checks 0.13 / 0.22 / 0.22 0.23 / 0.53 / 0.80 * indexOf 0.08 / 0.17 / 0.26 0.22 / 0.54 / 0.84 * indexOf + re test 0.07 / 0.17 / 0.28 0.19 / 0.50 / 0.85 * * An additional advantage of checking if replace actually needs to be called * is a reduction in the number of object allocations, so as the size of the * application grows the difference between the various methods would increase. * * @param {string} str string to be escaped. * @param {boolean=} opt_isLikelyToContainHtmlChars Don't perform a check to see * if the character needs replacing - use this option if you expect each of * the characters to appear often. Leave false if you expect few html * characters to occur in your strings, such as if you are escaping HTML. * @return {string} An escaped copy of `str`. */ export function htmlEscape(str: string, opt_isLikelyToContainHtmlChars?: boolean | undefined): string; /** * String comparison function that handles non-negative integer numbers in a * way humans might expect. Using this function, the string 'File 2.jpg' sorts * before 'File 10.jpg', and 'Version 1.9' before 'Version 1.10'. The comparison * is mostly case-insensitive, though strings that are identical except for case * are sorted with the upper-case strings before lower-case. * * This comparison function is up to 50x slower than either the default or the * case-insensitive compare. It should not be used in time-critical code, but * should be fast enough to sort several hundred short strings (like filenames) * with a reasonable delay. * * @param {string} str1 The string to compare in a numerically sensitive way. * @param {string} str2 The string to compare `str1` to. * @return {number} less than 0 if str1 < str2, 0 if str1 == str2, greater than * 0 if str1 > str2. */ export function intAwareCompare(str1: string, str2: string): number; /** * Checks if a string contains all letters. * @param {string} str string to check. * @return {boolean} True if `str` consists entirely of letters. */ export function isAlpha(str: string): boolean; /** * Checks if a string contains only numbers or letters. * @param {string} str string to check. * @return {boolean} True if `str` is alphanumeric. */ export function isAlphaNumeric(str: string): boolean; /** * Checks if a string is all breaking whitespace. * @param {string} str The string to check. * @return {boolean} Whether the string is all breaking whitespace. */ export function isBreakingWhitespace(str: string): boolean; /** * Checks if a string is empty or contains only whitespaces. * * @param {string} str The string to check. * @return {boolean} Whether `str` is empty or whitespace only. * @deprecated Use isEmptyOrWhitespace instead. */ export function isEmpty(str: string): boolean; /** * Checks if a string is empty or contains only whitespaces. * @param {string} str The string to check. * @return {boolean} Whether `str` is empty or whitespace only. */ export function isEmptyOrWhitespace(str: string): boolean; /** * Checks if a string is null, undefined, empty or contains only whitespaces. * @param {*} str The string to check. * @return {boolean} Whether `str` is null, undefined, empty, or * whitespace only. * @deprecated Use isEmptyOrWhitespace(makeSafe(str)) * instead. */ export function isEmptyOrWhitespaceSafe(str: any): boolean; /** * Checks if a string is null, undefined, empty or contains only whitespaces. * * @param {*} str The string to check. * @return {boolean} Whether `str` is null, undefined, empty, or * whitespace only. * @deprecated Use isEmptyOrWhitespace instead. */ export function isEmptySafe(str: any): boolean; /** * Checks if a string is empty. * @param {string} str The string to check. * @return {boolean} Whether `str` is empty. */ export function isEmptyString(str: string): boolean; /** * Returns whether the given string is lower camel case (e.g. "isFooBar"). * * Note that this assumes the string is entirely letters. * @see http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms * * @param {string} str String to test. * @return {boolean} Whether the string is lower camel case. */ export function isLowerCamelCase(str: string): boolean; /** * Checks if a string contains only numbers. * @param {*} str string to check. If not a string, it will be * casted to one. * @return {boolean} True if `str` is numeric. */ export function isNumeric(str: any): boolean; /** * Checks if a character is a space character. * @param {string} ch Character to check. * @return {boolean} True if `ch` is a space. */ export function isSpace(ch: string): boolean; /** * Checks if a character is a valid unicode character. * @param {string} ch Character to check. * @return {boolean} True if `ch` is a valid unicode character. */ export function isUnicodeChar(ch: string): boolean; /** * Returns whether the given string is upper camel case (e.g. "FooBarBaz"). * * Note that this assumes the string is entirely letters. * @see http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms * * @param {string} str String to test. * @return {boolean} Whether the string is upper camel case. */ export function isUpperCamelCase(str: string): boolean; /** * Finds the characters to the right of the last instance of any separator * * This function is similar to goog.string.path.baseName, except it can take a * list of characters to split the string on. It will return the rightmost * grouping of characters to the right of any separator as a left-to-right * oriented string. * * @see goog.string.path.baseName * @param {string} str The string * @param {string|!Array} separators A list of separator characters * @return {string} The last part of the string with respect to the separators */ export function lastComponent(str: string, separators: string | Array): string; /** * Returns a string representation of the given object, with * null and undefined being returned as the empty string. * * @param {*} obj The object to convert. * @return {string} A string representation of the `obj`. */ export function makeSafe(obj: any): string; /** * Converts \n to
s or
s. * @param {string} str The string in which to convert newlines. * @param {boolean=} opt_xml Whether to use XML compatible tags. * @return {string} A copy of `str` with converted newlines. */ export function newLineToBr(str: string, opt_xml?: boolean | undefined): string; /** * Normalizes spaces in a string, replacing all consecutive spaces and tabs * with a single space. Replaces non-breaking space with a space. * @param {string} str The string in which to normalize spaces. * @return {string} A copy of `str` with all consecutive spaces and tabs * replaced with a single space. */ export function normalizeSpaces(str: string): string; /** * Normalizes whitespace in a string, replacing all whitespace chars with * a space. * @param {string} str The string in which to normalize whitespace. * @return {string} A copy of `str` with all whitespace normalized. */ export function normalizeWhitespace(str: string): string; /** * Alias for {@link floatAwareCompare}. * * @param {string} str1 * @param {string} str2 * @return {number} */ export function numerateCompare(str1: string, str2: string): number; /** * Pads number to given length and optionally rounds it to a given precision. * For example: *
padNumber(1.25, 2, 3) -> '01.250'
 * padNumber(1.25, 2) -> '01.25'
 * padNumber(1.25, 2, 1) -> '01.3'
 * padNumber(1.25, 0) -> '1.25'
* * @param {number} num The number to pad. * @param {number} length The desired length. * @param {number=} opt_precision The desired precision. * @return {string} `num` as a string with the given options. */ export function padNumber(num: number, length: number, opt_precision?: number | undefined): string; /** * Preserve spaces that would be otherwise collapsed in HTML by replacing them * with non-breaking space Unicode characters. * @param {string} str The string in which to preserve whitespace. * @return {string} A copy of `str` with preserved whitespace. */ export function preserveSpaces(str: string): string; /** * Encloses a string in double quotes and escapes characters so that the * string is a valid JS string. The resulting string is safe to embed in * `