String Utilities API reference for string manipulation functions type Guide

String Utilities

The String utilities provide a set of functions for working with strings in JavaScript. These functions help in manipulating, transforming, and analyzing string data efficiently.

Functions

kebabToCamel

function kebabToCamel(str = '')

Converts a kebab-case string to camelCase.

Parameters

NameTypeDescription
strstringThe kebab-case string to convert

Returns

The camelCase version of the input string.

Example

import { kebabToCamel } from '@semantic-ui/utils';
console.log(kebabToCamel('foo-bar')); // 'fooBar'
console.log(kebabToCamel('background-color')); // 'backgroundColor'

camelToKebab

function camelToKebab(str = '')

Converts a camelCase string to kebab-case.

Parameters

NameTypeDescription
strstringThe camelCase string to convert

Returns

The kebab-case version of the input string.

Example

import { camelToKebab } from '@semantic-ui/utils';
console.log(camelToKebab('fooBar')); // 'foo-bar'
console.log(camelToKebab('backgroundColor')); // 'background-color'

capitalize

function capitalize(str = '')

Capitalizes the first character of a string.

Parameters

NameTypeDescription
strstringThe string to capitalize

Returns

The input string with its first character capitalized.

Example

import { capitalize } from '@semantic-ui/utils';
console.log(capitalize('hello')); // 'Hello'
console.log(capitalize('WORLD')); // 'WORLD'

capitalizeWords

function capitalizeWords(str = '')

Capitalizes the first character of each word in a string.

Parameters

NameTypeDescription
strstringThe string to capitalize

Returns

The input string with the first character of each word capitalized.

Example

import { capitalizeWords } from '@semantic-ui/utils';
console.log(capitalizeWords('hello world')); // 'Hello World'
console.log(capitalizeWords('THE QUICK BROWN FOX')); // 'The Quick Brown Fox'

toTitleCase

function toTitleCase(str = '')

Converts a string to title case, taking into account common English articles, conjunctions, and prepositions.

Parameters

NameTypeDescription
strstringThe string to convert to title case

Returns

The input string converted to title case.

Example

import { toTitleCase } from '@semantic-ui/utils';
console.log(toTitleCase('the quick brown fox')); // 'The Quick Brown Fox'
console.log(toTitleCase('a tale of two cities')); // 'A Tale of Two Cities'

joinWords

function joinWords(words, options = {})

Joins an array of words into a string with customizable separators and formatting.

Parameters

NameTypeDescription
wordsarrayThe array of words to join
optionsobjectOptional configuration object
Options
NameTypeDefaultDescription
separatorstring’, ‘The separator between words
lastSeparatorstring’ and ‘The separator before the last word
oxfordbooleantrueWhether to use an Oxford comma
quotesbooleanfalseWhether to wrap words in quotes
transformfunctionnullA function to transform each word

Returns

A string of joined words according to the specified options.

Example

import { joinWords } from '@semantic-ui/utils';
const fruits = ['apple', 'banana', 'cherry'];
console.log(joinWords(fruits));
// 'apple, banana, and cherry'
console.log(joinWords(fruits, { oxford: false }));
// 'apple, banana and cherry'
console.log(joinWords(fruits, { quotes: true }));
// '"apple", "banana", and "cherry"'
console.log(joinWords(fruits, { transform: word => word.toUpperCase() }));
// 'APPLE, BANANA, and CHERRY'

getArticle

function getArticle(word, settings = {})

Determines the appropriate indefinite article (‘a’ or ‘an’) for a given word.

Handling Special Cases This function uses a simple vowel check to determine the article. While this works for most cases, it may not be accurate for all words (e.g., “hour” or “unicorn”). For more complex cases, a more sophisticated algorithm or dictionary lookup might be necessary.

Parameters

NameTypeDescription
wordstringThe word to determine the article for
settingsobjectOptional settings object
Settings
NameTypeDefaultDescription
capitalizebooleanfalseWhether to capitalize the article

Returns

The appropriate indefinite article (‘a’ or ‘an’) for the given word.

Example

import { getArticle } from '@semantic-ui/utils';
console.log(getArticle('apple')); // 'an'
console.log(getArticle('banana')); // 'a'
console.log(getArticle('hour', { capitalize: true })); // 'An'

These string utilities provide a robust set of tools for working with strings in JavaScript, enhancing productivity and code readability. The notes highlight some of the unique aspects and potential limitations of certain functions.

tokenize

function tokenize(str = '')

Converts a string into a token by replacing spaces with hyphens and removing non-word characters.

Parameters

NameTypeDescription
strstringThe string to tokenize

Returns

A tokenized version of the input string.

Example

import { tokenize } from '@semantic-ui/utils';
console.log(tokenize("Hello World!")); // "hello-world"
console.log(tokenize("User's Name_123")); // "users-name-123"

These regular expression utilities provide helpful tools for working with strings and patterns in JavaScript, enhancing string manipulation capabilities and security in web applications.