@metamask/utils 3.1.0 → 3.3.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/dist/number.js 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 hex_1 = require("./hex");
5
+ const assert_1 = require("./assert");
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.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.js","sourceRoot":"","sources":["../src/number.ts"],"names":[],"mappings":";;;AAAA,+BAAiD;AACjD,qCAAkC;AAElC;;;;;;;;;;;;;;;;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 { add0x, assertIsHexString } from './hex';\nimport { assert } from './assert';\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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/utils",
3
- "version": "3.1.0",
3
+ "version": "3.3.0",
4
4
  "description": "Various JavaScript/TypeScript utilities of wide relevance to the MetaMask codebase.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,7 +22,9 @@
22
22
  "lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
23
23
  "lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' --ignore-path .gitignore",
24
24
  "prepublishOnly": "yarn build:clean && yarn lint && yarn test",
25
- "test": "jest",
25
+ "test": "yarn test:source && yarn test:types",
26
+ "test:source": "jest",
27
+ "test:types": "tsd",
26
28
  "test:watch": "jest --watch"
27
29
  },
28
30
  "resolutions": {
@@ -31,8 +33,7 @@
31
33
  "dependencies": {
32
34
  "@types/debug": "^4.1.7",
33
35
  "debug": "^4.3.4",
34
- "fast-deep-equal": "^3.1.3",
35
- "superstruct": "^0.16.5"
36
+ "superstruct": "^0.16.7"
36
37
  },
37
38
  "devDependencies": {
38
39
  "@lavamoat/allow-scripts": "^2.0.3",
@@ -58,6 +59,7 @@
58
59
  "rimraf": "^3.0.2",
59
60
  "stdio-mock": "^1.2.0",
60
61
  "ts-jest": "^28.0.8",
62
+ "tsd": "^0.24.1",
61
63
  "typedoc": "^0.23.10",
62
64
  "typescript": "~4.7.4"
63
65
  },
@@ -73,5 +75,8 @@
73
75
  "allowScripts": {
74
76
  "@lavamoat/preinstall-always-fail": false
75
77
  }
78
+ },
79
+ "tsd": {
80
+ "directory": "src"
76
81
  }
77
82
  }