@webdeveric/utils 0.76.0 → 0.77.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertIsSafeInteger = assertIsSafeInteger;
4
+ const isSafeInteger_js_1 = require("../predicate/isSafeInteger.js");
5
+ const getError_js_1 = require("./getError.js");
6
+ function assertIsSafeInteger(input, error = 'input is not a safe integer') {
7
+ if (!(0, isSafeInteger_js_1.isSafeInteger)(input)) {
8
+ throw (0, getError_js_1.getError)(error);
9
+ }
10
+ }
@@ -49,6 +49,7 @@ __exportStar(require("./assertIsPrimitive.js"), exports);
49
49
  __exportStar(require("./assertIsPromiseFulfilledResult.js"), exports);
50
50
  __exportStar(require("./assertIsPromiseRejectedResult.js"), exports);
51
51
  __exportStar(require("./assertIsPropertyKey.js"), exports);
52
+ __exportStar(require("./assertIsSafeInteger.js"), exports);
52
53
  __exportStar(require("./assertIsSizeAware.js"), exports);
53
54
  __exportStar(require("./assertIsString.js"), exports);
54
55
  __exportStar(require("./assertIsStringArray.js"), exports);
package/dist/cjs/index.js CHANGED
@@ -58,6 +58,7 @@ __exportStar(require("./joinStrings.js"), exports);
58
58
  __exportStar(require("./jsonParse.js"), exports);
59
59
  __exportStar(require("./looksLikeURL.js"), exports);
60
60
  __exportStar(require("./normalize.js"), exports);
61
+ __exportStar(require("./numbers.js"), exports);
61
62
  __exportStar(require("./parseNumber.js"), exports);
62
63
  __exportStar(require("./pathParts.js"), exports);
63
64
  __exportStar(require("./prefix.js"), exports);
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.uInt32Max = exports.uInt32Min = exports.int32Max = exports.int32Min = void 0;
4
+ exports.int32Min = -0x80000000;
5
+ exports.int32Max = 0x7fffffff;
6
+ exports.uInt32Min = 0;
7
+ exports.uInt32Max = 0xffffffff;
@@ -68,6 +68,7 @@ __exportStar(require("./isPromiseFulfilledResult.js"), exports);
68
68
  __exportStar(require("./isPromiseLike.js"), exports);
69
69
  __exportStar(require("./isPromiseRejectedResult.js"), exports);
70
70
  __exportStar(require("./isPropertyKey.js"), exports);
71
+ __exportStar(require("./isSafeInteger.js"), exports);
71
72
  __exportStar(require("./isSizeAware.js"), exports);
72
73
  __exportStar(require("./isString.js"), exports);
73
74
  __exportStar(require("./isStringArray.js"), exports);
@@ -75,6 +76,7 @@ __exportStar(require("./isStringRecord.js"), exports);
75
76
  __exportStar(require("./isStringWithLength.js"), exports);
76
77
  __exportStar(require("./isSymbol.js"), exports);
77
78
  __exportStar(require("./isSymbolArray.js"), exports);
79
+ __exportStar(require("./isUInt32.js"), exports);
78
80
  __exportStar(require("./isUndefined.js"), exports);
79
81
  __exportStar(require("./isUndefinedArray.js"), exports);
80
82
  __exportStar(require("./isUnknown.js"), exports);
@@ -1,8 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isInt32 = isInt32;
4
+ const numbers_js_1 = require("../numbers.js");
4
5
  function isInt32(input) {
5
- const int32Min = -2_147_483_648;
6
- const int32Max = 2_147_483_647;
7
- return typeof input === 'number' && Number.isInteger(input) && input >= int32Min && input <= int32Max;
6
+ return typeof input === 'number' && Number.isInteger(input) && input >= numbers_js_1.int32Min && input <= numbers_js_1.int32Max;
8
7
  }
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isSafeInteger = void 0;
4
+ const isSafeInteger = (input) => Number.isSafeInteger(input);
5
+ exports.isSafeInteger = isSafeInteger;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isUInt32 = isUInt32;
4
+ const numbers_js_1 = require("../numbers.js");
5
+ function isUInt32(input) {
6
+ return typeof input === 'number' && Number.isInteger(input) && input >= numbers_js_1.uInt32Min && input <= numbers_js_1.uInt32Max;
7
+ }
@@ -1,19 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.randomInt = randomInt;
4
- const assertIsInteger_js_1 = require("./assertion/assertIsInteger.js");
4
+ const assertIsSafeInteger_js_1 = require("./assertion/assertIsSafeInteger.js");
5
5
  const isObject_js_1 = require("./predicate/isObject.js");
6
6
  function randomInt(arg1, arg2) {
7
- const { min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER } = (0, isObject_js_1.isObject)(arg1)
8
- ? arg1
9
- : { min: arg1, max: arg2 };
10
- (0, assertIsInteger_js_1.assertIsInteger)(min, 'min must be an integer');
11
- (0, assertIsInteger_js_1.assertIsInteger)(max, 'max must be an integer');
12
- if (min === max) {
13
- return min;
14
- }
15
- if (min > max) {
7
+ const { min = 0, max = Number.MAX_SAFE_INTEGER } = (0, isObject_js_1.isObject)(arg1) ? arg1 : { min: arg1, max: arg2 };
8
+ (0, assertIsSafeInteger_js_1.assertIsSafeInteger)(min, 'min must be a safe integer');
9
+ (0, assertIsSafeInteger_js_1.assertIsSafeInteger)(max, 'max must be a safe integer');
10
+ if (min >= max) {
16
11
  throw new RangeError('min must be less than max');
17
12
  }
18
- return Math.floor(Math.random() * (max - min)) + min;
13
+ const minBigInt = BigInt(min);
14
+ const range = BigInt(max) - minBigInt;
15
+ const limit = (2n ** 64n / range) * range;
16
+ const data = new BigInt64Array(1);
17
+ let value;
18
+ do {
19
+ crypto.getRandomValues(data);
20
+ value = BigInt.asUintN(64, data[0]);
21
+ } while (value >= limit);
22
+ return Number(minBigInt + (value % range));
19
23
  }
@@ -3,8 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.byRandom = void 0;
4
4
  const byRandom = () => {
5
5
  let value;
6
+ const data = new Uint8Array(1);
6
7
  do {
7
- value = crypto.getRandomValues(new Uint8Array(1))[0];
8
+ value = crypto.getRandomValues(data)[0];
8
9
  } while (value >= 252);
9
10
  return (value % 3) - 1;
10
11
  };
@@ -0,0 +1,7 @@
1
+ import { isSafeInteger } from '../predicate/isSafeInteger.js';
2
+ import { getError } from './getError.js';
3
+ export function assertIsSafeInteger(input, error = 'input is not a safe integer') {
4
+ if (!isSafeInteger(input)) {
5
+ throw getError(error);
6
+ }
7
+ }
@@ -33,6 +33,7 @@ export * from './assertIsPrimitive.js';
33
33
  export * from './assertIsPromiseFulfilledResult.js';
34
34
  export * from './assertIsPromiseRejectedResult.js';
35
35
  export * from './assertIsPropertyKey.js';
36
+ export * from './assertIsSafeInteger.js';
36
37
  export * from './assertIsSizeAware.js';
37
38
  export * from './assertIsString.js';
38
39
  export * from './assertIsStringArray.js';
package/dist/mjs/index.js CHANGED
@@ -42,6 +42,7 @@ export * from './joinStrings.js';
42
42
  export * from './jsonParse.js';
43
43
  export * from './looksLikeURL.js';
44
44
  export * from './normalize.js';
45
+ export * from './numbers.js';
45
46
  export * from './parseNumber.js';
46
47
  export * from './pathParts.js';
47
48
  export * from './prefix.js';
@@ -0,0 +1,4 @@
1
+ export const int32Min = -0x80000000;
2
+ export const int32Max = 0x7fffffff;
3
+ export const uInt32Min = 0;
4
+ export const uInt32Max = 0xffffffff;
@@ -52,6 +52,7 @@ export * from './isPromiseFulfilledResult.js';
52
52
  export * from './isPromiseLike.js';
53
53
  export * from './isPromiseRejectedResult.js';
54
54
  export * from './isPropertyKey.js';
55
+ export * from './isSafeInteger.js';
55
56
  export * from './isSizeAware.js';
56
57
  export * from './isString.js';
57
58
  export * from './isStringArray.js';
@@ -59,6 +60,7 @@ export * from './isStringRecord.js';
59
60
  export * from './isStringWithLength.js';
60
61
  export * from './isSymbol.js';
61
62
  export * from './isSymbolArray.js';
63
+ export * from './isUInt32.js';
62
64
  export * from './isUndefined.js';
63
65
  export * from './isUndefinedArray.js';
64
66
  export * from './isUnknown.js';
@@ -1,5 +1,4 @@
1
+ import { int32Max, int32Min } from '../numbers.js';
1
2
  export function isInt32(input) {
2
- const int32Min = -2_147_483_648;
3
- const int32Max = 2_147_483_647;
4
3
  return typeof input === 'number' && Number.isInteger(input) && input >= int32Min && input <= int32Max;
5
4
  }
@@ -0,0 +1 @@
1
+ export const isSafeInteger = (input) => Number.isSafeInteger(input);
@@ -0,0 +1,4 @@
1
+ import { uInt32Max, uInt32Min } from '../numbers.js';
2
+ export function isUInt32(input) {
3
+ return typeof input === 'number' && Number.isInteger(input) && input >= uInt32Min && input <= uInt32Max;
4
+ }
@@ -1,16 +1,20 @@
1
- import { assertIsInteger } from './assertion/assertIsInteger.js';
1
+ import { assertIsSafeInteger } from './assertion/assertIsSafeInteger.js';
2
2
  import { isObject } from './predicate/isObject.js';
3
3
  export function randomInt(arg1, arg2) {
4
- const { min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER } = isObject(arg1)
5
- ? arg1
6
- : { min: arg1, max: arg2 };
7
- assertIsInteger(min, 'min must be an integer');
8
- assertIsInteger(max, 'max must be an integer');
9
- if (min === max) {
10
- return min;
11
- }
12
- if (min > max) {
4
+ const { min = 0, max = Number.MAX_SAFE_INTEGER } = isObject(arg1) ? arg1 : { min: arg1, max: arg2 };
5
+ assertIsSafeInteger(min, 'min must be a safe integer');
6
+ assertIsSafeInteger(max, 'max must be a safe integer');
7
+ if (min >= max) {
13
8
  throw new RangeError('min must be less than max');
14
9
  }
15
- return Math.floor(Math.random() * (max - min)) + min;
10
+ const minBigInt = BigInt(min);
11
+ const range = BigInt(max) - minBigInt;
12
+ const limit = (2n ** 64n / range) * range;
13
+ const data = new BigInt64Array(1);
14
+ let value;
15
+ do {
16
+ crypto.getRandomValues(data);
17
+ value = BigInt.asUintN(64, data[0]);
18
+ } while (value >= limit);
19
+ return Number(minBigInt + (value % range));
16
20
  }
@@ -1,7 +1,8 @@
1
1
  export const byRandom = () => {
2
2
  let value;
3
+ const data = new Uint8Array(1);
3
4
  do {
4
- value = crypto.getRandomValues(new Uint8Array(1))[0];
5
+ value = crypto.getRandomValues(data)[0];
5
6
  } while (value >= 252);
6
7
  return (value % 3) - 1;
7
8
  };
@@ -0,0 +1 @@
1
+ export declare function assertIsSafeInteger(input: unknown, error?: string | Error): asserts input is number;
@@ -33,6 +33,7 @@ export * from './assertIsPrimitive.js';
33
33
  export * from './assertIsPromiseFulfilledResult.js';
34
34
  export * from './assertIsPromiseRejectedResult.js';
35
35
  export * from './assertIsPropertyKey.js';
36
+ export * from './assertIsSafeInteger.js';
36
37
  export * from './assertIsSizeAware.js';
37
38
  export * from './assertIsString.js';
38
39
  export * from './assertIsStringArray.js';
@@ -42,6 +42,7 @@ export * from './joinStrings.js';
42
42
  export * from './jsonParse.js';
43
43
  export * from './looksLikeURL.js';
44
44
  export * from './normalize.js';
45
+ export * from './numbers.js';
45
46
  export * from './parseNumber.js';
46
47
  export * from './pathParts.js';
47
48
  export * from './prefix.js';
@@ -0,0 +1,4 @@
1
+ export declare const int32Min = -2147483648;
2
+ export declare const int32Max = 2147483647;
3
+ export declare const uInt32Min = 0;
4
+ export declare const uInt32Max = 4294967295;
@@ -52,6 +52,7 @@ export * from './isPromiseFulfilledResult.js';
52
52
  export * from './isPromiseLike.js';
53
53
  export * from './isPromiseRejectedResult.js';
54
54
  export * from './isPropertyKey.js';
55
+ export * from './isSafeInteger.js';
55
56
  export * from './isSizeAware.js';
56
57
  export * from './isString.js';
57
58
  export * from './isStringArray.js';
@@ -59,6 +60,7 @@ export * from './isStringRecord.js';
59
60
  export * from './isStringWithLength.js';
60
61
  export * from './isSymbol.js';
61
62
  export * from './isSymbolArray.js';
63
+ export * from './isUInt32.js';
62
64
  export * from './isUndefined.js';
63
65
  export * from './isUndefinedArray.js';
64
66
  export * from './isUnknown.js';
@@ -0,0 +1 @@
1
+ export declare const isSafeInteger: (input: unknown) => input is number;
@@ -0,0 +1,2 @@
1
+ import type { UInt32 } from '../types/numbers.js';
2
+ export declare function isUInt32(input: unknown): input is UInt32;
@@ -6,4 +6,5 @@ export type OctalNumberString = `0${'o' | 'O'}${number}`;
6
6
  export type BinaryNumberString = `0${'b' | 'B'}${number}`;
7
7
  export type ExponentiationString = `${number}${'e' | 'E'}${number}`;
8
8
  export type Int32 = Branded<number, 'Int32'>;
9
+ export type UInt32 = Branded<number, 'UInt32'>;
9
10
  export type NumberRange = [min: number] | [min: number, max: undefined] | [min: number, max: number] | [min: undefined, max: number];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@webdeveric/utils",
3
3
  "description": "Just another collection of utilities.",
4
- "version": "0.76.0",
4
+ "version": "0.77.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -66,7 +66,7 @@
66
66
  "url": "https://github.com/webdeveric/utils/issues"
67
67
  },
68
68
  "homepage": "https://github.com/webdeveric/utils/#readme",
69
- "packageManager": "pnpm@10.32.1+sha512.a706938f0e89ac1456b6563eab4edf1d1faf3368d1191fc5c59790e96dc918e4456ab2e67d613de1043d2e8c81f87303e6b40d4ffeca9df15ef1ad567348f2be",
69
+ "packageManager": "pnpm@10.33.0+sha512.10568bb4a6afb58c9eb3630da90cc9516417abebd3fabbe6739f0ae795728da1491e9db5a544c76ad8eb7570f5c4bb3d6c637b2cb41bfdcdb47fa823c8649319",
70
70
  "scripts": {
71
71
  "clean": "rimraf ./dist/",
72
72
  "prebuild": "pnpm clean",
@@ -87,26 +87,26 @@
87
87
  "devDependencies": {
88
88
  "@commitlint/config-conventional": "^20.5.0",
89
89
  "@commitlint/types": "^20.5.0",
90
- "@types/node": "^24.12.0",
91
- "@vitest/coverage-v8": "^4.1.0",
90
+ "@types/node": "^24.12.2",
91
+ "@vitest/coverage-v8": "^4.1.4",
92
92
  "@webdeveric/eslint-config-ts": "^0.12.0",
93
- "@webdeveric/prettier-config": "^0.3.0",
93
+ "@webdeveric/prettier-config": "^0.4.0",
94
94
  "commitlint": "^20.5.0",
95
- "commitlint-plugin-cspell": "^0.6.0",
96
- "conventional-changelog-conventionalcommits": "^9.3.0",
97
- "cspell": "^9.7.0",
95
+ "commitlint-plugin-cspell": "^0.8.0",
96
+ "conventional-changelog-conventionalcommits": "^9.3.1",
97
+ "cspell": "^10.0.0",
98
98
  "eslint": "^8.57.1",
99
99
  "eslint-config-prettier": "^10.1.8",
100
100
  "eslint-import-resolver-typescript": "^4.4.4",
101
101
  "eslint-plugin-import": "^2.32.0",
102
102
  "husky": "^9.1.7",
103
- "jsdom": "^29.0.0",
103
+ "jsdom": "^29.0.2",
104
104
  "lint-staged": "^16.4.0",
105
- "prettier": "^3.8.1",
105
+ "prettier": "^3.8.3",
106
106
  "rimraf": "^6.1.3",
107
107
  "semantic-release": "^25.0.3",
108
- "typescript": "6.0.0-dev.20260304",
109
- "validate-package-exports": "^0.21.0",
110
- "vitest": "^4.1.0"
108
+ "typescript": "^6.0.3",
109
+ "validate-package-exports": "^0.23.0",
110
+ "vitest": "^4.1.4"
111
111
  }
112
112
  }