Skip to content

Commit

Permalink
chore: Code organization (#45)
Browse files Browse the repository at this point in the history
* chore: Move files and functions around

* chore: Split deepCasingKeys from casingKeys functions
  • Loading branch information
gustavoguichard committed Oct 6, 2023
1 parent 2301931 commit 3331ce9
Show file tree
Hide file tree
Showing 22 changed files with 866 additions and 839 deletions.
61 changes: 6 additions & 55 deletions src/casing.test.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,7 @@
import type * as Subject from './casing'
import * as subject from './casing'

const weirdString = ' someWeird-cased$*String1986Foo Bar W_FOR_WUMBO' as const
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'
>
>
}
export const weirdString =
' someWeird-cased$*String1986Foo [Bar] W_FOR_WUMBO...' as const

describe('capitalize', () => {
test('it does nothing with a string that has no char at the beginning', () => {
Expand Down Expand Up @@ -88,14 +37,16 @@ describe('uncapitalize', () => {

describe('casing functions', () => {
test('toUpperCase', () => {
const expected = ' SOMEWEIRD-CASED$*STRING1986FOO BAR W_FOR_WUMBO' as const
const expected =
' SOMEWEIRD-CASED$*STRING1986FOO [BAR] W_FOR_WUMBO...' as const
const result = subject.toUpperCase(weirdString)
expect(result).toEqual(expected)
type test = Expect<Equal<typeof result, typeof expected>>
})

test('toLowerCase', () => {
const expected = ' someweird-cased$*string1986foo bar w_for_wumbo' as const
const expected =
' someweird-cased$*string1986foo [bar] w_for_wumbo...' as const
const result = subject.toLowerCase(weirdString)
expect(result).toEqual(expected)
type test = Expect<Equal<typeof result, typeof expected>>
Expand Down
56 changes: 56 additions & 0 deletions src/casing.types.test.ts
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))
45 changes: 45 additions & 0 deletions src/chars.ts
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 }
32 changes: 32 additions & 0 deletions src/chars.types.test.ts
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))
120 changes: 120 additions & 0 deletions src/deep-key-casing.test.ts
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>>
})
Loading

0 comments on commit 3331ce9

Please sign in to comment.