Skip to Content
UtilsString

String utils

import { slugify, truncate, titleCase } from '@gg-software/utils';

capitalize

Uppercase the first letter, leave the rest untouched.

capitalize('hello world'); // "Hello world"

truncate

Cut to a maximum length with an ellipsis. The result (including the ellipsis) never exceeds length.

truncate('Hello world', 8); // "Hello w…" truncate('Hi', 8); // "Hi" truncate('Hello world', 9, '...'); // "Hello..."
ParameterTypeDefault
valuestring
lengthnumber
ellipsisstring'…'

slugify

URL-safe slug: lowercase, diacritics stripped, non-alphanumerics collapsed into single dashes. Made for CMS slugs.

slugify('Hello World'); // "hello-world" slugify('Žluťoučký kůň!'); // "zlutoucky-kun"

camelCase / kebabCase / snakeCase / titleCase

Case converters. All of them understand spaces, dashes, underscores and camel boundaries in the input.

camelCase('user-id_name'); // "userIdName" kebabCase('userId'); // "user-id" snakeCase('Hello World'); // "hello_world" titleCase('user-id'); // "User Id"

pluralize

Pick the singular or plural form by count. The plural defaults to singular + "s".

pluralize(1, 'item'); // "item" pluralize(3, 'item'); // "items" pluralize(2, 'city', 'cities'); // "cities"
ParameterTypeDefault
countnumber
singularstring
pluralstringsingular + 's'

maskString

Mask a sensitive value, keeping only the last characters visible.

maskString('4111111111111111'); // "••••••••••••1111" maskString('secret-token', 4); // "••••••••oken" maskString('pin', 0, '*'); // "***"
ParameterTypeDefault
valuestring
visiblenumber4
maskCharstring'•'

randomString

Random string — non-crypto, do not use for secrets or tokens (use uuid for identifiers).

randomString(); // "tB3xX9aX" (length 8) randomString(4, 'ab'); // "abba"
ParameterTypeDefault
lengthnumber8
alphabetstringA–Z a–z 0–9