@metamask/utils 8.3.0 → 8.5.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.
- package/CHANGELOG.md +26 -1
- package/dist/assert.cjs +127 -0
- package/dist/assert.cjs.map +1 -0
- package/dist/{types/assert.d.ts → assert.d.cts} +3 -3
- package/dist/assert.d.cts.map +1 -0
- package/dist/assert.d.mts +61 -0
- package/dist/assert.d.mts.map +1 -0
- package/dist/assert.mjs +119 -15
- package/dist/assert.mjs.map +1 -1
- package/dist/base64.cjs +34 -0
- package/dist/base64.cjs.map +1 -0
- package/dist/{types/base64.d.ts → base64.d.cts} +3 -3
- package/dist/base64.d.cts.map +1 -0
- package/dist/base64.d.mts +25 -0
- package/dist/base64.d.mts.map +1 -0
- package/dist/base64.mjs +28 -9
- package/dist/base64.mjs.map +1 -1
- package/dist/bytes.cjs +400 -0
- package/dist/{chunk-QEPVHEP7.js.map → bytes.cjs.map} +1 -1
- package/dist/{types/bytes.d.ts → bytes.d.cts} +3 -3
- package/dist/bytes.d.cts.map +1 -0
- package/dist/bytes.d.mts +183 -0
- package/dist/bytes.d.mts.map +1 -0
- package/dist/bytes.mjs +379 -42
- package/dist/bytes.mjs.map +1 -1
- package/dist/caip-types.cjs +151 -0
- package/dist/caip-types.cjs.map +1 -0
- package/dist/{types/caip-types.d.ts → caip-types.d.cts} +33 -12
- package/dist/caip-types.d.cts.map +1 -0
- package/dist/caip-types.d.mts +114 -0
- package/dist/caip-types.d.mts.map +1 -0
- package/dist/caip-types.mjs +139 -39
- package/dist/caip-types.mjs.map +1 -1
- package/dist/checksum.cjs +7 -0
- package/dist/checksum.cjs.map +1 -0
- package/dist/checksum.d.cts +2 -0
- package/dist/checksum.d.cts.map +1 -0
- package/dist/checksum.d.mts +2 -0
- package/dist/checksum.d.mts.map +1 -0
- package/dist/checksum.mjs +3 -11
- package/dist/checksum.mjs.map +1 -1
- package/dist/coercers.cjs +162 -0
- package/dist/coercers.cjs.map +1 -0
- package/dist/{types/coercers.d.ts → coercers.d.cts} +7 -7
- package/dist/coercers.d.cts.map +1 -0
- package/dist/coercers.d.mts +97 -0
- package/dist/coercers.d.mts.map +1 -0
- package/dist/coercers.mjs +154 -17
- package/dist/coercers.mjs.map +1 -1
- package/dist/collections.cjs +109 -0
- package/dist/collections.cjs.map +1 -0
- package/dist/{types/collections.d.ts → collections.d.cts} +1 -1
- package/dist/collections.d.cts.map +1 -0
- package/dist/collections.d.mts +39 -0
- package/dist/collections.d.mts.map +1 -0
- package/dist/collections.mjs +103 -8
- package/dist/collections.mjs.map +1 -1
- package/dist/encryption-types.cjs +3 -0
- package/dist/encryption-types.cjs.map +1 -0
- package/dist/{types/encryption-types.d.ts → encryption-types.d.cts} +2 -2
- package/dist/encryption-types.d.cts.map +1 -0
- package/dist/encryption-types.d.mts +7 -0
- package/dist/encryption-types.d.mts.map +1 -0
- package/dist/encryption-types.mjs +1 -1
- package/dist/encryption-types.mjs.map +1 -1
- package/dist/errors.cjs +111 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/{types/errors.d.ts → errors.d.cts} +1 -1
- package/dist/errors.d.cts.map +1 -0
- package/dist/errors.d.mts +58 -0
- package/dist/errors.d.mts.map +1 -0
- package/dist/errors.mjs +102 -16
- package/dist/errors.mjs.map +1 -1
- package/dist/fs.cjs +248 -0
- package/dist/fs.cjs.map +1 -0
- package/dist/{types/fs.d.ts → fs.d.cts} +3 -3
- package/dist/fs.d.cts.map +1 -0
- package/dist/fs.d.mts +133 -0
- package/dist/fs.d.mts.map +1 -0
- package/dist/fs.mjs +209 -25
- package/dist/fs.mjs.map +1 -1
- package/dist/hex.cjs +134 -0
- package/dist/hex.cjs.map +1 -0
- package/dist/{types/hex.d.ts → hex.d.cts} +3 -3
- package/dist/hex.d.cts.map +1 -0
- package/dist/hex.d.mts +77 -0
- package/dist/hex.d.mts.map +1 -0
- package/dist/hex.mjs +121 -34
- package/dist/hex.mjs.map +1 -1
- package/dist/index.cjs +37 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +21 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +20 -292
- package/dist/index.mjs.map +1 -1
- package/dist/json.cjs +413 -0
- package/dist/json.cjs.map +1 -0
- package/dist/{types/json.d.ts → json.d.cts} +21 -22
- package/dist/json.d.cts.map +1 -0
- package/dist/json.d.mts +383 -0
- package/dist/json.d.mts.map +1 -0
- package/dist/json.mjs +389 -74
- package/dist/json.mjs.map +1 -1
- package/dist/keyring.cjs +3 -0
- package/dist/keyring.cjs.map +1 -0
- package/dist/{types/keyring.d.ts → keyring.d.cts} +7 -7
- package/dist/keyring.d.cts.map +1 -0
- package/dist/keyring.d.mts +224 -0
- package/dist/keyring.d.mts.map +1 -0
- package/dist/keyring.mjs +1 -1
- package/dist/keyring.mjs.map +1 -1
- package/dist/logging.cjs +43 -0
- package/dist/logging.cjs.map +1 -0
- package/dist/{types/logging.d.ts → logging.d.cts} +2 -2
- package/dist/logging.d.cts.map +1 -0
- package/dist/logging.d.mts +30 -0
- package/dist/logging.d.mts.map +1 -0
- package/dist/logging.mjs +34 -9
- package/dist/logging.mjs.map +1 -1
- package/dist/misc.cjs +149 -0
- package/dist/misc.cjs.map +1 -0
- package/dist/{types/misc.d.ts → misc.d.cts} +6 -6
- package/dist/misc.d.cts.map +1 -0
- package/dist/misc.d.mts +119 -0
- package/dist/misc.d.mts.map +1 -0
- package/dist/misc.mjs +136 -27
- package/dist/misc.mjs.map +1 -1
- package/dist/node.cjs +19 -0
- package/dist/node.cjs.map +1 -0
- package/dist/node.d.cts +3 -0
- package/dist/node.d.cts.map +1 -0
- package/dist/node.d.mts +3 -0
- package/dist/node.d.mts.map +1 -0
- package/dist/node.mjs +2 -312
- package/dist/node.mjs.map +1 -1
- package/dist/number.cjs +102 -0
- package/dist/number.cjs.map +1 -0
- package/dist/{types/number.d.ts → number.d.cts} +1 -1
- package/dist/number.d.cts.map +1 -0
- package/dist/number.d.mts +73 -0
- package/dist/number.d.mts.map +1 -0
- package/dist/number.mjs +93 -16
- package/dist/number.mjs.map +1 -1
- package/dist/opaque.cjs +3 -0
- package/dist/opaque.cjs.map +1 -0
- package/dist/opaque.d.cts +6 -0
- package/dist/opaque.d.cts.map +1 -0
- package/dist/opaque.d.mts +6 -0
- package/dist/opaque.d.mts.map +1 -0
- package/dist/opaque.mjs +1 -1
- package/dist/opaque.mjs.map +1 -1
- package/dist/promise.cjs +40 -0
- package/dist/promise.cjs.map +1 -0
- package/dist/{types/promise.d.ts → promise.d.cts} +12 -2
- package/dist/promise.d.cts.map +1 -0
- package/dist/promise.d.mts +45 -0
- package/dist/promise.d.mts.map +1 -0
- package/dist/promise.mjs +35 -7
- package/dist/promise.mjs.map +1 -1
- package/dist/time.cjs +67 -0
- package/dist/time.cjs.map +1 -0
- package/dist/{types/time.d.ts → time.d.cts} +1 -1
- package/dist/time.d.cts.map +1 -0
- package/dist/time.d.mts +49 -0
- package/dist/time.d.mts.map +1 -0
- package/dist/time.mjs +60 -10
- package/dist/time.mjs.map +1 -1
- package/dist/transaction-types.cjs +3 -0
- package/dist/transaction-types.cjs.map +1 -0
- package/dist/{types/transaction-types.d.ts → transaction-types.d.cts} +11 -11
- package/dist/transaction-types.d.cts.map +1 -0
- package/dist/transaction-types.d.mts +117 -0
- package/dist/transaction-types.d.mts.map +1 -0
- package/dist/transaction-types.mjs +1 -1
- package/dist/transaction-types.mjs.map +1 -1
- package/dist/versions.cjs +95 -0
- package/dist/versions.cjs.map +1 -0
- package/dist/{types/versions.d.ts → versions.d.cts} +5 -5
- package/dist/versions.d.cts.map +1 -0
- package/dist/versions.d.mts +101 -0
- package/dist/versions.d.mts.map +1 -0
- package/dist/versions.mjs +85 -26
- package/dist/versions.mjs.map +1 -1
- package/package.json +28 -17
- package/dist/assert.js +0 -16
- package/dist/assert.js.map +0 -1
- package/dist/base64.js +0 -11
- package/dist/base64.js.map +0 -1
- package/dist/bytes.js +0 -43
- package/dist/bytes.js.map +0 -1
- package/dist/caip-types.js +0 -40
- package/dist/caip-types.js.map +0 -1
- package/dist/checksum.js +0 -12
- package/dist/checksum.js.map +0 -1
- package/dist/chunk-2LBGT4GH.js +0 -15
- package/dist/chunk-2LBGT4GH.js.map +0 -1
- package/dist/chunk-3W5G4CYI.js +0 -25
- package/dist/chunk-3W5G4CYI.js.map +0 -1
- package/dist/chunk-4D6XQBHA.js +0 -69
- package/dist/chunk-4D6XQBHA.js.map +0 -1
- package/dist/chunk-4NIRTM4M.js +0 -23
- package/dist/chunk-4NIRTM4M.js.map +0 -1
- package/dist/chunk-4RMX5YWE.js +0 -34
- package/dist/chunk-4RMX5YWE.js.map +0 -1
- package/dist/chunk-52OU772R.mjs +0 -122
- package/dist/chunk-52OU772R.mjs.map +0 -1
- package/dist/chunk-5AVWINSB.js +0 -1
- package/dist/chunk-5AVWINSB.js.map +0 -1
- package/dist/chunk-622IOGVI.mjs +0 -1
- package/dist/chunk-622IOGVI.mjs.map +0 -1
- package/dist/chunk-6C35XQOF.mjs +0 -257
- package/dist/chunk-6C35XQOF.mjs.map +0 -1
- package/dist/chunk-6NZW4WK4.js +0 -35
- package/dist/chunk-6NZW4WK4.js.map +0 -1
- package/dist/chunk-6ZDHSOUV.js +0 -59
- package/dist/chunk-6ZDHSOUV.js.map +0 -1
- package/dist/chunk-74DGVJVE.mjs +0 -59
- package/dist/chunk-74DGVJVE.mjs.map +0 -1
- package/dist/chunk-AY6FDCBT.mjs +0 -1
- package/dist/chunk-AY6FDCBT.mjs.map +0 -1
- package/dist/chunk-B7LIM2PK.mjs +0 -23
- package/dist/chunk-B7LIM2PK.mjs.map +0 -1
- package/dist/chunk-BFQDMI3M.js +0 -122
- package/dist/chunk-BFQDMI3M.js.map +0 -1
- package/dist/chunk-DHVKFDHQ.js +0 -95
- package/dist/chunk-DHVKFDHQ.js.map +0 -1
- package/dist/chunk-E4C7EW4R.js +0 -16
- package/dist/chunk-E4C7EW4R.js.map +0 -1
- package/dist/chunk-EQMZL4XU.js +0 -1
- package/dist/chunk-EQMZL4XU.js.map +0 -1
- package/dist/chunk-GZS3IQBZ.mjs +0 -16
- package/dist/chunk-GZS3IQBZ.mjs.map +0 -1
- package/dist/chunk-H4YFDLB7.mjs +0 -70
- package/dist/chunk-H4YFDLB7.mjs.map +0 -1
- package/dist/chunk-I575FZFH.mjs +0 -1
- package/dist/chunk-I575FZFH.mjs.map +0 -1
- package/dist/chunk-IZC266HS.js +0 -55
- package/dist/chunk-IZC266HS.js.map +0 -1
- package/dist/chunk-JPAL7Q5S.mjs +0 -90
- package/dist/chunk-JPAL7Q5S.mjs.map +0 -1
- package/dist/chunk-LC2CRSWD.js +0 -1
- package/dist/chunk-LC2CRSWD.js.map +0 -1
- package/dist/chunk-NQMRFZHB.mjs +0 -35
- package/dist/chunk-NQMRFZHB.mjs.map +0 -1
- package/dist/chunk-O3EIM33O.mjs +0 -95
- package/dist/chunk-O3EIM33O.mjs.map +0 -1
- package/dist/chunk-OLLG4H35.js +0 -257
- package/dist/chunk-OLLG4H35.js.map +0 -1
- package/dist/chunk-QEPVHEP7.js +0 -273
- package/dist/chunk-QVEKZRZ2.js +0 -70
- package/dist/chunk-QVEKZRZ2.js.map +0 -1
- package/dist/chunk-RIRDOQPX.mjs +0 -15
- package/dist/chunk-RIRDOQPX.mjs.map +0 -1
- package/dist/chunk-RKRGAFXY.js +0 -1
- package/dist/chunk-RKRGAFXY.js.map +0 -1
- package/dist/chunk-ROQSKDP5.mjs +0 -69
- package/dist/chunk-ROQSKDP5.mjs.map +0 -1
- package/dist/chunk-RRYOWRCV.mjs +0 -1
- package/dist/chunk-RRYOWRCV.mjs.map +0 -1
- package/dist/chunk-S3UHBN2Z.mjs +0 -44
- package/dist/chunk-S3UHBN2Z.mjs.map +0 -1
- package/dist/chunk-TGOMTREC.mjs +0 -75
- package/dist/chunk-TGOMTREC.mjs.map +0 -1
- package/dist/chunk-THNLGEXV.mjs +0 -34
- package/dist/chunk-THNLGEXV.mjs.map +0 -1
- package/dist/chunk-U7ZUGCE7.js +0 -75
- package/dist/chunk-U7ZUGCE7.js.map +0 -1
- package/dist/chunk-UOTVU7OQ.js +0 -1
- package/dist/chunk-UOTVU7OQ.js.map +0 -1
- package/dist/chunk-VA2DRBDE.mjs +0 -273
- package/dist/chunk-VA2DRBDE.mjs.map +0 -1
- package/dist/chunk-VFXTVNXN.js +0 -44
- package/dist/chunk-VFXTVNXN.js.map +0 -1
- package/dist/chunk-WA4DHIND.mjs +0 -1
- package/dist/chunk-WA4DHIND.mjs.map +0 -1
- package/dist/chunk-X66SUIEF.mjs +0 -25
- package/dist/chunk-X66SUIEF.mjs.map +0 -1
- package/dist/chunk-XYGUOY6N.mjs +0 -55
- package/dist/chunk-XYGUOY6N.mjs.map +0 -1
- package/dist/chunk-Z2RGWDD7.js +0 -90
- package/dist/chunk-Z2RGWDD7.js.map +0 -1
- package/dist/coercers.js +0 -18
- package/dist/coercers.js.map +0 -1
- package/dist/collections.js +0 -10
- package/dist/collections.js.map +0 -1
- package/dist/encryption-types.js +0 -2
- package/dist/encryption-types.js.map +0 -1
- package/dist/errors.js +0 -17
- package/dist/errors.js.map +0 -1
- package/dist/fs.js +0 -26
- package/dist/fs.js.map +0 -1
- package/dist/hex.js +0 -35
- package/dist/hex.js.map +0 -1
- package/dist/index.js +0 -293
- package/dist/index.js.map +0 -1
- package/dist/json.js +0 -75
- package/dist/json.js.map +0 -1
- package/dist/keyring.js +0 -2
- package/dist/keyring.js.map +0 -1
- package/dist/logging.js +0 -10
- package/dist/logging.js.map +0 -1
- package/dist/misc.js +0 -28
- package/dist/misc.js.map +0 -1
- package/dist/node.js +0 -313
- package/dist/node.js.map +0 -1
- package/dist/number.js +0 -18
- package/dist/number.js.map +0 -1
- package/dist/opaque.js +0 -2
- package/dist/opaque.js.map +0 -1
- package/dist/promise.js +0 -8
- package/dist/promise.js.map +0 -1
- package/dist/time.js +0 -12
- package/dist/time.js.map +0 -1
- package/dist/transaction-types.js +0 -2
- package/dist/transaction-types.js.map +0 -1
- package/dist/types/assert.d.ts.map +0 -1
- package/dist/types/base64.d.ts.map +0 -1
- package/dist/types/bytes.d.ts.map +0 -1
- package/dist/types/caip-types.d.ts.map +0 -1
- package/dist/types/checksum.d.ts +0 -2
- package/dist/types/checksum.d.ts.map +0 -1
- package/dist/types/coercers.d.ts.map +0 -1
- package/dist/types/collections.d.ts.map +0 -1
- package/dist/types/encryption-types.d.ts.map +0 -1
- package/dist/types/errors.d.ts.map +0 -1
- package/dist/types/fs.d.ts.map +0 -1
- package/dist/types/hex.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -21
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/json.d.ts.map +0 -1
- package/dist/types/keyring.d.ts.map +0 -1
- package/dist/types/logging.d.ts.map +0 -1
- package/dist/types/misc.d.ts.map +0 -1
- package/dist/types/node.d.ts +0 -3
- package/dist/types/node.d.ts.map +0 -1
- package/dist/types/number.d.ts.map +0 -1
- package/dist/types/opaque.d.ts +0 -6
- package/dist/types/opaque.d.ts.map +0 -1
- package/dist/types/promise.d.ts.map +0 -1
- package/dist/types/time.d.ts.map +0 -1
- package/dist/types/transaction-types.d.ts.map +0 -1
- package/dist/types/versions.d.ts.map +0 -1
- package/dist/versions.js +0 -27
- package/dist/versions.js.map +0 -1
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createHex = exports.createBytes = exports.createBigInt = exports.createNumber = void 0;
|
|
4
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
5
|
+
const assert_1 = require("./assert.cjs");
|
|
6
|
+
const bytes_1 = require("./bytes.cjs");
|
|
7
|
+
const hex_1 = require("./hex.cjs");
|
|
8
|
+
const NumberLikeStruct = (0, superstruct_1.union)([(0, superstruct_1.number)(), (0, superstruct_1.bigint)(), (0, superstruct_1.string)(), hex_1.StrictHexStruct]);
|
|
9
|
+
const NumberCoercer = (0, superstruct_1.coerce)((0, superstruct_1.number)(), NumberLikeStruct, Number);
|
|
10
|
+
const BigIntCoercer = (0, superstruct_1.coerce)((0, superstruct_1.bigint)(), NumberLikeStruct, BigInt);
|
|
11
|
+
const BytesLikeStruct = (0, superstruct_1.union)([hex_1.StrictHexStruct, (0, superstruct_1.instance)(Uint8Array)]);
|
|
12
|
+
const BytesCoercer = (0, superstruct_1.coerce)((0, superstruct_1.instance)(Uint8Array), (0, superstruct_1.union)([hex_1.StrictHexStruct]), bytes_1.hexToBytes);
|
|
13
|
+
const HexCoercer = (0, superstruct_1.coerce)(hex_1.StrictHexStruct, (0, superstruct_1.instance)(Uint8Array), bytes_1.bytesToHex);
|
|
14
|
+
/**
|
|
15
|
+
* Create a number from a number-like value.
|
|
16
|
+
*
|
|
17
|
+
* - If the value is a number, it is returned as-is.
|
|
18
|
+
* - If the value is a `bigint`, it is converted to a number.
|
|
19
|
+
* - If the value is a string, it is interpreted as a decimal number.
|
|
20
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is
|
|
21
|
+
* interpreted as a hexadecimal number.
|
|
22
|
+
*
|
|
23
|
+
* This validates that the value is a number-like value, and that the resulting
|
|
24
|
+
* number is not `NaN` or `Infinity`.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const value = createNumber('0x010203');
|
|
29
|
+
* console.log(value); // 66051
|
|
30
|
+
*
|
|
31
|
+
* const otherValue = createNumber(123n);
|
|
32
|
+
* console.log(otherValue); // 123
|
|
33
|
+
* ```
|
|
34
|
+
* @param value - The value to create the number from.
|
|
35
|
+
* @returns The created number.
|
|
36
|
+
* @throws If the value is not a number-like value, or if the resulting number
|
|
37
|
+
* is `NaN` or `Infinity`.
|
|
38
|
+
*/
|
|
39
|
+
function createNumber(value) {
|
|
40
|
+
try {
|
|
41
|
+
const result = (0, superstruct_1.create)(value, NumberCoercer);
|
|
42
|
+
(0, assert_1.assert)(Number.isFinite(result), `Expected a number-like value, got "${value}".`);
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (error instanceof superstruct_1.StructError) {
|
|
47
|
+
throw new Error(`Expected a number-like value, got "${value}".`);
|
|
48
|
+
}
|
|
49
|
+
/* istanbul ignore next */
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.createNumber = createNumber;
|
|
54
|
+
/**
|
|
55
|
+
* Create a `bigint` from a number-like value.
|
|
56
|
+
*
|
|
57
|
+
* - If the value is a number, it is converted to a `bigint`.
|
|
58
|
+
* - If the value is a `bigint`, it is returned as-is.
|
|
59
|
+
* - If the value is a string, it is interpreted as a decimal number and
|
|
60
|
+
* converted to a `bigint`.
|
|
61
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is
|
|
62
|
+
* interpreted as a hexadecimal number and converted to a `bigint`.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const value = createBigInt('0x010203');
|
|
67
|
+
* console.log(value); // 16909060n
|
|
68
|
+
*
|
|
69
|
+
* const otherValue = createBigInt(123);
|
|
70
|
+
* console.log(otherValue); // 123n
|
|
71
|
+
* ```
|
|
72
|
+
* @param value - The value to create the bigint from.
|
|
73
|
+
* @returns The created bigint.
|
|
74
|
+
* @throws If the value is not a number-like value.
|
|
75
|
+
*/
|
|
76
|
+
function createBigInt(value) {
|
|
77
|
+
try {
|
|
78
|
+
// The `BigInt` constructor throws if the value is not a number-like value.
|
|
79
|
+
// There is no need to validate the value manually.
|
|
80
|
+
return (0, superstruct_1.create)(value, BigIntCoercer);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
if (error instanceof superstruct_1.StructError) {
|
|
84
|
+
throw new Error(`Expected a number-like value, got "${String(error.value)}".`);
|
|
85
|
+
}
|
|
86
|
+
/* istanbul ignore next */
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.createBigInt = createBigInt;
|
|
91
|
+
/**
|
|
92
|
+
* Create a byte array from a bytes-like value.
|
|
93
|
+
*
|
|
94
|
+
* - If the value is a byte array, it is returned as-is.
|
|
95
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is interpreted
|
|
96
|
+
* as a hexadecimal number and converted to a byte array.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const value = createBytes('0x010203');
|
|
101
|
+
* console.log(value); // Uint8Array [ 1, 2, 3 ]
|
|
102
|
+
*
|
|
103
|
+
* const otherValue = createBytes('0x010203');
|
|
104
|
+
* console.log(otherValue); // Uint8Array [ 1, 2, 3 ]
|
|
105
|
+
* ```
|
|
106
|
+
* @param value - The value to create the byte array from.
|
|
107
|
+
* @returns The created byte array.
|
|
108
|
+
* @throws If the value is not a bytes-like value.
|
|
109
|
+
*/
|
|
110
|
+
function createBytes(value) {
|
|
111
|
+
if (typeof value === 'string' && value.toLowerCase() === '0x') {
|
|
112
|
+
return new Uint8Array();
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
return (0, superstruct_1.create)(value, BytesCoercer);
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
if (error instanceof superstruct_1.StructError) {
|
|
119
|
+
throw new Error(`Expected a bytes-like value, got "${String(error.value)}".`);
|
|
120
|
+
}
|
|
121
|
+
/* istanbul ignore next */
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
exports.createBytes = createBytes;
|
|
126
|
+
/**
|
|
127
|
+
* Create a hexadecimal string from a bytes-like value.
|
|
128
|
+
*
|
|
129
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is returned
|
|
130
|
+
* as-is.
|
|
131
|
+
* - If the value is a `Uint8Array`, it is converted to a hex string.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const value = createHex(new Uint8Array([1, 2, 3]));
|
|
136
|
+
* console.log(value); // '0x010203'
|
|
137
|
+
*
|
|
138
|
+
* const otherValue = createHex('0x010203');
|
|
139
|
+
* console.log(otherValue); // '0x010203'
|
|
140
|
+
* ```
|
|
141
|
+
* @param value - The value to create the hex string from.
|
|
142
|
+
* @returns The created hex string.
|
|
143
|
+
* @throws If the value is not a bytes-like value.
|
|
144
|
+
*/
|
|
145
|
+
function createHex(value) {
|
|
146
|
+
if ((value instanceof Uint8Array && value.length === 0) ||
|
|
147
|
+
(typeof value === 'string' && value.toLowerCase() === '0x')) {
|
|
148
|
+
return '0x';
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
return (0, superstruct_1.create)(value, HexCoercer);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
if (error instanceof superstruct_1.StructError) {
|
|
155
|
+
throw new Error(`Expected a bytes-like value, got "${String(error.value)}".`);
|
|
156
|
+
}
|
|
157
|
+
/* istanbul ignore next */
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.createHex = createHex;
|
|
162
|
+
//# sourceMappingURL=coercers.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coercers.cjs","sourceRoot":"","sources":["../src/coercers.ts"],"names":[],"mappings":";;;AACA,uDAS+B;AAE/B,yCAAkC;AAClC,uCAAiD;AAEjD,mCAAwC;AAExC,MAAM,gBAAgB,GAAG,IAAA,mBAAK,EAAC,CAAC,IAAA,oBAAM,GAAE,EAAE,IAAA,oBAAM,GAAE,EAAE,IAAA,oBAAM,GAAE,EAAE,qBAAe,CAAC,CAAC,CAAC;AAChF,MAAM,aAAa,GAAG,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,gBAAgB,EAAE,MAAM,CAAC,CAAC;AACjE,MAAM,aAAa,GAAG,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,gBAAgB,EAAE,M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AC1B,MAAM,KAAK,CAAC;KACb;AACH,CAAC;AAfD,oCAeC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,WAAW,CAAC,KAAgB;IAC1C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,WAAW,EAAE,KAAK,IAAI,EAAE;QAC7D,OAAO,IAAI,UAAU,EAAE,CAAC;KACzB;IAED,IAAI;QACF,OAAO,IAAA,oBAAM,EAAC,KAAK,EAAE,YAAY,CAAC,CAAC;KACpC;IAAC,OAAO,KAAK,EAAE;QACd,IAAI,KAAK,YAAY,yBAAW,EAAE;YAChC,MAAM,IAAI,KAAK,CACb,qCAAqC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAC7D,CAAC;SACH;QAED,0BAA0B;QAC1B,MAAM,KAAK,CAAC;KACb;AACH,CAAC;AAjBD,kCAiBC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,SAAS,CAAC,KAAgB;IACxC,IACE,CAAC,KAAK,YAAY,UAAU,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC;QACnD,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,WAAW,EAAE,KAAK,IAAI,CAAC,EAC3D;QACA,OAAO,IAAI,CAAC;KACb;IAED,IAAI;QACF,OAAO,IAAA,oBAAM,EAAC,KAAK,EAAE,UAAU,CAAC,CAAC;KAClC;IAAC,OAAO,KAAK,EAAE;QACd,IAAI,KAAK,YAAY,yBAAW,EAAE;YAChC,MAAM,IAAI,KAAK,CACb,qCAAqC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAC7D,CAAC;SACH;QAED,0BAA0B;QAC1B,MAAM,KAAK,CAAC;KACb;AACH,CAAC;AApBD,8BAoBC","sourcesContent":["import type { Infer } from '@metamask/superstruct';\nimport {\n bigint,\n coerce,\n create,\n instance,\n number,\n string,\n StructError,\n union,\n} from '@metamask/superstruct';\n\nimport { assert } from './assert';\nimport { bytesToHex, hexToBytes } from './bytes';\nimport type { Hex } from './hex';\nimport { StrictHexStruct } from './hex';\n\nconst NumberLikeStruct = union([number(), bigint(), string(), StrictHexStruct]);\nconst NumberCoercer = coerce(number(), NumberLikeStruct, Number);\nconst BigIntCoercer = coerce(bigint(), NumberLikeStruct, BigInt);\n\nconst BytesLikeStruct = union([StrictHexStruct, instance(Uint8Array)]);\nconst BytesCoercer = coerce(\n instance(Uint8Array),\n union([StrictHexStruct]),\n hexToBytes,\n);\n\nconst HexCoercer = coerce(StrictHexStruct, instance(Uint8Array), bytesToHex);\n\nexport type NumberLike = Infer<typeof NumberLikeStruct>;\nexport type BytesLike = Infer<typeof BytesLikeStruct>;\n\n/**\n * Create a number from a number-like value.\n *\n * - If the value is a number, it is returned as-is.\n * - If the value is a `bigint`, it is converted to a number.\n * - If the value is a string, it is interpreted as a decimal number.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is\n * interpreted as a hexadecimal number.\n *\n * This validates that the value is a number-like value, and that the resulting\n * number is not `NaN` or `Infinity`.\n *\n * @example\n * ```typescript\n * const value = createNumber('0x010203');\n * console.log(value); // 66051\n *\n * const otherValue = createNumber(123n);\n * console.log(otherValue); // 123\n * ```\n * @param value - The value to create the number from.\n * @returns The created number.\n * @throws If the value is not a number-like value, or if the resulting number\n * is `NaN` or `Infinity`.\n */\nexport function createNumber(value: NumberLike): number {\n try {\n const result = create(value, NumberCoercer);\n\n assert(\n Number.isFinite(result),\n `Expected a number-like value, got \"${value}\".`,\n );\n\n return result;\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(`Expected a number-like value, got \"${value}\".`);\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a `bigint` from a number-like value.\n *\n * - If the value is a number, it is converted to a `bigint`.\n * - If the value is a `bigint`, it is returned as-is.\n * - If the value is a string, it is interpreted as a decimal number and\n * converted to a `bigint`.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is\n * interpreted as a hexadecimal number and converted to a `bigint`.\n *\n * @example\n * ```typescript\n * const value = createBigInt('0x010203');\n * console.log(value); // 16909060n\n *\n * const otherValue = createBigInt(123);\n * console.log(otherValue); // 123n\n * ```\n * @param value - The value to create the bigint from.\n * @returns The created bigint.\n * @throws If the value is not a number-like value.\n */\nexport function createBigInt(value: NumberLike): bigint {\n try {\n // The `BigInt` constructor throws if the value is not a number-like value.\n // There is no need to validate the value manually.\n return create(value, BigIntCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(\n `Expected a number-like value, got \"${String(error.value)}\".`,\n );\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a byte array from a bytes-like value.\n *\n * - If the value is a byte array, it is returned as-is.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is interpreted\n * as a hexadecimal number and converted to a byte array.\n *\n * @example\n * ```typescript\n * const value = createBytes('0x010203');\n * console.log(value); // Uint8Array [ 1, 2, 3 ]\n *\n * const otherValue = createBytes('0x010203');\n * console.log(otherValue); // Uint8Array [ 1, 2, 3 ]\n * ```\n * @param value - The value to create the byte array from.\n * @returns The created byte array.\n * @throws If the value is not a bytes-like value.\n */\nexport function createBytes(value: BytesLike): Uint8Array {\n if (typeof value === 'string' && value.toLowerCase() === '0x') {\n return new Uint8Array();\n }\n\n try {\n return create(value, BytesCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(\n `Expected a bytes-like value, got \"${String(error.value)}\".`,\n );\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a hexadecimal string from a bytes-like value.\n *\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is returned\n * as-is.\n * - If the value is a `Uint8Array`, it is converted to a hex string.\n *\n * @example\n * ```typescript\n * const value = createHex(new Uint8Array([1, 2, 3]));\n * console.log(value); // '0x010203'\n *\n * const otherValue = createHex('0x010203');\n * console.log(otherValue); // '0x010203'\n * ```\n * @param value - The value to create the hex string from.\n * @returns The created hex string.\n * @throws If the value is not a bytes-like value.\n */\nexport function createHex(value: BytesLike): Hex {\n if (\n (value instanceof Uint8Array && value.length === 0) ||\n (typeof value === 'string' && value.toLowerCase() === '0x')\n ) {\n return '0x';\n }\n\n try {\n return create(value, HexCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(\n `Expected a bytes-like value, got \"${String(error.value)}\".`,\n );\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n"]}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { Infer } from
|
|
2
|
-
import type { Hex } from
|
|
3
|
-
declare const NumberLikeStruct: import("superstruct").Struct<string | number | bigint, null>;
|
|
4
|
-
declare const BytesLikeStruct: import("superstruct").Struct<Uint8Array | `0x${string}`, null>;
|
|
5
|
-
export
|
|
6
|
-
export
|
|
1
|
+
import type { Infer } from "@metamask/superstruct";
|
|
2
|
+
import type { Hex } from "./hex.cjs";
|
|
3
|
+
declare const NumberLikeStruct: import("@metamask/superstruct").Struct<string | number | bigint, null>;
|
|
4
|
+
declare const BytesLikeStruct: import("@metamask/superstruct").Struct<Uint8Array | `0x${string}`, null>;
|
|
5
|
+
export type NumberLike = Infer<typeof NumberLikeStruct>;
|
|
6
|
+
export type BytesLike = Infer<typeof BytesLikeStruct>;
|
|
7
7
|
/**
|
|
8
8
|
* Create a number from a number-like value.
|
|
9
9
|
*
|
|
@@ -94,4 +94,4 @@ export declare function createBytes(value: BytesLike): Uint8Array;
|
|
|
94
94
|
*/
|
|
95
95
|
export declare function createHex(value: BytesLike): Hex;
|
|
96
96
|
export {};
|
|
97
|
-
//# sourceMappingURL=coercers.d.
|
|
97
|
+
//# sourceMappingURL=coercers.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coercers.d.cts","sourceRoot":"","sources":["../src/coercers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAcnD,OAAO,KAAK,EAAE,GAAG,EAAE,kBAAc;AAGjC,QAAA,MAAM,gBAAgB,wEAAyD,CAAC;AAIhF,QAAA,MAAM,eAAe,0EAAiD,CAAC;AASvE,MAAM,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC;AACxD,MAAM,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAkBtD;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAetD;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,SAAS,GAAG,UAAU,CAiBxD;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,SAAS,GAAG,GAAG,CAoB/C"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { Infer } from "@metamask/superstruct";
|
|
2
|
+
import type { Hex } from "./hex.mjs";
|
|
3
|
+
declare const NumberLikeStruct: import("@metamask/superstruct").Struct<string | number | bigint, null>;
|
|
4
|
+
declare const BytesLikeStruct: import("@metamask/superstruct").Struct<Uint8Array | `0x${string}`, null>;
|
|
5
|
+
export type NumberLike = Infer<typeof NumberLikeStruct>;
|
|
6
|
+
export type BytesLike = Infer<typeof BytesLikeStruct>;
|
|
7
|
+
/**
|
|
8
|
+
* Create a number from a number-like value.
|
|
9
|
+
*
|
|
10
|
+
* - If the value is a number, it is returned as-is.
|
|
11
|
+
* - If the value is a `bigint`, it is converted to a number.
|
|
12
|
+
* - If the value is a string, it is interpreted as a decimal number.
|
|
13
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is
|
|
14
|
+
* interpreted as a hexadecimal number.
|
|
15
|
+
*
|
|
16
|
+
* This validates that the value is a number-like value, and that the resulting
|
|
17
|
+
* number is not `NaN` or `Infinity`.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const value = createNumber('0x010203');
|
|
22
|
+
* console.log(value); // 66051
|
|
23
|
+
*
|
|
24
|
+
* const otherValue = createNumber(123n);
|
|
25
|
+
* console.log(otherValue); // 123
|
|
26
|
+
* ```
|
|
27
|
+
* @param value - The value to create the number from.
|
|
28
|
+
* @returns The created number.
|
|
29
|
+
* @throws If the value is not a number-like value, or if the resulting number
|
|
30
|
+
* is `NaN` or `Infinity`.
|
|
31
|
+
*/
|
|
32
|
+
export declare function createNumber(value: NumberLike): number;
|
|
33
|
+
/**
|
|
34
|
+
* Create a `bigint` from a number-like value.
|
|
35
|
+
*
|
|
36
|
+
* - If the value is a number, it is converted to a `bigint`.
|
|
37
|
+
* - If the value is a `bigint`, it is returned as-is.
|
|
38
|
+
* - If the value is a string, it is interpreted as a decimal number and
|
|
39
|
+
* converted to a `bigint`.
|
|
40
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is
|
|
41
|
+
* interpreted as a hexadecimal number and converted to a `bigint`.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const value = createBigInt('0x010203');
|
|
46
|
+
* console.log(value); // 16909060n
|
|
47
|
+
*
|
|
48
|
+
* const otherValue = createBigInt(123);
|
|
49
|
+
* console.log(otherValue); // 123n
|
|
50
|
+
* ```
|
|
51
|
+
* @param value - The value to create the bigint from.
|
|
52
|
+
* @returns The created bigint.
|
|
53
|
+
* @throws If the value is not a number-like value.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createBigInt(value: NumberLike): bigint;
|
|
56
|
+
/**
|
|
57
|
+
* Create a byte array from a bytes-like value.
|
|
58
|
+
*
|
|
59
|
+
* - If the value is a byte array, it is returned as-is.
|
|
60
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is interpreted
|
|
61
|
+
* as a hexadecimal number and converted to a byte array.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const value = createBytes('0x010203');
|
|
66
|
+
* console.log(value); // Uint8Array [ 1, 2, 3 ]
|
|
67
|
+
*
|
|
68
|
+
* const otherValue = createBytes('0x010203');
|
|
69
|
+
* console.log(otherValue); // Uint8Array [ 1, 2, 3 ]
|
|
70
|
+
* ```
|
|
71
|
+
* @param value - The value to create the byte array from.
|
|
72
|
+
* @returns The created byte array.
|
|
73
|
+
* @throws If the value is not a bytes-like value.
|
|
74
|
+
*/
|
|
75
|
+
export declare function createBytes(value: BytesLike): Uint8Array;
|
|
76
|
+
/**
|
|
77
|
+
* Create a hexadecimal string from a bytes-like value.
|
|
78
|
+
*
|
|
79
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is returned
|
|
80
|
+
* as-is.
|
|
81
|
+
* - If the value is a `Uint8Array`, it is converted to a hex string.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const value = createHex(new Uint8Array([1, 2, 3]));
|
|
86
|
+
* console.log(value); // '0x010203'
|
|
87
|
+
*
|
|
88
|
+
* const otherValue = createHex('0x010203');
|
|
89
|
+
* console.log(otherValue); // '0x010203'
|
|
90
|
+
* ```
|
|
91
|
+
* @param value - The value to create the hex string from.
|
|
92
|
+
* @returns The created hex string.
|
|
93
|
+
* @throws If the value is not a bytes-like value.
|
|
94
|
+
*/
|
|
95
|
+
export declare function createHex(value: BytesLike): Hex;
|
|
96
|
+
export {};
|
|
97
|
+
//# sourceMappingURL=coercers.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coercers.d.mts","sourceRoot":"","sources":["../src/coercers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAcnD,OAAO,KAAK,EAAE,GAAG,EAAE,kBAAc;AAGjC,QAAA,MAAM,gBAAgB,wEAAyD,CAAC;AAIhF,QAAA,MAAM,eAAe,0EAAiD,CAAC;AASvE,MAAM,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC;AACxD,MAAM,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAkBtD;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAetD;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,SAAS,GAAG,UAAU,CAiBxD;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,SAAS,GAAG,GAAG,CAoB/C"}
|
package/dist/coercers.mjs
CHANGED
|
@@ -1,18 +1,155 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import { bigint, coerce, create, instance, number, string, StructError, union } from "@metamask/superstruct";
|
|
2
|
+
import { assert } from "./assert.mjs";
|
|
3
|
+
import { bytesToHex, hexToBytes } from "./bytes.mjs";
|
|
4
|
+
import { StrictHexStruct } from "./hex.mjs";
|
|
5
|
+
const NumberLikeStruct = union([number(), bigint(), string(), StrictHexStruct]);
|
|
6
|
+
const NumberCoercer = coerce(number(), NumberLikeStruct, Number);
|
|
7
|
+
const BigIntCoercer = coerce(bigint(), NumberLikeStruct, BigInt);
|
|
8
|
+
const BytesLikeStruct = union([StrictHexStruct, instance(Uint8Array)]);
|
|
9
|
+
const BytesCoercer = coerce(instance(Uint8Array), union([StrictHexStruct]), hexToBytes);
|
|
10
|
+
const HexCoercer = coerce(StrictHexStruct, instance(Uint8Array), bytesToHex);
|
|
11
|
+
/**
|
|
12
|
+
* Create a number from a number-like value.
|
|
13
|
+
*
|
|
14
|
+
* - If the value is a number, it is returned as-is.
|
|
15
|
+
* - If the value is a `bigint`, it is converted to a number.
|
|
16
|
+
* - If the value is a string, it is interpreted as a decimal number.
|
|
17
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is
|
|
18
|
+
* interpreted as a hexadecimal number.
|
|
19
|
+
*
|
|
20
|
+
* This validates that the value is a number-like value, and that the resulting
|
|
21
|
+
* number is not `NaN` or `Infinity`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const value = createNumber('0x010203');
|
|
26
|
+
* console.log(value); // 66051
|
|
27
|
+
*
|
|
28
|
+
* const otherValue = createNumber(123n);
|
|
29
|
+
* console.log(otherValue); // 123
|
|
30
|
+
* ```
|
|
31
|
+
* @param value - The value to create the number from.
|
|
32
|
+
* @returns The created number.
|
|
33
|
+
* @throws If the value is not a number-like value, or if the resulting number
|
|
34
|
+
* is `NaN` or `Infinity`.
|
|
35
|
+
*/
|
|
36
|
+
export function createNumber(value) {
|
|
37
|
+
try {
|
|
38
|
+
const result = create(value, NumberCoercer);
|
|
39
|
+
assert(Number.isFinite(result), `Expected a number-like value, got "${value}".`);
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (error instanceof StructError) {
|
|
44
|
+
throw new Error(`Expected a number-like value, got "${value}".`);
|
|
45
|
+
}
|
|
46
|
+
/* istanbul ignore next */
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Create a `bigint` from a number-like value.
|
|
52
|
+
*
|
|
53
|
+
* - If the value is a number, it is converted to a `bigint`.
|
|
54
|
+
* - If the value is a `bigint`, it is returned as-is.
|
|
55
|
+
* - If the value is a string, it is interpreted as a decimal number and
|
|
56
|
+
* converted to a `bigint`.
|
|
57
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is
|
|
58
|
+
* interpreted as a hexadecimal number and converted to a `bigint`.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const value = createBigInt('0x010203');
|
|
63
|
+
* console.log(value); // 16909060n
|
|
64
|
+
*
|
|
65
|
+
* const otherValue = createBigInt(123);
|
|
66
|
+
* console.log(otherValue); // 123n
|
|
67
|
+
* ```
|
|
68
|
+
* @param value - The value to create the bigint from.
|
|
69
|
+
* @returns The created bigint.
|
|
70
|
+
* @throws If the value is not a number-like value.
|
|
71
|
+
*/
|
|
72
|
+
export function createBigInt(value) {
|
|
73
|
+
try {
|
|
74
|
+
// The `BigInt` constructor throws if the value is not a number-like value.
|
|
75
|
+
// There is no need to validate the value manually.
|
|
76
|
+
return create(value, BigIntCoercer);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (error instanceof StructError) {
|
|
80
|
+
throw new Error(`Expected a number-like value, got "${String(error.value)}".`);
|
|
81
|
+
}
|
|
82
|
+
/* istanbul ignore next */
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Create a byte array from a bytes-like value.
|
|
88
|
+
*
|
|
89
|
+
* - If the value is a byte array, it is returned as-is.
|
|
90
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is interpreted
|
|
91
|
+
* as a hexadecimal number and converted to a byte array.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const value = createBytes('0x010203');
|
|
96
|
+
* console.log(value); // Uint8Array [ 1, 2, 3 ]
|
|
97
|
+
*
|
|
98
|
+
* const otherValue = createBytes('0x010203');
|
|
99
|
+
* console.log(otherValue); // Uint8Array [ 1, 2, 3 ]
|
|
100
|
+
* ```
|
|
101
|
+
* @param value - The value to create the byte array from.
|
|
102
|
+
* @returns The created byte array.
|
|
103
|
+
* @throws If the value is not a bytes-like value.
|
|
104
|
+
*/
|
|
105
|
+
export function createBytes(value) {
|
|
106
|
+
if (typeof value === 'string' && value.toLowerCase() === '0x') {
|
|
107
|
+
return new Uint8Array();
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
return create(value, BytesCoercer);
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (error instanceof StructError) {
|
|
114
|
+
throw new Error(`Expected a bytes-like value, got "${String(error.value)}".`);
|
|
115
|
+
}
|
|
116
|
+
/* istanbul ignore next */
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Create a hexadecimal string from a bytes-like value.
|
|
122
|
+
*
|
|
123
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is returned
|
|
124
|
+
* as-is.
|
|
125
|
+
* - If the value is a `Uint8Array`, it is converted to a hex string.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const value = createHex(new Uint8Array([1, 2, 3]));
|
|
130
|
+
* console.log(value); // '0x010203'
|
|
131
|
+
*
|
|
132
|
+
* const otherValue = createHex('0x010203');
|
|
133
|
+
* console.log(otherValue); // '0x010203'
|
|
134
|
+
* ```
|
|
135
|
+
* @param value - The value to create the hex string from.
|
|
136
|
+
* @returns The created hex string.
|
|
137
|
+
* @throws If the value is not a bytes-like value.
|
|
138
|
+
*/
|
|
139
|
+
export function createHex(value) {
|
|
140
|
+
if ((value instanceof Uint8Array && value.length === 0) ||
|
|
141
|
+
(typeof value === 'string' && value.toLowerCase() === '0x')) {
|
|
142
|
+
return '0x';
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
return create(value, HexCoercer);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
if (error instanceof StructError) {
|
|
149
|
+
throw new Error(`Expected a bytes-like value, got "${String(error.value)}".`);
|
|
150
|
+
}
|
|
151
|
+
/* istanbul ignore next */
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
18
155
|
//# sourceMappingURL=coercers.mjs.map
|
package/dist/coercers.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"file":"coercers.mjs","sourceRoot":"","sources":["../src/coercers.ts"],"names":[],"mappings":"AACA,OAAO,EACL,MAAM,EACN,MAAM,EACN,MAAM,EACN,QAAQ,EACR,MAAM,EACN,MAAM,EACN,WAAW,EACX,KAAK,EACN,8BAA8B;AAE/B,OAAO,EAAE,MAAM,EAAE,qBAAiB;AAClC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,oBAAgB;AAEjD,OAAO,EAAE,eAAe,EAAE,kBAAc;AAExC,MAAM,gBAAgB,GAAG,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,EAAE,eAAe,CAAC,CAAC,CAAC;AAChF,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,EAAE,gBAAgB,EAAE,MAAM,CAAC,CAAC;AACjE,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,EAAE,gBAAgB,EAAE,MAAM,CAAC,CAAC;AAEjE,MAAM,eAAe,GAAG,KAAK,CAAC,CAAC,eAAe,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACvE,MAAM,YAAY,GAAG,MAAM,CACzB,QAAQ,CAAC,UAAU,CAAC,EACpB,KAAK,CAAC,CAAC,eAAe,CAAC,CAAC,EACxB,UAAU,CACX,CAAC;AAEF,MAAM,UAAU,GAAG,MAAM,CAAC,eAAe,EAAE,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC,CAAC;AAK7E;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,YAAY,C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KAAK,CAAC;KACb;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,SAAS,CAAC,KAAgB;IACxC,IACE,CAAC,KAAK,YAAY,UAAU,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC;QACnD,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,WAAW,EAAE,KAAK,IAAI,CAAC,EAC3D;QACA,OAAO,IAAI,CAAC;KACb;IAED,IAAI;QACF,OAAO,MAAM,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;KAClC;IAAC,OAAO,KAAK,EAAE;QACd,IAAI,KAAK,YAAY,WAAW,EAAE;YAChC,MAAM,IAAI,KAAK,CACb,qCAAqC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAC7D,CAAC;SACH;QAED,0BAA0B;QAC1B,MAAM,KAAK,CAAC;KACb;AACH,CAAC","sourcesContent":["import type { Infer } from '@metamask/superstruct';\nimport {\n bigint,\n coerce,\n create,\n instance,\n number,\n string,\n StructError,\n union,\n} from '@metamask/superstruct';\n\nimport { assert } from './assert';\nimport { bytesToHex, hexToBytes } from './bytes';\nimport type { Hex } from './hex';\nimport { StrictHexStruct } from './hex';\n\nconst NumberLikeStruct = union([number(), bigint(), string(), StrictHexStruct]);\nconst NumberCoercer = coerce(number(), NumberLikeStruct, Number);\nconst BigIntCoercer = coerce(bigint(), NumberLikeStruct, BigInt);\n\nconst BytesLikeStruct = union([StrictHexStruct, instance(Uint8Array)]);\nconst BytesCoercer = coerce(\n instance(Uint8Array),\n union([StrictHexStruct]),\n hexToBytes,\n);\n\nconst HexCoercer = coerce(StrictHexStruct, instance(Uint8Array), bytesToHex);\n\nexport type NumberLike = Infer<typeof NumberLikeStruct>;\nexport type BytesLike = Infer<typeof BytesLikeStruct>;\n\n/**\n * Create a number from a number-like value.\n *\n * - If the value is a number, it is returned as-is.\n * - If the value is a `bigint`, it is converted to a number.\n * - If the value is a string, it is interpreted as a decimal number.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is\n * interpreted as a hexadecimal number.\n *\n * This validates that the value is a number-like value, and that the resulting\n * number is not `NaN` or `Infinity`.\n *\n * @example\n * ```typescript\n * const value = createNumber('0x010203');\n * console.log(value); // 66051\n *\n * const otherValue = createNumber(123n);\n * console.log(otherValue); // 123\n * ```\n * @param value - The value to create the number from.\n * @returns The created number.\n * @throws If the value is not a number-like value, or if the resulting number\n * is `NaN` or `Infinity`.\n */\nexport function createNumber(value: NumberLike): number {\n try {\n const result = create(value, NumberCoercer);\n\n assert(\n Number.isFinite(result),\n `Expected a number-like value, got \"${value}\".`,\n );\n\n return result;\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(`Expected a number-like value, got \"${value}\".`);\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a `bigint` from a number-like value.\n *\n * - If the value is a number, it is converted to a `bigint`.\n * - If the value is a `bigint`, it is returned as-is.\n * - If the value is a string, it is interpreted as a decimal number and\n * converted to a `bigint`.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is\n * interpreted as a hexadecimal number and converted to a `bigint`.\n *\n * @example\n * ```typescript\n * const value = createBigInt('0x010203');\n * console.log(value); // 16909060n\n *\n * const otherValue = createBigInt(123);\n * console.log(otherValue); // 123n\n * ```\n * @param value - The value to create the bigint from.\n * @returns The created bigint.\n * @throws If the value is not a number-like value.\n */\nexport function createBigInt(value: NumberLike): bigint {\n try {\n // The `BigInt` constructor throws if the value is not a number-like value.\n // There is no need to validate the value manually.\n return create(value, BigIntCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(\n `Expected a number-like value, got \"${String(error.value)}\".`,\n );\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a byte array from a bytes-like value.\n *\n * - If the value is a byte array, it is returned as-is.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is interpreted\n * as a hexadecimal number and converted to a byte array.\n *\n * @example\n * ```typescript\n * const value = createBytes('0x010203');\n * console.log(value); // Uint8Array [ 1, 2, 3 ]\n *\n * const otherValue = createBytes('0x010203');\n * console.log(otherValue); // Uint8Array [ 1, 2, 3 ]\n * ```\n * @param value - The value to create the byte array from.\n * @returns The created byte array.\n * @throws If the value is not a bytes-like value.\n */\nexport function createBytes(value: BytesLike): Uint8Array {\n if (typeof value === 'string' && value.toLowerCase() === '0x') {\n return new Uint8Array();\n }\n\n try {\n return create(value, BytesCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(\n `Expected a bytes-like value, got \"${String(error.value)}\".`,\n );\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a hexadecimal string from a bytes-like value.\n *\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is returned\n * as-is.\n * - If the value is a `Uint8Array`, it is converted to a hex string.\n *\n * @example\n * ```typescript\n * const value = createHex(new Uint8Array([1, 2, 3]));\n * console.log(value); // '0x010203'\n *\n * const otherValue = createHex('0x010203');\n * console.log(otherValue); // '0x010203'\n * ```\n * @param value - The value to create the hex string from.\n * @returns The created hex string.\n * @throws If the value is not a bytes-like value.\n */\nexport function createHex(value: BytesLike): Hex {\n if (\n (value instanceof Uint8Array && value.length === 0) ||\n (typeof value === 'string' && value.toLowerCase() === '0x')\n ) {\n return '0x';\n }\n\n try {\n return create(value, HexCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(\n `Expected a bytes-like value, got \"${String(error.value)}\".`,\n );\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n"]}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
11
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
+
};
|
|
13
|
+
var _FrozenMap_map, _FrozenSet_set;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.FrozenSet = exports.FrozenMap = void 0;
|
|
16
|
+
/**
|
|
17
|
+
* A {@link ReadonlyMap} that cannot be modified after instantiation.
|
|
18
|
+
* The implementation uses an inner map hidden via a private field, and the
|
|
19
|
+
* immutability guarantee relies on it being impossible to get a reference
|
|
20
|
+
* to this map.
|
|
21
|
+
*/
|
|
22
|
+
class FrozenMap {
|
|
23
|
+
get size() {
|
|
24
|
+
return __classPrivateFieldGet(this, _FrozenMap_map, "f").size;
|
|
25
|
+
}
|
|
26
|
+
[(_FrozenMap_map = new WeakMap(), Symbol.iterator)]() {
|
|
27
|
+
return __classPrivateFieldGet(this, _FrozenMap_map, "f")[Symbol.iterator]();
|
|
28
|
+
}
|
|
29
|
+
constructor(entries) {
|
|
30
|
+
_FrozenMap_map.set(this, void 0);
|
|
31
|
+
__classPrivateFieldSet(this, _FrozenMap_map, new Map(entries), "f");
|
|
32
|
+
Object.freeze(this);
|
|
33
|
+
}
|
|
34
|
+
entries() {
|
|
35
|
+
return __classPrivateFieldGet(this, _FrozenMap_map, "f").entries();
|
|
36
|
+
}
|
|
37
|
+
forEach(callbackfn, thisArg) {
|
|
38
|
+
// We have to wrap the specified callback in order to prevent it from
|
|
39
|
+
// receiving a reference to the inner map.
|
|
40
|
+
return __classPrivateFieldGet(this, _FrozenMap_map, "f").forEach((value, key, _map) => callbackfn.call(thisArg, value, key, this));
|
|
41
|
+
}
|
|
42
|
+
get(key) {
|
|
43
|
+
return __classPrivateFieldGet(this, _FrozenMap_map, "f").get(key);
|
|
44
|
+
}
|
|
45
|
+
has(key) {
|
|
46
|
+
return __classPrivateFieldGet(this, _FrozenMap_map, "f").has(key);
|
|
47
|
+
}
|
|
48
|
+
keys() {
|
|
49
|
+
return __classPrivateFieldGet(this, _FrozenMap_map, "f").keys();
|
|
50
|
+
}
|
|
51
|
+
values() {
|
|
52
|
+
return __classPrivateFieldGet(this, _FrozenMap_map, "f").values();
|
|
53
|
+
}
|
|
54
|
+
toString() {
|
|
55
|
+
return `FrozenMap(${this.size}) {${this.size > 0
|
|
56
|
+
? ` ${[...this.entries()]
|
|
57
|
+
.map(([key, value]) => `${String(key)} => ${String(value)}`)
|
|
58
|
+
.join(', ')} `
|
|
59
|
+
: ''}}`;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.FrozenMap = FrozenMap;
|
|
63
|
+
/**
|
|
64
|
+
* A {@link ReadonlySet} that cannot be modified after instantiation.
|
|
65
|
+
* The implementation uses an inner set hidden via a private field, and the
|
|
66
|
+
* immutability guarantee relies on it being impossible to get a reference
|
|
67
|
+
* to this set.
|
|
68
|
+
*/
|
|
69
|
+
class FrozenSet {
|
|
70
|
+
get size() {
|
|
71
|
+
return __classPrivateFieldGet(this, _FrozenSet_set, "f").size;
|
|
72
|
+
}
|
|
73
|
+
[(_FrozenSet_set = new WeakMap(), Symbol.iterator)]() {
|
|
74
|
+
return __classPrivateFieldGet(this, _FrozenSet_set, "f")[Symbol.iterator]();
|
|
75
|
+
}
|
|
76
|
+
constructor(values) {
|
|
77
|
+
_FrozenSet_set.set(this, void 0);
|
|
78
|
+
__classPrivateFieldSet(this, _FrozenSet_set, new Set(values), "f");
|
|
79
|
+
Object.freeze(this);
|
|
80
|
+
}
|
|
81
|
+
entries() {
|
|
82
|
+
return __classPrivateFieldGet(this, _FrozenSet_set, "f").entries();
|
|
83
|
+
}
|
|
84
|
+
forEach(callbackfn, thisArg) {
|
|
85
|
+
// We have to wrap the specified callback in order to prevent it from
|
|
86
|
+
// receiving a reference to the inner set.
|
|
87
|
+
return __classPrivateFieldGet(this, _FrozenSet_set, "f").forEach((value, value2, _set) => callbackfn.call(thisArg, value, value2, this));
|
|
88
|
+
}
|
|
89
|
+
has(value) {
|
|
90
|
+
return __classPrivateFieldGet(this, _FrozenSet_set, "f").has(value);
|
|
91
|
+
}
|
|
92
|
+
keys() {
|
|
93
|
+
return __classPrivateFieldGet(this, _FrozenSet_set, "f").keys();
|
|
94
|
+
}
|
|
95
|
+
values() {
|
|
96
|
+
return __classPrivateFieldGet(this, _FrozenSet_set, "f").values();
|
|
97
|
+
}
|
|
98
|
+
toString() {
|
|
99
|
+
return `FrozenSet(${this.size}) {${this.size > 0
|
|
100
|
+
? ` ${[...this.values()].map((member) => String(member)).join(', ')} `
|
|
101
|
+
: ''}}`;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.FrozenSet = FrozenSet;
|
|
105
|
+
Object.freeze(FrozenMap);
|
|
106
|
+
Object.freeze(FrozenMap.prototype);
|
|
107
|
+
Object.freeze(FrozenSet);
|
|
108
|
+
Object.freeze(FrozenSet.prototype);
|
|
109
|
+
//# sourceMappingURL=collections.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.cjs","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA;;;;;GAKG;AACH,MAAM,SAAS;IAGb,IAAW,IAAI;QACb,OAAO,uBAAA,IAAI,sBAAK,CAAC,IAAI,CAAC;IACxB,CAAC;IAEM,kCAAC,MAAM,CAAC,QAAQ,EAAC;QACtB,OAAO,uBAAA,IAAI,sBAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,C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AAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG;YACtE,CAAC,CAAC,EACN,GAAG,CAAC;IACN,CAAC;CACF;AAQmB,8BAAS;AAN7B,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;AACzB,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;AAEnC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;AACzB,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC","sourcesContent":["/**\n * A {@link ReadonlyMap} that cannot be modified after instantiation.\n * The implementation uses an inner map hidden via a private field, and the\n * immutability guarantee relies on it being impossible to get a reference\n * to this map.\n */\nclass FrozenMap<Key, Value> implements ReadonlyMap<Key, Value> {\n readonly #map: Map<Key, Value>;\n\n public get size() {\n return this.#map.size;\n }\n\n public [Symbol.iterator]() {\n return this.#map[Symbol.iterator]();\n }\n\n constructor(entries?: readonly (readonly [Key, Value])[] | null) {\n this.#map = new Map<Key, Value>(entries);\n Object.freeze(this);\n }\n\n public entries() {\n return this.#map.entries();\n }\n\n public forEach(\n callbackfn: (value: Value, key: Key, map: this) => void,\n thisArg?: any,\n ): void {\n // We have to wrap the specified callback in order to prevent it from\n // receiving a reference to the inner map.\n return this.#map.forEach((value: Value, key: Key, _map: unknown) =>\n callbackfn.call(thisArg, value, key, this),\n );\n }\n\n public get(key: Key) {\n return this.#map.get(key);\n }\n\n public has(key: Key) {\n return this.#map.has(key);\n }\n\n public keys() {\n return this.#map.keys();\n }\n\n public values() {\n return this.#map.values();\n }\n\n public toString(): string {\n return `FrozenMap(${this.size}) {${\n this.size > 0\n ? ` ${[...this.entries()]\n .map(([key, value]) => `${String(key)} => ${String(value)}`)\n .join(', ')} `\n : ''\n }}`;\n }\n}\n\n/**\n * A {@link ReadonlySet} that cannot be modified after instantiation.\n * The implementation uses an inner set hidden via a private field, and the\n * immutability guarantee relies on it being impossible to get a reference\n * to this set.\n */\nclass FrozenSet<Value> implements ReadonlySet<Value> {\n readonly #set: Set<Value>;\n\n public get size() {\n return this.#set.size;\n }\n\n public [Symbol.iterator]() {\n return this.#set[Symbol.iterator]();\n }\n\n constructor(values?: readonly Value[] | null) {\n this.#set = new Set<Value>(values);\n Object.freeze(this);\n }\n\n public entries() {\n return this.#set.entries();\n }\n\n public forEach(\n callbackfn: (value: Value, value2: Value, set: this) => void,\n thisArg?: any,\n ): void {\n // We have to wrap the specified callback in order to prevent it from\n // receiving a reference to the inner set.\n return this.#set.forEach((value: Value, value2: Value, _set: unknown) =>\n callbackfn.call(thisArg, value, value2, this),\n );\n }\n\n public has(value: Value) {\n return this.#set.has(value);\n }\n\n public keys() {\n return this.#set.keys();\n }\n\n public values() {\n return this.#set.values();\n }\n\n public toString(): string {\n return `FrozenSet(${this.size}) {${\n this.size > 0\n ? ` ${[...this.values()].map((member) => String(member)).join(', ')} `\n : ''\n }}`;\n }\n}\n\nObject.freeze(FrozenMap);\nObject.freeze(FrozenMap.prototype);\n\nObject.freeze(FrozenSet);\nObject.freeze(FrozenSet.prototype);\n\nexport { FrozenMap, FrozenSet };\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.d.cts","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,cAAM,SAAS,CAAC,GAAG,EAAE,KAAK,CAAE,YAAW,WAAW,CAAC,GAAG,EAAE,KAAK,CAAC;;IAG5D,IAAW,IAAI,WAEd;IAEM,CAAC,MAAM,CAAC,QAAQ,CAAC;gBAIZ,OAAO,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,EAAE,GAAG,IAAI;IAKxD,OAAO;IAIP,OAAO,CACZ,UAAU,EAAE,CAAC,KAAK,EAAE,KAAK,EAA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