@metamask/utils 3.0.3 → 3.2.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 +19 -1
- package/README.md +1 -1
- package/dist/assert.d.ts +36 -0
- package/dist/assert.js +52 -0
- package/dist/assert.js.map +1 -0
- package/dist/bytes.d.ts +145 -0
- package/dist/bytes.js +344 -0
- package/dist/bytes.js.map +1 -0
- package/dist/coercers.d.ts +96 -0
- package/dist/coercers.js +162 -0
- package/dist/coercers.js.map +1 -0
- package/dist/hex.d.ts +50 -0
- package/dist/hex.js +81 -0
- package/dist/hex.js.map +1 -0
- package/dist/hex.test-d.d.ts +1 -0
- package/dist/hex.test-d.js +16 -0
- package/dist/hex.test-d.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/json.d.ts +81 -40
- package/dist/json.js +53 -14
- package/dist/json.js.map +1 -1
- package/dist/json.test-d.d.ts +1 -0
- package/dist/json.test-d.js +62 -0
- package/dist/json.test-d.js.map +1 -0
- package/dist/misc.js +1 -0
- package/dist/misc.js.map +1 -1
- package/dist/number.d.ts +72 -0
- package/dist/number.js +102 -0
- package/dist/number.js.map +1 -0
- package/package.json +22 -12
- package/dist/__fixtures__/index.d.ts +0 -1
- package/dist/__fixtures__/index.js +0 -18
- package/dist/__fixtures__/index.js.map +0 -1
- package/dist/__fixtures__/json.d.ts +0 -825
- package/dist/__fixtures__/json.js +0 -797
- package/dist/__fixtures__/json.js.map +0 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Infer } from 'superstruct';
|
|
2
|
+
import { Hex } from './hex';
|
|
3
|
+
declare const NumberLikeStruct: import("superstruct").Struct<string | number | bigint, null>;
|
|
4
|
+
declare const BytesLikeStruct: import("superstruct").Struct<Uint8Array | `0x${string}`, null>;
|
|
5
|
+
export declare type NumberLike = Infer<typeof NumberLikeStruct>;
|
|
6
|
+
export declare type BytesLike = Infer<typeof BytesLikeStruct>;
|
|
7
|
+
/**
|
|
8
|
+
* Create a number from a number-like value.
|
|
9
|
+
*
|
|
10
|
+
* - If the value is a number, it is returned as-is.
|
|
11
|
+
* - If the value is a `bigint`, it is converted to a number.
|
|
12
|
+
* - If the value is a string, it is interpreted as a decimal number.
|
|
13
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is
|
|
14
|
+
* interpreted as a hexadecimal number.
|
|
15
|
+
*
|
|
16
|
+
* This validates that the value is a number-like value, and that the resulting
|
|
17
|
+
* number is not `NaN` or `Infinity`.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const value = createNumber('0x010203');
|
|
22
|
+
* console.log(value); // 66051
|
|
23
|
+
*
|
|
24
|
+
* const otherValue = createNumber(123n);
|
|
25
|
+
* console.log(otherValue); // 123
|
|
26
|
+
* ```
|
|
27
|
+
* @param value - The value to create the number from.
|
|
28
|
+
* @returns The created number.
|
|
29
|
+
* @throws If the value is not a number-like value, or if the resulting number
|
|
30
|
+
* is `NaN` or `Infinity`.
|
|
31
|
+
*/
|
|
32
|
+
export declare function createNumber(value: NumberLike): number;
|
|
33
|
+
/**
|
|
34
|
+
* Create a `bigint` from a number-like value.
|
|
35
|
+
*
|
|
36
|
+
* - If the value is a number, it is converted to a `bigint`.
|
|
37
|
+
* - If the value is a `bigint`, it is returned as-is.
|
|
38
|
+
* - If the value is a string, it is interpreted as a decimal number and
|
|
39
|
+
* converted to a `bigint`.
|
|
40
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is
|
|
41
|
+
* interpreted as a hexadecimal number and converted to a `bigint`.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const value = createBigInt('0x010203');
|
|
46
|
+
* console.log(value); // 16909060n
|
|
47
|
+
*
|
|
48
|
+
* const otherValue = createBigInt(123);
|
|
49
|
+
* console.log(otherValue); // 123n
|
|
50
|
+
* ```
|
|
51
|
+
* @param value - The value to create the bigint from.
|
|
52
|
+
* @returns The created bigint.
|
|
53
|
+
* @throws If the value is not a number-like value.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createBigInt(value: NumberLike): bigint;
|
|
56
|
+
/**
|
|
57
|
+
* Create a byte array from a bytes-like value.
|
|
58
|
+
*
|
|
59
|
+
* - If the value is a byte array, it is returned as-is.
|
|
60
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is interpreted
|
|
61
|
+
* as a hexadecimal number and converted to a byte array.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const value = createBytes('0x010203');
|
|
66
|
+
* console.log(value); // Uint8Array [ 1, 2, 3 ]
|
|
67
|
+
*
|
|
68
|
+
* const otherValue = createBytes('0x010203');
|
|
69
|
+
* console.log(otherValue); // Uint8Array [ 1, 2, 3 ]
|
|
70
|
+
* ```
|
|
71
|
+
* @param value - The value to create the byte array from.
|
|
72
|
+
* @returns The created byte array.
|
|
73
|
+
* @throws If the value is not a bytes-like value.
|
|
74
|
+
*/
|
|
75
|
+
export declare function createBytes(value: BytesLike): Uint8Array;
|
|
76
|
+
/**
|
|
77
|
+
* Create a hexadecimal string from a bytes-like value.
|
|
78
|
+
*
|
|
79
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is returned
|
|
80
|
+
* as-is.
|
|
81
|
+
* - If the value is a `Uint8Array`, it is converted to a hex string.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const value = createHex(new Uint8Array([1, 2, 3]));
|
|
86
|
+
* console.log(value); // '0x010203'
|
|
87
|
+
*
|
|
88
|
+
* const otherValue = createHex('0x010203');
|
|
89
|
+
* console.log(otherValue); // '0x010203'
|
|
90
|
+
* ```
|
|
91
|
+
* @param value - The value to create the hex string from.
|
|
92
|
+
* @returns The created hex string.
|
|
93
|
+
* @throws If the value is not a bytes-like value.
|
|
94
|
+
*/
|
|
95
|
+
export declare function createHex(value: BytesLike): Hex;
|
|
96
|
+
export {};
|
package/dist/coercers.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createHex = exports.createBytes = exports.createBigInt = exports.createNumber = void 0;
|
|
4
|
+
const superstruct_1 = require("superstruct");
|
|
5
|
+
const hex_1 = require("./hex");
|
|
6
|
+
const assert_1 = require("./assert");
|
|
7
|
+
const bytes_1 = require("./bytes");
|
|
8
|
+
const NumberLikeStruct = (0, superstruct_1.union)([(0, superstruct_1.number)(), (0, superstruct_1.bigint)(), (0, superstruct_1.string)(), hex_1.StrictHexStruct]);
|
|
9
|
+
const NumberCoercer = (0, superstruct_1.coerce)((0, superstruct_1.number)(), NumberLikeStruct, Number);
|
|
10
|
+
const BigIntCoercer = (0, superstruct_1.coerce)((0, superstruct_1.bigint)(), NumberLikeStruct, BigInt);
|
|
11
|
+
const BytesLikeStruct = (0, superstruct_1.union)([hex_1.StrictHexStruct, (0, superstruct_1.instance)(Uint8Array)]);
|
|
12
|
+
const BytesCoercer = (0, superstruct_1.coerce)((0, superstruct_1.instance)(Uint8Array), (0, superstruct_1.union)([hex_1.StrictHexStruct]), bytes_1.hexToBytes);
|
|
13
|
+
const HexCoercer = (0, superstruct_1.coerce)(hex_1.StrictHexStruct, (0, superstruct_1.instance)(Uint8Array), bytes_1.bytesToHex);
|
|
14
|
+
/**
|
|
15
|
+
* Create a number from a number-like value.
|
|
16
|
+
*
|
|
17
|
+
* - If the value is a number, it is returned as-is.
|
|
18
|
+
* - If the value is a `bigint`, it is converted to a number.
|
|
19
|
+
* - If the value is a string, it is interpreted as a decimal number.
|
|
20
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is
|
|
21
|
+
* interpreted as a hexadecimal number.
|
|
22
|
+
*
|
|
23
|
+
* This validates that the value is a number-like value, and that the resulting
|
|
24
|
+
* number is not `NaN` or `Infinity`.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const value = createNumber('0x010203');
|
|
29
|
+
* console.log(value); // 66051
|
|
30
|
+
*
|
|
31
|
+
* const otherValue = createNumber(123n);
|
|
32
|
+
* console.log(otherValue); // 123
|
|
33
|
+
* ```
|
|
34
|
+
* @param value - The value to create the number from.
|
|
35
|
+
* @returns The created number.
|
|
36
|
+
* @throws If the value is not a number-like value, or if the resulting number
|
|
37
|
+
* is `NaN` or `Infinity`.
|
|
38
|
+
*/
|
|
39
|
+
function createNumber(value) {
|
|
40
|
+
try {
|
|
41
|
+
const result = (0, superstruct_1.create)(value, NumberCoercer);
|
|
42
|
+
(0, assert_1.assert)(Number.isFinite(result), `Expected a number-like value, got "${value}".`);
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (error instanceof superstruct_1.StructError) {
|
|
47
|
+
throw new Error(`Expected a number-like value, got "${value}".`);
|
|
48
|
+
}
|
|
49
|
+
/* istanbul ignore next */
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.createNumber = createNumber;
|
|
54
|
+
/**
|
|
55
|
+
* Create a `bigint` from a number-like value.
|
|
56
|
+
*
|
|
57
|
+
* - If the value is a number, it is converted to a `bigint`.
|
|
58
|
+
* - If the value is a `bigint`, it is returned as-is.
|
|
59
|
+
* - If the value is a string, it is interpreted as a decimal number and
|
|
60
|
+
* converted to a `bigint`.
|
|
61
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is
|
|
62
|
+
* interpreted as a hexadecimal number and converted to a `bigint`.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const value = createBigInt('0x010203');
|
|
67
|
+
* console.log(value); // 16909060n
|
|
68
|
+
*
|
|
69
|
+
* const otherValue = createBigInt(123);
|
|
70
|
+
* console.log(otherValue); // 123n
|
|
71
|
+
* ```
|
|
72
|
+
* @param value - The value to create the bigint from.
|
|
73
|
+
* @returns The created bigint.
|
|
74
|
+
* @throws If the value is not a number-like value.
|
|
75
|
+
*/
|
|
76
|
+
function createBigInt(value) {
|
|
77
|
+
try {
|
|
78
|
+
// The `BigInt` constructor throws if the value is not a number-like value.
|
|
79
|
+
// There is no need to validate the value manually.
|
|
80
|
+
return (0, superstruct_1.create)(value, BigIntCoercer);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
if (error instanceof superstruct_1.StructError) {
|
|
84
|
+
throw new Error(`Expected a number-like value, got "${error.value}".`);
|
|
85
|
+
}
|
|
86
|
+
/* istanbul ignore next */
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.createBigInt = createBigInt;
|
|
91
|
+
/**
|
|
92
|
+
* Create a byte array from a bytes-like value.
|
|
93
|
+
*
|
|
94
|
+
* - If the value is a byte array, it is returned as-is.
|
|
95
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is interpreted
|
|
96
|
+
* as a hexadecimal number and converted to a byte array.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const value = createBytes('0x010203');
|
|
101
|
+
* console.log(value); // Uint8Array [ 1, 2, 3 ]
|
|
102
|
+
*
|
|
103
|
+
* const otherValue = createBytes('0x010203');
|
|
104
|
+
* console.log(otherValue); // Uint8Array [ 1, 2, 3 ]
|
|
105
|
+
* ```
|
|
106
|
+
* @param value - The value to create the byte array from.
|
|
107
|
+
* @returns The created byte array.
|
|
108
|
+
* @throws If the value is not a bytes-like value.
|
|
109
|
+
*/
|
|
110
|
+
function createBytes(value) {
|
|
111
|
+
if (typeof value === 'string' && value.toLowerCase() === '0x') {
|
|
112
|
+
return new Uint8Array();
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
return (0, superstruct_1.create)(value, BytesCoercer);
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
if (error instanceof superstruct_1.StructError) {
|
|
119
|
+
throw new Error(`Expected a bytes-like value, got "${error.value}".`);
|
|
120
|
+
}
|
|
121
|
+
/* istanbul ignore next */
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
exports.createBytes = createBytes;
|
|
126
|
+
/**
|
|
127
|
+
* Create a hexadecimal string from a bytes-like value.
|
|
128
|
+
*
|
|
129
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is returned
|
|
130
|
+
* as-is.
|
|
131
|
+
* - If the value is a `Uint8Array`, it is converted to a hex string.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const value = createHex(new Uint8Array([1, 2, 3]));
|
|
136
|
+
* console.log(value); // '0x010203'
|
|
137
|
+
*
|
|
138
|
+
* const otherValue = createHex('0x010203');
|
|
139
|
+
* console.log(otherValue); // '0x010203'
|
|
140
|
+
* ```
|
|
141
|
+
* @param value - The value to create the hex string from.
|
|
142
|
+
* @returns The created hex string.
|
|
143
|
+
* @throws If the value is not a bytes-like value.
|
|
144
|
+
*/
|
|
145
|
+
function createHex(value) {
|
|
146
|
+
if ((value instanceof Uint8Array && value.length === 0) ||
|
|
147
|
+
(typeof value === 'string' && value.toLowerCase() === '0x')) {
|
|
148
|
+
return '0x';
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
return (0, superstruct_1.create)(value, HexCoercer);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
if (error instanceof superstruct_1.StructError) {
|
|
155
|
+
throw new Error(`Expected a bytes-like value, got "${error.value}".`);
|
|
156
|
+
}
|
|
157
|
+
/* istanbul ignore next */
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.createHex = createHex;
|
|
162
|
+
//# sourceMappingURL=coercers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coercers.js","sourceRoot":"","sources":["../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AAM,EAAC,KAAK,EAAE,UAAU,CAAC,CAAC;KAClC;IAAC,OAAO,KAAK,EAAE;QACd,IAAI,KAAK,YAAY,yBAAW,EAAE;YAChC,MAAM,IAAI,KAAK,CAAC,qCAAqC,KAAK,CAAC,KAAK,IAAI,CAAC,CAAC;SACvE;QAED,0BAA0B;QAC1B,MAAM,KAAK,CAAC;KACb;AACH,CAAC;AAlBD,8BAkBC","sourcesContent":["import {\n bigint,\n coerce,\n create,\n Infer,\n instance,\n number,\n string,\n StructError,\n union,\n} from 'superstruct';\nimport { Hex, StrictHexStruct } from './hex';\nimport { assert } from './assert';\nimport { bytesToHex, hexToBytes } from './bytes';\n\nconst NumberLikeStruct = union([number(), bigint(), string(), StrictHexStruct]);\nconst NumberCoercer = coerce(number(), NumberLikeStruct, Number);\nconst BigIntCoercer = coerce(bigint(), NumberLikeStruct, BigInt);\n\nconst BytesLikeStruct = union([StrictHexStruct, instance(Uint8Array)]);\nconst BytesCoercer = coerce(\n instance(Uint8Array),\n union([StrictHexStruct]),\n hexToBytes,\n);\n\nconst HexCoercer = coerce(StrictHexStruct, instance(Uint8Array), bytesToHex);\n\nexport type NumberLike = Infer<typeof NumberLikeStruct>;\nexport type BytesLike = Infer<typeof BytesLikeStruct>;\n\n/**\n * Create a number from a number-like value.\n *\n * - If the value is a number, it is returned as-is.\n * - If the value is a `bigint`, it is converted to a number.\n * - If the value is a string, it is interpreted as a decimal number.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is\n * interpreted as a hexadecimal number.\n *\n * This validates that the value is a number-like value, and that the resulting\n * number is not `NaN` or `Infinity`.\n *\n * @example\n * ```typescript\n * const value = createNumber('0x010203');\n * console.log(value); // 66051\n *\n * const otherValue = createNumber(123n);\n * console.log(otherValue); // 123\n * ```\n * @param value - The value to create the number from.\n * @returns The created number.\n * @throws If the value is not a number-like value, or if the resulting number\n * is `NaN` or `Infinity`.\n */\nexport function createNumber(value: NumberLike): number {\n try {\n const result = create(value, NumberCoercer);\n\n assert(\n Number.isFinite(result),\n `Expected a number-like value, got \"${value}\".`,\n );\n\n return result;\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(`Expected a number-like value, got \"${value}\".`);\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a `bigint` from a number-like value.\n *\n * - If the value is a number, it is converted to a `bigint`.\n * - If the value is a `bigint`, it is returned as-is.\n * - If the value is a string, it is interpreted as a decimal number and\n * converted to a `bigint`.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is\n * interpreted as a hexadecimal number and converted to a `bigint`.\n *\n * @example\n * ```typescript\n * const value = createBigInt('0x010203');\n * console.log(value); // 16909060n\n *\n * const otherValue = createBigInt(123);\n * console.log(otherValue); // 123n\n * ```\n * @param value - The value to create the bigint from.\n * @returns The created bigint.\n * @throws If the value is not a number-like value.\n */\nexport function createBigInt(value: NumberLike): bigint {\n try {\n // The `BigInt` constructor throws if the value is not a number-like value.\n // There is no need to validate the value manually.\n return create(value, BigIntCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(`Expected a number-like value, got \"${error.value}\".`);\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a byte array from a bytes-like value.\n *\n * - If the value is a byte array, it is returned as-is.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is interpreted\n * as a hexadecimal number and converted to a byte array.\n *\n * @example\n * ```typescript\n * const value = createBytes('0x010203');\n * console.log(value); // Uint8Array [ 1, 2, 3 ]\n *\n * const otherValue = createBytes('0x010203');\n * console.log(otherValue); // Uint8Array [ 1, 2, 3 ]\n * ```\n * @param value - The value to create the byte array from.\n * @returns The created byte array.\n * @throws If the value is not a bytes-like value.\n */\nexport function createBytes(value: BytesLike): Uint8Array {\n if (typeof value === 'string' && value.toLowerCase() === '0x') {\n return new Uint8Array();\n }\n\n try {\n return create(value, BytesCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(`Expected a bytes-like value, got \"${error.value}\".`);\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a hexadecimal string from a bytes-like value.\n *\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is returned\n * as-is.\n * - If the value is a `Uint8Array`, it is converted to a hex string.\n *\n * @example\n * ```typescript\n * const value = createHex(new Uint8Array([1, 2, 3]));\n * console.log(value); // '0x010203'\n *\n * const otherValue = createHex('0x010203');\n * console.log(otherValue); // '0x010203'\n * ```\n * @param value - The value to create the hex string from.\n * @returns The created hex string.\n * @throws If the value is not a bytes-like value.\n */\nexport function createHex(value: BytesLike): Hex {\n if (\n (value instanceof Uint8Array && value.length === 0) ||\n (typeof value === 'string' && value.toLowerCase() === '0x')\n ) {\n return '0x';\n }\n\n try {\n return create(value, HexCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(`Expected a bytes-like value, got \"${error.value}\".`);\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n"]}
|
package/dist/hex.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Struct } from 'superstruct';
|
|
2
|
+
export declare type Hex = `0x${string}`;
|
|
3
|
+
export declare const HexStruct: Struct<string, null>;
|
|
4
|
+
export declare const StrictHexStruct: Struct<`0x${string}`, null>;
|
|
5
|
+
/**
|
|
6
|
+
* Check if a string is a valid hex string.
|
|
7
|
+
*
|
|
8
|
+
* @param value - The value to check.
|
|
9
|
+
* @returns Whether the value is a valid hex string.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isHexString(value: unknown): value is string;
|
|
12
|
+
/**
|
|
13
|
+
* Strictly check if a string is a valid hex string. A valid hex string must
|
|
14
|
+
* start with the "0x"-prefix.
|
|
15
|
+
*
|
|
16
|
+
* @param value - The value to check.
|
|
17
|
+
* @returns Whether the value is a valid hex string.
|
|
18
|
+
*/
|
|
19
|
+
export declare function isStrictHexString(value: unknown): value is Hex;
|
|
20
|
+
/**
|
|
21
|
+
* Assert that a value is a valid hex string.
|
|
22
|
+
*
|
|
23
|
+
* @param value - The value to check.
|
|
24
|
+
* @throws If the value is not a valid hex string.
|
|
25
|
+
*/
|
|
26
|
+
export declare function assertIsHexString(value: unknown): asserts value is string;
|
|
27
|
+
/**
|
|
28
|
+
* Assert that a value is a valid hex string. A valid hex string must start with
|
|
29
|
+
* the "0x"-prefix.
|
|
30
|
+
*
|
|
31
|
+
* @param value - The value to check.
|
|
32
|
+
* @throws If the value is not a valid hex string.
|
|
33
|
+
*/
|
|
34
|
+
export declare function assertIsStrictHexString(value: unknown): asserts value is Hex;
|
|
35
|
+
/**
|
|
36
|
+
* Add the `0x`-prefix to a hexadecimal string. If the string already has the
|
|
37
|
+
* prefix, it is returned as-is.
|
|
38
|
+
*
|
|
39
|
+
* @param hex - The hexadecimal string to add the prefix to.
|
|
40
|
+
* @returns The prefixed hexadecimal string.
|
|
41
|
+
*/
|
|
42
|
+
export declare function add0x(hex: string): Hex;
|
|
43
|
+
/**
|
|
44
|
+
* Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have
|
|
45
|
+
* the prefix, it is returned as-is.
|
|
46
|
+
*
|
|
47
|
+
* @param hex - The hexadecimal string to remove the prefix from.
|
|
48
|
+
* @returns The un-prefixed hexadecimal string.
|
|
49
|
+
*/
|
|
50
|
+
export declare function remove0x(hex: string): string;
|
package/dist/hex.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.remove0x = exports.add0x = exports.assertIsStrictHexString = exports.assertIsHexString = exports.isStrictHexString = exports.isHexString = exports.StrictHexStruct = exports.HexStruct = void 0;
|
|
4
|
+
const superstruct_1 = require("superstruct");
|
|
5
|
+
const assert_1 = require("./assert");
|
|
6
|
+
exports.HexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^(?:0x)?[0-9a-f]+$/iu);
|
|
7
|
+
exports.StrictHexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-f]+$/iu);
|
|
8
|
+
/**
|
|
9
|
+
* Check if a string is a valid hex string.
|
|
10
|
+
*
|
|
11
|
+
* @param value - The value to check.
|
|
12
|
+
* @returns Whether the value is a valid hex string.
|
|
13
|
+
*/
|
|
14
|
+
function isHexString(value) {
|
|
15
|
+
return (0, superstruct_1.is)(value, exports.HexStruct);
|
|
16
|
+
}
|
|
17
|
+
exports.isHexString = isHexString;
|
|
18
|
+
/**
|
|
19
|
+
* Strictly check if a string is a valid hex string. A valid hex string must
|
|
20
|
+
* start with the "0x"-prefix.
|
|
21
|
+
*
|
|
22
|
+
* @param value - The value to check.
|
|
23
|
+
* @returns Whether the value is a valid hex string.
|
|
24
|
+
*/
|
|
25
|
+
function isStrictHexString(value) {
|
|
26
|
+
return (0, superstruct_1.is)(value, exports.StrictHexStruct);
|
|
27
|
+
}
|
|
28
|
+
exports.isStrictHexString = isStrictHexString;
|
|
29
|
+
/**
|
|
30
|
+
* Assert that a value is a valid hex string.
|
|
31
|
+
*
|
|
32
|
+
* @param value - The value to check.
|
|
33
|
+
* @throws If the value is not a valid hex string.
|
|
34
|
+
*/
|
|
35
|
+
function assertIsHexString(value) {
|
|
36
|
+
(0, assert_1.assert)(isHexString(value), 'Value must be a hexadecimal string.');
|
|
37
|
+
}
|
|
38
|
+
exports.assertIsHexString = assertIsHexString;
|
|
39
|
+
/**
|
|
40
|
+
* Assert that a value is a valid hex string. A valid hex string must start with
|
|
41
|
+
* the "0x"-prefix.
|
|
42
|
+
*
|
|
43
|
+
* @param value - The value to check.
|
|
44
|
+
* @throws If the value is not a valid hex string.
|
|
45
|
+
*/
|
|
46
|
+
function assertIsStrictHexString(value) {
|
|
47
|
+
(0, assert_1.assert)(isStrictHexString(value), 'Value must be a hexadecimal string, starting with "0x".');
|
|
48
|
+
}
|
|
49
|
+
exports.assertIsStrictHexString = assertIsStrictHexString;
|
|
50
|
+
/**
|
|
51
|
+
* Add the `0x`-prefix to a hexadecimal string. If the string already has the
|
|
52
|
+
* prefix, it is returned as-is.
|
|
53
|
+
*
|
|
54
|
+
* @param hex - The hexadecimal string to add the prefix to.
|
|
55
|
+
* @returns The prefixed hexadecimal string.
|
|
56
|
+
*/
|
|
57
|
+
function add0x(hex) {
|
|
58
|
+
if (hex.startsWith('0x')) {
|
|
59
|
+
return hex;
|
|
60
|
+
}
|
|
61
|
+
if (hex.startsWith('0X')) {
|
|
62
|
+
return `0x${hex.substring(2)}`;
|
|
63
|
+
}
|
|
64
|
+
return `0x${hex}`;
|
|
65
|
+
}
|
|
66
|
+
exports.add0x = add0x;
|
|
67
|
+
/**
|
|
68
|
+
* Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have
|
|
69
|
+
* the prefix, it is returned as-is.
|
|
70
|
+
*
|
|
71
|
+
* @param hex - The hexadecimal string to remove the prefix from.
|
|
72
|
+
* @returns The un-prefixed hexadecimal string.
|
|
73
|
+
*/
|
|
74
|
+
function remove0x(hex) {
|
|
75
|
+
if (hex.startsWith('0x') || hex.startsWith('0X')) {
|
|
76
|
+
return hex.substring(2);
|
|
77
|
+
}
|
|
78
|
+
return hex;
|
|
79
|
+
}
|
|
80
|
+
exports.remove0x = remove0x;
|
|
81
|
+
//# sourceMappingURL=hex.js.map
|
package/dist/hex.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hex.js","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":";;;AAAA,6CAA0D;AAC1D,qCAAkC;AAIrB,QAAA,SAAS,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,sBAAsB,CAAC,CAAC;AACtD,QAAA,eAAe,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,iBAAiB,CAGjE,CAAC;AAEF;;;;;GAKG;AACH,SAAgB,WAAW,CAAC,KAAc;IACxC,OAAO,IAAA,gBAAE,EAAC,KAAK,EAAE,iBAAS,CAAC,CAAC;AAC9B,CAAC;AAFD,kCAEC;AAED;;;;;;GAMG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,OAAO,IAAA,gBAAE,EAAC,KAAK,EAAE,uBAAe,CAAC,CAAC;AACpC,CAAC;AAFD,8CAEC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,IAAA,eAAM,EAAC,WAAW,CAAC,KAAK,CAAC,EAAE,qCAAqC,CAAC,CAAC;AACpE,CAAC;AAFD,8CAEC;AAED;;;;;;GAMG;AACH,SAAgB,uBAAuB,CAAC,KAAc;IACpD,IAAA,eAAM,EACJ,iBAAiB,CAAC,KAAK,CAAC,EACxB,yDAAyD,CAC1D,CAAC;AACJ,CAAC;AALD,0DAKC;AAED;;;;;;GAMG;AACH,SAAgB,KAAK,CAAC,GAAW;IAC/B,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QACxB,OAAO,GAAU,CAAC;KACnB;IAED,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QACxB,OAAO,KAAK,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;KAChC;IAED,OAAO,KAAK,GAAG,EAAE,CAAC;AACpB,CAAC;AAVD,sBAUC;AAED;;;;;;GAMG;AACH,SAAgB,QAAQ,CAAC,GAAW;IAClC,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAChD,OAAO,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;KACzB;IAED,OAAO,GAAG,CAAC;AACb,CAAC;AAND,4BAMC","sourcesContent":["import { is, pattern, string, Struct } from 'superstruct';\nimport { assert } from './assert';\n\nexport type Hex = `0x${string}`;\n\nexport const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu);\nexport const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu) as Struct<\n Hex,\n null\n>;\n\n/**\n * Check if a string is a valid hex string.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isHexString(value: unknown): value is string {\n return is(value, HexStruct);\n}\n\n/**\n * Strictly check if a string is a valid hex string. A valid hex string must\n * start with the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isStrictHexString(value: unknown): value is Hex {\n return is(value, StrictHexStruct);\n}\n\n/**\n * Assert that a value is a valid hex string.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsHexString(value: unknown): asserts value is string {\n assert(isHexString(value), 'Value must be a hexadecimal string.');\n}\n\n/**\n * Assert that a value is a valid hex string. A valid hex string must start with\n * the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsStrictHexString(value: unknown): asserts value is Hex {\n assert(\n isStrictHexString(value),\n 'Value must be a hexadecimal string, starting with \"0x\".',\n );\n}\n\n/**\n * Add the `0x`-prefix to a hexadecimal string. If the string already has the\n * prefix, it is returned as-is.\n *\n * @param hex - The hexadecimal string to add the prefix to.\n * @returns The prefixed hexadecimal string.\n */\nexport function add0x(hex: string): Hex {\n if (hex.startsWith('0x')) {\n return hex as Hex;\n }\n\n if (hex.startsWith('0X')) {\n return `0x${hex.substring(2)}`;\n }\n\n return `0x${hex}`;\n}\n\n/**\n * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have\n * the prefix, it is returned as-is.\n *\n * @param hex - The hexadecimal string to remove the prefix from.\n * @returns The un-prefixed hexadecimal string.\n */\nexport function remove0x(hex: string): string {\n if (hex.startsWith('0x') || hex.startsWith('0X')) {\n return hex.substring(2);\n }\n\n return hex;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tsd_1 = require("tsd");
|
|
4
|
+
// Valid hex strings:
|
|
5
|
+
(0, tsd_1.expectAssignable)('0x');
|
|
6
|
+
(0, tsd_1.expectAssignable)('0x0');
|
|
7
|
+
(0, tsd_1.expectAssignable)('0x😀');
|
|
8
|
+
const embeddedString = 'test';
|
|
9
|
+
(0, tsd_1.expectAssignable)(`0x${embeddedString}`);
|
|
10
|
+
// Not valid hex strings:
|
|
11
|
+
(0, tsd_1.expectNotAssignable)(`0X${embeddedString}`);
|
|
12
|
+
(0, tsd_1.expectNotAssignable)(`1x${embeddedString}`);
|
|
13
|
+
(0, tsd_1.expectNotAssignable)(0);
|
|
14
|
+
(0, tsd_1.expectNotAssignable)('0');
|
|
15
|
+
(0, tsd_1.expectNotAssignable)('🙃');
|
|
16
|
+
//# sourceMappingURL=hex.test-d.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hex.test-d.js","sourceRoot":"","sources":["../src/hex.test-d.ts"],"names":[],"mappings":";;AAAA,6BAA4D;AAI5D,qBAAqB;AAErB,IAAA,sBAAgB,EAAM,IAAI,CAAC,CAAC;AAE5B,IAAA,sBAAgB,EAAM,KAAK,CAAC,CAAC;AAE7B,IAAA,sBAAgB,EAAM,MAAM,CAAC,CAAC;AAE9B,MAAM,cAAc,GAAG,MAAM,CAAC;AAC9B,IAAA,sBAAgB,EAAM,KAAK,cAAc,EAAE,CAAC,CAAC;AAE7C,yBAAyB;AAEzB,IAAA,yBAAmB,EAAM,KAAK,cAAc,EAAE,CAAC,CAAC;AAEhD,IAAA,yBAAmB,EAAM,KAAK,cAAc,EAAE,CAAC,CAAC;AAEhD,IAAA,yBAAmB,EAAM,CAAC,CAAC,CAAC;AAE5B,IAAA,yBAAmB,EAAM,GAAG,CAAC,CAAC;AAE9B,IAAA,yBAAmB,EAAM,IAAI,CAAC,CAAC","sourcesContent":["import { expectAssignable, expectNotAssignable } from 'tsd';\n\nimport { Hex } from '.';\n\n// Valid hex strings:\n\nexpectAssignable<Hex>('0x');\n\nexpectAssignable<Hex>('0x0');\n\nexpectAssignable<Hex>('0x😀');\n\nconst embeddedString = 'test';\nexpectAssignable<Hex>(`0x${embeddedString}`);\n\n// Not valid hex strings:\n\nexpectNotAssignable<Hex>(`0X${embeddedString}`);\n\nexpectNotAssignable<Hex>(`1x${embeddedString}`);\n\nexpectNotAssignable<Hex>(0);\n\nexpectNotAssignable<Hex>('0');\n\nexpectNotAssignable<Hex>('🙃');\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
export * from './assert';
|
|
2
|
+
export * from './bytes';
|
|
3
|
+
export * from './coercers';
|
|
1
4
|
export * from './collections';
|
|
5
|
+
export * from './hex';
|
|
2
6
|
export * from './json';
|
|
3
7
|
export * from './logging';
|
|
4
8
|
export * from './misc';
|
|
9
|
+
export * from './number';
|
|
5
10
|
export * from './time';
|
package/dist/index.js
CHANGED
|
@@ -14,9 +14,14 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./assert"), exports);
|
|
18
|
+
__exportStar(require("./bytes"), exports);
|
|
19
|
+
__exportStar(require("./coercers"), exports);
|
|
17
20
|
__exportStar(require("./collections"), exports);
|
|
21
|
+
__exportStar(require("./hex"), exports);
|
|
18
22
|
__exportStar(require("./json"), exports);
|
|
19
23
|
__exportStar(require("./logging"), exports);
|
|
20
24
|
__exportStar(require("./misc"), exports);
|
|
25
|
+
__exportStar(require("./number"), exports);
|
|
21
26
|
__exportStar(require("./time"), exports);
|
|
22
27
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B;AAC9B,yCAAuB;AACvB,4CAA0B;AAC1B,yCAAuB;AACvB,yCAAuB","sourcesContent":["export * from './collections';\nexport * from './json';\nexport * from './logging';\nexport * from './misc';\nexport * from './time';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,0CAAwB;AACxB,6CAA2B;AAC3B,gDAA8B;AAC9B,wCAAsB;AACtB,yCAAuB;AACvB,4CAA0B;AAC1B,yCAAuB;AACvB,2CAAyB;AACzB,yCAAuB","sourcesContent":["export * from './assert';\nexport * from './bytes';\nexport * from './coercers';\nexport * from './collections';\nexport * from './hex';\nexport * from './json';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './time';\n"]}
|