Skip to content

Commit

Permalink
BigInt helpers and Forbid type
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Oct 13, 2024
1 parent 8b3d5d8 commit c7ba096
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 34 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 1.8.0

### Minor Changes

- BigInt helpers and Forbid type

## 1.7.0

### Minor Changes
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/constants/numbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const INT4_MAX = 2147483647;
export const INT4_MIN = -INT4_MAX;
8 changes: 8 additions & 0 deletions src/types/Date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
*/
Expand Down
4 changes: 4 additions & 0 deletions src/types/Required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ export type Required<T> = {
};

export type PickRequired<T, K extends keyof T> = Required<Pick<T, K>>;

export type Forbid<T, K extends keyof any> = {
[P in keyof T]: P extends K ? never : T[P];
};
23 changes: 0 additions & 23 deletions src/types/WithDatey.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ export * from "./Point";
export * from "./PrismaSelect";
export * from "./Required";
export * from "./Serialized";
export * from "./WithDatey";
43 changes: 43 additions & 0 deletions src/validators/isNumber.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { expect, it, describe } from "@jest/globals";
import {
isBigInt,
isBigIntString,
isEven,
isInt,
isNegativeInt,
isNumber,
isOdd,
isOutsideInt4,
isPositiveInt,
} from "./isNumber";

Expand Down Expand Up @@ -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);
});
});
19 changes: 19 additions & 0 deletions src/validators/isNumber.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
};
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"types": ["node"],
"target": "es6",
"target": "ES2020",
"composite": false,
"declaration": true,
"declarationMap": true,
Expand Down

0 comments on commit c7ba096

Please sign in to comment.