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
14 changes: 14 additions & 0 deletions src/string/isDigit/algo.ts
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
88 changes: 88 additions & 0 deletions src/string/isDigit/index.test.ts
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>()
})
})
11 changes: 11 additions & 0 deletions src/string/isDigit/index.ts
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.
*/
Comment on lines +1 to +6

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
*/


import type { _IsDigit } from "./algo"

export type IsDigit<T extends string> =
_IsDigit<T>