@metamask-previews/utils 11.12.1-preview-9962b7e

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.
Files changed (112) hide show
  1. package/CHANGELOG.md +584 -0
  2. package/LICENSE +15 -0
  3. package/README.md +106 -0
  4. package/dist/assert.d.ts +61 -0
  5. package/dist/assert.d.ts.map +1 -0
  6. package/dist/assert.js +115 -0
  7. package/dist/assert.js.map +1 -0
  8. package/dist/base64.d.ts +25 -0
  9. package/dist/base64.d.ts.map +1 -0
  10. package/dist/base64.js +30 -0
  11. package/dist/base64.js.map +1 -0
  12. package/dist/bytes.d.ts +198 -0
  13. package/dist/bytes.d.ts.map +1 -0
  14. package/dist/bytes.js +406 -0
  15. package/dist/bytes.js.map +1 -0
  16. package/dist/caip-types.d.ts +294 -0
  17. package/dist/caip-types.d.ts.map +1 -0
  18. package/dist/caip-types.js +369 -0
  19. package/dist/caip-types.js.map +1 -0
  20. package/dist/checksum.d.ts +2 -0
  21. package/dist/checksum.d.ts.map +1 -0
  22. package/dist/checksum.js +4 -0
  23. package/dist/checksum.js.map +1 -0
  24. package/dist/coercers.d.ts +97 -0
  25. package/dist/coercers.d.ts.map +1 -0
  26. package/dist/coercers.js +159 -0
  27. package/dist/coercers.js.map +1 -0
  28. package/dist/collections.d.ts +39 -0
  29. package/dist/collections.d.ts.map +1 -0
  30. package/dist/collections.js +105 -0
  31. package/dist/collections.js.map +1 -0
  32. package/dist/encryption-types.d.ts +7 -0
  33. package/dist/encryption-types.d.ts.map +1 -0
  34. package/dist/encryption-types.js +2 -0
  35. package/dist/encryption-types.js.map +1 -0
  36. package/dist/errors.d.ts +68 -0
  37. package/dist/errors.d.ts.map +1 -0
  38. package/dist/errors.js +121 -0
  39. package/dist/errors.js.map +1 -0
  40. package/dist/fs.d.ts +133 -0
  41. package/dist/fs.d.ts.map +1 -0
  42. package/dist/fs.js +210 -0
  43. package/dist/fs.js.map +1 -0
  44. package/dist/hashing.d.ts +28 -0
  45. package/dist/hashing.d.ts.map +1 -0
  46. package/dist/hashing.js +59 -0
  47. package/dist/hashing.js.map +1 -0
  48. package/dist/hex.d.ts +117 -0
  49. package/dist/hex.d.ts.map +1 -0
  50. package/dist/hex.js +174 -0
  51. package/dist/hex.js.map +1 -0
  52. package/dist/index.d.ts +26 -0
  53. package/dist/index.d.ts.map +1 -0
  54. package/dist/index.js +21 -0
  55. package/dist/index.js.map +1 -0
  56. package/dist/json.d.ts +398 -0
  57. package/dist/json.d.ts.map +1 -0
  58. package/dist/json.js +402 -0
  59. package/dist/json.js.map +1 -0
  60. package/dist/keyring.d.ts +243 -0
  61. package/dist/keyring.d.ts.map +1 -0
  62. package/dist/keyring.js +2 -0
  63. package/dist/keyring.js.map +1 -0
  64. package/dist/logging.d.ts +30 -0
  65. package/dist/logging.d.ts.map +1 -0
  66. package/dist/logging.js +35 -0
  67. package/dist/logging.js.map +1 -0
  68. package/dist/misc.d.ts +127 -0
  69. package/dist/misc.d.ts.map +1 -0
  70. package/dist/misc.js +142 -0
  71. package/dist/misc.js.map +1 -0
  72. package/dist/mnemonic.d.ts +14 -0
  73. package/dist/mnemonic.d.ts.map +1 -0
  74. package/dist/mnemonic.js +25 -0
  75. package/dist/mnemonic.js.map +1 -0
  76. package/dist/node.d.ts +3 -0
  77. package/dist/node.d.ts.map +1 -0
  78. package/dist/node.js +3 -0
  79. package/dist/node.js.map +1 -0
  80. package/dist/number.d.ts +74 -0
  81. package/dist/number.d.ts.map +1 -0
  82. package/dist/number.js +95 -0
  83. package/dist/number.js.map +1 -0
  84. package/dist/opaque.d.ts +6 -0
  85. package/dist/opaque.d.ts.map +1 -0
  86. package/dist/opaque.js +2 -0
  87. package/dist/opaque.js.map +1 -0
  88. package/dist/promise.d.ts +45 -0
  89. package/dist/promise.d.ts.map +1 -0
  90. package/dist/promise.js +40 -0
  91. package/dist/promise.js.map +1 -0
  92. package/dist/superstruct.d.ts +20 -0
  93. package/dist/superstruct.d.ts.map +1 -0
  94. package/dist/superstruct.js +24 -0
  95. package/dist/superstruct.js.map +1 -0
  96. package/dist/time.d.ts +49 -0
  97. package/dist/time.d.ts.map +1 -0
  98. package/dist/time.js +62 -0
  99. package/dist/time.js.map +1 -0
  100. package/dist/transaction-types.d.ts +117 -0
  101. package/dist/transaction-types.d.ts.map +1 -0
  102. package/dist/transaction-types.js +2 -0
  103. package/dist/transaction-types.js.map +1 -0
  104. package/dist/unitsConversion.d.ts +80 -0
  105. package/dist/unitsConversion.d.ts.map +1 -0
  106. package/dist/unitsConversion.js +209 -0
  107. package/dist/unitsConversion.js.map +1 -0
  108. package/dist/versions.d.ts +101 -0
  109. package/dist/versions.d.ts.map +1 -0
  110. package/dist/versions.js +85 -0
  111. package/dist/versions.js.map +1 -0
  112. package/package.json +122 -0
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Transform a typed array containing mnemonic data to the seed phrase.
3
+ * @param uint8Array - Typed array containing mnemonic data.
4
+ * @returns The seed phrase.
5
+ */
6
+ export declare function uint8ArrayToMnemonic(uint8Array: Uint8Array): string;
7
+ /**
8
+ * Encodes a BIP-39 mnemonic as the indices of words in the English BIP-39 wordlist.
9
+ *
10
+ * @param mnemonic - The BIP-39 mnemonic.
11
+ * @returns The Unicode code points for the seed phrase formed from the words in the wordlist.
12
+ */
13
+ export declare function convertMnemonicToWordlistIndices(mnemonic: string): Uint8Array;
14
+ //# sourceMappingURL=mnemonic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mnemonic.d.ts","sourceRoot":"","sources":["../src/mnemonic.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAYnE;AAED;;;;;GAKG;AACH,wBAAgB,gCAAgC,CAAC,QAAQ,EAAE,MAAM,GAAG,UAAU,CAG7E"}
@@ -0,0 +1,25 @@
1
+ import * as englishWordlist from '@metamask/scure-bip39/dist/wordlists/english.js';
2
+ const { wordlist } = englishWordlist;
3
+ /**
4
+ * Transform a typed array containing mnemonic data to the seed phrase.
5
+ * @param uint8Array - Typed array containing mnemonic data.
6
+ * @returns The seed phrase.
7
+ */
8
+ export function uint8ArrayToMnemonic(uint8Array) {
9
+ if (uint8Array.length === 0) {
10
+ throw new Error('The method uint8ArrayToMnemonic expects a non-empty array');
11
+ }
12
+ const recoveredIndices = Array.from(new Uint16Array(new Uint8Array(uint8Array).buffer));
13
+ return recoveredIndices.map((i) => wordlist[i]).join(' ');
14
+ }
15
+ /**
16
+ * Encodes a BIP-39 mnemonic as the indices of words in the English BIP-39 wordlist.
17
+ *
18
+ * @param mnemonic - The BIP-39 mnemonic.
19
+ * @returns The Unicode code points for the seed phrase formed from the words in the wordlist.
20
+ */
21
+ export function convertMnemonicToWordlistIndices(mnemonic) {
22
+ const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word));
23
+ return new Uint8Array(new Uint16Array(indices).buffer);
24
+ }
25
+ //# sourceMappingURL=mnemonic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mnemonic.js","sourceRoot":"","sources":["../src/mnemonic.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,eAAe,MAAM,iDAAiD,CAAC;AAEnF,MAAM,EAAE,QAAQ,EAAE,GAAG,eAAe,CAAC;AAErC;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,UAAsB;IACzD,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CACb,2DAA2D,CAC5D,CAAC;IACJ,CAAC;IAED,MAAM,gBAAgB,GAAG,KAAK,CAAC,IAAI,CACjC,IAAI,WAAW,CAAC,IAAI,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,CACnD,CAAC;IAEF,OAAO,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC5D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gCAAgC,CAAC,QAAgB;IAC/D,MAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IAC1E,OAAO,IAAI,UAAU,CAAC,IAAI,WAAW,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC;AACzD,CAAC","sourcesContent":["import * as englishWordlist from '@metamask/scure-bip39/dist/wordlists/english.js';\n\nconst { wordlist } = englishWordlist;\n\n/**\n * Transform a typed array containing mnemonic data to the seed phrase.\n * @param uint8Array - Typed array containing mnemonic data.\n * @returns The seed phrase.\n */\nexport function uint8ArrayToMnemonic(uint8Array: Uint8Array): string {\n if (uint8Array.length === 0) {\n throw new Error(\n 'The method uint8ArrayToMnemonic expects a non-empty array',\n );\n }\n\n const recoveredIndices = Array.from(\n new Uint16Array(new Uint8Array(uint8Array).buffer),\n );\n\n return recoveredIndices.map((i) => wordlist[i]).join(' ');\n}\n\n/**\n * Encodes a BIP-39 mnemonic as the indices of words in the English BIP-39 wordlist.\n *\n * @param mnemonic - The BIP-39 mnemonic.\n * @returns The Unicode code points for the seed phrase formed from the words in the wordlist.\n */\nexport function convertMnemonicToWordlistIndices(mnemonic: string): Uint8Array {\n const indices = mnemonic.split(' ').map((word) => wordlist.indexOf(word));\n return new Uint8Array(new Uint16Array(indices).buffer);\n}\n"]}
package/dist/node.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './index.js';
2
+ export * from './fs.js';
3
+ //# sourceMappingURL=node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC"}
package/dist/node.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './index.js';
2
+ export * from './fs.js';
3
+ //# sourceMappingURL=node.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC","sourcesContent":["export * from './index.js';\nexport * from './fs.js';\n"]}
@@ -0,0 +1,74 @@
1
+ import type { Hex } from './hex.js';
2
+ /**
3
+ * Convert a number to a hexadecimal string. This verifies that the number is a
4
+ * non-negative safe integer.
5
+ *
6
+ * To convert a `bigint` to a hexadecimal string instead, use
7
+ * {@link bigIntToHex}.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * numberToHex(0); // '0x0'
12
+ * numberToHex(1); // '0x1'
13
+ * numberToHex(16); // '0x10'
14
+ * ```
15
+ * @param value - The number to convert to a hexadecimal string.
16
+ * @returns The hexadecimal string, with the "0x"-prefix.
17
+ * @throws If the number is not a non-negative safe integer.
18
+ */
19
+ export declare const numberToHex: (value: number) => Hex;
20
+ /**
21
+ * Convert a `bigint` to a hexadecimal string. This verifies that the `bigint`
22
+ * is a non-negative integer.
23
+ *
24
+ * To convert a number to a hexadecimal string instead, use {@link numberToHex}.
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * bigIntToHex(0n); // '0x0'
29
+ * bigIntToHex(1n); // '0x1'
30
+ * bigIntToHex(16n); // '0x10'
31
+ * ```
32
+ * @param value - The `bigint` to convert to a hexadecimal string.
33
+ * @returns The hexadecimal string, with the "0x"-prefix.
34
+ * @throws If the `bigint` is not a non-negative integer.
35
+ */
36
+ export declare const bigIntToHex: (value: bigint) => Hex;
37
+ /**
38
+ * Convert a hexadecimal string to a number. This verifies that the string is a
39
+ * valid hex string, and that the resulting number is a safe integer. Both
40
+ * "0x"-prefixed and unprefixed strings are supported.
41
+ *
42
+ * To convert a hexadecimal string to a `bigint` instead, use
43
+ * {@link hexToBigInt}.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * hexToNumber('0x0'); // 0
48
+ * hexToNumber('0x1'); // 1
49
+ * hexToNumber('0x10'); // 16
50
+ * ```
51
+ * @param value - The hexadecimal string to convert to a number.
52
+ * @returns The number.
53
+ * @throws If the value is not a valid hexadecimal string, or if the resulting
54
+ * number is not a safe integer.
55
+ */
56
+ export declare const hexToNumber: (value: string) => number;
57
+ /**
58
+ * Convert a hexadecimal string to a `bigint`. This verifies that the string is
59
+ * a valid hex string. Both "0x"-prefixed and unprefixed strings are supported.
60
+ *
61
+ * To convert a hexadecimal string to a number instead, use {@link hexToNumber}.
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * hexToBigInt('0x0'); // 0n
66
+ * hexToBigInt('0x1'); // 1n
67
+ * hexToBigInt('0x10'); // 16n
68
+ * ```
69
+ * @param value - The hexadecimal string to convert to a `bigint`.
70
+ * @returns The `bigint`.
71
+ * @throws If the value is not a valid hexadecimal string.
72
+ */
73
+ export declare const hexToBigInt: (value: string) => bigint;
74
+ //# sourceMappingURL=number.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../src/number.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAGpC;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,KAAG,GAS3C,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,KAAG,GAK3C,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.js ADDED
@@ -0,0 +1,95 @@
1
+ import { assert } from './assert.js';
2
+ import { add0x, assertIsHexString } from './hex.js';
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));
94
+ };
95
+ //# sourceMappingURL=number.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.js","sourceRoot":"","sources":["../src/number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAEpD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAa,EAAO,EAAE;IAChD,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,EAAO,EAAE;IAChD,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.js';\nimport type { Hex } from './hex.js';\nimport { add0x, assertIsHexString } from './hex.js';\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): Hex => {\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): Hex => {\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"]}
@@ -0,0 +1,6 @@
1
+ declare const brand: unique symbol;
2
+ export type Opaque<Base, Brand extends symbol> = Base & {
3
+ [brand]: Brand;
4
+ };
5
+ export {};
6
+ //# sourceMappingURL=opaque.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opaque.d.ts","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.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=opaque.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opaque.js","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,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.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promise.d.ts","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,CAyB/B"}
@@ -0,0 +1,40 @@
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
+ /* eslint-disable promise/param-names -- Deliberately not named
25
+ 'resolve'/'reject' so they do not shadow the outer bindings assigned
26
+ just below. */
27
+ const promise = new Promise((innerResolve, innerReject) => {
28
+ resolve = innerResolve;
29
+ reject = innerReject;
30
+ });
31
+ /* eslint-enable promise/param-names */
32
+ if (suppressUnhandledRejection) {
33
+ promise.catch((_error) => {
34
+ // This handler is used to suppress the UnhandledPromiseRejection error
35
+ });
36
+ }
37
+ // @ts-expect-error We know that these are assigned, but TypeScript doesn't
38
+ return { promise, resolve, reject };
39
+ }
40
+ //# sourceMappingURL=promise.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promise.js","sourceRoot":"","sources":["../src/promise.ts"],"names":[],"mappings":"AAsBA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,qBAAqB,CAAgB,EACnD,0BAA0B,GAAG,KAAK,GACnC,GAEG,EAAE;IACJ,IAAI,OAA2C,CAAC;IAChD,IAAI,MAAyC,CAAC;IAC9C;;qBAEiB;IACjB,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;IACF,uCAAuC;IAEvC,IAAI,0BAA0B,EAAE,CAAC;QAC/B,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE;YACvB,uEAAuE;QACzE,CAAC,CAAC,CAAC;IACL,CAAC;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 /* eslint-disable promise/param-names -- Deliberately not named\n 'resolve'/'reject' so they do not shadow the outer bindings assigned\n just below. */\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 /* eslint-enable promise/param-names */\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"]}
@@ -0,0 +1,20 @@
1
+ import type { Struct } from '@metamask/superstruct';
2
+ /**
3
+ * Defines a new string-struct matching a regular expression.
4
+ *
5
+ * @example
6
+ * const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);
7
+ * type EthAddress = Infer<typeof EthAddressStruct>; // string
8
+ *
9
+ * const CaipChainIdStruct = defineTypedPattern<`${string}:${string}`>(
10
+ * 'CaipChainId',
11
+ * /^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/u;
12
+ * );
13
+ * type CaipChainId = Infer<typeof CaipChainIdStruct>; // `${string}:${string}`
14
+ * @param name - Type name.
15
+ * @param pattern - Regular expression to match.
16
+ * @template Pattern - The pattern type, defaults to `string`.
17
+ * @returns A new string-struct that matches the given pattern.
18
+ */
19
+ export declare function definePattern<Pattern extends string = string>(name: string, pattern: RegExp): Struct<Pattern, null>;
20
+ //# sourceMappingURL=superstruct.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"superstruct.d.ts","sourceRoot":"","sources":["../src/superstruct.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAGpD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,aAAa,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM,EAC3D,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,GACd,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,CAIvB"}
@@ -0,0 +1,24 @@
1
+ import { define } from '@metamask/superstruct';
2
+ /**
3
+ * Defines a new string-struct matching a regular expression.
4
+ *
5
+ * @example
6
+ * const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);
7
+ * type EthAddress = Infer<typeof EthAddressStruct>; // string
8
+ *
9
+ * const CaipChainIdStruct = defineTypedPattern<`${string}:${string}`>(
10
+ * 'CaipChainId',
11
+ * /^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/u;
12
+ * );
13
+ * type CaipChainId = Infer<typeof CaipChainIdStruct>; // `${string}:${string}`
14
+ * @param name - Type name.
15
+ * @param pattern - Regular expression to match.
16
+ * @template Pattern - The pattern type, defaults to `string`.
17
+ * @returns A new string-struct that matches the given pattern.
18
+ */
19
+ export function definePattern(name, pattern) {
20
+ return define(name, (value) => {
21
+ return typeof value === 'string' && pattern.test(value);
22
+ });
23
+ }
24
+ //# sourceMappingURL=superstruct.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"superstruct.js","sourceRoot":"","sources":["../src/superstruct.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAE/C;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,aAAa,CAC3B,IAAY,EACZ,OAAe;IAEf,OAAO,MAAM,CAAU,IAAI,EAAE,CAAC,KAAc,EAAoB,EAAE;QAChE,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1D,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { define } from '@metamask/superstruct';\n\n/**\n * Defines a new string-struct matching a regular expression.\n *\n * @example\n * const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);\n * type EthAddress = Infer<typeof EthAddressStruct>; // string\n *\n * const CaipChainIdStruct = defineTypedPattern<`${string}:${string}`>(\n * 'CaipChainId',\n * /^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/u;\n * );\n * type CaipChainId = Infer<typeof CaipChainIdStruct>; // `${string}:${string}`\n * @param name - Type name.\n * @param pattern - Regular expression to match.\n * @template Pattern - The pattern type, defaults to `string`.\n * @returns A new string-struct that matches the given pattern.\n */\nexport function definePattern<Pattern extends string = string>(\n name: string,\n pattern: RegExp,\n): Struct<Pattern, null> {\n return define<Pattern>(name, (value: unknown): boolean | string => {\n return typeof value === 'string' && pattern.test(value);\n });\n}\n"]}
package/dist/time.d.ts ADDED
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Common duration constants, in milliseconds.
3
+ */
4
+ export declare enum Duration {
5
+ /**
6
+ * A millisecond.
7
+ */
8
+ Millisecond = 1,
9
+ /**
10
+ * A second, in milliseconds.
11
+ */
12
+ Second = 1000,// Millisecond * 1000
13
+ /**
14
+ * A minute, in milliseconds.
15
+ */
16
+ Minute = 60000,// Second * 60
17
+ /**
18
+ * An hour, in milliseconds.
19
+ */
20
+ Hour = 3600000,// Minute * 60
21
+ /**
22
+ * A day, in milliseconds.
23
+ */
24
+ Day = 86400000,// Hour * 24
25
+ /**
26
+ * A week, in milliseconds.
27
+ */
28
+ Week = 604800000,// Day * 7
29
+ /**
30
+ * A year, in milliseconds.
31
+ */
32
+ Year = 31536000000
33
+ }
34
+ /**
35
+ * Calculates the millisecond value of the specified number of units of time.
36
+ *
37
+ * @param count - The number of units of time.
38
+ * @param duration - The unit of time to count.
39
+ * @returns The count multiplied by the specified duration.
40
+ */
41
+ export declare function inMilliseconds(count: number, duration: Duration): number;
42
+ /**
43
+ * Gets the milliseconds since a particular Unix epoch timestamp.
44
+ *
45
+ * @param timestamp - A Unix millisecond timestamp.
46
+ * @returns The number of milliseconds elapsed since the specified timestamp.
47
+ */
48
+ export declare function timeSince(timestamp: number): number;
49
+ //# sourceMappingURL=time.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,QAAQ;IAClB;;OAEG;IACH,WAAW,IAAI;IAEf;;OAEG;IACH,MAAM,OAAO,CAAE,qBAAqB;IAEpC;;OAEG;IACH,MAAM,QAAS,CAAE,cAAc;IAE/B;;OAEG;IACH,IAAI,UAAY,CAAE,cAAc;IAEhC;;OAEG;IACH,GAAG,WAAa,CAAE,YAAY;IAE9B;;OAEG;IACH,IAAI,YAAc,CAAE,UAAU;IAE9B;;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"}
package/dist/time.js ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Common duration constants, in milliseconds.
3
+ */
4
+ export var Duration;
5
+ (function (Duration) {
6
+ /**
7
+ * A millisecond.
8
+ */
9
+ Duration[Duration["Millisecond"] = 1] = "Millisecond";
10
+ /**
11
+ * A second, in milliseconds.
12
+ */
13
+ Duration[Duration["Second"] = 1000] = "Second";
14
+ /**
15
+ * A minute, in milliseconds.
16
+ */
17
+ Duration[Duration["Minute"] = 60000] = "Minute";
18
+ /**
19
+ * An hour, in milliseconds.
20
+ */
21
+ Duration[Duration["Hour"] = 3600000] = "Hour";
22
+ /**
23
+ * A day, in milliseconds.
24
+ */
25
+ Duration[Duration["Day"] = 86400000] = "Day";
26
+ /**
27
+ * A week, in milliseconds.
28
+ */
29
+ Duration[Duration["Week"] = 604800000] = "Week";
30
+ /**
31
+ * A year, in milliseconds.
32
+ */
33
+ Duration[Duration["Year"] = 31536000000] = "Year";
34
+ })(Duration || (Duration = {}));
35
+ const isNonNegativeInteger = (number) => Number.isInteger(number) && number >= 0;
36
+ const assertIsNonNegativeInteger = (number, name) => {
37
+ if (!isNonNegativeInteger(number)) {
38
+ throw new Error(`"${name}" must be a non-negative integer. Received: "${number}".`);
39
+ }
40
+ };
41
+ /**
42
+ * Calculates the millisecond value of the specified number of units of time.
43
+ *
44
+ * @param count - The number of units of time.
45
+ * @param duration - The unit of time to count.
46
+ * @returns The count multiplied by the specified duration.
47
+ */
48
+ export function inMilliseconds(count, duration) {
49
+ assertIsNonNegativeInteger(count, 'count');
50
+ return count * duration;
51
+ }
52
+ /**
53
+ * Gets the milliseconds since a particular Unix epoch timestamp.
54
+ *
55
+ * @param timestamp - A Unix millisecond timestamp.
56
+ * @returns The number of milliseconds elapsed since the specified timestamp.
57
+ */
58
+ export function timeSince(timestamp) {
59
+ assertIsNonNegativeInteger(timestamp, 'timestamp');
60
+ return Date.now() - timestamp;
61
+ }
62
+ //# sourceMappingURL=time.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"time.js","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAN,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,KAAR,QAAQ,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,CAAC;QAClC,MAAM,IAAI,KAAK,CACb,IAAI,IAAI,gDAAgD,MAAM,IAAI,CACnE,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,KAAa,EAAE,QAAkB;IAC9D,0BAA0B,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC3C,OAAO,KAAK,GAAG,QAAQ,CAAC;AAC1B,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,SAAiB;IACzC,0BAA0B,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;IACnD,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC;AAChC,CAAC","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"]}