UNPKG

@cute-dw/core

Version:

This TypeScript library is the main part of a more powerfull package designed for the fast WEB software development. The cornerstone of the library is the **DataStore** class, which might be useful when you need a full control of the data, but do not need

204 lines (196 loc) 12.5 kB
export type StringLike = string | null | undefined; /** * This class consists exclusively of the static methods that operate on or return string values */ export declare class Strings { /** * Checks for string emptiness. String is empty if it doesn't contain any characters or equals to null/undefined. * @param str String value to check * @returns true/false */ static isEmpty(str: StringLike): boolean; /** * Returns a new string if the specified string is empty, i.e. has only blank characters, _null_ or _undefined_ * @param str String value to check * @param def String value to return if `str` is empty * @returns `str` or `def` string if `str` is empty */ static ifEmpty(str: StringLike, def: string): string; /** * SQL Like testing function * * @param str Source string for testing * @param search Search pattern that may has wildcards of sql **LIKE** operator. * The percent sign (%) represents zero, one, or multiple characters * The underscore sign (_) represents one, single character * @returns _true_ if check matches, _false_ if not */ static like(str: StringLike, search: StringLike): boolean; /** * Obtains a specified number of characters from the beginning of a string. * @param str The string you want to search * @param len A long specifying the number of characters you want to return * @returns {string} Returns the leftmost `len` characters in string if it succeeds and the empty string ("") if an error occurs. * If any argument's value is null, *left* returns null. If `len` is greater than or equal to the length of the string, function returns the entire string. It does not add spaces to make the return value's length equal to `len`. * @see {@link right} */ static left(str: StringLike, len: number): StringLike; /** * Obtains a specified number of characters from the end of a string. * @param str The string from which you want characters returned * @param len A long whose value is the number of characters you want returned from the right end of string * @returns Returns the rightmost `len` characters in string if it succeeds and the empty string ("") if an error occurs. If any argument's value is null, function returns null. If `len` is greater than or equal to the length of the string, *right* returns the entire string. It does not add spaces to make the return value's length equal to `len`. * @see {@link left} */ static right(str: StringLike, len: number): StringLike; /** * Removes leading and trailing specified characters from a string. * @param str The string you want returned with leading and trailing `chars` deleted * @param chars Characters to delete from the start and the end of a string. Default is all types of spaces. * @returns Returns a copy of string with all leading and trailing `chars` deleted if it succeeds. * If string is null, _trimAll_ returns null. * @see {@link trimLeft} * @see {@link trimRight} */ static trimAll(str: StringLike, chars?: string | string[]): StringLike; /** * Removes trailing specified characters from a string. * @param str The string you want returned with trailing `chars` deleted * @param chars Characters to delete from the end of a string. Default is all types of spaces. * @returns Returns a copy of string with all trailing `chars` deleted if it succeeds. * If string is null, _trimRight_ returns null. * @see {@link trimLeft} * @see {@link trimAll} */ static trimRight(str: StringLike, chars?: string | string[]): StringLike; /** * Removes leading specified characters from a string. * @param str The string you want returned with leading `chars` deleted * @param chars Characters to delete from the start of a string. Default is all types of spaces. * @returns Returns a copy of string with all leading `chars` deleted if it succeeds. * If `str` is null, _trimLeft_ returns null. * @see {@link trimAll} * @see {@link trimRight} */ static trimLeft(str: StringLike, chars?: string | string[]): StringLike; /** * Bites out the _first_ element of the splitted string `str` on the base of the `separator` value. If the `separator` is undefined, returns a whole string * @param str Source string * @param separator String-delimitor * @returns Tuple of two elements: Token string and the Remaining part of the `str` after the bitting token */ static getToken(str: StringLike, separator: StringLike): [string | undefined, string | undefined]; /** * Bites out the _last_ element of the splitted string `str` on the base of the `separator` value. If the `separator` is undefined, returns a whole string * @param str Source string * @param separator String-delimitor * @returns Tuple of two elements: Token string and the Remaining part of the `str` after the bitting token */ static getLastToken(str: StringLike, separator: StringLike): [string | undefined, string | undefined]; /** * Bites out _all_ elements of the splitted string `str` on the base of the `separator` value(s). * @param str Source string * @param separator String or array of the delimiter characters. Default are whitespace characters. * @returns Array of string tokens * @since 0.5.0 */ static getTokens(str: StringLike, separator?: string | string[]): string[]; /** * Gets the value portion of a keyword=value pair from a string * @param source The string to be searched * @param keyword The keyword to be searched for * @param separator The separator character used in the source string * @returns The value found for the `keyword`. If no matching keyword is found, _undefined_ is returned. * If any argument's value is _null_, the function returns _null_. * @since 0.5.0 * @example * let valB = Strings.getKeyValue("a=123; b=foo; c=true", "b", ";"); // foo * let valM = Strings.getKeyValue("a=123; b=foo; c=true", "missing", ";"); // undefined */ static getKeyValue(source: StringLike, keyword: StringLike, separator: StringLike): StringLike; /** * Returns the number that `search` occurs in the string `source` * @param source The source string to be searched * @param search The search string * @returns Number of occurs */ static occurs(source: StringLike, search: StringLike): number | null; /** * Reverses the order or characters in a string. * @param str A string whose characters you want to reorder so that the last character is first and the first character is last * @returns A string with the characters of `str` in reversed order. Returns the empty string if it fails. */ static reverse(str: StringLike): StringLike; /** * Replaces all matches of `search` string with `newstr` value in the source string `str` * @param str The source string * @param search Search string to replace * @param newstr Replacing string value * @param ignoreCase Set _true_, if you want to ignore character case. Default is _false_ * @returns New string or _null_ if any argument has a nullish value. */ static replaceAll(str: StringLike, search: StringLike, newstr: StringLike, ignoreCase?: boolean): StringLike; /** * Removes single and double quotes from the left and right sides of the string * @param str Source string * @returns Unquoted string */ static unquote(str: StringLike): StringLike; /** * A function that parses a tagged template literal and returns a closure function * @param strings An array of string values of the template literal. For any template, its length is equal to the number of substitutions (occurrences of ${…}) plus one, and is therefore always non-empty. * @param keys The remaining arguments are related to the expressions (substitutions). Each argument may have an _integer_ or _string_ type. The former is an index of the element in the `values` array. The latest is the key of the object's property that assigned to the last element of the `values` array in the closure. * @returns A closure function that should be called with `values` for each expression in `keys`. The last argument can be a JavaScript object whose key names can be used as values in `keys`. * @example * let t1closure = Strings.parseTemplate`${0}${1}${0}!`; * t1closure('Y', 'A'); // "YAY!" * let t2closure = Strings.parseTemplate`${0} ${'foo'}!`; * t2closure('Hello', {foo: 'World'}); // "Hello World!" * (Strings.parseTemplate`${0} ${'user'}!`)('Hello', {user: 'Mike'}); // "Hello Mike!" */ static parseTemplate(strings: TemplateStringsArray, ...keys: any[]): Function; /** C#-like `Format` method that replaces the format item in a specified `template` with the text equivalent of the value of the `args` array argument. The `template` parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called _format items_, that correspond to an object in the parameter list of this method. The formatting process replaces each format item with the text representation of the value of the corresponding object. The syntax of a format item is `{index[,alignment][:formatString]}`, which specifies a mandatory index, the optional length and alignment of the formatted text, and an optional string of format specifier characters that govern how the value of the corresponding object is formatted. The components of a format item are: _index_ A zero-based integer that indicates which element in a list of objects to format. If the object specified by index is null reference, then the format item is replaced by the empty string (""). _alignment_ An optional integer indicating the minimum width of the region to contain the formatted value. If the length of the formatted value is less than alignment, then the region is padded with spaces. If alignment is negative, the formatted value is left justified in the region; if alignment is positive, the formatted value is right justified. If _alignment_ is not specified, the length of the region is the length of the formatted value. The comma is required if alignment is specified. _formatString_ An optional string of format specifiers. If formatString is not specified and the corresponding argument implements the IFormattable interface, then null reference is used. Therefore, all implementations of IFormattable..::.ToString are required to allow nullNothingnullptra null reference (Nothing in Visual Basic) as a format string, and return default formatting of the object representation as a String object. The colon is required if formatString is specified. The leading and trailing brace characters, '{' and '}', are required. To specify a single literal brace character in `template`, specify two leading or trailing brace characters; that is, "{{" or "}}". @param template Template string @param args Argument list @returns Formatted string or _null_ if the `template` is _null_ @example let s1 = Strings.format("{0} {1}!", "Hello", "World"); // Hello World! let s2 = Strings.format("[{0, 10}]", 1234.56); // [ 1234.56] let s3 = Strings.format("[{0,-10}]", 1234.56); // [1234.56 ] */ static format(template: StringLike, ...args: any[]): StringLike; /** * Compare the version string `ver1` against the version string `ver2`. A version * string looks like: **1.2.3.100** or possibly truncated: **1.2.3** or **1.2**. * @param ver1 First version string * @param ver2 Second version string * @returns A positive number if ver1>ver2, _0_ if ver1==ver2 and a negative number if ver1<ver2. * @since 0.5.0 */ static compareVersions(ver1: StringLike, ver2: StringLike): number | null; }