Last active
January 15, 2022 14:49
-
-
Save ephys/510bf04992f961809d8f9c3e57642f2a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// note: unfinished implementation, we still need to list a bunch of characters. See below | |
type CamelCaseImpl<S extends string, Separator extends string> = | |
S extends `${infer P1}${Separator}${infer P2}${infer P3}` | |
? CamelCaseImpl<`${P1}${Uppercase<P2>}${P3}`, Separator> | |
: S; | |
// must be nested, cannot be CamelCaseImpl<S, '-' | '_' | ' '> | |
// or strings like 'first-second_third' will not parse properly. | |
type Camelize<S extends string> = CamelCaseImpl<CamelCaseImpl<CamelCaseImpl<Trim<S>, '-'>, '_'>, ' '>; | |
// according to https://tc39.es/ecma262/multipage/text-processing.html#sec-string.prototype.trim | |
// <SPACE> <TAB> <ZWNBSP> | |
type WhiteSpace = ' ' | '\t' | '\u2060'; | |
// <VT> | |
// <FF> | |
// <USP>; // https://www.compart.com/en/unicode/category/Zs | |
// <LF> <CR> | |
type LineTerminator = '\n' | '\r'; | |
// <LS> | |
// <PS> | |
type Trimable = WhiteSpace | LineTerminator; | |
type TrimRight<T extends string> = T extends `${infer R}${Trimable}` ? TrimRight<R> : T; | |
type TrimLeft<T extends string> = T extends `${Trimable}${infer R}` ? TrimLeft<R> : T; | |
type Trim<T extends string> = TrimRight<TrimLeft<T>>; | |
const a = { | |
test_test: { | |
test_test: '', | |
}, | |
' first--second_third FOURTH -- test \t': 10, | |
// patate: 10, | |
// 5: 10, | |
// ' test-test-test ': 10, | |
// ABC: 10, | |
}; | |
type CamelizeObject<Obj extends object> = { | |
[key in keyof Obj as key extends string ? Camelize<key> : key]: Obj[key] | |
}; | |
type CamelizeObjectDeep<Obj extends object> = { | |
[key in keyof Obj as key extends string ? Camelize<key> : key]: Obj[key] extends object | |
? CamelizeObjectDeep<Obj[key]> | |
: Obj[key] | |
}; | |
function camelCaseProps<In extends object>(input: In): CamelizeObjectDeep<In> { | |
return input; | |
} | |
const b = camelCaseProps(a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment