Skip to content

feat(isDigit): add isDigit type#6

Open
dziekonskik wants to merge 1 commit into
mainfrom
feat/string--isDigit
Open

feat(isDigit): add isDigit type#6
dziekonskik wants to merge 1 commit into
mainfrom
feat/string--isDigit

Conversation

@dziekonskik

@dziekonskik dziekonskik commented Jan 14, 2025

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features

    • Added type-level utilities for checking if a string represents a digit
    • Introduced new type IsDigit for compile-time validation of numeric string types
    • Enhanced type safety with comprehensive type checking mechanisms
  • Tests

    • Added extensive test cases for digit string type validation
    • Verified behavior of type utilities across various input scenarios

@dziekonskik dziekonskik requested a review from HideoKun January 14, 2025 19:52
@coderabbitai

coderabbitai Bot commented Jan 14, 2025

Copy link
Copy Markdown

Walkthrough

The pull request introduces a set of TypeScript utility types for checking whether a given type represents a digit string. These types are implemented across three files in the src/string/isDigit/ directory. The new types provide compile-time type safety for identifying string representations of numbers, with different levels of type checking implemented through _IsDigit, IsDigit_Back, and IsDigit types.

Changes

File Change Summary
src/string/isDigit/algo.ts Added two new types:
- _IsDigit<T>: Checks if type is a string representation of a number
- IsDigit_Back<T>: Extends _IsDigit with additional string literal checking
src/string/isDigit/index.test.ts Added comprehensive type tests for _IsDigit and IsDigit_Back types, covering various input scenarios
src/string/isDigit/index.ts Introduced IsDigit<T extends string> type with JSDoc documentation

Poem

🐰 Digits dancing in type's embrace,
Strings transformed with algorithmic grace
Numbers parsed with compiler's might
Type safety shining ever so bright!
A rabbit's code, precise and clean 🔢

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
src/string/isDigit/algo.ts (2)

3-7: Consider adding JSDoc comments for the _IsDigit type.

While the implementation is correct, adding documentation would help explain the purpose of this internal utility type and its expected behavior.

+/**
+ * Internal utility type that checks if a type parameter extends a numeric string pattern.
+ * @template T - The type to check
+ * @returns {boolean} - True if T extends `${number}`, false otherwise
+ * @internal
+ */
 export type _IsDigit<T> = [T] extends [
   `${number}`,
 ]
   ? true
   : false

9-14: Consider renaming IsDigit_Back and adding documentation.

The "_Back" suffix is unclear. If this is an alternative implementation, consider a more descriptive name like "IsDigitStrict" since it adds string literal validation.

+/**
+ * Strict type guard that checks if a type parameter is both a string literal
+ * and matches a numeric string pattern.
+ * @template T - The type to check
+ * @returns {boolean} - True if T is a string literal and extends `${number}`, false otherwise
+ */
-export type IsDigit_Back<T> =
+export type IsDigitStrict<T> =
   IsStringLiteral<T> extends true
     ? [T] extends [`${number}`]
       ? true
       : false
     : false
src/string/isDigit/index.test.ts (2)

12-44: Add tests for additional numeric string patterns.

The test suite is thorough but could be expanded to cover more edge cases:

  • Scientific notation (e.g., "1e5")
  • Negative numbers (e.g., "-123")
  • Leading/trailing whitespace
 describe("_IsDigit type tests", () => {
   it("should return true for string literals that match numeric patterns", () => {
     type T1 = _IsDigit<"0">
     type T2 = _IsDigit<"123">
     type T3 = _IsDigit<"001">
     type T4 = _IsDigit<"1.2">
+    type T5 = _IsDigit<"-123">
+    type T6 = _IsDigit<"1e5">
     expectTypeOf<T1>().toEqualTypeOf<true>()
     expectTypeOf<T2>().toEqualTypeOf<true>()
     expectTypeOf<T3>().toEqualTypeOf<true>()
     expectTypeOf<T4>().toEqualTypeOf<true>()
+    expectTypeOf<T5>().toEqualTypeOf<true>()
+    expectTypeOf<T6>().toEqualTypeOf<true>()
   })

   it("should return false for string literals that are not numeric", () => {
     type T1 = _IsDigit<"">
     type T2 = _IsDigit<"abc">
     type T3 = _IsDigit<"1a2">
     type T4 = _IsDigit<"one">
+    type T5 = _IsDigit<" 123 ">
     expectTypeOf<T1>().toEqualTypeOf<false>()
     expectTypeOf<T2>().toEqualTypeOf<false>()
     expectTypeOf<T3>().toEqualTypeOf<false>()
     expectTypeOf<T4>().toEqualTypeOf<false>()
+    expectTypeOf<T5>().toEqualTypeOf<false>()
   })

76-87: Improve comments for union type tests.

The current comments about IsStringLiteral are repeated and could be more precise about the expected behavior with unions.

-  // If IsStringLiteral is strictly checking for a single literal type,
-  // then a union of two string literals is not "a single literal."
+  // Test that a union containing any non-digit string literal evaluates to false
   type T1 = IsDigit_Back<"123" | "abc" | "234">
   expectTypeOf<T1>().toEqualTypeOf<false>()
 })
 it("should return true when given a union of string literals (all digits)", () => {
-  // If IsStringLiteral is strictly checking for a single literal type,
-  // then a union of two string literals is not "a single literal."
+  // Test that a union of only digit string literals evaluates to true
   type T1 = IsDigit_Back<"123" | "345" | "1214">
   expectTypeOf<T1>().toEqualTypeOf<true>()
 })
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2ecdf95 and 162133f.

📒 Files selected for processing (3)
  • src/string/isDigit/algo.ts (1 hunks)
  • src/string/isDigit/index.test.ts (1 hunks)
  • src/string/isDigit/index.ts (1 hunks)

Comment on lines +1 to +6
/**
* A type that returns true if a param is a stringified number, and false otherwise.
*
* @template Str - The string literal type to reverse.
* @returns {string} - Returns the reversed string.
*/

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

Fix incorrect JSDoc documentation.

The current documentation has incorrect template parameter description and return type. It appears to be copied from a string reversal utility.

 /**
  * A type that returns true if a param is a stringified number, and false otherwise.
  *
- * @template Str - The string literal type to reverse.
- * @returns {string} - Returns the reversed string.
+ * @template T - The string type to check.
+ * @returns {boolean} - Returns true if T represents a valid numeric string, false otherwise.
+ * @example
+ * type T1 = IsDigit<"123">  // true
+ * type T2 = IsDigit<"abc">  // false
+ * type T3 = IsDigit<"1.2">  // true
+ * type T4 = IsDigit<"">     // false
  */
📝 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
/**
* A type that returns true if a param is a stringified number, and false otherwise.
*
* @template Str - The string literal type to reverse.
* @returns {string} - Returns the reversed string.
*/
/**
* A type that returns true if a param is a stringified number, and false otherwise.
*
* @template T - The string type to check.
* @returns {boolean} - Returns true if T represents a valid numeric string, false otherwise.
* @example
* type T1 = IsDigit<"123"> // true
* type T2 = IsDigit<"abc"> // false
* type T3 = IsDigit<"1.2"> // true
* type T4 = IsDigit<""> // false
*/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant