Repository navigation
fix: split case conversions on spaces #150
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| type Splitter = "-" | "_" | "/" | "."; | ||
| type Splitter = "-" | "_" | "/" | "." | " "; | ||
| type FirstOfString<S extends string> = S extends `${infer F}${string}` | ||
| ? F | ||
| : never; | ||
|
|
@@ -51,7 +51,9 @@ export type SplitByCase< | |
| ? string[] | ||
| : T extends `${infer F}${infer R}` | ||
| ? [LastOfArray<Accumulator>] extends [never] | ||
| ? SplitByCase<R, Separator, [F]> | ||
| ? F extends Separator | ||
| ? SplitByCase<R, Separator, ["", ""]> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace the leading empty placeholder with the first word token. For input with a leading separator and an uppercase letter followed by a lowercase letter, the type-level split does not match the runtime result. For The second Suggested fix
|
||
| : SplitByCase<R, Separator, [F]> | ||
| : LastOfArray<Accumulator> extends string | ||
| ? R extends "" | ||
| ? SplitByCase< | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Treat a leading separator as a word boundary in the type-level splitter.
SplitByCasestores the first character in the accumulator before it checks for a separator. For" foo", runtimesplitByCasereturns["", "foo"], butSplitByCase<" foo">returns[" foo"]. The type-level result therefore includes the separator in the first word.The same first-character path applies to every
Splittermember."-foo","_foo","/foo", and".foo"show the same divergence. The new" "member makes space input subject to this mismatch as well.The first character must be checked against
Separatorbefore it is added to a word. A leading separator must produce an empty first part, as the runtime does.Handle a trailing separator in
SplitByCase.For
"foo ", runtimesplitByCasereturns["foo", ""], andkebabCase("foo ")returns"foo-".SplitByCase<"foo ">returns["foo "]because theR extends ""branch appends the final separator to the last word.JoinByCase<"foo ", "-">therefore returns"foo ".The same branch affects a trailing
-.SplitByCase<"foo-">returns["foo-"], soJoinByCase<"foo-", "_">returns"foo-", while runtimekebabCase("foo-", "_")returns"foo_". The mismatch affects type-level results only. Runtime output is unchanged.When the final character is a separator, start a new empty word instead of appending it:
Suggested fix
? SplitByCase< R, Separator, - [ - ...RemoveLastOfArray<Accumulator>, - `${LastOfArray<Accumulator>}${F}`, - ] + F extends Separator + ? [...Accumulator, ""] + : [ + ...RemoveLastOfArray<Accumulator>, + `${LastOfArray<Accumulator>}${F}`, + ] >Add a type assertion for
SplitByCase<"foo ">andJoinByCase<"foo ", "-">totest/types.test-d.ts.