@ayonli/jsext
Version:
A JavaScript extension package for building strong and modern applications.
67 lines (66 loc) • 2.99 kB
TypeScript
import type { ByteArray } from "../bytes.ts";
declare global {
interface StringConstructor {
/**
* Compares two strings, returns `-1` if `a < b`, `0` if `a === b` and `1` if `a > b`.
*/
compare(str1: string, str2: string): -1 | 0 | 1;
/**
* Returns a random string restricted by `length` (character-wise).
*
* @param chars Default value: `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`.
*/
random(length: number, chars?: string): string;
/**
* A template literal tag tag that removes extra indentation from the string.
*
* @see https://github.com/tc39/proposal-string-dedent
*/
dedent(strings: TemplateStringsArray, ...values: any[]): string;
}
interface String {
/** Counts the occurrence of the sub-string in the string. */
count(sub: string): number;
/**
* Capitalizes the string, if `all` is true, all words are capitalized, otherwise only
* the first word will be capitalized.
*/
capitalize(all?: boolean): string;
/** Replaces the spaces between non-empty characters of the string with hyphens (`-`). */
hyphenate(): string;
/** Returns the bytes of the given string. */
bytes(): ByteArray;
/** Returns the characters of the string (emojis are supported). */
chars(): string[];
/** Extracts words (in latin characters) from the string. */
words(): string[];
/** Splits the string into lines by `\n` or `\r\n`. */
lines(): string[];
/** Breaks the string into smaller chunks according to the given length. */
chunk(length: number): string[];
/** Truncates the string to the given length (including the ending `...`). */
truncate(length: number): string;
/** Removes leading and trailing spaces or custom characters of the string. */
trim(chars?: string): string;
/** Removes trailing spaces or custom characters of the string. */
trimEnd(chars?: string): string;
/** Removes leading spaces or custom characters of the string. */
trimStart(chars?: string): string;
/** Removes the given prefix of the string if present. */
stripStart(prefix: string): string;
/** Removes the given suffix of the string if present. */
stripEnd(suffix: string): string;
/**
* Removes extra indentation from the string.
*
* **NOTE:** This function also removes leading and trailing newlines.
*/
dedent(): string;
/** Returns the byte length of the string. */
byteLength(): number;
/** Checks if all characters in the string are within the ASCII range. */
isAscii(printableOnly?: boolean): boolean;
/** Checks if all characters in the string are emojis. */
isEmoji(): boolean;
}
}