-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support regex patterns in Strings.Replace
#33
Comments
import {Pipe, Compose, S, T} from 'hotscript'
export type CamelCase<Str> = Pipe<Str, [
// try replacing this
S.Replace<'([A-Z]_?[a-z])|([a-z]_?[A-Z])', '&1-&2'>,
// with:
// S.Replace<'_', '-'>,
S.Split<'-'>,
T.Map<Compose<[S.Capitalize, S.Lowercase]>>,
T.Join<''>,
S.Uncapitalize
]>
type res1 = CamelCase<'ONE_two-three'>;
// ^?
type test1 = Expect<Equal<res1, 'oneTwoThree'>>;
type res2 = CamelCase<'one_TWO-three'>;
// ^?
type test2 = Expect<Equal<res2, 'oneTwoThree'>>;
type res3 = CamelCase<'one_two-THREE'>;
// ^?
type test3 = Expect<Equal<res3, 'oneTwoThree'>>;
type res4 = CamelCase<'ONE_TWO-THREE'>;
// ^?
type test4 = Expect<Equal<res4, 'oneTwoThree'>>;
type res5 = CamelCase<"alreadyInCamelCase">
// ^?
type test5 = Expect<Equal<res5, 'alreadyInCamelCase'>>;
export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
T,
>() => T extends Y ? 1 : 2
? true
: false;
export type Expect<T extends true> = T; |
2 tasks
Hi @gvergnaud, @franssu export type CamelCase<Str> = Pipe<
Str,
[
S.Replace<"/([a-z]+)_(?<second>\\w+)/i", "$1-$<second>">,
S.Split<"-">,
T.Map<Compose<[S.Capitalize, S.Lowercase]>>,
T.Join<"">,
S.Uncapitalize,
S.Match<"/(?<g1>[a-z]+)/i">,
O.Get<"groups.g1">
]
>;
type res1 = CamelCase<"ONE_two-three">;
// ^?
type test1 = Expect<Equal<res1, "oneTwoThree">>;
type res2 = CamelCase<"one_TWO-three">;
// ^?
type test2 = Expect<Equal<res2, "oneTwoThree">>;
type res3 = CamelCase<"one_two-THREE">;
// ^?
type test3 = Expect<Equal<res3, "oneTwoThree">>;
type res4 = CamelCase<"ONE_TWO-THREE">;
// ^?
type test4 = Expect<Equal<res4, "oneTwoThree">>;
type res5 = CamelCase<"alreadyInCamelCase">;
// ^?
type test5 = Expect<Equal<res5, "alreadyincamelcase">>; And feedbacks or inputs are very welcome, thanks 😊 |
2 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wouldn't it be nice if we could write things like
Strings.Replace<'([A-Z]_?[a-z])|([a-z]_?[A-Z])', '&1-&2'>
?If somebody is up for a type challenge, here is a playground: Playground 😘
The text was updated successfully, but these errors were encountered: