String Utilities
A collection of useful string utilities for converting strings between different cases. Also includes other random string formatting utilities.
Usage
- Add the package to your project:
deno add @vef/string-utils
- Import a utility and use it:
import { toSnakeCase } from "@vef/string-utils";
const snakeCaseString = toSnakeCase("HelloWorld");
console.log(snakeCaseString); // hello_world
String Conversion Utilities
A collection of functions to convert strings between different cases such as camel case, snake case, pascal case, and title case.
camelToSnakeCase
Converts a string from camel case to snake case. Also works for Pascal case.
camelToSnakeCase(inputString: string): string
Parameters:
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"
toCamelCase
Converts a string to camel case.
toCamelCase(inputString: string): string
Parameters:
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"
toPascalCase
Converts a string to pascal case.
toPascalCase(inputString: string): string
Parameters:
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"
toSnakeCase
Converts a string to snake case.
toSnakeCase(inputString: string): string
Parameters:
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"
toTitleCase
Converts a string to title case.
toTitleCase(inputString: string): string
Parameters:
inputString
(string): The input string.
Returns:
- (string): The converted string in title case.
Examples:
toTitleCase("hello_world"); // "Hello World"
camelToTitleCase
Converts a camel case string to title case.
camelToTitleCase(inputString: string): string
Parameters:
inputString
(string): The input string in camel case.
Returns:
- (string): The converted string in title case.
Examples:
camelToTitleCase("helloWorld"); // "Hello World"
Other Utilities
These are other random useful utilities
sanitizeString
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.
Parameters
inputString
(string
): The input string.
Returns
string
: The sanitized string.
Example
import { sanitizeString } from "./utils";
const result = sanitizeString("Hello World!");
console.log(result); // Output: hello_world
generateRandomString
Generates a random string of a given length.
Parameters
length
(number
, optional): The length of the random string. Default is24
.
Returns
string
: The generated random string.
Example
import { generateRandomString } from "./utils";
const randomString = generateRandomString(16);
console.log(randomString); // Output: A random string of 16 characters
addLeadingZeros
Adds leading zeros to a number or string.
Parameters
value
(number | string
): The value to add leading zeros to.length
(number
, optional): The length of the resulting string. Default is2
.
Returns
string
: The value with leading zeros.
Example
import { addLeadingZeros } from "./utils";
const paddedNumber = addLeadingZeros(5, 4);
console.log(paddedNumber); // Output: 0005