@dvcol/common-utils
Version:
Typescript library for common utility functions and constants
71 lines (68 loc) • 2.4 kB
text/typescript
import { v4 } from 'uuid';
/**
* Parse string to JSON
* @param json
* @see JSON.parse
*/
declare const parseJSON: <T>(json?: string | object) => T;
/**
* Create FormData from a record
* @param params
* @see FormData
*/
declare const buildFormData: (params: Record<string, string | string[] | Blob>) => FormData;
/**
* Stringify record into url safe parameters
* @param params
*/
declare const stringifyParams: (params: {
[key: string]: string | string[];
}) => string;
/**
* Stringify keys of a record
* @param record
* @param stringify uses JSON.stringify instead of calling toString if true
*/
declare const stringifyKeys: <T extends Record<string, any>>(record: T, stringify?: boolean) => Record<string, string>;
/**
* Returns 1 if version is higher than compared, -1 if it's lower and 0 if it's equal
* @param version
* @param compared
*/
declare const versionCheck: (version: string, compared: string) => 1 | 0 | -1;
declare const sentenceCase: (input?: string) => string;
declare const capitalizeEachWord: (input: string) => string;
declare const deCapitalise: (input?: string) => string;
declare const getUUID: typeof v4;
declare const toPathSegment: (str?: string, trailing?: boolean) => string;
declare const computeAbsolutePath: (parent: string, relative: string) => string;
declare const toCase: (str: string, pattern: RegExp, separator: string) => string;
declare const toSnakeCase: (str: string) => string;
declare const toKebabCase: (str: string) => string;
type MarkAndToken = {
/**
* The matched mark or pattern.
*/
mark: string | false;
/**
* The token or text before the mark.
*/
token: string;
};
type MarkTokenizerOptions = {
/**
* Optional flags for the regex.
*
* @default 'gi'
**/
flags?: string;
/**
* If true, the tokenizer will ignore HTML tags and only tokenize text content.
*
* @default false
*/
html?: boolean;
};
type MarkTokenizer = (str?: string, pattern?: string, options?: MarkTokenizerOptions) => MarkAndToken[];
declare const markTokenizer: MarkTokenizer;
export { type MarkAndToken, type MarkTokenizer, type MarkTokenizerOptions, buildFormData, capitalizeEachWord, computeAbsolutePath, deCapitalise, getUUID, markTokenizer, parseJSON, sentenceCase, stringifyKeys, stringifyParams, toCase, toKebabCase, toPathSegment, toSnakeCase, versionCheck };