This module provides various utility functions to convert strings between different cases such as camel case, snake case, pascal case, and title case.
Converts a string from camel case to snake case. Also works for Pascal case.
camelToSnakeCase(inputString: string): stringParameters:
inputString(string): The input string in camel case or Pascal case.
Returns:
- (string): The converted string in snake case.
Examples:
camelToSnakeCase("helloWorld"); // "hello_world"
camelToSnakeCase("HelloWorld"); // "hello_world"Converts a string to camel case.
toCamelCase(inputString: string): stringParameters:
inputString(string): The input string.
Returns:
- (string): The converted string in camel case.
Examples:
toCamelCase("hello_world"); // "helloWorld"
toCamelCase("hello-world"); // "helloWorld"
toCamelCase("hello world"); // "helloWorld"
toCamelCase("hello world"); // "helloWorld"Converts a string to pascal case.
toPascalCase(inputString: string): stringParameters:
inputString(string): The input string.
Returns:
- (string): The converted string in pascal case.
Examples:
toPascalCase("hello_world"); // "HelloWorld"
toPascalCase("hello-world"); // "HelloWorld"
toPascalCase("hello world"); // "HelloWorld"
toPascalCase("hello world"); // "HelloWorld"Converts a string to snake case.
toSnakeCase(inputString: string): stringParameters:
inputString(string): The input string.
Returns:
- (string): The converted string in snake case.
Examples:
toSnakeCase("HelloWorld"); // "hello_world"
toSnakeCase("hello-world"); // "hello_world"
toSnakeCase("hello world"); // "hello_world"
toSnakeCase("hello world"); // "hello_world"Converts a string to title case.
toTitleCase(inputString: string): stringParameters:
inputString(string): The input string.
Returns:
- (string): The converted string in title case.
Examples:
toTitleCase("hello_world"); // "Hello World"Converts a camel case string to title case.
camelToTitleCase(inputString: string): stringParameters:
inputString(string): The input string in camel case.
Returns:
- (string): The converted string in title case.
Examples:
camelToTitleCase("helloWorld"); // "Hello World"These are random useful utilities
Effectively converts a string to snake case.
- Removes non-alphanumeric characters from a string.
- Strips leading and trailing whitespace and delimiters.
- Replaces delimiters (
_,-, and space) with underscores. - Replaces multiple underscores with a single underscore.
- Converts the string to lowercase.
inputString(string): The input string.
string: The sanitized string.
import { sanitizeString } from "./utils";
const result = sanitizeString("Hello World!");
console.log(result); // Output: hello_worldGenerates a random string of a given length.
length(number, optional): The length of the random string. Default is24.
string: The generated random string.
import { generateRandomString } from "./utils";
const randomString = generateRandomString(16);
console.log(randomString); // Output: A random string of 16 charactersAdds leading zeros to a number or string.
value(number | string): The value to add leading zeros to.length(number, optional): The length of the resulting string. Default is2.
string: The value with leading zeros.
import { addLeadingZeros } from "./utils";
const paddedNumber = addLeadingZeros(5, 4);
console.log(paddedNumber); // Output: 0005