Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/array/arrayContains/algo.ts
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]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Replace magic number with 1 for index increment.

The index array is being incremented with 7 which seems incorrect. For array traversal, the index should be incremented by 1.

-        [...IndexArr, 7]
+        [...IndexArr, 1]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
[...IndexArr, 7]
[...IndexArr, 1]

>
104 changes: 104 additions & 0 deletions src/array/arrayContains/index.test.ts
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>()
})
})
20 changes: 20 additions & 0 deletions src/array/arrayContains/index.ts
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, []>
12 changes: 6 additions & 6 deletions src/string/strToArr/algo.ts
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
25 changes: 25 additions & 0 deletions src/string/toKebabCase/algo.ts
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>
91 changes: 91 additions & 0 deletions src/string/toKebabCase/index.test.ts
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">()
})
})
15 changes: 15 additions & 0 deletions src/string/toKebabCase/index.ts
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>
25 changes: 25 additions & 0 deletions src/string/toSnakeCase/algo.ts
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>
Loading