feat(toCase): add ToKebabCase and ToSakeCase and ArrayContains#7
feat(toCase): add ToKebabCase and ToSakeCase and ArrayContains#7dziekonskik wants to merge 2 commits into
Conversation
WalkthroughThe pull request introduces several TypeScript type utilities focused on string and array manipulation. A new Changes
Sequence DiagramsequenceDiagram
participant Type as Type System
participant ArrayContains as ArrayContains<A, V>
participant ToKebabCase as ToKebabCase<Str>
participant ToSnakeCase as ToSnakeCase<Str>
Type->>ArrayContains: Check value existence
ArrayContains-->>Type: Return boolean
Type->>ToKebabCase: Convert string case
ToKebabCase-->>Type: Return kebab-case string type
Type->>ToSnakeCase: Convert string case
ToSnakeCase-->>Type: Return snake_case string type
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (14)
src/string/toSnakeCase/algo.ts (3)
4-4: Add JSDoc comment to document the Separators type.Consider adding documentation to explain the purpose and usage of the Separators type.
+/** Array of characters that are considered as word separators in the input string */ type Separators = [" ", "_", "-"]
6-11: Add JSDoc comment to document the Sanitize type.The Sanitize type implementation looks good, but would benefit from documentation explaining its purpose and edge cases it handles.
+/** + * Sanitizes the final snake_case string by: + * 1. Removing leading underscores + * 2. Replacing consecutive underscores with a single underscore + * 3. Converting the result to lowercase + */ type Saintize<Result extends string> =Also, consider fixing the typo in the type name from
SaintizetoSanitize.
13-25: Add JSDoc comment and consider performance optimization.The implementation looks correct but would benefit from documentation and potential performance optimization.
+/** + * Converts a string literal type to snake_case. + * Handles: + * - camelCase/PascalCase to snake_case + * - Spaces, hyphens, and underscores as separators + * - Multiple consecutive separators + * - Leading/trailing separators + * @example + * type T = _ToSnakeCase<"camelCase"> // "camel_case" + */ export type _ToSnakeCase< Str extends string, Builder extends string = "", > = Str extends `${infer First}${infer Rest}`Consider using a lookup type for better performance with large strings:
type UpperToSnakeCase = { [K in string]: K extends Uppercase<K> ? `_${Lowercase<K>}` : K }src/string/toSnakeCase/index.test.ts (1)
8-91: Consider adding more edge cases to the test suite.The test coverage is comprehensive, but consider adding these additional test cases:
+ it("should handle strings with numbers", () => { + type T1 = _ToSnakeCase<"user123Name"> + type T2 = _ToSnakeCase<"123UserName"> + expectTypeOf<T1>().toEqualTypeOf<"user123_name">() + expectTypeOf<T2>().toEqualTypeOf<"123_user_name">() + }) + it("should handle single character strings", () => { + type T1 = _ToSnakeCase<"A"> + type T2 = _ToSnakeCase<"a"> + expectTypeOf<T1>().toEqualTypeOf<"a">() + expectTypeOf<T2>().toEqualTypeOf<"a">() + })src/string/strToArr/algo.ts (2)
1-4: Add JSDoc documentation for the type utility.Consider adding documentation to explain the purpose, usage, and examples of this string-to-array conversion utility.
Example documentation:
/** * Converts a string into an array of characters. * * @template Str - The input string type * @template Acc - The accumulator array type * @example * type Result = _StrToArr<"hello", []> // ["h", "e", "l", "l", "o"] */
8-9: Consider renaming_StrToArr_Back$for clarity.The
Back$suffix in the type name is unclear. Consider a more descriptive name that reflects its purpose as an entry point or initializer.Example alternatives:
_InitStrToArr_CreateStrToArrsrc/array/arrayContains/algo.ts (1)
1-13: Add JSDoc documentation for the algorithm.Consider adding documentation to explain the recursive array traversal algorithm and its type parameters.
Example documentation:
/** * Internal type that recursively checks if a value exists in an array. * * @template Array - The input array type * @template Value - The value type to search for * @template IndexArr - Array tracking the current index in recursion * @example * type Result = _ArrayContains<[1, 2, 3], 2, []> // true */src/array/arrayContains/index.ts (1)
12-12: Fix typo in example documentation.The second example uses
StartsWithinstead ofArrayContains.- type ArrayContainsFalse = StartsWith<[true, true, false], false>; // Result: true + type ArrayContainsFalse = ArrayContains<[true, true, false], false>; // Result: truesrc/string/toKebabCase/algo.ts (3)
4-4: Consider renamingSeparatorsto be more specific.The type name could be more descriptive to indicate its purpose in the kebab case conversion.
-type Separators = [" ", "_", "-"] +type KebabCaseSeparators = [" ", "_", "-"]
6-11: Fix typo in type nameSaintize.The type name appears to have a typo - it should be
Sanitize.-type Saintize<Result extends string> = +type Sanitize<Result extends string> = Result extends `-${infer Rest}` - ? Saintize<Rest> + ? Sanitize<Rest> : Result extends `${infer P}--${infer Q}` - ? Saintize<`${P}-${Q}`> + ? Sanitize<`${P}-${Q}`> : Uncapitalize<Result>
13-25: Consider adding JSDoc comments for the type utility.The
_ToKebabCasetype utility would benefit from documentation explaining its purpose, parameters, and usage examples.+/** + * Converts a string to kebab-case at type level. + * @example + * type T = _ToKebabCase<"helloWorld"> // "hello-world" + */ export type _ToKebabCase< Str extends string, Builder extends string = "", > = ...src/string/toKebabCase/index.test.ts (1)
8-91: Consider adding more edge cases to the test suite.The test suite is comprehensive but could benefit from additional edge cases:
- Strings containing numbers (e.g., "hello123World")
- Empty strings with separators (e.g., "---")
- Special characters (e.g., "hello@World")
Example additional test cases:
it("should handle strings with numbers and special characters", () => { type T1 = _ToKebabCase<"hello123World"> type T2 = _ToKebabCase<"hello@World"> type T3 = _ToKebabCase<"---"> expectTypeOf<T1>().toEqualTypeOf<"hello123-world">() expectTypeOf<T2>().toEqualTypeOf<"hello@-world">() expectTypeOf<T3>().toEqualTypeOf<"">() })src/array/arrayContains/index.test.ts (2)
25-27: Fix incorrect comment for test case.The comment "false is in the array" contradicts the test name "should return false if the array does not contain the element".
- > // false is in the array + > // checking for false in array containing true
90-103: Improve test description for type compatibility checks.The test description "should return false for arrays that do not match the specified type" could be more precise as it's testing type literals against type definitions.
- it("should return false for arrays that do not match the specified type", () => { + it("should return false when checking type definitions against arrays of primitive values", () => {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (10)
src/array/arrayContains/algo.ts(1 hunks)src/array/arrayContains/index.test.ts(1 hunks)src/array/arrayContains/index.ts(1 hunks)src/string/strToArr/algo.ts(1 hunks)src/string/toKebabCase/algo.ts(1 hunks)src/string/toKebabCase/index.test.ts(1 hunks)src/string/toKebabCase/index.ts(1 hunks)src/string/toSnakeCase/algo.ts(1 hunks)src/string/toSnakeCase/index.test.ts(1 hunks)src/string/toSnakeCase/index.ts(1 hunks)
🔇 Additional comments (4)
src/string/toSnakeCase/index.ts (1)
3-4: LGTM! Clean and well-structured type export.The type export follows best practices with a clear separation between the public API and internal implementation.
src/string/toKebabCase/index.ts (1)
1-4: LGTM! Clean and follows TypeScript naming conventions.The implementation correctly follows the TypeScript naming convention of using underscore prefix for internal types while exposing a clean public API.
src/array/arrayContains/index.ts (1)
17-20: LGTM! Clean public API with proper type constraints.The implementation provides a user-friendly interface while correctly delegating to the internal implementation.
src/string/toKebabCase/index.test.ts (1)
69-70: Fix inconsistent test case for uppercase strings.The expected output for
"UPPERCASE"creates unnecessary hyphens between each letter. Consider if this is the desired behavior.The current implementation produces
"u-p-p-e-r-c-a-s-e"but a more intuitive result might be"uppercase". Please verify if this is intentional.
| : _ArrayContains< | ||
| Array, | ||
| Value, | ||
| [...IndexArr, 7] |
There was a problem hiding this comment.
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.
| [...IndexArr, 7] | |
| [...IndexArr, 1] |
Summary by CodeRabbit
New Features
ArrayContainstype utility to check if a value exists in an array at the type level.ToKebabCasetype for converting string types to kebab-case format.ToSnakeCasetype for converting string types to snake_case format.Refactor
strToArr/algo.tsfor improved clarity and syntax.Tests
ArrayContains,ToKebabCase, andToSnakeCasetype utilities.