@pivanov/utils
Version:
A focused collection of TypeScript utilities for modern web development
128 lines (127 loc) • 3.53 kB
TypeScript
/**
* Converts a string to camelCase.
*
* @example
* ```ts
* camelCase('foo-bar'); // 'fooBar'
* camelCase('FOO_BAR'); // 'fooBar'
* ```
*/
export declare const camelCase: (str: string) => string;
/**
* Converts a string to PascalCase.
*
* @example
* ```ts
* pascalCase('foo-bar'); // 'FooBar'
* pascalCase('foo123bar'); // 'Foo123Bar'
* ```
*/
export declare const pascalCase: (str: string) => string;
/**
* Capitalizes the first character of a string (runtime).
* For TypeScript literal-type preservation, use `capitalize` instead.
*/
export declare const capitalizeFirstLetter: (string: string) => string;
/**
* Converts a string to kebab-case.
*
* @example
* ```ts
* kebabCase('fooBar'); // 'foo-bar'
* kebabCase('XMLHttpRequest'); // 'xml-http-request'
* ```
*/
export declare const kebabCase: (str: string) => string;
/**
* Converts a string to snake_case.
*
* @example
* ```ts
* snakeCase('fooBar'); // 'foo_bar'
* snakeCase('XMLHttpRequest'); // 'xml_http_request'
* ```
*/
export declare const snakeCase: (str: string) => string;
/**
* Converts a string to Title Case - each word capitalized, separators
* normalized to single spaces.
*
* @example
* ```ts
* titleCase('hello world'); // 'Hello World'
* titleCase('foo-bar_baz'); // 'Foo Bar Baz'
* ```
*/
export declare const titleCase: (str: string) => string;
/**
* Converts a string into a URL-friendly slug. More aggressive than
* `kebabCase` - strips all non-ASCII-word characters.
*
* @example
* ```ts
* slugify('Hello World!'); // 'hello-world'
* slugify('Über Café'); // 'uber-cafe'
* ```
*/
export declare const slugify: (str: string) => string;
/**
* Capitalizes the first character; preserves TypeScript literal types.
*/
export declare const capitalize: <S extends string>(str: S) => Capitalize<S>;
/**
* Lower-cases the first character; preserves TypeScript literal types.
*/
export declare const uncapitalize: <S extends string>(str: S) => Uncapitalize<S>;
/**
* Truncates a string to `maxLength` characters, appending an ellipsis
* (default `…`) if truncation happened. The ellipsis is included in
* the final length.
*
* @example
* ```ts
* truncate('Hello, world!', 8); // 'Hello, …'
* truncate('Hello, world!', 8, '...'); // 'Hello...'
* truncate('Short', 20); // 'Short'
* ```
*/
export declare const truncate: (str: string, maxLength: number, ellipsis?: string) => string;
/**
* Escapes HTML special characters for safe interpolation into markup.
*
* @example
* ```ts
* escapeHtml('<script>alert(1)</script>');
* // '<script>alert(1)</script>'
* ```
*/
export declare const escapeHtml: (str: string) => string;
/**
* Escapes characters that have special meaning in a regular expression so the
* string can be safely embedded as a literal match.
*
* @example
* ```ts
* new RegExp(escapeRegExp('a.b*c')); // matches the literal "a.b*c"
* ```
*/
export declare const escapeRegExp: (str: string) => string;
/**
* Splits a string into words by whitespace, dashes, and underscores.
* Preserves case; filters out empty segments.
*
* @example
* ```ts
* words('hello_world-foo bar'); // ['hello', 'world', 'foo', 'bar']
* ```
*/
export declare const words: (str: string) => string[];
/**
* Splits a string by line breaks (`\r\n`, `\n`, or `\r`).
*
* @example
* ```ts
* lines('a\nb\r\nc'); // ['a', 'b', 'c']
* ```
*/
export declare const lines: (str: string) => string[];