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.
conststr='hello-world';constresult=str.replace('-',' ');// you should use: as 'hello world'// ^? stringThis 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';conststr='hello-world';constresult=replace(str,'-',' ');// ^ 'hello world'npm install string-ts- Runtime counterparts of native type utilities
- Strongly-typed alternatives to native runtime utilities
- Strongly-typed alternatives to common loosely-typed functions
- Strongly-typed deep transformation of objects
- Type Utilities
- Runtime-only utilities
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';conststr='hello world';constresult=capitalize(str);// ^ 'Hello world'This function is a strongly-typed counterpart of String.prototype.toUpperCase.
import{toUpperCase}from'string-ts';conststr='hello world';constresult=toUpperCase(str);// ^ 'HELLO WORLD'This function is a strongly-typed counterpart of String.prototype.toLowerCase.
import{toLowerCase}from'string-ts';conststr='HELLO WORLD';constresult=toLowerCase(str);// ^ 'hello world'This function is a strongly-typed counterpart of String.prototype.trim.
import{trim}from'string-ts';conststr=' hello world ';constresult=trim(str);// ^ 'hello world'This function is a strongly-typed counterpart of String.prototype.trimStart.
import{trimStart}from'string-ts';conststr=' hello world ';constresult=trimStart(str);// ^ 'hello world 'This function is a strongly-typed counterpart of String.prototype.trimEnd.
import{trimEnd}from'string-ts';conststr=' hello world ';constresult=trimEnd(str);// ^ ' hello world'This function is a strongly-typed counterpart of Array.prototype.join.
import{join}from'string-ts';conststr=['hello','world']as['hello','world'];constresult=join(str,' ');// ^ 'hello world'This function is a strongly-typed counterpart of String.prototype.replace.
import{replace}from'string-ts';conststr='hello-world-';constresult=replace(str,'-',' ');// ^ 'hello world-'This function is a strongly-typed counterpart of String.prototype.replaceAll.
import{replaceAll}from'string-ts';conststr='hello-world-';constresult=replaceAll(str,'-',' ');// ^ 'hello world 'This function is a strongly-typed counterpart of String.prototype.split.
import{split}from'string-ts';conststr='hello-world';constresult=split(str,'-');// ^ ['hello', 'world']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';conststr='-20someVery-weird String';constresult=words(str);// ^ ['20', 'some', 'Very', 'weird', 'String']This function converts a string to a new case with a custom delimiter at both runtime and type levels.
import{toDelimiterCase}from'string-ts';conststr='helloWorld';constresult=toDelimiterCase(str,'.');// ^ 'hello.World'This function converts a string to camelCase at both runtime and type levels.
import{toCamelCase}from'string-ts';conststr='hello-world';constresult=toCamelCase(str);// ^ 'helloWorld'This function converts a string to PascalCase at both runtime and type levels.
import{toPascalCase}from'string-ts';conststr='hello-world';constresult=toPascalCase(str);// ^ 'HelloWorld'This function converts a string to kebab-case at both runtime and type levels.
import{toKebabCase}from'string-ts';conststr='helloWorld';constresult=toKebabCase(str);// ^ 'hello-world'This function converts a string to snake_case at both runtime and type levels.
import{toSnakeCase}from'string-ts';conststr='helloWorld';constresult=toSnakeCase(str);// ^ 'hello_world'This function converts a string to CONSTANT_CASE at both runtime and type levels.
import{toConstantCase}from'string-ts';conststr='helloWorld';constresult=toConstantCase(str);// ^ 'HELLO_WORLD'This function converts a string to Title Case at both runtime and type levels.
import{toTitleCase}from'string-ts';conststr='helloWorld';constresult=toTitleCase(str);// ^ 'Hello World'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';constdata={'hello-world': {'foo-bar': 'baz',},}asconst;constresult=deepDelimiterKeys(data,'.');// ^ { 'hello.world': { 'foo.bar': 'baz' }}This function recursively converts the keys of an object to camelCase at both runtime and type levels.
import{deepCamelKeys}from'string-ts';constdata={'hello-world': {'foo-bar': 'baz',},}asconst;constresult=deepCamelKeys(data);// ^ { helloWorld: { fooBar: 'baz' }}This function recursively converts the keys of an object to PascalCase at both runtime and type levels.
import{deepPascalKeys}from'string-ts';constdata={'hello-world': {'foo-bar': 'baz',},}asconst;constresult=deepPascalKeys(data);// ^ { HelloWorld: { FooBar: 'baz' }}This function recursively converts the keys of an object to kebab-case at both runtime and type levels.
import{deepKebabKeys}from'string-ts';constdata={'helloWorld': {'fooBar': 'baz',},}asconst;constresult=deepKebabKeys(data);// ^ { 'hello-world': { 'foo-bar': 'baz' }}This function recursively converts the keys of an object to snake_case at both runtime and type levels.
import{deepSnakeKeys}from'string-ts';constdata={'helloWorld': {'fooBar': 'baz',},}asconst;constresult=deepSnakeKeys(data);// ^ { 'hello_world': { 'foo_bar': 'baz' }}This function recursively converts the keys of an object to CONSTANT_CASE at both runtime and type levels.
import{deepConstantKeys}from'string-ts';constdata={'helloWorld': {'fooBar': 'baz',},}asconst;constresult=deepConstantKeys(data);// ^ { 'HELLO_WORLD': { 'FOO_BAR': 'baz' }}All the functions presented in this API have associated type counterparts.
importtype*asStfrom'string-ts';Capitalize<'hello world'>// 'Hello world'Lowercase<'HELLO WORLD'>// 'hello world'Uppercase<'hello world'>// 'HELLO WORLD'St.Words<'hello-world'>// ['hello', 'world']St.Join<['hello','world'],'-'>// 'hello-world'St.Replace<'hello-world','l','1'>// 'he1lo-world'St.ReplaceAll<'hello-world','l','1'>// 'he11o-wor1d'St.Split<'hello-world','-'>// ['hello', 'world']St.TrimStart<' hello world '>// 'hello world 'St.TrimEnd<' hello world '>// ' hello world'St.Trim<' hello world '>// 'hello world'St.CamelCase<'hello-world'>// 'helloWorld'St.PascalCase<'hello-world'>// 'HelloWorld'St.KebabCase<'helloWorld'>// 'hello-world'St.SnakeCase<'helloWorld'>// 'hello_world'St.ConstantCase<'helloWorld'>// 'HELLO_WORLD'St.TitleCase<'helloWorld'>// 'Hello World'St.DelimiterCase<'hello world','.'>// 'hello.world'St.DeepDelimiterKeys<{'hello-world': {'foo-bar': 'baz'}},'.'>// { 'hello.world': { 'foo.bar': 'baz' }}St.DeepCamelKeys<{'hello-world': {'foo-bar': 'baz'}}>// { helloWorld: { fooBar: 'baz' }}St.DeepPascalKeys<{'hello-world': {'foo-bar': 'baz'}}>// { HelloWorld: { FooBar: 'baz' }}St.DeepKebabKeys<{'helloWorld': {'fooBar': 'baz'}}>// { 'hello-world': { 'foo-bar': 'baz' }}St.DeepSnakeKeys<{'helloWorld': {'fooBar': 'baz'}}>// { 'hello_world': { 'foo_bar': 'baz' }}St.DeepConstantKeys<{'helloWorld': {'fooBar': 'baz'}}>// { 'HELLO_WORLD': { 'FOO_BAR': 'baz' }}St.IsDigit<'a'>// falseSt.IsDigit<'1'>// trueSt.IsLetter<'a'>// trueSt.IsLetter<'1'>// falseSt.IsLower<'a'>// trueSt.IsLower<'A'>// falseSt.IsUpper<'a'>// falseSt.IsUpper<'A'>// trueSt.IsSeparator<' '>// trueSt.IsSeparator<'-'>// trueSt.IsSeparator<'a'>// falseSt.IsSpecial<'a'>// falseSt.IsSpecial<'!'>// trueSt.IsSpecial<' '>// falseThis function recursively converts the keys of an object to a custom format, but only at runtime level.
import{deepTransformKeys,toUpperCase}from'string-ts';constdata={'helloWorld': 'baz'}asconst;typeMyType<T>={[KinkeyofTasUppercase<K>]: T[K]}constresult=deepTransformKeys(data,toUpperCase)asMyType<typeofdata>;// ^ { 'HELLOWORLD': 'baz' }This library doesn't support every internal character for the sake of keeping the maintainer's sanity.