Strongly-typed string functions for all!
😬 The problem
When you are working with literal strings, the string manipulation functions only work at the runtime level and the types don't follow those transformations.
You end up losing type information and possibly having to cast the result.
const str = 'hello-world' as const;
const result = str.replace('-', ' ');
🤓 The solution
This library aims to solve this problem by providing a set of common functions that work with literal strings at both type and runtime level.
import { replace } from 'string-ts';
const str = 'hello-world' as const;
const result = replace(str, '-', ' ');
📦 Installation
npm install string-ts
📖 API
Runtime counterparts of native type utilities
capitalize
Capitalizes the first letter of a string. This is a runtime counterpart of Capitalize<T>
from src/types.d.ts
.
import { capitalize } from 'string-ts';
const str = 'hello world' as const;
const result = capitalize(str);
Strongly-typed alternatives to native runtime utilities
toUpperCase
This function is a strongly-typed counterpart of String.prototype.toUpperCase
.
import { toUpperCase } from 'string-ts';
const str = 'hello world' as const;
const result = toUpperCase(str);
toLowerCase
This function is a strongly-typed counterpart of String.prototype.toLowerCase
.
import { toLowerCase } from 'string-ts';
const str = 'HELLO WORLD' as const;
const result = toLowerCase(str);
trim
This function is a strongly-typed counterpart of String.prototype.trim
.
import { trim } from 'string-ts';
const str = ' hello world ' as const;
const result = trim(str);
trimStart
This function is a strongly-typed counterpart of String.prototype.trimStart
.
import { trimStart } from 'string-ts';
const str = ' hello world ' as const;
const result = trimStart(str);
trimEnd
This function is a strongly-typed counterpart of String.prototype.trimEnd
.
import { trimEnd } from 'string-ts';
const str = ' hello world ' as const;
const result = trimEnd(str);
join
This function is a strongly-typed counterpart of Array.prototype.join
.
import { join } from 'string-ts';
const str = ['hello', 'world'] as ['hello', 'world'];
const result = join(str, ' ');
replace
This function is a strongly-typed counterpart of String.prototype.replace
.
import { replace } from 'string-ts';
const str = 'hello-world-' as const;
const result = replace(str, '-', ' ');
replaceAll
This function is a strongly-typed counterpart of String.prototype.replaceAll
.
import { replaceAll } from 'string-ts';
const str = 'hello-world-' as const;
const result = replaceAll(str, '-', ' ');
split
This function is a strongly-typed counterpart of String.prototype.split
.
import { split } from 'string-ts';
const str = 'hello-world' as const;
const result = split(str, '-');
Strongly-typed alternatives to common loosely-typed functions
words
This function identifies the words in a string and returns a tuple of words split by separators, differences in casing, numbers, and etc.
import { words } from 'string-ts';
const str = '-20someVery-weird String' as const;
const result = words(str);
toDelimiterCase
This function converts a string to a new case with a custom delimiter at both runtime and type levels.
import { toDelimiterCase } from 'string-ts';
const str = 'helloWorld' as const;
const result = toDelimiterCase(str, '.');
toCamelCase
This function converts a string to camelCase
at both runtime and type levels.
import { toCamelCase } from 'string-ts';
const str = 'hello-world' as const;
const result = toCamelCase(str);
toPascalCase
This function converts a string to PascalCase
at both runtime and type levels.
import { toPascalCase } from 'string-ts';
const str = 'hello-world' as const;
const result = toPascalCase(str);
toKebabCase
This function converts a string to kebab-case
at both runtime and type levels.
import { toKebabCase } from 'string-ts';
const str = 'helloWorld' as const;
const result = toKebabCase(str);
toSnakeCase
This function converts a string to snake_case
at both runtime and type levels.
import { toSnakeCase } from 'string-ts';
const str = 'helloWorld' as const;
const result = toSnakeCase(str);
toConstantCase
This function converts a string to CONSTANT_CASE
at both runtime and type levels.
import { toConstantCase } from 'string-ts';
const str = 'helloWorld' as const;
const result = toConstantCase(str);
toTitleCase
This function converts a string to Title Case
at both runtime and type levels.
import { toTitleCase } from 'string-ts';
const str = 'helloWorld' as const;
const result = toTitleCase(str);
Strongly-typed deep transformation of objects
deepDelimiterKeys
This function recursively converts the keys of an object to a new case with a custom delimiter at both runtime and type levels.
import { deepDelimiterKeys } from 'string-ts';
const data = {
'hello-world': {
'foo-bar': 'baz',
},
} as const;
const result = deepDelimiterKeys(data, '.');
deepCamelKeys
This function recursively converts the keys of an object to camelCase
at both runtime and type levels.
import { deepCamelKeys } from 'string-ts';
const data = {
'hello-world': {
'foo-bar': 'baz',
},
} as const;
const result = deepCamelKeys(data);
deepPascalKeys
This function recursively converts the keys of an object to PascalCase
at both runtime and type levels.
import { deepPascalKeys } from 'string-ts';
const data = {
'hello-world': {
'foo-bar': 'baz',
},
} as const;
const result = deepPascalKeys(data);
deepKebabKeys
This function recursively converts the keys of an object to kebab-case
at both runtime and type levels.
import { deepKebabKeys } from 'string-ts';
const data = {
'helloWorld': {
'fooBar': 'baz',
},
} as const;
const result = deepKebabKeys(data);
deepSnakeKeys
This function recursively converts the keys of an object to snake_case
at both runtime and type levels.
import { deepSnakeKeys } from 'string-ts';
const data = {
'helloWorld': {
'fooBar': 'baz',
},
} as const;
const result = deepSnakeKeys(data);
deepConstantKeys
This function recursively converts the keys of an object to CONSTANT_CASE
at both runtime and type levels.
import { deepConstantKeys } from 'string-ts';
const data = {
'helloWorld': {
'fooBar': 'baz',
},
} as const;
const result = deepConstantKeys(data);
Type Utilities
All the functions presented in this API have associated type counterparts.
import type * as St from 'string-ts';
Native TS type utilities
Capitalize<'hello world'>
Lowercase<'HELLO WORLD'>
Uppercase<'hello world'>
General Type utilities from this library
St.Words<'hello-world'>
St.Join<['hello', 'world'], '-'>
St.Replace<'hello-world', 'l', '1'>
St.ReplaceAll<'hello-world', 'l', '1'>
St.Split<'hello-world', '-'>
St.TrimStart<' hello world '>
St.TrimEnd<' hello world '>
St.Trim<' hello world '>
Casing type utilities
St.CamelCase<'hello-world'>
St.PascalCase<'hello-world'>
St.KebabCase<'helloWorld'>
St.SnakeCase<'helloWorld'>
St.ConstantCase<'helloWorld'>
St.TitleCase<'helloWorld'>
St.DelimiterCase<'hello world', '.'>
St.DeepDelimiterKeys<{
'hello-world': { 'foo-bar': 'baz' }
}, '.'>
St.DeepCamelKeys<{
'hello-world': { 'foo-bar': 'baz' }
}>
St.DeepPascalKeys<{
'hello-world': { 'foo-bar': 'baz' }
}>
St.DeepKebabKeys<{
'helloWorld': { 'fooBar': 'baz' }
}>
St.DeepSnakeKeys<{
'helloWorld': { 'fooBar': 'baz' }
}>
St.DeepConstantKeys<{
'helloWorld': { 'fooBar': 'baz' }
}>
Other exported type utilities
St.IsDigit<'a'>
St.IsDigit<'1'>
St.IsLetter<'a'>
St.IsLetter<'1'>
St.IsLower<'a'>
St.IsLower<'A'>
St.IsUpper<'a'>
St.IsUpper<'A'>
St.IsSeparator<' '>
St.IsSeparator<'-'>
St.IsSeparator<'a'>
St.IsSpecial<'a'>
St.IsSpecial<'!'>
St.IsSpecial<' '>
Runtime-only utilities
deepTransformKeys
This function recursively converts the keys of an object to a custom format, but only at runtime level.
import { deepTransformKeys, toUpperCase } from 'string-ts';
const data = { 'helloWorld': 'baz' } as const;
type MyType<T> = { [K in keyof T as Uppercase<K>]: T[K] }
const result = deepTransformKeys(data, toUpperCase) as MyType<typeof data>;
Disclaimer
This library doesn't support every internal character for the sake of keeping the maintainer's sanity.