-
Notifications
You must be signed in to change notification settings - Fork 0
feat(isDigit): add isDigit type #6
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
1
commit into
main
Choose a base branch
from
feat/string--isDigit
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.
+113
−0
Open
Changes from all commits
Commits
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,14 @@ | ||
| import type { IsStringLiteral } from "@predicates" | ||
|
|
||
| export type _IsDigit<T> = [T] extends [ | ||
| `${number}`, | ||
| ] | ||
| ? true | ||
| : false | ||
|
|
||
| export type IsDigit_Back<T> = | ||
| IsStringLiteral<T> extends true | ||
| ? [T] extends [`${number}`] | ||
| ? true | ||
| : false | ||
| : 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,88 @@ | ||
| import { | ||
| describe, | ||
| it, | ||
| expectTypeOf, | ||
| } from "vitest" | ||
|
|
||
| import type { | ||
| _IsDigit, | ||
| IsDigit_Back, | ||
| } from "./algo" | ||
|
|
||
| describe("_IsDigit type tests", () => { | ||
| it("should return true for string literals that match numeric patterns", () => { | ||
| type T1 = _IsDigit<"0"> // "0" extends `${number}` => true | ||
| type T2 = _IsDigit<"123"> // "123" extends `${number}` => true | ||
| type T3 = _IsDigit<"001"> // "001" extends `${number}` => true | ||
| type T4 = _IsDigit<"1.2"> // "1.2" extends `${number}` => true (valid numeric string) | ||
| expectTypeOf<T1>().toEqualTypeOf<true>() | ||
| expectTypeOf<T2>().toEqualTypeOf<true>() | ||
| expectTypeOf<T3>().toEqualTypeOf<true>() | ||
| expectTypeOf<T4>().toEqualTypeOf<true>() | ||
| }) | ||
|
|
||
| it("should return false for string literals that are not numeric", () => { | ||
| type T1 = _IsDigit<""> // empty string => not a valid number | ||
| type T2 = _IsDigit<"abc"> // not a numeric string | ||
| type T3 = _IsDigit<"1a2"> // contains non-numeric characters | ||
| type T4 = _IsDigit<"one"> // obviously not numeric | ||
| expectTypeOf<T1>().toEqualTypeOf<false>() | ||
| expectTypeOf<T2>().toEqualTypeOf<false>() | ||
| expectTypeOf<T3>().toEqualTypeOf<false>() | ||
| expectTypeOf<T4>().toEqualTypeOf<false>() | ||
| }) | ||
|
|
||
| it("should return false for non-literal or non-string types", () => { | ||
| // For a generic string, TS cannot confirm it's strictly `${number}` literal. | ||
| type T1 = _IsDigit<string> | ||
| type T2 = _IsDigit<number> // definitely not a string literal | ||
| type T3 = _IsDigit<123> // same, not a string | ||
| expectTypeOf<T1>().toEqualTypeOf<false>() | ||
| expectTypeOf<T2>().toEqualTypeOf<false>() | ||
| expectTypeOf<T3>().toEqualTypeOf<false>() | ||
| }) | ||
| }) | ||
|
|
||
| describe("IsDigit_Back type tests", () => { | ||
| it("should return true for numeric string literals", () => { | ||
| type T1 = IsDigit_Back<"0"> | ||
| type T2 = IsDigit_Back<"123"> | ||
| type T3 = IsDigit_Back<"001"> | ||
| type T4 = IsDigit_Back<"1.2"> | ||
| expectTypeOf<T1>().toEqualTypeOf<true>() | ||
| expectTypeOf<T2>().toEqualTypeOf<true>() | ||
| expectTypeOf<T3>().toEqualTypeOf<true>() | ||
| expectTypeOf<T4>().toEqualTypeOf<true>() | ||
| }) | ||
|
|
||
| it("should return false for non-numeric or non-literal strings", () => { | ||
| type T1 = IsDigit_Back<"abc"> // not numeric | ||
| type T2 = IsDigit_Back<"1a2"> // contains letters | ||
| type T3 = IsDigit_Back<""> // empty string => not a valid number | ||
| expectTypeOf<T1>().toEqualTypeOf<false>() | ||
| expectTypeOf<T2>().toEqualTypeOf<false>() | ||
| expectTypeOf<T3>().toEqualTypeOf<false>() | ||
| }) | ||
|
|
||
| it("should return false for generic string or non-string types", () => { | ||
| type T1 = IsDigit_Back<string> // not a literal | ||
| type T2 = IsDigit_Back<number> // not a string at all | ||
| type T3 = IsDigit_Back<123> // also not a string | ||
| expectTypeOf<T1>().toEqualTypeOf<false>() | ||
| expectTypeOf<T2>().toEqualTypeOf<false>() | ||
| expectTypeOf<T3>().toEqualTypeOf<false>() | ||
| }) | ||
|
|
||
| it("should return false when given a union of string literals (one not digit)", () => { | ||
| // If IsStringLiteral is strictly checking for a single literal type, | ||
| // then a union of two string literals is not "a single literal." | ||
| 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." | ||
| type T1 = IsDigit_Back<"123" | "345" | "1214"> | ||
| expectTypeOf<T1>().toEqualTypeOf<true>() | ||
| }) | ||
| }) |
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,11 @@ | ||
| /** | ||
| * 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. | ||
| */ | ||
|
|
||
| import type { _IsDigit } from "./algo" | ||
|
|
||
| export type IsDigit<T extends string> = | ||
| _IsDigit<T> | ||
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.
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.
📝 Committable suggestion