Skip to content
This repository was archived by the owner on Dec 30, 2023. It is now read-only.

Commit 1268648

Browse files
authored
feat(number): ✨ Add validateFloatNumber() and improve isNumber() (#73)
1 parent 5f96726 commit 1268648

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

.changeset/clean-masks-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@terminal-nerds/snippets-number": minor
3+
---
4+
5+
✨ Add `validateFloatNumber()` and use zod on `isNumber()`
Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
1-
import { describe } from "vitest";
1+
import { returns, throws } from "@terminal-nerds/snippets-test/unit";
2+
import { describe, it } from "vitest";
3+
import { ZodError } from "zod";
24

3-
describe.todo("validateFloatNumber(value)");
5+
import { isFloatNumber, validateFloatNumber } from "./float.ts";
46

5-
describe.todo("isFloatNumber(value)");
7+
const SAMPLE_INTEGER = 1337;
8+
const SAMPLE_FLOAT = 1.234;
9+
10+
describe("validateFloatNumber(value)", () => {
11+
it(throws(ZodError).on(`sample integer number`).sample(SAMPLE_INTEGER), ({ expect }) => {
12+
expect(() => validateFloatNumber(SAMPLE_INTEGER)).toThrowError(ZodError);
13+
});
14+
15+
it(throws(ZodError).on(`infinity values`).sample(`Infinity`), ({ expect }) => {
16+
expect(() => validateFloatNumber(Number.NEGATIVE_INFINITY)).toThrowError(ZodError);
17+
expect(() => validateFloatNumber(Number.POSITIVE_INFINITY)).toThrowError(ZodError);
18+
});
19+
20+
it(returns().on(`sample float number`).sample(SAMPLE_FLOAT), ({ expect }) => {
21+
expect(() => validateFloatNumber(SAMPLE_FLOAT)).not.toThrowError(ZodError);
22+
});
23+
});
24+
25+
describe("isFloatNumber(value)", () => {
26+
it(returns(false).on(`sample integer number`).sample(SAMPLE_INTEGER), ({ expect }) => {
27+
expect(isFloatNumber(SAMPLE_INTEGER)).toBe(false);
28+
});
29+
30+
it(returns(false).on(`passed Infinity`).sample(`Infinity`), ({ expect }) => {
31+
expect(isFloatNumber(Number.NEGATIVE_INFINITY)).toBe(false);
32+
expect(isFloatNumber(Number.POSITIVE_INFINITY)).toBe(false);
33+
});
34+
35+
it(returns(true).on(`sample float number`).sample(SAMPLE_FLOAT), ({ expect }) => {
36+
expect(isFloatNumber(SAMPLE_FLOAT)).toBe(true);
37+
});
38+
});
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import type { Float } from "type-fest/source/numeric";
2+
import { z } from "zod";
23

3-
/* import { z } from "zod"; */
4-
import { isFiniteNumber } from "../finite/finite.ts";
5-
import { isInteger } from "../integer/integer.ts";
4+
const FLOAT_NUMBER_SCHEMA = z.number().refine((n) => {
5+
return !z.number().int().safeParse(n).success && z.number().finite().safeParse(n).success;
6+
}, "should not be an integer");
67

78
export type { Float } from "type-fest/source/numeric";
89

9-
/* export const FLOAT_NUMBER_SCHEMA = z.number().; */
10-
11-
/* export function validateFloatNumber<N extends number>(value: N): asserts value is Float<N> { */
12-
/* FLOAT_NUMBER_SCHEMA.parse(value); */
13-
/* } */
10+
export function validateFloatNumber<N extends number>(value: N): asserts value is Float<N> {
11+
FLOAT_NUMBER_SCHEMA.parse(value);
12+
}
1413

15-
export function isFloat<N extends number>(value: N): value is Float<N> {
16-
return isFiniteNumber(value) && !isInteger(value);
14+
export function isFloatNumber<N extends number>(value: N): value is Float<N> {
15+
return FLOAT_NUMBER_SCHEMA.safeParse(value).success;
1716
}

0 commit comments

Comments
 (0)