Skip to main content

@rtorcato/js-common / strings

strings

Other

snakeCase()

snakeCase(str): string

Defined in: strings/index.ts:91

Converts a string to snake_case.

Parameters

str

string

The string to convert.

Returns

string

The snake_cased string.


truncate()

truncate(str, maxLength): string

Defined in: strings/index.ts:104

Truncates a string to a maximum length, adding ellipsis if needed.

Parameters

str

string

The string to truncate.

maxLength

number

The maximum length.

Returns

string

The truncated string.


reverse()

reverse(str): string

Defined in: strings/index.ts:120

Reverses a string.

Example

reverse('hello') // 'olleh'
reverse('abc123') // '321cba'

Parameters

str

string

The string to reverse

Returns

string

The reversed string


slugify()

slugify(str): string

Defined in: strings/index.ts:138

Converts a string to a URL-friendly slug. Handles unicode, accents, and special characters.

Example

slugify('Hello World!') // 'hello-world'
slugify('Café & Résumé') // 'cafe-resume'
slugify(' Multiple Spaces ') // 'multiple-spaces'

Parameters

str

string

The string to slugify

Returns

string

The slugified string


words()

words(str): string[]

Defined in: strings/index.ts:163

Splits a string into an array of words.

Example

words('hello world') // ['hello', 'world']
words('camelCaseString') // ['camel', 'Case', 'String']
words('hello-world_test') // ['hello', 'world', 'test']

Parameters

str

string

The string to split

Returns

string[]

Array of words


wordCount()

wordCount(str): number

Defined in: strings/index.ts:185

Counts the number of words in a string.

Example

wordCount('hello world') // 2
wordCount(' one two three ') // 3
wordCount('') // 0

Parameters

str

string

The string to count words in

Returns

number

The word count


pluralize()

pluralize(word, count, plural?): string

Defined in: strings/index.ts:207

Returns the plural form of a word based on count.

Example

pluralize('item', 0) // 'items'
pluralize('item', 1) // 'item'
pluralize('item', 5) // 'items'
pluralize('box', 2, 'boxes') // 'boxes'

Parameters

word

string

The singular word

count

number

The count

plural?

string

Optional custom plural form

Returns

string

The pluralized word


ordinalize()

ordinalize(num): string

Defined in: strings/index.ts:228

Converts a number to its ordinal form.

Example

ordinalize(1) // '1st'
ordinalize(2) // '2nd'
ordinalize(3) // '3rd'
ordinalize(4) // '4th'
ordinalize(11) // '11th'
ordinalize(21) // '21st'

Parameters

num

number

The number to ordinalize

Returns

string

The ordinal string


isBlank()

isBlank(str): boolean

Defined in: strings/index.ts:264

Checks if a string is blank (empty or only whitespace).

Example

isBlank('') // true
isBlank(' ') // true
isBlank('\t\n') // true
isBlank('hello') // false
isBlank(' a ') // false

Parameters

str

string

The string to check

Returns

boolean

True if blank, false otherwise


mask()

mask(str, start, end, char?): string

Defined in: strings/index.ts:285

Masks part of a string with a specified character.

Example

mask('1234567890', 0, 4) // '******7890'
mask('1234567890', 6, 10) // '123456****'
mask('secret', 0, 3, 'X') // 'XXXret'
mask('4111111111111111', 0, 12) // '************1111'

Parameters

str

string

The string to mask

start

number

Start index of masking

end

number

End index of masking

char?

string = '*'

The masking character (default: '*')

Returns

string

The masked string


template()

template(str, data): string

Defined in: strings/index.ts:308

Simple template interpolation using ${key} syntax.

Example

template('Hello, ${name}!', { name: 'World' }) // 'Hello, World!'
template('${a} + ${b} = ${c}', { a: 1, b: 2, c: 3 }) // '1 + 2 = 3'
template('Missing ${key}', {}) // 'Missing ${key}'

String Utilities

Parameters

str

string

The template string

data

Record<string, unknown>

The data object with values

Returns

string

The interpolated string

titleCase()

titleCase(str): string

Defined in: strings/index.ts:16

Converts a string to Title Case (capitalizes the first letter of each word).

Example

titleCase('hello world') // 'Hello World'
titleCase('javaScript is awesome') // 'Javascript Is Awesome'
titleCase('UPPER CASE TEXT') // 'Upper Case Text'
titleCase('mixed-case_string') // 'Mixed-case_string'

Parameters

str

string

The string to convert to title case

Returns

string

The title-cased string


capitalize()

capitalize(str): string

Defined in: strings/index.ts:35

Capitalizes the first letter of a string.

Example

capitalize('hello') // 'Hello'
capitalize('WORLD') // 'WORLD'
capitalize('') // ''
capitalize('a') // 'A'

Parameters

str

string

The string to capitalize

Returns

string

The capitalized string


camelCase()

camelCase(str): string

Defined in: strings/index.ts:56

Converts a string to camelCase. Removes hyphens, underscores, and spaces while capitalizing following letters.

Example

camelCase('hello world') // 'helloWorld'
camelCase('hello-world') // 'helloWorld'
camelCase('hello_world') // 'helloWorld'
camelCase('Hello World') // 'helloWorld'
camelCase('API_KEY_NAME') // 'aPIKEYNAME'

Parameters

str

string

The string to convert to camelCase

Returns

string

The camelCased string


kebabCase()

kebabCase(str): string

Defined in: strings/index.ts:79

Converts a string to kebab-case. Converts camelCase and spaces/underscores to hyphen-separated lowercase.

Example

kebabCase('helloWorld') // 'hello-world'
kebabCase('Hello World') // 'hello-world'
kebabCase('hello_world') // 'hello-world'
kebabCase('APIKeyName') // 'a-p-i-key-name'
kebabCase('camelCaseString') // 'camel-case-string'

Parameters

str

string

The string to convert to kebab-case

Returns

string

The kebab-cased string