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..."| Parameter | Type | Default |
|---|---|---|
value | string | — |
length | number | — |
ellipsis | string | '…' |
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"| Parameter | Type | Default |
|---|---|---|
count | number | — |
singular | string | — |
plural | string | singular + 's' |
maskString
Mask a sensitive value, keeping only the last characters visible.
maskString('4111111111111111'); // "••••••••••••1111"
maskString('secret-token', 4); // "••••••••oken"
maskString('pin', 0, '*'); // "***"| Parameter | Type | Default |
|---|---|---|
value | string | — |
visible | number | 4 |
maskChar | string | '•' |
randomString
Random string — non-crypto, do not use for secrets or tokens (use
uuid for identifiers).
randomString(); // "tB3xX9aX" (length 8)
randomString(4, 'ab'); // "abba"| Parameter | Type | Default |
|---|---|---|
length | number | 8 |
alphabet | string | A–Z a–z 0–9 |