lib0 1.0.0-rc.35 → 1.0.0-rc.36
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.
- package/dist/number.d.ts +1 -0
- package/dist/schema.d.ts +12 -3
- package/package.json +1 -1
- package/src/number.js +1 -0
- package/src/schema.js +22 -8
package/dist/number.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export const LOWEST_INT32: number;
|
|
|
4
4
|
export const HIGHEST_INT32: 2147483647;
|
|
5
5
|
export const HIGHEST_UINT32: 4294967295;
|
|
6
6
|
export const isInteger: (number: unknown) => boolean;
|
|
7
|
+
export const isSafeInteger: (number: unknown) => boolean;
|
|
7
8
|
export const isNaN: (number: unknown) => boolean;
|
|
8
9
|
export const parseInt: (string: string, radix?: number) => number;
|
|
9
10
|
export function countBits(n: number): number;
|
package/dist/schema.d.ts
CHANGED
|
@@ -480,17 +480,26 @@ export const $$symbol: Schema<Schema<Symbol>>;
|
|
|
480
480
|
export const $number: Schema<number>;
|
|
481
481
|
export const $$number: Schema<Schema<number>>;
|
|
482
482
|
/**
|
|
483
|
-
* A number without fractional component
|
|
484
|
-
* `number` - JS has no separate integer type.
|
|
483
|
+
* A number without fractional component in the safe integer range (`Number.isSafeInteger`,
|
|
484
|
+
* i.e. `|n| <= 2^53 - 1`). The unwrapped type is still `number` - JS has no separate integer type.
|
|
485
|
+
*
|
|
486
|
+
* The range restriction guarantees that every `$int` is exactly representable as a 64-bit
|
|
487
|
+
* signed integer (`BigInt(n)` is lossless, databases can store it in an `int64` / `BIGINT` column).
|
|
488
|
+
* Values like `1e20` are integers to `Number.isInteger`, but they are neither exact (every float
|
|
489
|
+
* beyond 2^53 rounds to a multiple of a power of two) nor storable in 64 bits, so they are
|
|
490
|
+
* rejected. Use `$number` for unbounded floats and `$bigint` for arbitrary-precision integers.
|
|
485
491
|
*
|
|
486
492
|
* @type {Schema<number>}
|
|
487
493
|
*/
|
|
488
494
|
export const $int: Schema<number>;
|
|
489
495
|
export const $$int: Schema<Schema<number>>;
|
|
490
496
|
/**
|
|
491
|
-
* An unsigned integer: a
|
|
497
|
+
* An unsigned integer: a `$int` that is `>= 0` (so `0 <= n <= 2^53 - 1`). The unwrapped type is
|
|
492
498
|
* still `number` - JS has no separate integer type.
|
|
493
499
|
*
|
|
500
|
+
* Like `$int`, every `$uint` is exactly representable as a 64-bit integer (signed or unsigned).
|
|
501
|
+
* See `$int` for why the range is restricted.
|
|
502
|
+
*
|
|
494
503
|
* @type {Schema<number>}
|
|
495
504
|
*/
|
|
496
505
|
export const $uint: Schema<number>;
|
package/package.json
CHANGED
package/src/number.js
CHANGED
|
@@ -14,6 +14,7 @@ export const HIGHEST_INT32 = binary.BITS31
|
|
|
14
14
|
export const HIGHEST_UINT32 = binary.BITS32
|
|
15
15
|
|
|
16
16
|
export const isInteger = Number.isInteger
|
|
17
|
+
export const isSafeInteger = Number.isSafeInteger
|
|
17
18
|
export const isNaN = Number.isNaN
|
|
18
19
|
export const parseInt = Number.parseInt
|
|
19
20
|
|
package/src/schema.js
CHANGED
|
@@ -670,7 +670,12 @@ export class $Record extends Schema {
|
|
|
670
670
|
* @return {o is { [key in Unwrap<Keys>]: Unwrap<Values> }}
|
|
671
671
|
*/
|
|
672
672
|
check (o, err) {
|
|
673
|
-
|
|
673
|
+
if (o === null || !object.isObject(o)) {
|
|
674
|
+
/* c8 ignore next */
|
|
675
|
+
err?.extend(null, 'Record', o === null ? 'null' : typeof o)
|
|
676
|
+
return false
|
|
677
|
+
}
|
|
678
|
+
return object.every(o, (vv, vk) => {
|
|
674
679
|
const ck = this.shape.keys.check(vk, err)
|
|
675
680
|
/* c8 ignore next */
|
|
676
681
|
!ck && err?.extend(vk + '', 'Record', typeof o, ck ? 'Key doesn\'t match schema' : 'Value doesn\'t match value')
|
|
@@ -1028,21 +1033,30 @@ export const $number = /* @__PURE__ */$custom(o => typeof o === 'number')
|
|
|
1028
1033
|
export const $$number = /** @type {Schema<Schema<number>>} */ (/* @__PURE__ */$type('s:$number', $number))
|
|
1029
1034
|
|
|
1030
1035
|
/**
|
|
1031
|
-
* A number without fractional component
|
|
1032
|
-
* `number` - JS has no separate integer type.
|
|
1036
|
+
* A number without fractional component in the safe integer range (`Number.isSafeInteger`,
|
|
1037
|
+
* i.e. `|n| <= 2^53 - 1`). The unwrapped type is still `number` - JS has no separate integer type.
|
|
1038
|
+
*
|
|
1039
|
+
* The range restriction guarantees that every `$int` is exactly representable as a 64-bit
|
|
1040
|
+
* signed integer (`BigInt(n)` is lossless, databases can store it in an `int64` / `BIGINT` column).
|
|
1041
|
+
* Values like `1e20` are integers to `Number.isInteger`, but they are neither exact (every float
|
|
1042
|
+
* beyond 2^53 rounds to a multiple of a power of two) nor storable in 64 bits, so they are
|
|
1043
|
+
* rejected. Use `$number` for unbounded floats and `$bigint` for arbitrary-precision integers.
|
|
1033
1044
|
*
|
|
1034
1045
|
* @type {Schema<number>}
|
|
1035
1046
|
*/
|
|
1036
|
-
export const $int = /* @__PURE__ */$custom(o => number.
|
|
1047
|
+
export const $int = /* @__PURE__ */$custom(o => number.isSafeInteger(o))
|
|
1037
1048
|
export const $$int = /** @type {Schema<Schema<number>>} */ (/* @__PURE__ */$type('s:$int', $int))
|
|
1038
1049
|
|
|
1039
1050
|
/**
|
|
1040
|
-
* An unsigned integer: a
|
|
1051
|
+
* An unsigned integer: a `$int` that is `>= 0` (so `0 <= n <= 2^53 - 1`). The unwrapped type is
|
|
1041
1052
|
* still `number` - JS has no separate integer type.
|
|
1042
1053
|
*
|
|
1054
|
+
* Like `$int`, every `$uint` is exactly representable as a 64-bit integer (signed or unsigned).
|
|
1055
|
+
* See `$int` for why the range is restricted.
|
|
1056
|
+
*
|
|
1043
1057
|
* @type {Schema<number>}
|
|
1044
1058
|
*/
|
|
1045
|
-
export const $uint = /* @__PURE__ */$custom(o => number.
|
|
1059
|
+
export const $uint = /* @__PURE__ */$custom(o => number.isSafeInteger(o) && o >= 0)
|
|
1046
1060
|
export const $$uint = /** @type {Schema<Schema<number>>} */ (/* @__PURE__ */$type('s:$uint', $uint))
|
|
1047
1061
|
|
|
1048
1062
|
/**
|
|
@@ -1425,11 +1439,11 @@ const _createCoercer = ($s, cache) => {
|
|
|
1425
1439
|
if (_isMeta($$int, $s) || _isMeta($$uint, $s)) {
|
|
1426
1440
|
const min = _isMeta($$uint, $s) ? 0 : -Infinity
|
|
1427
1441
|
return (o, path, ctx) => {
|
|
1428
|
-
if (number.
|
|
1442
|
+
if (number.isSafeInteger(o) && o >= min) return o
|
|
1429
1443
|
const t = typeof o
|
|
1430
1444
|
if (t === 'boolean' || t === 'bigint' || (t === 'string' && o.trim() !== '')) {
|
|
1431
1445
|
const n = Number(o)
|
|
1432
|
-
if (number.
|
|
1446
|
+
if (number.isSafeInteger(n) && n >= min) return n
|
|
1433
1447
|
}
|
|
1434
1448
|
return _fail(ctx, path, o, expected)
|
|
1435
1449
|
}
|