From c7ba096c8a8a1949beff27e400a06f966b9ef4b4 Mon Sep 17 00:00:00 2001 From: Orlando Date: Sun, 13 Oct 2024 22:14:26 +0100 Subject: [PATCH] BigInt helpers and Forbid type --- CHANGELOG.md | 6 +++++ README.md | 19 +++++++++------ package.json | 2 +- src/constants/numbers.ts | 2 ++ src/types/Date.ts | 8 ++++++ src/types/Required.ts | 4 +++ src/types/WithDatey.ts | 23 ------------------ src/types/index.ts | 1 - src/validators/isNumber.test.ts | 43 +++++++++++++++++++++++++++++++++ src/validators/isNumber.ts | 19 +++++++++++++++ tsconfig.json | 2 +- 11 files changed, 95 insertions(+), 34 deletions(-) create mode 100644 src/constants/numbers.ts delete mode 100644 src/types/WithDatey.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 57810cd..6ae7f96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 1.8.0 + +### Minor Changes + +- BigInt helpers and Forbid type + ## 1.7.0 ### Minor Changes diff --git a/README.md b/README.md index 1c54e0e..c8e79e1 100644 --- a/README.md +++ b/README.md @@ -184,26 +184,29 @@ These functions are optimized for low entropy random data generation useful for - `Coords` - `DateLike` - `Dimensions` +- `HashMap<>` + - `BoolMap` + - `NumberMap` + - `StringMap` + - `TrueMap` +- `Forbid<>` +- `Key` - `Maybe<>` - `MaybePromise<>` - `MaybePromiseOrValue<>` - `MaybePromiseOrValueArray<>` - `NonUndefined` -- `Key` +- `ObjectEntries<>` +- `ObjectEntry<>` - `ObjectKey<>` - `ObjectKeys<>` - `ObjectValue<>` - `ObjectValues<>` -- `ObjectEntry<>` -- `ObjectEntries<>` - ⭐ `PlainObject` use this instead of `Record<,>` or `extends object`, also makes sure it's not an array +- `PickRequired<>` - `PlainKey` - `Point` -- `HashMap<>` - - `BoolMap` - - `NumberMap` - - `StringMap` - - `TrueMap` +- `Required<>` - `VoidFn` ## Development diff --git a/package.json b/package.json index e41d7ab..a4daea5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "1.7.0", + "version": "1.8.0", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/constants/numbers.ts b/src/constants/numbers.ts new file mode 100644 index 0000000..6c2af6f --- /dev/null +++ b/src/constants/numbers.ts @@ -0,0 +1,2 @@ +export const INT4_MAX = 2147483647; +export const INT4_MIN = -INT4_MAX; diff --git a/src/types/Date.ts b/src/types/Date.ts index 2f294b9..510d379 100644 --- a/src/types/Date.ts +++ b/src/types/Date.ts @@ -9,6 +9,14 @@ export type ISODate = string; * @example "2021-01-01" */ export type ISODay = string; +/** + * @example "2021-01" + */ +export type ISOMonth = string; +/** + * @example "2021" + */ +export type ISOYear = string; /** * @example "America/New_York" */ diff --git a/src/types/Required.ts b/src/types/Required.ts index f3832bc..4bdf2a2 100644 --- a/src/types/Required.ts +++ b/src/types/Required.ts @@ -3,3 +3,7 @@ export type Required = { }; export type PickRequired = Required>; + +export type Forbid = { + [P in keyof T]: P extends K ? never : T[P]; +}; diff --git a/src/types/WithDatey.ts b/src/types/WithDatey.ts deleted file mode 100644 index 7be1da3..0000000 --- a/src/types/WithDatey.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { Datey } from "./Date"; - -export type WithDatey = T extends Date - ? Datey - : T extends Array - ? Array> - : T extends object - ? { [K in keyof T]: WithDatey } - : T; - -// TEST -// type WithDates = { -// a: Date; -// b: { -// c: Date; -// c1: Date | null; -// c12: Date | undefined; -// c123: Date | undefined | null; -// }; -// d: Date[]; -// }; - -// type DateyDates = WithDatey; diff --git a/src/types/index.ts b/src/types/index.ts index eb0ff06..ebd318d 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -11,4 +11,3 @@ export * from "./Point"; export * from "./PrismaSelect"; export * from "./Required"; export * from "./Serialized"; -export * from "./WithDatey"; diff --git a/src/validators/isNumber.test.ts b/src/validators/isNumber.test.ts index cb16d99..e45c39d 100644 --- a/src/validators/isNumber.test.ts +++ b/src/validators/isNumber.test.ts @@ -1,10 +1,13 @@ import { expect, it, describe } from "@jest/globals"; import { + isBigInt, + isBigIntString, isEven, isInt, isNegativeInt, isNumber, isOdd, + isOutsideInt4, isPositiveInt, } from "./isNumber"; @@ -98,4 +101,44 @@ describe("isNumber", function () { expect(isNegativeInt("0" as unknown as number)).toBe(false); }); }); + + describe("isBigInt", function () { + it("checks true", function () { + expect(isBigInt(BigInt(12345678901234567890))).toBe(true); + expect(isBigInt(1n)).toBe(true); + expect(isBigInt(0n)).toBe(true); + }); + + it("checks false", function () { + expect(isBigInt(Number.MAX_SAFE_INTEGER)).toBe(false); + expect(isBigInt(Number.MAX_SAFE_INTEGER + 1)).toBe(false); // ??? + expect(isBigInt(1)).toBe(false); + expect(isBigInt(0)).toBe(false); + }); + }); +}); + +describe("isBigIntString", function () { + it("checks true", function () { + expect(isBigIntString("12345678901234567890")).toBe(true); + }); + + it("checks false", function () { + expect(isBigIntString(Number.MAX_SAFE_INTEGER.toString())).toBe(false); + expect(isBigIntString("1")).toBe(false); + }); +}); + +describe("isOutsideInt4", function () { + it("checks true", function () { + expect(isOutsideInt4(12345678901234567890)).toBe(true); + expect(isOutsideInt4(-12345678901234567890)).toBe(true); + }); + + it("checks false", function () { + expect(isOutsideInt4(Number.MAX_SAFE_INTEGER)).toBe(false); + expect(isOutsideInt4(1)).toBe(false); + expect(isOutsideInt4(0)).toBe(false); + expect(isOutsideInt4(12345678901234567890 / 10e9)).toBe(false); + }); }); diff --git a/src/validators/isNumber.ts b/src/validators/isNumber.ts index d944837..5627f43 100644 --- a/src/validators/isNumber.ts +++ b/src/validators/isNumber.ts @@ -1,3 +1,6 @@ +import { INT4_MAX, INT4_MIN } from "../constants/numbers"; +import { isOutside } from "../math/isOutside"; + export const isInt = (arg: any) => Number.isInteger(arg); export const isEven = (arg: any) => isInt(arg) && !(arg % 2); @@ -23,3 +26,19 @@ export const isNumber = (arg: any): arg is number => { Object.prototype.toString.apply(arg) === "[object Number]" && isFinite(arg) ); }; + +export const isBigInt = (arg: any): arg is BigInt => { + return Object.prototype.toString.apply(arg) === "[object BigInt]"; +}; + +export const isBigIntString = (arg: string): boolean => { + try { + return BigInt(arg) > Number.MAX_SAFE_INTEGER; + } catch (e) { + return false; + } +}; + +export const isOutsideInt4 = (num: number): boolean => { + return isOutside(num, INT4_MIN, INT4_MAX); +}; diff --git a/tsconfig.json b/tsconfig.json index b8f0914..0a78f8e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,7 @@ "compilerOptions": { "lib": ["dom", "dom.iterable", "esnext"], "types": ["node"], - "target": "es6", + "target": "ES2020", "composite": false, "declaration": true, "declarationMap": true,