@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.
padStart()
padStart(
str,targetLength,padString?):string
Defined in: strings/index.ts:115
Pads the start of a string to a given length.
Parameters
str
string
The string to pad.
targetLength
number
The target length.
padString?
string = ' '
The string to pad with.
Returns
string
The padded string.
padEnd()
padEnd(
str,targetLength,padString?):string
Defined in: strings/index.ts:126
Pads the end of a string to a given length.
Parameters
str
string
The string to pad.
targetLength
number
The target length.
padString?
string = ' '
The string to pad with.
Returns
string
The padded string.
randomString()
randomString(
length,chars):string
Defined in: strings/index.ts:136
get a random string var rStr= randomString(32,'0123456789abcdefghijklmn');
Parameters
length
number
chars
string
Returns
string
replaceString()
replaceString(
str,search,replacement):string
Defined in: strings/index.ts:150
replace all items in a string using regex
Parameters
str
string
search
string
replacement
string
Returns
string
sanitizeString()
sanitizeString(
str):string
Defined in: strings/index.ts:159
Sanitizes a string by removing script tags and event handlers.
Parameters
str
string
The string to sanitize.
Returns
string
The sanitized string.
reverse()
reverse(
str):string
Defined in: strings/index.ts:175
Reverses a string.
Parameters
str
string
The string to reverse
Returns
string
The reversed string
Example
reverse('hello') // 'olleh'
reverse('abc123') // '321cba'
slugify()
slugify(
str):string
Defined in: strings/index.ts:193
Converts a string to a URL-friendly slug. Handles unicode, accents, and special characters.
Parameters
str
string
The string to slugify
Returns
string
The slugified string
Example
slugify('Hello World!') // 'hello-world'
slugify('Café & Résumé') // 'cafe-resume'
slugify(' Multiple Spaces ') // 'multiple-spaces'
words()
words(
str):string[]
Defined in: strings/index.ts:218
Splits a string into an array of words.
Parameters
str
string
The string to split
Returns
string[]
Array of words
Example
words('hello world') // ['hello', 'world']
words('camelCaseString') // ['camel', 'Case', 'String']
words('hello-world_test') // ['hello', 'world', 'test']
wordCount()
wordCount(
str):number
Defined in: strings/index.ts:240
Counts the number of words in a string.
Parameters
str
string
The string to count words in
Returns
number
The word count
Example
wordCount('hello world') // 2
wordCount(' one two three ') // 3
wordCount('') // 0
escapeHtml()
escapeHtml(
str):string
Defined in: strings/index.ts:258
Escapes HTML special characters to prevent XSS.
Parameters
str
string
The string to escape
Returns
string
The escaped string
Example
escapeHtml('<div>Hello & "World"</div>') // '<div>Hello & "World"</div>'
escapeHtml("It's <script>") // "It's <script>"
unescapeHtml()
unescapeHtml(
str):string
Defined in: strings/index.ts:281
Unescapes HTML entities back to their original characters.
Parameters
str
string
The string to unescape
Returns
string
The unescaped string
Example
unescapeHtml('<div>') // '<div>'
unescapeHtml('&"'') // '&"''
stripHtml()
stripHtml(
str):string
Defined in: strings/index.ts:310
Removes all HTML tags from a string.
Parameters
str
string
The string to strip HTML from
Returns
string
The string without HTML tags
Example
stripHtml('<p>Hello <b>World</b></p>') // 'Hello World'
stripHtml('<script>alert(1)</script>Text') // 'Text'
pluralize()
pluralize(
word,count,plural?):string
Defined in: strings/index.ts:330
Returns the plural form of a word based on count.
Parameters
word
string
The singular word
count
number
The count
plural?
string
Optional custom plural form
Returns
string
The pluralized word
Example
pluralize('item', 0) // 'items'
pluralize('item', 1) // 'item'
pluralize('item', 5) // 'items'
pluralize('box', 2, 'boxes') // 'boxes'
ordinalize()
ordinalize(
num):string
Defined in: strings/index.ts:351
Converts a number to its ordinal form.
Parameters
num
number
The number to ordinalize
Returns
string
The ordinal string
Example
ordinalize(1) // '1st'
ordinalize(2) // '2nd'
ordinalize(3) // '3rd'
ordinalize(4) // '4th'
ordinalize(11) // '11th'
ordinalize(21) // '21st'
isBlank()
isBlank(
str):boolean
Defined in: strings/index.ts:387
Checks if a string is blank (empty or only whitespace).
Parameters
str
string
The string to check
Returns
boolean
True if blank, false otherwise
Example
isBlank('') // true
isBlank(' ') // true
isBlank('\t\n') // true
isBlank('hello') // false
isBlank(' a ') // false
mask()
mask(
str,start,end,char?):string
Defined in: strings/index.ts:408
Masks part of a string with a specified character.
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
Example
mask('1234567890', 0, 4) // '******7890'
mask('1234567890', 6, 10) // '123456****'
mask('secret', 0, 3, 'X') // 'XXXret'
mask('4111111111111111', 0, 12) // '************1111'
template()
template(
str,data):string
Defined in: strings/index.ts:431
Simple template interpolation using ${key} syntax.
Parameters
str
string
The template string
data
Record<string, unknown>
The data object with values
Returns
string
The interpolated string
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
titleCase()
titleCase(
str):string
Defined in: strings/index.ts:16
Converts a string to Title Case (capitalizes the first letter of each word).
Parameters
str
string
The string to convert to title case
Returns
string
The title-cased string
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'
capitalize()
capitalize(
str):string
Defined in: strings/index.ts:35
Capitalizes the first letter of a string.
Parameters
str
string
The string to capitalize
Returns
string
The capitalized string
Example
capitalize('hello') // 'Hello'
capitalize('WORLD') // 'WORLD'
capitalize('') // ''
capitalize('a') // 'A'
camelCase()
camelCase(
str):string
Defined in: strings/index.ts:56
Converts a string to camelCase. Removes hyphens, underscores, and spaces while capitalizing following letters.
Parameters
str
string
The string to convert to camelCase
Returns
string
The camelCased string
Example
camelCase('hello world') // 'helloWorld'
camelCase('hello-world') // 'helloWorld'
camelCase('hello_world') // 'helloWorld'
camelCase('Hello World') // 'helloWorld'
camelCase('API_KEY_NAME') // 'aPIKEYNAME'
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.
Parameters
str
string
The string to convert to kebab-case
Returns
string
The kebab-cased string
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'