-
Notifications
You must be signed in to change notification settings - Fork 0
feat(toCase): add ToKebabCase and ToSakeCase and ArrayContains #7
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
Open
dziekonskik
wants to merge
2
commits into
main
Choose a base branch
from
feat/string--toCase-and-ArrayContains
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| export type _ArrayContains< | ||
| Array extends (string | number | boolean)[], | ||
| Value extends string | number | boolean, | ||
| IndexArr extends number[] = [], | ||
| > = IndexArr["length"] extends Array["length"] | ||
| ? false | ||
| : Array[IndexArr["length"]] extends Value | ||
| ? true | ||
| : _ArrayContains< | ||
| Array, | ||
| Value, | ||
| [...IndexArr, 7] | ||
| > | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { | ||
| describe, | ||
| it, | ||
| expectTypeOf, | ||
| } from "vitest" | ||
| import type { _ArrayContains } from "./algo" | ||
|
|
||
| describe("ArrayContains type tests", () => { | ||
| it("should return true if the array contains the element", () => { | ||
| type T1 = _ArrayContains<[1, 2, 3], 2> // 2 is in the array | ||
| type T2 = _ArrayContains<["a", "b", "c"], "b"> // "b" is in the array | ||
| type T3 = _ArrayContains< | ||
| [true, false, true], | ||
| true | ||
| > // true is in the array | ||
| expectTypeOf<T1>().toEqualTypeOf<true>() | ||
| expectTypeOf<T2>().toEqualTypeOf<true>() | ||
| expectTypeOf<T3>().toEqualTypeOf<true>() | ||
| }) | ||
|
|
||
| it("should return false if the array does not contain the element", () => { | ||
| type T1 = _ArrayContains<[1, 2, 3], 4> // 4 is not in the array | ||
| type T2 = _ArrayContains<["a", "b", "c"], "d"> // "d" is not in the array | ||
| type T3 = _ArrayContains< | ||
| [true, false, true], | ||
| false | ||
| > // false is in the array | ||
| type T4 = _ArrayContains<[], "anything"> // empty array, always false | ||
| expectTypeOf<T1>().toEqualTypeOf<false>() | ||
| expectTypeOf<T2>().toEqualTypeOf<false>() | ||
| expectTypeOf<T3>().toEqualTypeOf<true>() | ||
| expectTypeOf<T4>().toEqualTypeOf<false>() | ||
| }) | ||
|
|
||
| it("should handle arrays of mixed types correctly", () => { | ||
| type T1 = _ArrayContains<[1, "a", true], 1> // 1 is in the array | ||
| type T2 = _ArrayContains<[1, "a", true], "a"> // "a" is in the array | ||
| type T3 = _ArrayContains< | ||
| [1, "a", true], | ||
| false | ||
| > // false is not in the array | ||
| type T4 = _ArrayContains<[1, "a", true], "b"> // "b" is not in the array | ||
| expectTypeOf<T1>().toEqualTypeOf<true>() | ||
| expectTypeOf<T2>().toEqualTypeOf<true>() | ||
| expectTypeOf<T3>().toEqualTypeOf<false>() | ||
| expectTypeOf<T4>().toEqualTypeOf<false>() | ||
| }) | ||
|
|
||
| it("should return false for incompatible types", () => { | ||
| type T1 = _ArrayContains<[1, 2, 3], "1"> // string "1" is not the same as number 1 | ||
| type T2 = _ArrayContains<["a", "b", "c"], 1> // numbers are not in a string array | ||
| type T3 = _ArrayContains< | ||
| [true, false], | ||
| "true" | ||
| > // string "true" is not the same as boolean true | ||
| expectTypeOf<T1>().toEqualTypeOf<false>() | ||
| expectTypeOf<T2>().toEqualTypeOf<false>() | ||
| expectTypeOf<T3>().toEqualTypeOf<false>() | ||
| }) | ||
|
|
||
| it("should handle arrays of a single type", () => { | ||
| type T1 = _ArrayContains< | ||
| ["apple", "banana", "cherry"], | ||
| "banana" | ||
| > // "banana" is in the array | ||
| type T2 = _ArrayContains<[1, 2, 3], 3> // 3 is in the array | ||
| type T3 = _ArrayContains<[true, false], true> // true is in the array | ||
| type T4 = _ArrayContains<[true, false], false> // false is in the array | ||
| expectTypeOf<T1>().toEqualTypeOf<true>() | ||
| expectTypeOf<T2>().toEqualTypeOf<true>() | ||
| expectTypeOf<T3>().toEqualTypeOf<true>() | ||
| expectTypeOf<T4>().toEqualTypeOf<true>() | ||
| }) | ||
|
|
||
| it("should handle arrays with duplicate values", () => { | ||
| type T1 = _ArrayContains<[1, 2, 2, 3], 2> // 2 appears multiple times, still true | ||
| type T2 = _ArrayContains< | ||
| ["a", "b", "a", "c"], | ||
| "a" | ||
| > // "a" appears multiple times, still true | ||
| type T3 = _ArrayContains< | ||
| [true, false, true], | ||
| false | ||
| > // false is present, still true | ||
| expectTypeOf<T1>().toEqualTypeOf<true>() | ||
| expectTypeOf<T2>().toEqualTypeOf<true>() | ||
| expectTypeOf<T3>().toEqualTypeOf<true>() | ||
| }) | ||
|
|
||
| it("should return false for arrays that do not match the specified type", () => { | ||
| type T1 = _ArrayContains< | ||
| ["a", "b", "c"], | ||
| number | ||
| > // numbers are not in a string array | ||
| type T2 = _ArrayContains<[1, 2, 3], boolean> // booleans are not in a number array | ||
| type T3 = _ArrayContains< | ||
| [true, false], | ||
| string | ||
| > // strings are not in a boolean array | ||
| expectTypeOf<T1>().toEqualTypeOf<false>() | ||
| expectTypeOf<T2>().toEqualTypeOf<false>() | ||
| expectTypeOf<T3>().toEqualTypeOf<false>() | ||
| }) | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** | ||
| * A type that searches for a match in the array, if there is a match between array element and given the thing you look for it returns true. | ||
| * Only primitive values are supported. | ||
| * | ||
| * @template Array - Array of primitive values. | ||
| * @template Value - Value you look for. | ||
| * @returns {boolean} - Returns `true` if array contains or 'false' otherwise. | ||
| * | ||
| * | ||
| * @example | ||
| * type ArrayContainsBanana = ArrayContains<["apple", "banana", "cherry"], "banana">; // Result: true | ||
| * type ArrayContainsFalse = StartsWith<[true, true, false], false>; // Result: true | ||
| */ | ||
|
|
||
| import type { _ArrayContains } from "./algo" | ||
|
|
||
| export type ArrayContains< | ||
| A extends (string | number | boolean)[], | ||
| V extends string | number | boolean, | ||
| > = _ArrayContains<A, V, []> |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| export type _StrToArr<Str extends string, Acc extends string[]> = [ | ||
| Str, | ||
| ] extends [`${infer First}${infer Rest}`] | ||
| export type _StrToArr< | ||
| Str extends string, | ||
| Acc extends string[], | ||
| > = [Str] extends [`${infer First}${infer Rest}`] | ||
| ? _StrToArr<Rest, [...Acc, First]> | ||
| : Acc | ||
|
|
||
| export type _StrToArr_Back$<Str> = Str extends string | ||
| ? _StrToArr<Str, []> | ||
| : never | ||
| export type _StrToArr_Back$<Str> = | ||
| Str extends string ? _StrToArr<Str, []> : never |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import type { ArrayContains } from "array/arrayContains" | ||
| import type { _StrToArr } from "string/strToArr/algo" | ||
|
|
||
| type Separators = [" ", "_", "-"] | ||
|
|
||
| type Saintize<Result extends string> = | ||
| Result extends `-${infer Rest}` | ||
| ? Saintize<Rest> | ||
| : Result extends `${infer P}--${infer Q}` | ||
| ? Saintize<`${P}-${Q}`> | ||
| : Uncapitalize<Result> | ||
|
|
||
| export type _ToKebabCase< | ||
| Str extends string, | ||
| Builder extends string = "", | ||
| > = Str extends `${infer First}${infer Rest}` | ||
| ? ArrayContains<Separators, First> extends true | ||
| ? _ToKebabCase<Rest, `${Builder}-`> | ||
| : First extends Uppercase<First> | ||
| ? _ToKebabCase< | ||
| Rest, | ||
| `${Builder}-${Lowercase<First>}` | ||
| > | ||
| : _ToKebabCase<Rest, `${Builder}${First}`> | ||
| : Saintize<Builder> |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { | ||
| describe, | ||
| it, | ||
| expectTypeOf, | ||
| } from "vitest" | ||
| import type { _ToKebabCase } from "./algo" | ||
|
|
||
| describe("_ToKebabCase type tests", () => { | ||
| it("should convert camelCase to kebab-case", () => { | ||
| type T1 = _ToKebabCase<"camelCase"> | ||
| type T2 = _ToKebabCase<"thisIsATest"> | ||
| type T3 = _ToKebabCase<"anotherExampleHere"> | ||
| expectTypeOf<T1>().toEqualTypeOf<"camel-case">() | ||
| expectTypeOf<T2>().toEqualTypeOf<"this-is-a-test">() | ||
| expectTypeOf<T3>().toEqualTypeOf<"another-example-here">() | ||
| }) | ||
|
|
||
| it("should handle strings with separators (space, underscore, hyphen)", () => { | ||
| type T1 = _ToKebabCase<"hello world"> | ||
| type T2 = _ToKebabCase<"hello_world"> | ||
| type T3 = _ToKebabCase<"hello-world"> | ||
| expectTypeOf<T1>().toEqualTypeOf<"hello-world">() | ||
| expectTypeOf<T2>().toEqualTypeOf<"hello-world">() | ||
| expectTypeOf<T3>().toEqualTypeOf<"hello-world">() | ||
| }) | ||
|
|
||
| it("should handle uppercase letters properly", () => { | ||
| type T1 = _ToKebabCase<"HelloWorld"> | ||
| type T2 = _ToKebabCase<"UpperCase"> | ||
| expectTypeOf<T1>().toEqualTypeOf<"hello-world">() | ||
| expectTypeOf<T2>().toEqualTypeOf<"upper-case">() | ||
| }) | ||
|
|
||
| it("should handle strings with mixed separators and cases", () => { | ||
| type T1 = | ||
| _ToKebabCase<"MixOf_Separators And-Cases"> | ||
| type T2 = | ||
| _ToKebabCase<"another_Example-Here Today"> | ||
| expectTypeOf<T1>().toEqualTypeOf<"mix-of-separators-and-cases">() | ||
| expectTypeOf<T2>().toEqualTypeOf<"another-example-here-today">() | ||
| }) | ||
|
|
||
| it("should handle strings without any uppercase letters or separators", () => { | ||
| type T1 = _ToKebabCase<"lowercase"> | ||
| type T2 = _ToKebabCase<"already-kebab"> | ||
| type T3 = _ToKebabCase<"plain"> | ||
| expectTypeOf<T1>().toEqualTypeOf<"lowercase">() | ||
| expectTypeOf<T2>().toEqualTypeOf<"already-kebab">() | ||
| expectTypeOf<T3>().toEqualTypeOf<"plain">() | ||
| }) | ||
|
|
||
| it("should handle empty strings and strings without transformations", () => { | ||
| type T1 = _ToKebabCase<""> | ||
| type T2 = _ToKebabCase<"simple"> | ||
| expectTypeOf<T1>().toEqualTypeOf<"">() | ||
| expectTypeOf<T2>().toEqualTypeOf<"simple">() | ||
| }) | ||
|
|
||
| it("should handle strings with leading or trailing separators", () => { | ||
| type T1 = _ToKebabCase<"trailing-space "> | ||
| type T2 = _ToKebabCase<"trailing-hyphen-"> | ||
| expectTypeOf<T1>().toEqualTypeOf<"trailing-space-">() | ||
| expectTypeOf<T2>().toEqualTypeOf<"trailing-hyphen-">() | ||
| }) | ||
|
|
||
| it("should handle uppercase strings", () => { | ||
| type T1 = _ToKebabCase<"UPPERCASE"> | ||
| type T2 = _ToKebabCase<"MIXEDUppercase"> | ||
| expectTypeOf<T1>().toEqualTypeOf<"u-p-p-e-r-c-a-s-e">() | ||
| expectTypeOf<T2>().toEqualTypeOf<"m-i-x-e-d-uppercase">() | ||
| }) | ||
|
|
||
| it("should handle strings with multiple separators in sequence", () => { | ||
| type T1 = | ||
| _ToKebabCase<"string__with__multiple__underscores"> | ||
| type T2 = | ||
| _ToKebabCase<"string--with--multiple--hyphens"> | ||
| type T3 = | ||
| _ToKebabCase<"string with multiple spaces"> | ||
| expectTypeOf<T1>().toEqualTypeOf<"string-with-multiple-underscores">() | ||
| expectTypeOf<T2>().toEqualTypeOf<"string-with-multiple-hyphens">() | ||
| expectTypeOf<T3>().toEqualTypeOf<"string-with-multiple-spaces">() | ||
| }) | ||
|
|
||
| it("should preserve existing kebab-case", () => { | ||
| type T1 = _ToKebabCase<"already-kebab-case"> | ||
| type T2 = _ToKebabCase<"more-kebab-case-here"> | ||
| expectTypeOf<T1>().toEqualTypeOf<"already-kebab-case">() | ||
| expectTypeOf<T2>().toEqualTypeOf<"more-kebab-case-here">() | ||
| }) | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /** | ||
| * A ToKebabCase implementation on a type level. | ||
| * | ||
| * @template String - A string to turn into kebab case. | ||
| * @returns {string} - a-string-to-turn-into-kebab-case. | ||
| * | ||
| * | ||
| * @example | ||
| * type KebabCased = ToKebabCase<"MixOf_Separators And-Cases">; // Result: mix-of-separators-and-cases | ||
| */ | ||
|
|
||
| import type { _ToKebabCase } from "./algo" | ||
|
|
||
| export type ToKebabCase<Str extends string> = | ||
| _ToKebabCase<Str> |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import type { ArrayContains } from "array/arrayContains" | ||
| import type { _StrToArr } from "string/strToArr/algo" | ||
|
|
||
| type Separators = [" ", "_", "-"] | ||
|
|
||
| type Saintize<Result extends string> = | ||
| Result extends `_${infer Rest}` | ||
| ? Saintize<Rest> | ||
| : Result extends `${infer P}__${infer Q}` | ||
| ? Saintize<`${P}_${Q}`> | ||
| : Uncapitalize<Result> | ||
|
|
||
| export type _ToSnakeCase< | ||
| Str extends string, | ||
| Builder extends string = "", | ||
| > = Str extends `${infer First}${infer Rest}` | ||
| ? ArrayContains<Separators, First> extends true | ||
| ? _ToSnakeCase<Rest, `${Builder}_`> | ||
| : First extends Uppercase<First> | ||
| ? _ToSnakeCase< | ||
| Rest, | ||
| `${Builder}_${Lowercase<First>}` | ||
| > | ||
| : _ToSnakeCase<Rest, `${Builder}${First}`> | ||
| : Saintize<Builder> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Replace magic number with 1 for index increment.
The index array is being incremented with
7which seems incorrect. For array traversal, the index should be incremented by1.📝 Committable suggestion