On This Page
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
Converts a kebab-case string to camelCase.
Parameters
Name | Type | Description |
---|---|---|
str | string | The kebab-case string to convert |
Returns
The camelCase version of the input string.
Example
camelToKebab
Converts a camelCase string to kebab-case.
Parameters
Name | Type | Description |
---|---|---|
str | string | The camelCase string to convert |
Returns
The kebab-case version of the input string.
Example
capitalize
Capitalizes the first character of a string.
Parameters
Name | Type | Description |
---|---|---|
str | string | The string to capitalize |
Returns
The input string with its first character capitalized.
Example
capitalizeWords
Capitalizes the first character of each word in a string.
Parameters
Name | Type | Description |
---|---|---|
str | string | The string to capitalize |
Returns
The input string with the first character of each word capitalized.
Example
toTitleCase
Converts a string to title case, taking into account common English articles, conjunctions, and prepositions.
Parameters
Name | Type | Description |
---|---|---|
str | string | The string to convert to title case |
Returns
The input string converted to title case.
Example
joinWords
Joins an array of words into a string with customizable separators and formatting.
Parameters
Name | Type | Description |
---|---|---|
words | array | The array of words to join |
options | object | Optional configuration object |
Options
Name | Type | Default | Description |
---|---|---|---|
separator | string | ’, ‘ | The separator between words |
lastSeparator | string | ’ and ‘ | The separator before the last word |
oxford | boolean | true | Whether to use an Oxford comma |
quotes | boolean | false | Whether to wrap words in quotes |
transform | function | null | A function to transform each word |
Returns
A string of joined words according to the specified options.
Example
getArticle
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
Name | Type | Description |
---|---|---|
word | string | The word to determine the article for |
settings | object | Optional settings object |
Settings
Name | Type | Default | Description |
---|---|---|---|
capitalize | boolean | false | Whether to capitalize the article |
Returns
The appropriate indefinite article (‘a’ or ‘an’) for the given word.
Example
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
Converts a string into a token by replacing spaces with hyphens and removing non-word characters.
Parameters
Name | Type | Description |
---|---|---|
str | string | The string to tokenize |
Returns
A tokenized version of the input string.
Example
These regular expression utilities provide helpful tools for working with strings and patterns in JavaScript, enhancing string manipulation capabilities and security in web applications.