@metamask/utils 8.4.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 +14 -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} +12 -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 -43
- 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 -296
- 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 -316
- 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} +2 -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 +26 -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 -44
- 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-4RMX5YWE.js +0 -34
- package/dist/chunk-4RMX5YWE.js.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-C6HGFEYL.js +0 -23
- package/dist/chunk-C6HGFEYL.js.map +0 -1
- package/dist/chunk-DHVKFDHQ.js +0 -95
- package/dist/chunk-DHVKFDHQ.js.map +0 -1
- package/dist/chunk-DYRKZQMK.mjs +0 -122
- package/dist/chunk-DYRKZQMK.mjs.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-OUZ3LSLX.mjs +0 -94
- package/dist/chunk-OUZ3LSLX.mjs.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-RNEXSCNP.js +0 -122
- package/dist/chunk-RNEXSCNP.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-THNLGEXV.mjs +0 -34
- package/dist/chunk-THNLGEXV.mjs.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-W5XI7ERF.mjs +0 -23
- package/dist/chunk-W5XI7ERF.mjs.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-YWAID473.js +0 -94
- package/dist/chunk-YWAID473.js.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 -297
- 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 -317
- 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
package/dist/number.cjs
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hexToBigInt = exports.hexToNumber = exports.bigIntToHex = exports.numberToHex = void 0;
|
|
4
|
+
const assert_1 = require("./assert.cjs");
|
|
5
|
+
const hex_1 = require("./hex.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Convert a number to a hexadecimal string. This verifies that the number is a
|
|
8
|
+
* non-negative safe integer.
|
|
9
|
+
*
|
|
10
|
+
* To convert a `bigint` to a hexadecimal string instead, use
|
|
11
|
+
* {@link bigIntToHex}.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* numberToHex(0); // '0x0'
|
|
16
|
+
* numberToHex(1); // '0x1'
|
|
17
|
+
* numberToHex(16); // '0x10'
|
|
18
|
+
* ```
|
|
19
|
+
* @param value - The number to convert to a hexadecimal string.
|
|
20
|
+
* @returns The hexadecimal string, with the "0x"-prefix.
|
|
21
|
+
* @throws If the number is not a non-negative safe integer.
|
|
22
|
+
*/
|
|
23
|
+
const numberToHex = (value) => {
|
|
24
|
+
(0, assert_1.assert)(typeof value === 'number', 'Value must be a number.');
|
|
25
|
+
(0, assert_1.assert)(value >= 0, 'Value must be a non-negative number.');
|
|
26
|
+
(0, assert_1.assert)(Number.isSafeInteger(value), 'Value is not a safe integer. Use `bigIntToHex` instead.');
|
|
27
|
+
return (0, hex_1.add0x)(value.toString(16));
|
|
28
|
+
};
|
|
29
|
+
exports.numberToHex = numberToHex;
|
|
30
|
+
/**
|
|
31
|
+
* Convert a `bigint` to a hexadecimal string. This verifies that the `bigint`
|
|
32
|
+
* is a non-negative integer.
|
|
33
|
+
*
|
|
34
|
+
* To convert a number to a hexadecimal string instead, use {@link numberToHex}.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* bigIntToHex(0n); // '0x0'
|
|
39
|
+
* bigIntToHex(1n); // '0x1'
|
|
40
|
+
* bigIntToHex(16n); // '0x10'
|
|
41
|
+
* ```
|
|
42
|
+
* @param value - The `bigint` to convert to a hexadecimal string.
|
|
43
|
+
* @returns The hexadecimal string, with the "0x"-prefix.
|
|
44
|
+
* @throws If the `bigint` is not a non-negative integer.
|
|
45
|
+
*/
|
|
46
|
+
const bigIntToHex = (value) => {
|
|
47
|
+
(0, assert_1.assert)(typeof value === 'bigint', 'Value must be a bigint.');
|
|
48
|
+
(0, assert_1.assert)(value >= 0, 'Value must be a non-negative bigint.');
|
|
49
|
+
return (0, hex_1.add0x)(value.toString(16));
|
|
50
|
+
};
|
|
51
|
+
exports.bigIntToHex = bigIntToHex;
|
|
52
|
+
/**
|
|
53
|
+
* Convert a hexadecimal string to a number. This verifies that the string is a
|
|
54
|
+
* valid hex string, and that the resulting number is a safe integer. Both
|
|
55
|
+
* "0x"-prefixed and unprefixed strings are supported.
|
|
56
|
+
*
|
|
57
|
+
* To convert a hexadecimal string to a `bigint` instead, use
|
|
58
|
+
* {@link hexToBigInt}.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* hexToNumber('0x0'); // 0
|
|
63
|
+
* hexToNumber('0x1'); // 1
|
|
64
|
+
* hexToNumber('0x10'); // 16
|
|
65
|
+
* ```
|
|
66
|
+
* @param value - The hexadecimal string to convert to a number.
|
|
67
|
+
* @returns The number.
|
|
68
|
+
* @throws If the value is not a valid hexadecimal string, or if the resulting
|
|
69
|
+
* number is not a safe integer.
|
|
70
|
+
*/
|
|
71
|
+
const hexToNumber = (value) => {
|
|
72
|
+
(0, hex_1.assertIsHexString)(value);
|
|
73
|
+
// `parseInt` accepts values without the "0x"-prefix, whereas `Number` does
|
|
74
|
+
// not. Using this is slightly faster than `Number(add0x(value))`.
|
|
75
|
+
const numberValue = parseInt(value, 16);
|
|
76
|
+
(0, assert_1.assert)(Number.isSafeInteger(numberValue), 'Value is not a safe integer. Use `hexToBigInt` instead.');
|
|
77
|
+
return numberValue;
|
|
78
|
+
};
|
|
79
|
+
exports.hexToNumber = hexToNumber;
|
|
80
|
+
/**
|
|
81
|
+
* Convert a hexadecimal string to a `bigint`. This verifies that the string is
|
|
82
|
+
* a valid hex string. Both "0x"-prefixed and unprefixed strings are supported.
|
|
83
|
+
*
|
|
84
|
+
* To convert a hexadecimal string to a number instead, use {@link hexToNumber}.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* hexToBigInt('0x0'); // 0n
|
|
89
|
+
* hexToBigInt('0x1'); // 1n
|
|
90
|
+
* hexToBigInt('0x10'); // 16n
|
|
91
|
+
* ```
|
|
92
|
+
* @param value - The hexadecimal string to convert to a `bigint`.
|
|
93
|
+
* @returns The `bigint`.
|
|
94
|
+
* @throws If the value is not a valid hexadecimal string.
|
|
95
|
+
*/
|
|
96
|
+
const hexToBigInt = (value) => {
|
|
97
|
+
(0, hex_1.assertIsHexString)(value);
|
|
98
|
+
// The `BigInt` constructor requires the "0x"-prefix to parse a hex string.
|
|
99
|
+
return BigInt((0, hex_1.add0x)(value));
|
|
100
|
+
};
|
|
101
|
+
exports.hexToBigInt = hexToBigInt;
|
|
102
|
+
//# sourceMappingURL=number.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number.cjs","sourceRoot":"","sources":["../src/number.ts"],"names":[],"mappings":";;;AAAA,yCAAkC;AAClC,mCAAiD;AAEjD;;;;;;;;;;;;;;;;GAgBG;AACI,MAAM,WAAW,GAAG,CAAC,KAAa,EAAU,EAAE;IACnD,IAAA,eAAM,EAAC,OAAO,KAAK,KAAK,QAAQ,EAAE,yBAAyB,CAAC,CAAC;IAC7D,IAAA,eAAM,EAAC,KAAK,IAAI,CAAC,EAAE,sCAAsC,CAAC,CAAC;IAC3D,IAAA,eAAM,EACJ,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,EAC3B,yDAAyD,CAC1D,CAAC;IAEF,OAAO,IAAA,WAAK,EAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;AATW,QAAA,WAAW,eAStB;AAEF;;;;;;;;;;;;;;;GAeG;AACI,MAAM,WAAW,GAAG,CAAC,KAAa,EAAU,EAAE;IACnD,IAAA,eAAM,EAAC,OAAO,KAAK,KAAK,QAAQ,EAAE,yBAAyB,CAAC,CAAC;IAC7D,IAAA,eAAM,EAAC,KAAK,IAAI,CAAC,EAAE,sCAAsC,CAAC,CAAC;IAE3D,OAAO,IAAA,WAAK,EAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;AALW,QAAA,WAAW,eAKtB;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACI,MAAM,WAAW,GAAG,CAAC,KAAa,EAAU,EAAE;IACnD,IAAA,uBAAiB,EAAC,KAAK,CAAC,CAAC;IAEzB,2EAA2E;IAC3E,kEAAkE;IAClE,MAAM,WAAW,GAAG,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAExC,IAAA,eAAM,EACJ,MAAM,CAAC,aAAa,CAAC,WAAW,CAAC,EACjC,yDAAyD,CAC1D,CAAC;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAbW,QAAA,WAAW,eAatB;AAEF;;;;;;;;;;;;;;;GAeG;AACI,MAAM,WAAW,GAAG,CAAC,KAAa,EAAU,EAAE;IACnD,IAAA,uBAAiB,EAAC,KAAK,CAAC,CAAC;IAEzB,2EAA2E;IAC3E,OAAO,MAAM,CAAC,IAAA,WAAK,EAAC,KAAK,CAAC,CAAC,CAAC;AAC9B,CAAC,CAAC;AALW,QAAA,WAAW,eAKtB","sourcesContent":["import { assert } from './assert';\nimport { add0x, assertIsHexString } from './hex';\n\n/**\n * Convert a number to a hexadecimal string. This verifies that the number is a\n * non-negative safe integer.\n *\n * To convert a `bigint` to a hexadecimal string instead, use\n * {@link bigIntToHex}.\n *\n * @example\n * ```typescript\n * numberToHex(0); // '0x0'\n * numberToHex(1); // '0x1'\n * numberToHex(16); // '0x10'\n * ```\n * @param value - The number to convert to a hexadecimal string.\n * @returns The hexadecimal string, with the \"0x\"-prefix.\n * @throws If the number is not a non-negative safe integer.\n */\nexport const numberToHex = (value: number): string => {\n assert(typeof value === 'number', 'Value must be a number.');\n assert(value >= 0, 'Value must be a non-negative number.');\n assert(\n Number.isSafeInteger(value),\n 'Value is not a safe integer. Use `bigIntToHex` instead.',\n );\n\n return add0x(value.toString(16));\n};\n\n/**\n * Convert a `bigint` to a hexadecimal string. This verifies that the `bigint`\n * is a non-negative integer.\n *\n * To convert a number to a hexadecimal string instead, use {@link numberToHex}.\n *\n * @example\n * ```typescript\n * bigIntToHex(0n); // '0x0'\n * bigIntToHex(1n); // '0x1'\n * bigIntToHex(16n); // '0x10'\n * ```\n * @param value - The `bigint` to convert to a hexadecimal string.\n * @returns The hexadecimal string, with the \"0x\"-prefix.\n * @throws If the `bigint` is not a non-negative integer.\n */\nexport const bigIntToHex = (value: bigint): string => {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(value >= 0, 'Value must be a non-negative bigint.');\n\n return add0x(value.toString(16));\n};\n\n/**\n * Convert a hexadecimal string to a number. This verifies that the string is a\n * valid hex string, and that the resulting number is a safe integer. Both\n * \"0x\"-prefixed and unprefixed strings are supported.\n *\n * To convert a hexadecimal string to a `bigint` instead, use\n * {@link hexToBigInt}.\n *\n * @example\n * ```typescript\n * hexToNumber('0x0'); // 0\n * hexToNumber('0x1'); // 1\n * hexToNumber('0x10'); // 16\n * ```\n * @param value - The hexadecimal string to convert to a number.\n * @returns The number.\n * @throws If the value is not a valid hexadecimal string, or if the resulting\n * number is not a safe integer.\n */\nexport const hexToNumber = (value: string): number => {\n assertIsHexString(value);\n\n // `parseInt` accepts values without the \"0x\"-prefix, whereas `Number` does\n // not. Using this is slightly faster than `Number(add0x(value))`.\n const numberValue = parseInt(value, 16);\n\n assert(\n Number.isSafeInteger(numberValue),\n 'Value is not a safe integer. Use `hexToBigInt` instead.',\n );\n\n return numberValue;\n};\n\n/**\n * Convert a hexadecimal string to a `bigint`. This verifies that the string is\n * a valid hex string. Both \"0x\"-prefixed and unprefixed strings are supported.\n *\n * To convert a hexadecimal string to a number instead, use {@link hexToNumber}.\n *\n * @example\n * ```typescript\n * hexToBigInt('0x0'); // 0n\n * hexToBigInt('0x1'); // 1n\n * hexToBigInt('0x10'); // 16n\n * ```\n * @param value - The hexadecimal string to convert to a `bigint`.\n * @returns The `bigint`.\n * @throws If the value is not a valid hexadecimal string.\n */\nexport const hexToBigInt = (value: string): bigint => {\n assertIsHexString(value);\n\n // The `BigInt` constructor requires the \"0x\"-prefix to parse a hex string.\n return BigInt(add0x(value));\n};\n"]}
|
|
@@ -70,4 +70,4 @@ export declare const hexToNumber: (value: string) => number;
|
|
|
70
70
|
* @throws If the value is not a valid hexadecimal string.
|
|
71
71
|
*/
|
|
72
72
|
export declare const hexToBigInt: (value: string) => bigint;
|
|
73
|
-
//# sourceMappingURL=number.d.
|
|
73
|
+
//# sourceMappingURL=number.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number.d.cts","sourceRoot":"","sources":["../src/number.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,KAAG,MAS3C,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,KAAG,MAK3C,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,KAAG,MAa3C,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,KAAG,MAK3C,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a number to a hexadecimal string. This verifies that the number is a
|
|
3
|
+
* non-negative safe integer.
|
|
4
|
+
*
|
|
5
|
+
* To convert a `bigint` to a hexadecimal string instead, use
|
|
6
|
+
* {@link bigIntToHex}.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* numberToHex(0); // '0x0'
|
|
11
|
+
* numberToHex(1); // '0x1'
|
|
12
|
+
* numberToHex(16); // '0x10'
|
|
13
|
+
* ```
|
|
14
|
+
* @param value - The number to convert to a hexadecimal string.
|
|
15
|
+
* @returns The hexadecimal string, with the "0x"-prefix.
|
|
16
|
+
* @throws If the number is not a non-negative safe integer.
|
|
17
|
+
*/
|
|
18
|
+
export declare const numberToHex: (value: number) => string;
|
|
19
|
+
/**
|
|
20
|
+
* Convert a `bigint` to a hexadecimal string. This verifies that the `bigint`
|
|
21
|
+
* is a non-negative integer.
|
|
22
|
+
*
|
|
23
|
+
* To convert a number to a hexadecimal string instead, use {@link numberToHex}.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* bigIntToHex(0n); // '0x0'
|
|
28
|
+
* bigIntToHex(1n); // '0x1'
|
|
29
|
+
* bigIntToHex(16n); // '0x10'
|
|
30
|
+
* ```
|
|
31
|
+
* @param value - The `bigint` to convert to a hexadecimal string.
|
|
32
|
+
* @returns The hexadecimal string, with the "0x"-prefix.
|
|
33
|
+
* @throws If the `bigint` is not a non-negative integer.
|
|
34
|
+
*/
|
|
35
|
+
export declare const bigIntToHex: (value: bigint) => string;
|
|
36
|
+
/**
|
|
37
|
+
* Convert a hexadecimal string to a number. This verifies that the string is a
|
|
38
|
+
* valid hex string, and that the resulting number is a safe integer. Both
|
|
39
|
+
* "0x"-prefixed and unprefixed strings are supported.
|
|
40
|
+
*
|
|
41
|
+
* To convert a hexadecimal string to a `bigint` instead, use
|
|
42
|
+
* {@link hexToBigInt}.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* hexToNumber('0x0'); // 0
|
|
47
|
+
* hexToNumber('0x1'); // 1
|
|
48
|
+
* hexToNumber('0x10'); // 16
|
|
49
|
+
* ```
|
|
50
|
+
* @param value - The hexadecimal string to convert to a number.
|
|
51
|
+
* @returns The number.
|
|
52
|
+
* @throws If the value is not a valid hexadecimal string, or if the resulting
|
|
53
|
+
* number is not a safe integer.
|
|
54
|
+
*/
|
|
55
|
+
export declare const hexToNumber: (value: string) => number;
|
|
56
|
+
/**
|
|
57
|
+
* Convert a hexadecimal string to a `bigint`. This verifies that the string is
|
|
58
|
+
* a valid hex string. Both "0x"-prefixed and unprefixed strings are supported.
|
|
59
|
+
*
|
|
60
|
+
* To convert a hexadecimal string to a number instead, use {@link hexToNumber}.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* hexToBigInt('0x0'); // 0n
|
|
65
|
+
* hexToBigInt('0x1'); // 1n
|
|
66
|
+
* hexToBigInt('0x10'); // 16n
|
|
67
|
+
* ```
|
|
68
|
+
* @param value - The hexadecimal string to convert to a `bigint`.
|
|
69
|
+
* @returns The `bigint`.
|
|
70
|
+
* @throws If the value is not a valid hexadecimal string.
|
|
71
|
+
*/
|
|
72
|
+
export declare const hexToBigInt: (value: string) => bigint;
|
|
73
|
+
//# sourceMappingURL=number.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number.d.mts","sourceRoot":"","sources":["../src/number.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,KAAG,MAS3C,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,KAAG,MAK3C,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,KAAG,MAa3C,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,KAAG,MAK3C,CAAC"}
|
package/dist/number.mjs
CHANGED
|
@@ -1,18 +1,95 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
import { assert } from "./assert.mjs";
|
|
2
|
+
import { add0x, assertIsHexString } from "./hex.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Convert a number to a hexadecimal string. This verifies that the number is a
|
|
5
|
+
* non-negative safe integer.
|
|
6
|
+
*
|
|
7
|
+
* To convert a `bigint` to a hexadecimal string instead, use
|
|
8
|
+
* {@link bigIntToHex}.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* numberToHex(0); // '0x0'
|
|
13
|
+
* numberToHex(1); // '0x1'
|
|
14
|
+
* numberToHex(16); // '0x10'
|
|
15
|
+
* ```
|
|
16
|
+
* @param value - The number to convert to a hexadecimal string.
|
|
17
|
+
* @returns The hexadecimal string, with the "0x"-prefix.
|
|
18
|
+
* @throws If the number is not a non-negative safe integer.
|
|
19
|
+
*/
|
|
20
|
+
export const numberToHex = (value) => {
|
|
21
|
+
assert(typeof value === 'number', 'Value must be a number.');
|
|
22
|
+
assert(value >= 0, 'Value must be a non-negative number.');
|
|
23
|
+
assert(Number.isSafeInteger(value), 'Value is not a safe integer. Use `bigIntToHex` instead.');
|
|
24
|
+
return add0x(value.toString(16));
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Convert a `bigint` to a hexadecimal string. This verifies that the `bigint`
|
|
28
|
+
* is a non-negative integer.
|
|
29
|
+
*
|
|
30
|
+
* To convert a number to a hexadecimal string instead, use {@link numberToHex}.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* bigIntToHex(0n); // '0x0'
|
|
35
|
+
* bigIntToHex(1n); // '0x1'
|
|
36
|
+
* bigIntToHex(16n); // '0x10'
|
|
37
|
+
* ```
|
|
38
|
+
* @param value - The `bigint` to convert to a hexadecimal string.
|
|
39
|
+
* @returns The hexadecimal string, with the "0x"-prefix.
|
|
40
|
+
* @throws If the `bigint` is not a non-negative integer.
|
|
41
|
+
*/
|
|
42
|
+
export const bigIntToHex = (value) => {
|
|
43
|
+
assert(typeof value === 'bigint', 'Value must be a bigint.');
|
|
44
|
+
assert(value >= 0, 'Value must be a non-negative bigint.');
|
|
45
|
+
return add0x(value.toString(16));
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Convert a hexadecimal string to a number. This verifies that the string is a
|
|
49
|
+
* valid hex string, and that the resulting number is a safe integer. Both
|
|
50
|
+
* "0x"-prefixed and unprefixed strings are supported.
|
|
51
|
+
*
|
|
52
|
+
* To convert a hexadecimal string to a `bigint` instead, use
|
|
53
|
+
* {@link hexToBigInt}.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* hexToNumber('0x0'); // 0
|
|
58
|
+
* hexToNumber('0x1'); // 1
|
|
59
|
+
* hexToNumber('0x10'); // 16
|
|
60
|
+
* ```
|
|
61
|
+
* @param value - The hexadecimal string to convert to a number.
|
|
62
|
+
* @returns The number.
|
|
63
|
+
* @throws If the value is not a valid hexadecimal string, or if the resulting
|
|
64
|
+
* number is not a safe integer.
|
|
65
|
+
*/
|
|
66
|
+
export const hexToNumber = (value) => {
|
|
67
|
+
assertIsHexString(value);
|
|
68
|
+
// `parseInt` accepts values without the "0x"-prefix, whereas `Number` does
|
|
69
|
+
// not. Using this is slightly faster than `Number(add0x(value))`.
|
|
70
|
+
const numberValue = parseInt(value, 16);
|
|
71
|
+
assert(Number.isSafeInteger(numberValue), 'Value is not a safe integer. Use `hexToBigInt` instead.');
|
|
72
|
+
return numberValue;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Convert a hexadecimal string to a `bigint`. This verifies that the string is
|
|
76
|
+
* a valid hex string. Both "0x"-prefixed and unprefixed strings are supported.
|
|
77
|
+
*
|
|
78
|
+
* To convert a hexadecimal string to a number instead, use {@link hexToNumber}.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* hexToBigInt('0x0'); // 0n
|
|
83
|
+
* hexToBigInt('0x1'); // 1n
|
|
84
|
+
* hexToBigInt('0x10'); // 16n
|
|
85
|
+
* ```
|
|
86
|
+
* @param value - The hexadecimal string to convert to a `bigint`.
|
|
87
|
+
* @returns The `bigint`.
|
|
88
|
+
* @throws If the value is not a valid hexadecimal string.
|
|
89
|
+
*/
|
|
90
|
+
export const hexToBigInt = (value) => {
|
|
91
|
+
assertIsHexString(value);
|
|
92
|
+
// The `BigInt` constructor requires the "0x"-prefix to parse a hex string.
|
|
93
|
+
return BigInt(add0x(value));
|
|
17
94
|
};
|
|
18
95
|
//# sourceMappingURL=number.mjs.map
|
package/dist/number.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"file":"number.mjs","sourceRoot":"","sources":["../src/number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,qBAAiB;AAClC,OAAO,EAAE,KAAK,EAAE,iBAAiB,EAAE,kBAAc;AAEjD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAa,EAAU,EAAE;IACnD,MAAM,CAAC,OAAO,KAAK,KAAK,QAAQ,EAAE,yBAAyB,CAAC,CAAC;IAC7D,MAAM,CAAC,KAAK,IAAI,CAAC,EAAE,sCAAsC,CAAC,CAAC;IAC3D,MAAM,CACJ,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,EAC3B,yDAAyD,CAC1D,CAAC;IAEF,OAAO,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAa,EAAU,EAAE;IACnD,MAAM,CAAC,OAAO,KAAK,KAAK,QAAQ,EAAE,yBAAyB,CAAC,CAAC;IAC7D,MAAM,CAAC,KAAK,IAAI,CAAC,EAAE,sCAAsC,CAAC,CAAC;IAE3D,OAAO,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAa,EAAU,EAAE;IACnD,iBAAiB,CAAC,KAAK,CAAC,CAAC;IAEzB,2EAA2E;IAC3E,kEAAkE;IAClE,MAAM,WAAW,GAAG,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAExC,MAAM,CACJ,MAAM,CAAC,aAAa,CAAC,WAAW,CAAC,EACjC,yDAAyD,CAC1D,CAAC;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAa,EAAU,EAAE;IACnD,iBAAiB,CAAC,KAAK,CAAC,CAAC;IAEzB,2EAA2E;IAC3E,OAAO,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;AAC9B,CAAC,CAAC","sourcesContent":["import { assert } from './assert';\nimport { add0x, assertIsHexString } from './hex';\n\n/**\n * Convert a number to a hexadecimal string. This verifies that the number is a\n * non-negative safe integer.\n *\n * To convert a `bigint` to a hexadecimal string instead, use\n * {@link bigIntToHex}.\n *\n * @example\n * ```typescript\n * numberToHex(0); // '0x0'\n * numberToHex(1); // '0x1'\n * numberToHex(16); // '0x10'\n * ```\n * @param value - The number to convert to a hexadecimal string.\n * @returns The hexadecimal string, with the \"0x\"-prefix.\n * @throws If the number is not a non-negative safe integer.\n */\nexport const numberToHex = (value: number): string => {\n assert(typeof value === 'number', 'Value must be a number.');\n assert(value >= 0, 'Value must be a non-negative number.');\n assert(\n Number.isSafeInteger(value),\n 'Value is not a safe integer. Use `bigIntToHex` instead.',\n );\n\n return add0x(value.toString(16));\n};\n\n/**\n * Convert a `bigint` to a hexadecimal string. This verifies that the `bigint`\n * is a non-negative integer.\n *\n * To convert a number to a hexadecimal string instead, use {@link numberToHex}.\n *\n * @example\n * ```typescript\n * bigIntToHex(0n); // '0x0'\n * bigIntToHex(1n); // '0x1'\n * bigIntToHex(16n); // '0x10'\n * ```\n * @param value - The `bigint` to convert to a hexadecimal string.\n * @returns The hexadecimal string, with the \"0x\"-prefix.\n * @throws If the `bigint` is not a non-negative integer.\n */\nexport const bigIntToHex = (value: bigint): string => {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(value >= 0, 'Value must be a non-negative bigint.');\n\n return add0x(value.toString(16));\n};\n\n/**\n * Convert a hexadecimal string to a number. This verifies that the string is a\n * valid hex string, and that the resulting number is a safe integer. Both\n * \"0x\"-prefixed and unprefixed strings are supported.\n *\n * To convert a hexadecimal string to a `bigint` instead, use\n * {@link hexToBigInt}.\n *\n * @example\n * ```typescript\n * hexToNumber('0x0'); // 0\n * hexToNumber('0x1'); // 1\n * hexToNumber('0x10'); // 16\n * ```\n * @param value - The hexadecimal string to convert to a number.\n * @returns The number.\n * @throws If the value is not a valid hexadecimal string, or if the resulting\n * number is not a safe integer.\n */\nexport const hexToNumber = (value: string): number => {\n assertIsHexString(value);\n\n // `parseInt` accepts values without the \"0x\"-prefix, whereas `Number` does\n // not. Using this is slightly faster than `Number(add0x(value))`.\n const numberValue = parseInt(value, 16);\n\n assert(\n Number.isSafeInteger(numberValue),\n 'Value is not a safe integer. Use `hexToBigInt` instead.',\n );\n\n return numberValue;\n};\n\n/**\n * Convert a hexadecimal string to a `bigint`. This verifies that the string is\n * a valid hex string. Both \"0x\"-prefixed and unprefixed strings are supported.\n *\n * To convert a hexadecimal string to a number instead, use {@link hexToNumber}.\n *\n * @example\n * ```typescript\n * hexToBigInt('0x0'); // 0n\n * hexToBigInt('0x1'); // 1n\n * hexToBigInt('0x10'); // 16n\n * ```\n * @param value - The hexadecimal string to convert to a `bigint`.\n * @returns The `bigint`.\n * @throws If the value is not a valid hexadecimal string.\n */\nexport const hexToBigInt = (value: string): bigint => {\n assertIsHexString(value);\n\n // The `BigInt` constructor requires the \"0x\"-prefix to parse a hex string.\n return BigInt(add0x(value));\n};\n"]}
|
package/dist/opaque.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"opaque.cjs","sourceRoot":"","sources":["../src/opaque.ts"],"names":[],"mappings":"","sourcesContent":["// We use a symbol property name instead of { _type: Brand }, because that would show up in IDE suggestions,\n// while internal symbols do not.\ndeclare const brand: unique symbol;\nexport type Opaque<Base, Brand extends symbol> = Base & { [brand]: Brand };\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"opaque.d.cts","sourceRoot":"","sources":["../src/opaque.ts"],"names":[],"mappings":"AAEA,OAAO,CAAC,MAAM,KAAK,EAAE,OAAO,MAAM,CAAC;AACnC,MAAM,MAAM,MAAM,CAAC,IAAI,EAAE,KAAK,SAAS,MAAM,IAAI,IAAI,GAAG;IAAE,CAAC,KAAK,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"opaque.d.mts","sourceRoot":"","sources":["../src/opaque.ts"],"names":[],"mappings":"AAEA,OAAO,CAAC,MAAM,KAAK,EAAE,OAAO,MAAM,CAAC;AACnC,MAAM,MAAM,MAAM,CAAC,IAAI,EAAE,KAAK,SAAS,MAAM,IAAI,IAAI,GAAG;IAAE,CAAC,KAAK,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC"}
|
package/dist/opaque.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
export {};
|
|
2
2
|
//# sourceMappingURL=opaque.mjs.map
|
package/dist/opaque.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"file":"opaque.mjs","sourceRoot":"","sources":["../src/opaque.ts"],"names":[],"mappings":"","sourcesContent":["// We use a symbol property name instead of { _type: Brand }, because that would show up in IDE suggestions,\n// while internal symbols do not.\ndeclare const brand: unique symbol;\nexport type Opaque<Base, Brand extends symbol> = Base & { [brand]: Brand };\n"]}
|
package/dist/promise.cjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDeferredPromise = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Create a defered Promise.
|
|
6
|
+
*
|
|
7
|
+
* If the Promise is rejected prior to a handler being added, this can result in an
|
|
8
|
+
* `UnhandledPromiseRejection` error. Optionally this can be suppressed with the
|
|
9
|
+
* `suppressUnhandledRejection` flag, as it's common to belatedly handle deferred Promises, or to
|
|
10
|
+
* ignore them if they're no longer relevant (e.g. related to a cancelled request).
|
|
11
|
+
*
|
|
12
|
+
* However, be very careful that you have handled the Promise if you do this. Suppressing these
|
|
13
|
+
* errors is dangerous, they exist for good reason. An unhandled rejection can hide errors, making
|
|
14
|
+
* debugging extremely difficult. They should only be suppressed if you're confident that the
|
|
15
|
+
* Promise is always handled correctly, in both the success and failure cases.
|
|
16
|
+
*
|
|
17
|
+
* @param args - The arguments.
|
|
18
|
+
* @param args.suppressUnhandledRejection - This option adds an empty error handler
|
|
19
|
+
* to the Promise to suppress the UnhandledPromiseRejection error. This can be
|
|
20
|
+
* useful if the deferred Promise is sometimes intentionally not used.
|
|
21
|
+
* @returns A deferred Promise.
|
|
22
|
+
* @template Result - The result type of the Promise.
|
|
23
|
+
*/
|
|
24
|
+
function createDeferredPromise({ suppressUnhandledRejection = false, } = {}) {
|
|
25
|
+
let resolve;
|
|
26
|
+
let reject;
|
|
27
|
+
const promise = new Promise((innerResolve, innerReject) => {
|
|
28
|
+
resolve = innerResolve;
|
|
29
|
+
reject = innerReject;
|
|
30
|
+
});
|
|
31
|
+
if (suppressUnhandledRejection) {
|
|
32
|
+
promise.catch((_error) => {
|
|
33
|
+
// This handler is used to suppress the UnhandledPromiseRejection error
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
// @ts-expect-error We know that these are assigned, but TypeScript doesn't
|
|
37
|
+
return { promise, resolve, reject };
|
|
38
|
+
}
|
|
39
|
+
exports.createDeferredPromise = createDeferredPromise;
|
|
40
|
+
//# sourceMappingURL=promise.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promise.cjs","sourceRoot":"","sources":["../src/promise.ts"],"names":[],"mappings":";;;AAsBA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,qBAAqB,CAAgB,EACnD,0BAA0B,GAAG,KAAK,MAGhC,EAAE;IACJ,IAAI,OAA2C,CAAC;IAChD,IAAI,MAAyC,CAAC;IAC9C,MAAM,OAAO,GAAG,IAAI,OAAO,CACzB,CACE,YAAgD,EAChD,WAA8C,EAC9C,EAAE;QACF,OAAO,GAAG,YAAY,CAAC;QACvB,MAAM,GAAG,WAAW,CAAC;IACvB,CAAC,CACF,CAAC;IAEF,IAAI,0BAA0B,EAAE;QAC9B,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE;YACvB,uEAAuE;QACzE,CAAC,CAAC,CAAC;KACJ;IAED,2EAA2E;IAC3E,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AACtC,CAAC;AAzBD,sDAyBC","sourcesContent":["/**\n * A deferred Promise.\n *\n * A deferred Promise is one that can be resolved or rejected independently of\n * the Promise construction.\n * @template Result - The result type of the Promise.\n */\nexport type DeferredPromise<Result = void> = {\n /**\n * The Promise that has been deferred.\n */\n promise: Promise<Result>;\n /**\n * A function that resolves the Promise.\n */\n resolve: (result: Result) => void;\n /**\n * A function that rejects the Promise.\n */\n reject: (error: unknown) => void;\n};\n\n/**\n * Create a defered Promise.\n *\n * If the Promise is rejected prior to a handler being added, this can result in an\n * `UnhandledPromiseRejection` error. Optionally this can be suppressed with the\n * `suppressUnhandledRejection` flag, as it's common to belatedly handle deferred Promises, or to\n * ignore them if they're no longer relevant (e.g. related to a cancelled request).\n *\n * However, be very careful that you have handled the Promise if you do this. Suppressing these\n * errors is dangerous, they exist for good reason. An unhandled rejection can hide errors, making\n * debugging extremely difficult. They should only be suppressed if you're confident that the\n * Promise is always handled correctly, in both the success and failure cases.\n *\n * @param args - The arguments.\n * @param args.suppressUnhandledRejection - This option adds an empty error handler\n * to the Promise to suppress the UnhandledPromiseRejection error. This can be\n * useful if the deferred Promise is sometimes intentionally not used.\n * @returns A deferred Promise.\n * @template Result - The result type of the Promise.\n */\nexport function createDeferredPromise<Result = void>({\n suppressUnhandledRejection = false,\n}: {\n suppressUnhandledRejection?: boolean;\n} = {}): DeferredPromise<Result> {\n let resolve: DeferredPromise<Result>['resolve'];\n let reject: DeferredPromise<Result>['reject'];\n const promise = new Promise<Result>(\n (\n innerResolve: DeferredPromise<Result>['resolve'],\n innerReject: DeferredPromise<Result>['reject'],\n ) => {\n resolve = innerResolve;\n reject = innerReject;\n },\n );\n\n if (suppressUnhandledRejection) {\n promise.catch((_error) => {\n // This handler is used to suppress the UnhandledPromiseRejection error\n });\n }\n\n // @ts-expect-error We know that these are assigned, but TypeScript doesn't\n return { promise, resolve, reject };\n}\n"]}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* the Promise construction.
|
|
6
6
|
* @template Result - The result type of the Promise.
|
|
7
7
|
*/
|
|
8
|
-
export
|
|
8
|
+
export type DeferredPromise<Result = void> = {
|
|
9
9
|
/**
|
|
10
10
|
* The Promise that has been deferred.
|
|
11
11
|
*/
|
|
@@ -42,4 +42,4 @@ export declare type DeferredPromise<Result = void> = {
|
|
|
42
42
|
export declare function createDeferredPromise<Result = void>({ suppressUnhandledRejection, }?: {
|
|
43
43
|
suppressUnhandledRejection?: boolean;
|
|
44
44
|
}): DeferredPromise<Result>;
|
|
45
|
-
//# sourceMappingURL=promise.d.
|
|
45
|
+
//# sourceMappingURL=promise.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promise.d.cts","sourceRoot":"","sources":["../src/promise.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,CAAC,MAAM,GAAG,IAAI,IAAI;IAC3C;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IACzB;;OAEG;IACH,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC;;OAEG;IACH,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CAClC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,GAAG,IAAI,EAAE,EACnD,0BAAkC,GACnC,GAAE;IACD,0BAA0B,CAAC,EAAE,OAAO,CAAC;CACjC,GAAG,eAAe,CAAC,MAAM,CAAC,CAqB/B"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A deferred Promise.
|
|
3
|
+
*
|
|
4
|
+
* A deferred Promise is one that can be resolved or rejected independently of
|
|
5
|
+
* the Promise construction.
|
|
6
|
+
* @template Result - The result type of the Promise.
|
|
7
|
+
*/
|
|
8
|
+
export type DeferredPromise<Result = void> = {
|
|
9
|
+
/**
|
|
10
|
+
* The Promise that has been deferred.
|
|
11
|
+
*/
|
|
12
|
+
promise: Promise<Result>;
|
|
13
|
+
/**
|
|
14
|
+
* A function that resolves the Promise.
|
|
15
|
+
*/
|
|
16
|
+
resolve: (result: Result) => void;
|
|
17
|
+
/**
|
|
18
|
+
* A function that rejects the Promise.
|
|
19
|
+
*/
|
|
20
|
+
reject: (error: unknown) => void;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Create a defered Promise.
|
|
24
|
+
*
|
|
25
|
+
* If the Promise is rejected prior to a handler being added, this can result in an
|
|
26
|
+
* `UnhandledPromiseRejection` error. Optionally this can be suppressed with the
|
|
27
|
+
* `suppressUnhandledRejection` flag, as it's common to belatedly handle deferred Promises, or to
|
|
28
|
+
* ignore them if they're no longer relevant (e.g. related to a cancelled request).
|
|
29
|
+
*
|
|
30
|
+
* However, be very careful that you have handled the Promise if you do this. Suppressing these
|
|
31
|
+
* errors is dangerous, they exist for good reason. An unhandled rejection can hide errors, making
|
|
32
|
+
* debugging extremely difficult. They should only be suppressed if you're confident that the
|
|
33
|
+
* Promise is always handled correctly, in both the success and failure cases.
|
|
34
|
+
*
|
|
35
|
+
* @param args - The arguments.
|
|
36
|
+
* @param args.suppressUnhandledRejection - This option adds an empty error handler
|
|
37
|
+
* to the Promise to suppress the UnhandledPromiseRejection error. This can be
|
|
38
|
+
* useful if the deferred Promise is sometimes intentionally not used.
|
|
39
|
+
* @returns A deferred Promise.
|
|
40
|
+
* @template Result - The result type of the Promise.
|
|
41
|
+
*/
|
|
42
|
+
export declare function createDeferredPromise<Result = void>({ suppressUnhandledRejection, }?: {
|
|
43
|
+
suppressUnhandledRejection?: boolean;
|
|
44
|
+
}): DeferredPromise<Result>;
|
|
45
|
+
//# sourceMappingURL=promise.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promise.d.mts","sourceRoot":"","sources":["../src/promise.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,CAAC,MAAM,GAAG,IAAI,IAAI;IAC3C;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IACzB;;OAEG;IACH,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC;;OAEG;IACH,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CAClC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,GAAG,IAAI,EAAE,EACnD,0BAAkC,GACnC,GAAE;IACD,0BAA0B,CAAC,EAAE,OAAO,CAAC;CACjC,GAAG,eAAe,CAAC,MAAM,CAAC,CAqB/B"}
|
package/dist/promise.mjs
CHANGED
|
@@ -1,8 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Create a defered Promise.
|
|
3
|
+
*
|
|
4
|
+
* If the Promise is rejected prior to a handler being added, this can result in an
|
|
5
|
+
* `UnhandledPromiseRejection` error. Optionally this can be suppressed with the
|
|
6
|
+
* `suppressUnhandledRejection` flag, as it's common to belatedly handle deferred Promises, or to
|
|
7
|
+
* ignore them if they're no longer relevant (e.g. related to a cancelled request).
|
|
8
|
+
*
|
|
9
|
+
* However, be very careful that you have handled the Promise if you do this. Suppressing these
|
|
10
|
+
* errors is dangerous, they exist for good reason. An unhandled rejection can hide errors, making
|
|
11
|
+
* debugging extremely difficult. They should only be suppressed if you're confident that the
|
|
12
|
+
* Promise is always handled correctly, in both the success and failure cases.
|
|
13
|
+
*
|
|
14
|
+
* @param args - The arguments.
|
|
15
|
+
* @param args.suppressUnhandledRejection - This option adds an empty error handler
|
|
16
|
+
* to the Promise to suppress the UnhandledPromiseRejection error. This can be
|
|
17
|
+
* useful if the deferred Promise is sometimes intentionally not used.
|
|
18
|
+
* @returns A deferred Promise.
|
|
19
|
+
* @template Result - The result type of the Promise.
|
|
20
|
+
*/
|
|
21
|
+
export function createDeferredPromise({ suppressUnhandledRejection = false, } = {}) {
|
|
22
|
+
let resolve;
|
|
23
|
+
let reject;
|
|
24
|
+
const promise = new Promise((innerResolve, innerReject) => {
|
|
25
|
+
resolve = innerResolve;
|
|
26
|
+
reject = innerReject;
|
|
27
|
+
});
|
|
28
|
+
if (suppressUnhandledRejection) {
|
|
29
|
+
promise.catch((_error) => {
|
|
30
|
+
// This handler is used to suppress the UnhandledPromiseRejection error
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
// @ts-expect-error We know that these are assigned, but TypeScript doesn't
|
|
34
|
+
return { promise, resolve, reject };
|
|
35
|
+
}
|
|
8
36
|
//# sourceMappingURL=promise.mjs.map
|
package/dist/promise.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"
|
|
1
|
+
{"version":3,"file":"promise.mjs","sourceRoot":"","sources":["../src/promise.ts"],"names":[],"mappings":"AAsBA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,qBAAqB,CAAgB,EACnD,0BAA0B,GAAG,KAAK,MAGhC,EAAE;IACJ,IAAI,OAA2C,CAAC;IAChD,IAAI,MAAyC,CAAC;IAC9C,MAAM,OAAO,GAAG,IAAI,OAAO,CACzB,CACE,YAAgD,EAChD,WAA8C,EAC9C,EAAE;QACF,OAAO,GAAG,YAAY,CAAC;QACvB,MAAM,GAAG,WAAW,CAAC;IACvB,CAAC,CACF,CAAC;IAEF,IAAI,0BAA0B,EAAE;QAC9B,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE;YACvB,uEAAuE;QACzE,CAAC,CAAC,CAAC;KACJ;IAED,2EAA2E;IAC3E,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AACtC,CAAC","sourcesContent":["/**\n * A deferred Promise.\n *\n * A deferred Promise is one that can be resolved or rejected independently of\n * the Promise construction.\n * @template Result - The result type of the Promise.\n */\nexport type DeferredPromise<Result = void> = {\n /**\n * The Promise that has been deferred.\n */\n promise: Promise<Result>;\n /**\n * A function that resolves the Promise.\n */\n resolve: (result: Result) => void;\n /**\n * A function that rejects the Promise.\n */\n reject: (error: unknown) => void;\n};\n\n/**\n * Create a defered Promise.\n *\n * If the Promise is rejected prior to a handler being added, this can result in an\n * `UnhandledPromiseRejection` error. Optionally this can be suppressed with the\n * `suppressUnhandledRejection` flag, as it's common to belatedly handle deferred Promises, or to\n * ignore them if they're no longer relevant (e.g. related to a cancelled request).\n *\n * However, be very careful that you have handled the Promise if you do this. Suppressing these\n * errors is dangerous, they exist for good reason. An unhandled rejection can hide errors, making\n * debugging extremely difficult. They should only be suppressed if you're confident that the\n * Promise is always handled correctly, in both the success and failure cases.\n *\n * @param args - The arguments.\n * @param args.suppressUnhandledRejection - This option adds an empty error handler\n * to the Promise to suppress the UnhandledPromiseRejection error. This can be\n * useful if the deferred Promise is sometimes intentionally not used.\n * @returns A deferred Promise.\n * @template Result - The result type of the Promise.\n */\nexport function createDeferredPromise<Result = void>({\n suppressUnhandledRejection = false,\n}: {\n suppressUnhandledRejection?: boolean;\n} = {}): DeferredPromise<Result> {\n let resolve: DeferredPromise<Result>['resolve'];\n let reject: DeferredPromise<Result>['reject'];\n const promise = new Promise<Result>(\n (\n innerResolve: DeferredPromise<Result>['resolve'],\n innerReject: DeferredPromise<Result>['reject'],\n ) => {\n resolve = innerResolve;\n reject = innerReject;\n },\n );\n\n if (suppressUnhandledRejection) {\n promise.catch((_error) => {\n // This handler is used to suppress the UnhandledPromiseRejection error\n });\n }\n\n // @ts-expect-error We know that these are assigned, but TypeScript doesn't\n return { promise, resolve, reject };\n}\n"]}
|
package/dist/time.cjs
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.timeSince = exports.inMilliseconds = exports.Duration = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Common duration constants, in milliseconds.
|
|
6
|
+
*/
|
|
7
|
+
var Duration;
|
|
8
|
+
(function (Duration) {
|
|
9
|
+
/**
|
|
10
|
+
* A millisecond.
|
|
11
|
+
*/
|
|
12
|
+
Duration[Duration["Millisecond"] = 1] = "Millisecond";
|
|
13
|
+
/**
|
|
14
|
+
* A second, in milliseconds.
|
|
15
|
+
*/
|
|
16
|
+
Duration[Duration["Second"] = 1000] = "Second";
|
|
17
|
+
/**
|
|
18
|
+
* A minute, in milliseconds.
|
|
19
|
+
*/
|
|
20
|
+
Duration[Duration["Minute"] = 60000] = "Minute";
|
|
21
|
+
/**
|
|
22
|
+
* An hour, in milliseconds.
|
|
23
|
+
*/
|
|
24
|
+
Duration[Duration["Hour"] = 3600000] = "Hour";
|
|
25
|
+
/**
|
|
26
|
+
* A day, in milliseconds.
|
|
27
|
+
*/
|
|
28
|
+
Duration[Duration["Day"] = 86400000] = "Day";
|
|
29
|
+
/**
|
|
30
|
+
* A week, in milliseconds.
|
|
31
|
+
*/
|
|
32
|
+
Duration[Duration["Week"] = 604800000] = "Week";
|
|
33
|
+
/**
|
|
34
|
+
* A year, in milliseconds.
|
|
35
|
+
*/
|
|
36
|
+
Duration[Duration["Year"] = 31536000000] = "Year";
|
|
37
|
+
})(Duration = exports.Duration || (exports.Duration = {}));
|
|
38
|
+
const isNonNegativeInteger = (number) => Number.isInteger(number) && number >= 0;
|
|
39
|
+
const assertIsNonNegativeInteger = (number, name) => {
|
|
40
|
+
if (!isNonNegativeInteger(number)) {
|
|
41
|
+
throw new Error(`"${name}" must be a non-negative integer. Received: "${number}".`);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Calculates the millisecond value of the specified number of units of time.
|
|
46
|
+
*
|
|
47
|
+
* @param count - The number of units of time.
|
|
48
|
+
* @param duration - The unit of time to count.
|
|
49
|
+
* @returns The count multiplied by the specified duration.
|
|
50
|
+
*/
|
|
51
|
+
function inMilliseconds(count, duration) {
|
|
52
|
+
assertIsNonNegativeInteger(count, 'count');
|
|
53
|
+
return count * duration;
|
|
54
|
+
}
|
|
55
|
+
exports.inMilliseconds = inMilliseconds;
|
|
56
|
+
/**
|
|
57
|
+
* Gets the milliseconds since a particular Unix epoch timestamp.
|
|
58
|
+
*
|
|
59
|
+
* @param timestamp - A Unix millisecond timestamp.
|
|
60
|
+
* @returns The number of milliseconds elapsed since the specified timestamp.
|
|
61
|
+
*/
|
|
62
|
+
function timeSince(timestamp) {
|
|
63
|
+
assertIsNonNegativeInteger(timestamp, 'timestamp');
|
|
64
|
+
return Date.now() - timestamp;
|
|
65
|
+
}
|
|
66
|
+
exports.timeSince = timeSince;
|
|
67
|
+
//# sourceMappingURL=time.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"time.cjs","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,IAAY,QAmCX;AAnCD,WAAY,QAAQ;IAClB;;OAEG;IACH,qDAAe,CAAA;IAEf;;OAEG;IACH,8CAAa,CAAA;IAEb;;OAEG;IACH,+CAAe,CAAA;IAEf;;OAEG;IACH,6CAAgB,CAAA;IAEhB;;OAEG;IACH,4CAAgB,CAAA;IAEhB;;OAEG;IACH,+CAAkB,CAAA;IAElB;;OAEG;IACH,iDAAqB,CAAA;AACvB,CAAC,EAnCW,QAAQ,GAAR,gBAAQ,KAAR,gBAAQ,QAmCnB;AAED,MAAM,oBAAoB,GAAG,CAAC,MAAc,EAAE,EAAE,CAC9C,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,CAAC;AAE1C,MAAM,0BAA0B,GAAG,CAAC,MAAc,EAAE,IAAY,EAAE,EAAE;IAClE,IAAI,CAAC,oBAAoB,CAAC,MAAM,CAAC,EAAE;QACjC,MAAM,IAAI,KAAK,CACb,IAAI,IAAI,gDAAgD,MAAM,IAAI,CACnE,CAAC;KACH;AACH,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,SAAgB,cAAc,CAAC,KAAa,EAAE,QAAkB;IAC9D,0BAA0B,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC3C,OAAO,KAAK,GAAG,QAAQ,CAAC;AAC1B,CAAC;AAHD,wCAGC;AAED;;;;;GAKG;AACH,SAAgB,SAAS,CAAC,SAAiB;IACzC,0BAA0B,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;IACnD,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC;AAChC,CAAC;AAHD,8BAGC","sourcesContent":["/**\n * Common duration constants, in milliseconds.\n */\nexport enum Duration {\n /**\n * A millisecond.\n */\n Millisecond = 1,\n\n /**\n * A second, in milliseconds.\n */\n Second = 1000, // Millisecond * 1000\n\n /**\n * A minute, in milliseconds.\n */\n Minute = 60_000, // Second * 60\n\n /**\n * An hour, in milliseconds.\n */\n Hour = 3_600_000, // Minute * 60\n\n /**\n * A day, in milliseconds.\n */\n Day = 86_400_000, // Hour * 24\n\n /**\n * A week, in milliseconds.\n */\n Week = 604_800_000, // Day * 7\n\n /**\n * A year, in milliseconds.\n */\n Year = 31_536_000_000, // Day * 365\n}\n\nconst isNonNegativeInteger = (number: number) =>\n Number.isInteger(number) && number >= 0;\n\nconst assertIsNonNegativeInteger = (number: number, name: string) => {\n if (!isNonNegativeInteger(number)) {\n throw new Error(\n `\"${name}\" must be a non-negative integer. Received: \"${number}\".`,\n );\n }\n};\n\n/**\n * Calculates the millisecond value of the specified number of units of time.\n *\n * @param count - The number of units of time.\n * @param duration - The unit of time to count.\n * @returns The count multiplied by the specified duration.\n */\nexport function inMilliseconds(count: number, duration: Duration): number {\n assertIsNonNegativeInteger(count, 'count');\n return count * duration;\n}\n\n/**\n * Gets the milliseconds since a particular Unix epoch timestamp.\n *\n * @param timestamp - A Unix millisecond timestamp.\n * @returns The number of milliseconds elapsed since the specified timestamp.\n */\nexport function timeSince(timestamp: number): number {\n assertIsNonNegativeInteger(timestamp, 'timestamp');\n return Date.now() - timestamp;\n}\n"]}
|
|
@@ -46,4 +46,4 @@ export declare function inMilliseconds(count: number, duration: Duration): numbe
|
|
|
46
46
|
* @returns The number of milliseconds elapsed since the specified timestamp.
|
|
47
47
|
*/
|
|
48
48
|
export declare function timeSince(timestamp: number): number;
|
|
49
|
-
//# sourceMappingURL=time.d.
|
|
49
|
+
//# sourceMappingURL=time.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"time.d.cts","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,QAAQ;IAClB;;OAEG;IACH,WAAW,IAAI;IAEf;;OAEG;IACH,MAAM,OAAO;IAEb;;OAEG;IACH,MAAM,QAAS;IAEf;;OAEG;IACH,IAAI,UAAY;IAEhB;;OAEG;IACH,GAAG,WAAa;IAEhB;;OAEG;IACH,IAAI,YAAc;IAElB;;OAEG;IACH,IAAI,cAAiB;CACtB;AAaD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,MAAM,CAGxE;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAGnD"}
|