-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: Move files and functions around * chore: Split deepCasingKeys from casingKeys functions
- Loading branch information
1 parent
2301931
commit 3331ce9
Showing
22 changed files
with
866 additions
and
839 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type * as Subject from './casing' | ||
import type { weirdString } from './casing.test' | ||
|
||
type WeirdString = typeof weirdString | 'dont.distribute unions' | ||
|
||
namespace TypeTransforms { | ||
type test = Expect< | ||
Equal< | ||
Subject.DelimiterCase<WeirdString, '%'>, | ||
| 'some%Weird%cased%$*%String%1986%Foo%Bar%W%FOR%WUMBO' | ||
| 'dont%distribute%unions' | ||
> | ||
> | ||
type test1 = Expect< | ||
Equal< | ||
Subject.CamelCase<WeirdString>, | ||
'someWeirdCased$*String1986FooBarWForWumbo' | 'dontDistributeUnions' | ||
> | ||
> | ||
type test2 = Expect< | ||
Equal< | ||
Subject.PascalCase<WeirdString>, | ||
'SomeWeirdCased$*String1986FooBarWForWumbo' | 'DontDistributeUnions' | ||
> | ||
> | ||
type test3 = Expect< | ||
Equal< | ||
Subject.KebabCase<WeirdString>, | ||
| 'some-weird-cased-$*-string-1986-foo-bar-w-for-wumbo' | ||
| 'dont-distribute-unions' | ||
> | ||
> | ||
type test4 = Expect< | ||
Equal< | ||
Subject.SnakeCase<WeirdString>, | ||
| 'some_weird_cased_$*_string_1986_foo_bar_w_for_wumbo' | ||
| 'dont_distribute_unions' | ||
> | ||
> | ||
type test5 = Expect< | ||
Equal< | ||
Subject.ConstantCase<WeirdString>, | ||
| 'SOME_WEIRD_CASED_$*_STRING_1986_FOO_BAR_W_FOR_WUMBO' | ||
| 'DONT_DISTRIBUTE_UNIONS' | ||
> | ||
> | ||
type test6 = Expect< | ||
Equal< | ||
Subject.TitleCase<WeirdString>, | ||
| 'Some Weird Cased $* String 1986 Foo Bar W For Wumbo' | ||
| 'Dont Distribute Unions' | ||
> | ||
> | ||
} | ||
|
||
test('dummy test', () => expect(true).toBe(true)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { IsSeparator } from './separators' | ||
|
||
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | ||
// prettier-ignore | ||
type UpperChars = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z' | ||
type LowerChars = Lowercase<UpperChars> | ||
|
||
// UTILITIES FOR DETECTING CHARS | ||
/** | ||
* 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 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 | ||
|
||
export type { Digit, IsDigit, IsLetter, IsLower, IsSpecial, IsUpper } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type * as Subject from './chars' | ||
|
||
namespace TypeChecks { | ||
type test1 = Expect<Equal<Subject.IsDigit<'1'>, true>> | ||
type test2 = Expect<Equal<Subject.IsDigit<'a'>, false>> | ||
type test3 = Expect<Equal<Subject.IsDigit<'A'>, false>> | ||
type test4 = Expect<Equal<Subject.IsDigit<'$'>, false>> | ||
|
||
type test5 = Expect<Equal<Subject.IsLower<'1'>, false>> | ||
type test6 = Expect<Equal<Subject.IsLower<'a'>, true>> | ||
type test7 = Expect<Equal<Subject.IsLower<'A'>, false>> | ||
type test8 = Expect<Equal<Subject.IsLower<'$'>, false>> | ||
|
||
type test9 = Expect<Equal<Subject.IsUpper<'1'>, false>> | ||
type test10 = Expect<Equal<Subject.IsUpper<'a'>, false>> | ||
type test11 = Expect<Equal<Subject.IsUpper<'A'>, true>> | ||
type test12 = Expect<Equal<Subject.IsUpper<'$'>, false>> | ||
|
||
type test13 = Expect<Equal<Subject.IsLetter<'1'>, false>> | ||
type test14 = Expect<Equal<Subject.IsLetter<'a'>, true>> | ||
type test15 = Expect<Equal<Subject.IsLetter<'A'>, true>> | ||
type test16 = Expect<Equal<Subject.IsLetter<'$'>, false>> | ||
|
||
type test26 = Expect<Equal<Subject.IsSpecial<'1'>, false>> | ||
type test27 = Expect<Equal<Subject.IsSpecial<'a'>, false>> | ||
type test28 = Expect<Equal<Subject.IsSpecial<'A'>, false>> | ||
type test29 = Expect<Equal<Subject.IsSpecial<'$'>, true>> | ||
type test30 = Expect<Equal<Subject.IsSpecial<' '>, false>> | ||
type test31 = Expect<Equal<Subject.IsSpecial<'*'>, true>> | ||
type test32 = Expect<Equal<Subject.IsSpecial<'_'>, false>> | ||
} | ||
test('dummy test', () => expect(true).toBe(true)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import * as subject from './deep-key-casing' | ||
|
||
describe('deepTransformKeys', () => { | ||
test('should deeply transform the keys of an object', () => { | ||
const expected = { | ||
SOME: { 'DEEP-NESTED': { VALUE: true } }, | ||
'OTHER-VALUE': true, | ||
} | ||
const result = subject.deepTransformKeys( | ||
{ | ||
some: { 'deep-nested': { value: true } }, | ||
'other-value': true, | ||
}, | ||
(key) => key.toUpperCase(), | ||
) | ||
expect(result).toEqual(expected) | ||
}) | ||
|
||
test('should handle null properly', () => { | ||
const expected = null | ||
const result = subject.deepConstantKeys(null) | ||
expect(result).toEqual(expected) | ||
type test = Expect<Equal<typeof result, typeof expected>> | ||
}) | ||
}) | ||
|
||
describe('deepCamelKeys', () => { | ||
test('should camelize the object', () => { | ||
const expected = { | ||
some: { deepNested: { value: true } }, | ||
otherValue: true, | ||
} | ||
const result = subject.deepCamelKeys({ | ||
some: { 'deep-nested': { value: true } }, | ||
'other-value': true, | ||
}) | ||
expect(result).toEqual(expected) | ||
type test = Expect<Equal<typeof result, typeof expected>> | ||
}) | ||
|
||
test('should camelize from SCREAMING_SNAKE_CASE', () => { | ||
const obj = { | ||
NODE_ENV: 'development', | ||
} | ||
const expected = { | ||
nodeEnv: 'development', | ||
} | ||
const result = subject.deepCamelKeys(obj) | ||
expect(result).toEqual(expected) | ||
type test = Expect<Equal<typeof result, typeof expected>> | ||
}) | ||
}) | ||
|
||
test('deepConstantKeys', () => { | ||
const expected = { | ||
SOME: { DEEP_NESTED: { VALUE: true } }, | ||
OTHER_VALUE: true, | ||
} | ||
const result = subject.deepConstantKeys({ | ||
some: { deepNested: { value: true } }, | ||
otherValue: true, | ||
}) | ||
expect(result).toEqual(expected) | ||
type test = Expect<Equal<typeof result, typeof expected>> | ||
}) | ||
|
||
test('deepDelimiterKeys', () => { | ||
const expected = { | ||
some: { 'deep@nested': { value: true } }, | ||
'other@value': true, | ||
} | ||
const result = subject.deepDelimiterKeys( | ||
{ | ||
some: { 'deep-nested': { value: true } }, | ||
'other-value': true, | ||
}, | ||
'@', | ||
) | ||
expect(result).toEqual(expected) | ||
type test = Expect<Equal<typeof result, typeof expected>> | ||
}) | ||
|
||
test('deepKebabKeys', () => { | ||
const expected = { | ||
some: { 'deep-nested': { value: true } }, | ||
'other-value': true, | ||
} | ||
const result = subject.deepKebabKeys({ | ||
some: { deepNested: { value: true } }, | ||
otherValue: true, | ||
}) | ||
expect(result).toEqual(expected) | ||
type test = Expect<Equal<typeof result, typeof expected>> | ||
}) | ||
|
||
test('deepPascalKeys', () => { | ||
const expected = { | ||
Some: { DeepNested: { Value: true } }, | ||
OtherValue: true, | ||
} | ||
const result = subject.deepPascalKeys({ | ||
some: { deepNested: { value: true } }, | ||
otherValue: true, | ||
}) | ||
expect(result).toEqual(expected) | ||
type test = Expect<Equal<typeof result, typeof expected>> | ||
}) | ||
|
||
test('deepSnakeKeys', () => { | ||
const expected = { | ||
some: { deep_nested: { value: true } }, | ||
other_value: true, | ||
} | ||
const result = subject.deepSnakeKeys({ | ||
some: { deepNested: { value: true } }, | ||
otherValue: true, | ||
}) | ||
expect(result).toEqual(expected) | ||
type test = Expect<Equal<typeof result, typeof expected>> | ||
}) |
Oops, something went wrong.