Comparing version 0.2.0 to 0.2.1
@@ -28,3 +28,3 @@ /** | ||
/** | ||
* Assures the generic matches the given condition so we avoid nesting more conditional types. | ||
* Assures the generic matches the given condition. | ||
*/ | ||
@@ -34,7 +34,27 @@ type Is<T, cond> = Extract<T, cond>; | ||
type LowerChars = Lowercase<UpperChars>; | ||
/** | ||
* Checks if the given character is an upper case letter. | ||
*/ | ||
type IsUpper<T extends string> = T extends UpperChars ? true : false; | ||
/** | ||
* Checks if the given character is a letter. | ||
*/ | ||
type IsLetter<T extends string> = IsUpper<T> extends true ? true : IsLower<T> extends true ? true : false; | ||
/** | ||
* Checks if the given character is a lower case letter. | ||
*/ | ||
type IsLower<T extends string> = T extends LowerChars ? true : false; | ||
/** | ||
* Checks if the given character is a number. | ||
*/ | ||
type IsDigit<T extends string> = T extends Digit ? true : false; | ||
/** | ||
* Checks if the given character is a separator. | ||
* E.g. space, underscore, dash, dot, slash. | ||
*/ | ||
type IsSeparator<T extends string> = T extends Separator ? true : false; | ||
/** | ||
* Checks if the given character is a special character. | ||
* E.g. not a letter, number, or separator. | ||
*/ | ||
type IsSpecial<T extends string> = IsLetter<T> extends true ? false : IsDigit<T> extends true ? false : IsSeparator<T> extends true ? false : true; | ||
@@ -169,2 +189,6 @@ /** | ||
/** | ||
* Transforms a string to camelCase. | ||
*/ | ||
type CamelCase<T extends string> = Words<T> extends [infer first, ...infer rest] ? Join<[Lowercase<Is<first, string>>, ...CapitalizeAll<Is<rest, string[]>>]> : T; | ||
/** | ||
* A strongly typed version of `toCamelCase` that works in both runtime and type level. | ||
@@ -174,5 +198,8 @@ * @param str the string to convert to camel case. | ||
*/ | ||
type CamelCase<T extends string> = Words<T> extends [infer first, ...infer rest] ? Join<[Lowercase<Is<first, string>>, ...CapitalizeAll<Is<rest, string[]>>]> : T; | ||
declare function toCamelCase<T extends string>(str: T): CamelCase<T>; | ||
/** | ||
* Transforms a string to PascalCase. | ||
*/ | ||
type PascalCase<T extends string> = Capitalize<CamelCase<T>>; | ||
/** | ||
* A strongly typed version of `toPascalCase` that works in both runtime and type level. | ||
@@ -182,8 +209,5 @@ * @param str the string to convert to pascal case. | ||
*/ | ||
type PascalCase<T extends string> = Capitalize<CamelCase<T>>; | ||
declare function toPascalCase<T extends string>(str: T): Capitalize<CamelCase<T>>; | ||
/** | ||
* A strongly typed version of `toKebabCase` that works in both runtime and type level. | ||
* @param str the string to convert to kebab case. | ||
* @returns the kebab cased string. | ||
* Transforms a string to kebab-case. | ||
*/ | ||
@@ -194,7 +218,10 @@ type KebabCase<T extends string> = Words<T> extends [infer first, ...infer rest] ? Join<[ | ||
], '-'> : T; | ||
/** | ||
* A strongly typed version of `toKebabCase` that works in both runtime and type level. | ||
* @param str the string to convert to kebab case. | ||
* @returns the kebab cased string. | ||
*/ | ||
declare function toKebabCase<T extends string>(str: T): KebabCase<T>; | ||
/** | ||
* A strongly typed version of `toSnakeCase` that works in both runtime and type level. | ||
* @param str the string to convert to snake case. | ||
* @returns the snake cased string. | ||
* Transforms a string to snake_case. | ||
*/ | ||
@@ -205,4 +232,13 @@ type SnakeCase<T extends string> = Words<T> extends [infer first, ...infer rest] ? Join<[ | ||
], '_'> : T; | ||
/** | ||
* A strongly typed version of `toSnakeCase` that works in both runtime and type level. | ||
* @param str the string to convert to snake case. | ||
* @returns the snake cased string. | ||
*/ | ||
declare function toSnakeCase<T extends string>(str: T): SnakeCase<T>; | ||
/** | ||
* Transforms a string to CONSTANT_CASE. | ||
*/ | ||
type ConstantCase<T extends string> = Uppercase<SnakeCase<T>>; | ||
/** | ||
* A strongly typed version of `toConstantCase` that works in both runtime and type level. | ||
@@ -212,5 +248,8 @@ * @param str the string to convert to constant case. | ||
*/ | ||
type ConstantCase<T extends string> = Uppercase<SnakeCase<T>>; | ||
declare function toConstantCase<T extends string>(str: T): Uppercase<SnakeCase<T>>; | ||
/** | ||
* Transforms a string to "Title Case". | ||
*/ | ||
type TitleCase<T extends string> = Join<Words<PascalCase<T>>, ' '>; | ||
/** | ||
* A strongly typed version of `toTitleCase` that works in both runtime and type level. | ||
@@ -220,6 +259,16 @@ * @param str the string to convert to title case. | ||
*/ | ||
type TitleCase<T extends string> = Join<Words<PascalCase<T>>, ' '>; | ||
declare function toTitleCase<T extends string>(str: T): Join<Words<Capitalize<CamelCase<T>>, "", "">, " ">; | ||
/** | ||
* This function is used to transform the keys of an object deeply. | ||
* It will only be transformed at runtime, so it's not type safe. | ||
* @param obj the object to transform. | ||
* @param transform the function to transform the keys from string to string. | ||
* @returns the transformed object. | ||
*/ | ||
declare function deepTransformKeys<T>(obj: T, transform: (s: string) => string): T; | ||
/** | ||
* Transforms the keys of an Record to camelCase. | ||
* T: the type of the Record to transform. | ||
*/ | ||
type DeepCamelKeys<T> = T extends [any, ...any] ? { | ||
@@ -230,9 +279,35 @@ [I in keyof T]: DeepCamelKeys<T[I]>; | ||
}; | ||
/** | ||
* A strongly typed function that recursively transforms the keys of an object to camelCase. The transformation is done both at runtime and type level. | ||
* @param obj the object to transform. | ||
* @returns the transformed object. | ||
* @example | ||
* ```ts | ||
* deepCamelKeys({ 'foo-bar': 1, 'baz-qux': 2 }) // { fooBar: 1, bazQux: 2 } | ||
* ``` | ||
*/ | ||
declare function deepCamelKeys<T>(obj: T): DeepCamelKeys<T>; | ||
type DeepSnakeKeys<T> = T extends [any, ...any] ? { | ||
[I in keyof T]: DeepSnakeKeys<T[I]>; | ||
} : T extends (infer V)[] ? DeepSnakeKeys<V>[] : { | ||
[K in keyof T as SnakeCase<Is<K, string>>]: DeepSnakeKeys<T[K]>; | ||
/** | ||
* Transforms the keys of an Record to PascalCase. | ||
* T: the type of the Record to transform. | ||
*/ | ||
type DeepPascalKeys<T> = T extends [any, ...any] ? { | ||
[I in keyof T]: DeepPascalKeys<T[I]>; | ||
} : T extends (infer V)[] ? DeepPascalKeys<V>[] : { | ||
[K in keyof T as PascalCase<Is<K, string>>]: DeepPascalKeys<T[K]>; | ||
}; | ||
declare function deepSnakeKeys<T>(obj: T): DeepSnakeKeys<T>; | ||
/** | ||
* A strongly typed function that recursively transforms the keys of an object to pascal case. The transformation is done both at runtime and type level. | ||
* @param obj the object to transform. | ||
* @returns the transformed object. | ||
* @example | ||
* ```ts | ||
* deepPascalKeys({ 'foo-bar': 1, 'baz-qux': 2 }) // { FooBar: 1, BazQux: 2 } | ||
* ``` | ||
*/ | ||
declare function deepPascalKeys<T>(obj: T): DeepPascalKeys<T>; | ||
/** | ||
* Transforms the keys of an Record to kebab-case. | ||
* T: the type of the Record to transform. | ||
*/ | ||
type DeepKebabKeys<T> = T extends [any, ...any] ? { | ||
@@ -243,9 +318,35 @@ [I in keyof T]: DeepKebabKeys<T[I]>; | ||
}; | ||
/** | ||
* A strongly typed function that recursively transforms the keys of an object to kebab-case. The transformation is done both at runtime and type level. | ||
* @param obj the object to transform. | ||
* @returns the transformed object. | ||
* @example | ||
* ```ts | ||
* deepKebabKeys({ 'foo-bar': 1, 'baz-qux': 2 }) // { 'foo-bar': 1, 'baz-qux': 2 } | ||
* ``` | ||
*/ | ||
declare function deepKebabKeys<T>(obj: T): DeepKebabKeys<T>; | ||
type DeepPascalKeys<T> = T extends [any, ...any] ? { | ||
[I in keyof T]: DeepPascalKeys<T[I]>; | ||
} : T extends (infer V)[] ? DeepPascalKeys<V>[] : { | ||
[K in keyof T as PascalCase<Is<K, string>>]: DeepPascalKeys<T[K]>; | ||
/** | ||
* Transforms the keys of an Record to snake_case. | ||
* T: the type of the Record to transform. | ||
*/ | ||
type DeepSnakeKeys<T> = T extends [any, ...any] ? { | ||
[I in keyof T]: DeepSnakeKeys<T[I]>; | ||
} : T extends (infer V)[] ? DeepSnakeKeys<V>[] : { | ||
[K in keyof T as SnakeCase<Is<K, string>>]: DeepSnakeKeys<T[K]>; | ||
}; | ||
declare function deepPascalKeys<T>(obj: T): DeepPascalKeys<T>; | ||
/** | ||
* A strongly typed function that recursively transforms the keys of an object to snake_case. The transformation is done both at runtime and type level. | ||
* @param obj the object to transform. | ||
* @returns the transformed object. | ||
* @example | ||
* ```ts | ||
* deepSnakeKeys({ 'foo-bar': 1, 'baz-qux': 2 }) // { foo_bar: 1, baz_qux: 2 } | ||
* ``` | ||
*/ | ||
declare function deepSnakeKeys<T>(obj: T): DeepSnakeKeys<T>; | ||
/** | ||
* Transforms the keys of an Record to CONSTANT_CASE. | ||
* T: the type of the Record to transform. | ||
*/ | ||
type DeepConstantKeys<T> = T extends [any, ...any] ? { | ||
@@ -256,4 +357,13 @@ [I in keyof T]: DeepConstantKeys<T[I]>; | ||
}; | ||
/** | ||
* A strongly typed function that recursively transforms the keys of an object to CONSTANT_CASE. The transformation is done both at runtime and type level. | ||
* @param obj the object to transform. | ||
* @returns the transformed object. | ||
* @example | ||
* ```ts | ||
* deepConstantKeys({ 'foo-bar': 1, 'baz-qux': 2 }) // { FOO_BAR: 1, BAZ_QUX: 2 } | ||
* ``` | ||
*/ | ||
declare function deepConstantKeys<T>(obj: T): DeepConstantKeys<T>; | ||
export { CamelCase, ConstantCase, DeepCamelKeys, DeepConstantKeys, DeepKebabKeys, DeepPascalKeys, DeepSnakeKeys, Digit, Is, IsDigit, IsLetter, IsLower, IsSeparator, IsSpecial, IsUpper, Join, KebabCase, PascalCase, Replace, ReplaceAll, Separator, SnakeCase, TitleCase, Trim, TrimEnd, TrimStart, Words, capitalize, deepCamelKeys, deepConstantKeys, deepKebabKeys, deepPascalKeys, deepSnakeKeys, deepTransformKeys, join, replace, replaceAll, toCamelCase, toConstantCase, toKebabCase, toLowerCase, toPascalCase, toSnakeCase, toTitleCase, toUpperCase, trim, trimEnd, trimStart, words }; |
@@ -133,4 +133,4 @@ "use strict"; | ||
} | ||
function deepSnakeKeys(obj) { | ||
return deepTransformKeys(obj, toSnakeCase); | ||
function deepPascalKeys(obj) { | ||
return deepTransformKeys(obj, toPascalCase); | ||
} | ||
@@ -140,4 +140,4 @@ function deepKebabKeys(obj) { | ||
} | ||
function deepPascalKeys(obj) { | ||
return deepTransformKeys(obj, toPascalCase); | ||
function deepSnakeKeys(obj) { | ||
return deepTransformKeys(obj, toSnakeCase); | ||
} | ||
@@ -144,0 +144,0 @@ function deepConstantKeys(obj) { |
{ | ||
"name": "string-ts", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Strongly-typed string functions.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -420,17 +420,9 @@ # Strongly typed string functions for all! | ||
```ts | ||
import { deepTransformKeys } from 'string-ts'; | ||
import { deepTransformKeys, toUpperCase } from 'string-ts'; | ||
const data = { | ||
'helloWorld': { | ||
'fooBar': 'baz', | ||
}, | ||
} as const; | ||
const data = { 'helloWorld': 'baz' } as const; | ||
type MyType<T> = { [K in keyof T as Uppercase<K>]: MyType<T[K]> } | ||
const result = deepTransformKeys(data, (key) => key.toUpperCase()) as MyType<typeof data>; | ||
// ^ { | ||
// 'HELLOWORLD': { | ||
// 'FOOBAR': 'baz', | ||
// }, | ||
// } | ||
type MyType<T> = { [K in keyof T as Uppercase<K>]: T[K] } | ||
const result = deepTransformKeys(data, toUpperCase) as MyType<typeof data>; | ||
// ^ { 'HELLOWORLD': 'baz' } | ||
``` | ||
@@ -437,0 +429,0 @@ |
Sorry, the diff of this file is not supported yet
35934
637
432