Skip to content

String API ​

decodeURIComponentRepeatedly Decode ​

Repeatedly decodes a URI component until stable or the depth limit is reached.

Signature ​

ts
export function decodeURIComponentRepeatedly(value: string, maxDepth = 10): string;

Example ​

ts
import { decodeURIComponentRepeatedly } from "@fast-china/utils";

const result = decodeURIComponentRepeatedly("Fast 文档", 10);

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredEncoded component without URI path semantics.
maxDepthunknownOptional; defaults to 10Maximum decoding passes; defaults to 10.

Returns ​

ValueTypeDescription
resultstringComponent text after stabilization or the limit.

parseQueryString Parse ​

Parses an absolute URL containing ://, a ?query, or a bare query string.

Signature ​

ts
export function parseQueryString(input: string): ParsedQueryParameters;

Example ​

ts
import { parseQueryString } from "@fast-china/utils";

const result = parseQueryString("?page=1&tag=vue&tag=tsx");

Input ​

InputTypeRequired / defaultDescription
inputstringRequiredFull URL or query text with or without a leading question mark.

Returns ​

ValueTypeDescription
resultParsedQueryParametersRepeated keys become string arrays; empty values remain empty strings.

Errors and encoding boundaries ​

parseQueryString converts + to a space before native decodeURIComponent. Malformed percent encoding or invalid UTF-8 throws URIError instead of replacement with U+FFFD. Repeated keys, empty values, and ordinary own __proto__ properties retain their behavior. Raw unescaped lone surrogates are preserved according to native decoding semantics.

isValidJson Validate ​

Checks for any valid JSON value, including scalars and null.

Signature ​

ts
export function isValidJson(value: string): boolean;

Example ​

ts
import { isValidJson } from "@fast-china/utils";

const result = isValidJson(`{"name":"Fast"}`);
console.log(result); // true

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredText to parse; whitespace alone is not JSON.

Returns ​

ValueTypeDescription
resultbooleantrue when JSON.parse can parse the entire input.

splitWords Words ​

Splits words at case boundaries, hyphens, underscores, and whitespace.

Signature ​

ts
export function splitWords(value: string): string[];

Example ​

ts
import { splitWords } from "@fast-china/utils";

const result = splitWords("Fast 文档");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredText to split.

Returns ​

ValueTypeDescription
resultstring[]Word array preserving input order with empty entries removed.

upperFirst Case ​

Uppercases the first Unicode code point.

Signature ​

ts
export function upperFirst(value: string, locale?: StringLocale): string;

Example ​

ts
import { upperFirst } from "@fast-china/utils";

const result = upperFirst("Fast 文档", "zh-CN");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredInput text; empty strings remain empty.
localeStringLocaleOptionalExplicit locale; omitted locales use standard Unicode casing.

Returns ​

ValueTypeDescription
resultstringText after converting the first Unicode code point.

lowerFirst Case ​

Lowercases the first Unicode code point.

Signature ​

ts
export function lowerFirst(value: string, locale?: StringLocale): string;

Example ​

ts
import { lowerFirst } from "@fast-china/utils";

const result = lowerFirst("Fast 文档", "zh-CN");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredInput text; empty strings remain empty.
localeStringLocaleOptionalExplicit locale; omitted locales use standard Unicode casing.

Returns ​

ValueTypeDescription
resultstringText after converting the first Unicode code point.

camelCase Camel ​

Converts text to camelCase.

Signature ​

ts
export function camelCase(value: string, locale?: StringLocale): string;

Example ​

ts
import { camelCase } from "@fast-china/utils";

const result = camelCase("Fast 文档", "zh-CN");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredText separated by case boundaries, hyphens, underscores, or whitespace.
localeStringLocaleOptionalCase-conversion locale; omitted locales use standard Unicode casing.

Returns ​

ValueTypeDescription
resultstringcamelCase text.

pascalCase Pascal ​

Converts text to PascalCase.

Signature ​

ts
export function pascalCase(value: string, locale?: StringLocale): string;

Example ​

ts
import { pascalCase } from "@fast-china/utils";

const result = pascalCase("Fast 文档", "zh-CN");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUses the same argument semantics as the related case-conversion function.
localeStringLocaleOptionalExplicit locale for case conversion.

Returns ​

ValueTypeDescription
resultstringPascalCase text.

kebabCase Kebab ​

Converts text to kebab-case.

Signature ​

ts
export function kebabCase(value: string, locale?: StringLocale): string;

Example ​

ts
import { kebabCase } from "@fast-china/utils";

const result = kebabCase("Fast 文档", "zh-CN");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredUses the same argument semantics as the related case-conversion function.
localeStringLocaleOptionalExplicit locale for case conversion.

Returns ​

ValueTypeDescription
resultstringkebab-case text.

truncateGraphemes Truncate ​

Truncates by Unicode grapheme clusters without splitting emoji, combining marks, or surrogate pairs.

Signature ​

ts
export function truncateGraphemes(value: string, maxLength: number, suffix = "…", locale?: StringLocale): string;

Example ​

ts
import { truncateGraphemes } from "@fast-china/utils";

const result = truncateGraphemes("Fast 文档", 4, "…", "zh-CN");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredInput text.
maxLengthnumberRequiredMaximum grapheme clusters to retain.
suffixunknownOptional; defaults to "…"Suffix appended on truncation; defaults to … and does not count toward the limit.
localeStringLocaleOptionalGrapheme segmentation locale; fixed default en-US.

Returns ​

ValueTypeDescription
resultstringOriginal string when within the limit, otherwise truncated content plus suffix.

copy Copy ​

Copies text to the system clipboard.

Signature ​

ts
export async function copy(value: string): Promise<void>;

Example ​

ts
import { copy } from "@fast-china/utils";

const result = await copy("Fast 文档");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredText to copy.

Returns ​

ValueTypeDescription
resultPromise<void>Promise fulfilled after copying completes.

randomString Random ​

Generates a random string.

Signature ​

ts
export function randomString(length: number, alphabet: string = defaultRandomAlphabet): string;

Example ​

ts
import { randomString } from "@fast-china/utils";

const result = randomString(16, "ABCDEFGHJKLMNPQRSTUVWXYZ23456789");

Input ​

InputTypeRequired / defaultDescription
lengthnumberRequiredCharacter count; a safe integer from 0 to 1,000,000.
alphabetstringOptional; defaults to defaultRandomAlphabetMust be nonempty, contain no duplicate characters, and have at most 2^32 Unicode code points.

Returns ​

ValueTypeDescription
resultstringRandom text composed of Unicode code points from alphabet.

generateUuidV4 UUID ​

Generates an RFC 4122 version 4 UUID.

Signature ​

ts
export function generateUuidV4(): string;

Example ​

ts
import { generateUuidV4 } from "@fast-china/utils";

const result = generateUuidV4();

Input ​

This method has no input parameters.

Returns ​

ValueTypeDescription
resultstringLowercase hyphenated UUID v4.

isUuidV4 UUID ​

Checks for an RFC 4122 version 4 UUID.

Signature ​

ts
export function isUuidV4(value: string): boolean;

Example ​

ts
import { isUuidV4 } from "@fast-china/utils";

const result = isUuidV4("550e8400-e29b-41d4-a716-446655440000");
console.log(result); // true

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredText to validate; hexadecimal letters are case-insensitive.

Returns ​

ValueTypeDescription
resultbooleantrue when version and variant bits are correct.

escapeHtml Escape ​

Escapes five special characters in HTML text context.

Signature ​

ts
export function escapeHtml(value: string): string;

Example ​

ts
import { escapeHtml } from "@fast-china/utils";

const result = escapeHtml("Fast 文档");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredString intended as HTML text-node content.

Returns ​

ValueTypeDescription
resultstringText with &, <, >, double quotes, and single quotes escaped.

normalizeWhitespace Whitespace ​

Collapses Unicode whitespace runs to one space and trims both ends.

Signature ​

ts
export function normalizeWhitespace(value: string): string;

Example ​

ts
import { normalizeWhitespace } from "@fast-china/utils";

const result = normalizeWhitespace("Fast 文档");

Input ​

InputTypeRequired / defaultDescription
valuestringRequiredInput text.

Returns ​

ValueTypeDescription
resultstringNormalized text; all-whitespace input becomes an empty string.