@metamask/utils 5.0.0 → 5.0.1

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 ADDED
@@ -0,0 +1,155 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [5.0.1]
10
+ ### Fixed
11
+ - Keep original type when using `hasProperty` if defined ([#94](https://github.com/MetaMask/utils/pull/94))
12
+
13
+ ## [5.0.0]
14
+ ### Changed
15
+ - **BREAKING:** Update `Keyring` type ([#89](https://github.com/MetaMask/utils/pull/89))
16
+ - The `Keyring` class now uses the data types `TypedTransaction` and `TxData` from `@ethereumjs/tx` (`v4.1.1`).
17
+ - The `Keyring` now exposes a new optional method called `generateRandomMnemonic`.
18
+
19
+ ## [4.0.0]
20
+ ### Changed
21
+ - Export new modules (`keyring`, `transaction-types`, and `encryption-types`) ([#86](https://github.com/MetaMask/utils/pull/86))
22
+ - **BREAKING:** Improve JSON validation ([#85](https://github.com/MetaMask/utils/pull/85))
23
+ - Fixes edge cases in our JSON validation logic.
24
+ - The previous function used for JSON validation (`validateJsonAndGetSize`) was removed.
25
+ - The `isValidJson` function now uses the new JSON validation logic.
26
+ - To get the size of a JSON value, you can use the `getJsonSize` function.
27
+
28
+ ## [3.6.0]
29
+ ### Added
30
+ - Add `Keyring` types ([#74](https://github.com/MetaMask/utils/pull/74))
31
+ - New data types added. These are `Keyring`, `Transaction` (`LegacyTransaction`, `EIP2930Transaction`, `EIP1559Transaction`), `SignedTransaction`, `Signature`, and `Eip1024EncryptedData`.
32
+
33
+ ## [3.5.0]
34
+ ### Changed
35
+ - Improve the `hasProperty` function ([#79](https://github.com/MetaMask/utils/pull/79), [#80](https://github.com/MetaMask/utils/pull/80))
36
+ - This function now acts as a type guard, informing TypeScript that the property exists.
37
+ - The function is now compatible with more types of objects, such as Errors and class instances.
38
+
39
+ ## [3.4.1]
40
+ ### Fixed
41
+ - Bump `superstruct` to `^1.0.3` ([#71](https://github.com/MetaMask/utils/pull/71))
42
+
43
+ ## [3.4.0]
44
+ ### Added
45
+ - Add types and utility functions for validating versions and checksums ([#67](https://github.com/MetaMask/utils/pull/67), [#69](https://github.com/MetaMask/utils/pull/69))
46
+
47
+ ### Fixed
48
+ - JSON-RPC types now have a default generic `Params` value ([#54](https://github.com/MetaMask/utils/pull/54))
49
+
50
+ ## [3.3.1]
51
+ ### Fixed
52
+ - JSON-RPC parameters are now properly cast to Json upon validation ([#51](https://github.com/MetaMask/utils/pull/51))
53
+
54
+ ## [3.3.0]
55
+ ### Added
56
+ - Add more assertion utils ([#49](https://github.com/MetaMask/utils/pull/49))
57
+ - Add JSON-RPC error validation functions ([#46](https://github.com/MetaMask/utils/pull/46))
58
+ - Add convenience function for creating a `DataView` ([#45](https://github.com/MetaMask/utils/pull/45))
59
+
60
+ ### Fixed
61
+ - Update JSON validation logic ([#47](https://github.com/MetaMask/utils/pull/47))
62
+ - Validation would previously allow for `undefined` values, which is not a standard JSON type
63
+
64
+ ## [3.2.0]
65
+ ### Added
66
+ - Add `PendingJsonRpcResponse` type ([#43](https://github.com/MetaMask/utils/pull/43))
67
+ - Add utils for converting between numbers and hex ([#41](https://github.com/MetaMask/utils/pull/41))
68
+ - Add coercion utils ([#38](https://github.com/MetaMask/utils/pull/38))
69
+
70
+ ## [3.1.0]
71
+ ### Added
72
+ - Add assertion utils ([#33](https://github.com/MetaMask/utils/pull/33))
73
+ - Add util functions for encoding and decoding bytes ([#34](https://github.com/MetaMask/utils/pull/34))
74
+
75
+ ### Fixed
76
+ - Make JSON-RPC error `data` property optional ([#31](https://github.com/MetaMask/utils/pull/31))
77
+ - Don't include test files in dist folder ([#35](https://github.com/MetaMask/utils/pull/35))
78
+ - Fix typo in README ([#28](https://github.com/MetaMask/utils/pull/28))
79
+
80
+ ## [3.0.3]
81
+ ### Fixed
82
+ - Allow omitting JSON-RPC params when params can be undefined ([#29](https://github.com/MetaMask/utils/pull/29))
83
+
84
+ ## [3.0.2]
85
+ ### Fixed
86
+ - Bump `superstruct` to ^0.16.5 ([#26](https://github.com/MetaMask/utils/pull/26))
87
+ - `superstruct`s 0.16.1 through 0.16.4 were not compatible with Node 14; this restores that compatibility.
88
+
89
+ ## [3.0.1]
90
+ ### Fixed
91
+ - Promote `@types/debug` from development dependencies to production dependencies ([#23](https://github.com/MetaMask/utils/pull/23))
92
+
93
+ ## [3.0.0]
94
+ ### Added
95
+ - Add logging functions ([#20](https://github.com/MetaMask/utils/pull/20))
96
+ - Add frozen collections (implemented in [#5](https://github.com/MetaMask/utils/pull/5) but exported in [#19](https://github.com/MetaMask/utils/pull/19))
97
+
98
+ ### Changed
99
+ - **BREAKING:** Improve types and type validation ([#19](https://github.com/MetaMask/utils/pull/19))
100
+ - Various type changes have been made that might be breaking:
101
+ - The `JsonRpcRequest` and `JsonRpcNotification` types now include a generic constraint requiring that the `Params` type extends the `JsonRpcParams` type.
102
+ - The `JsonRpcSuccess` and `JsonRpcResponse` types now include a generic contraint for the `Result` type, requiring that it extends the `Json` type.
103
+ - Various validation functions now accept `unknown` parameters rather than specific types. This should not be breaking except that it may affect type inference for the parameters passed in.
104
+ - New JSON-related functions have been added:
105
+ - `assertIsJsonRpcResponse`
106
+ - `isJsonRpcResponse`
107
+ - `InferWithParams`
108
+ - `JsonRpcParams`
109
+ - New JSON Struct types have been added:
110
+ - `JsonRpcErrorStruct`
111
+ - `JsonRpcFailureStruct`
112
+ - `JsonRpcIdStruct`
113
+ - `JsonRpcParamsStruct`
114
+ - `JsonRpcRequestStruct`
115
+ - `JsonRpcResponseStruct`
116
+ - `JsonRpcSuccessStruct`
117
+ - `JsonRpcVersionStruct`
118
+ - `JsonStruct`
119
+
120
+ ## [2.1.0]
121
+ ### Added
122
+ - Add JSON storage validation and limit utilities ([#14](https://github.com/MetaMask/utils/pull/14))
123
+ - Adds a new function `validateJsonAndGetSize`.
124
+
125
+ ## [2.0.0]
126
+ ### Added
127
+ - Add more JSON utils ([#8](https://github.com/MetaMask/utils/pull/8))
128
+
129
+ ### Changed
130
+ - **BREAKING:** Refactor and expand time utils ([#9](https://github.com/MetaMask/utils/pull/9))
131
+ - Adds a new function, `inMilliseconds`, and moves the time constants into a TypeScript `enum`.
132
+
133
+ ## [1.0.0]
134
+ ### Added
135
+ - Initial release
136
+
137
+ [Unreleased]: https://github.com/MetaMask/utils/compare/v5.0.1...HEAD
138
+ [5.0.1]: https://github.com/MetaMask/utils/compare/v5.0.0...v5.0.1
139
+ [5.0.0]: https://github.com/MetaMask/utils/compare/v4.0.0...v5.0.0
140
+ [4.0.0]: https://github.com/MetaMask/utils/compare/v3.6.0...v4.0.0
141
+ [3.6.0]: https://github.com/MetaMask/utils/compare/v3.5.0...v3.6.0
142
+ [3.5.0]: https://github.com/MetaMask/utils/compare/v3.4.1...v3.5.0
143
+ [3.4.1]: https://github.com/MetaMask/utils/compare/v3.4.0...v3.4.1
144
+ [3.4.0]: https://github.com/MetaMask/utils/compare/v3.3.1...v3.4.0
145
+ [3.3.1]: https://github.com/MetaMask/utils/compare/v3.3.0...v3.3.1
146
+ [3.3.0]: https://github.com/MetaMask/utils/compare/v3.2.0...v3.3.0
147
+ [3.2.0]: https://github.com/MetaMask/utils/compare/v3.1.0...v3.2.0
148
+ [3.1.0]: https://github.com/MetaMask/utils/compare/v3.0.3...v3.1.0
149
+ [3.0.3]: https://github.com/MetaMask/utils/compare/v3.0.2...v3.0.3
150
+ [3.0.2]: https://github.com/MetaMask/utils/compare/v3.0.1...v3.0.2
151
+ [3.0.1]: https://github.com/MetaMask/utils/compare/v3.0.0...v3.0.1
152
+ [3.0.0]: https://github.com/MetaMask/utils/compare/v2.1.0...v3.0.0
153
+ [2.1.0]: https://github.com/MetaMask/utils/compare/v2.0.0...v2.1.0
154
+ [2.0.0]: https://github.com/MetaMask/utils/compare/v1.0.0...v2.0.0
155
+ [1.0.0]: https://github.com/MetaMask/utils/releases/tag/v1.0.0
package/dist/misc.d.ts CHANGED
@@ -56,7 +56,7 @@ export declare function isObject(value: unknown): value is RuntimeObject;
56
56
  * @returns Whether the specified object has an own property with the specified
57
57
  * name, regardless of whether it is enumerable or not.
58
58
  */
59
- export declare const hasProperty: <ObjectToCheck extends Object, Property extends PropertyKey>(objectToCheck: ObjectToCheck, name: Property) => objectToCheck is ObjectToCheck & Record<Property, unknown>;
59
+ export declare const hasProperty: <ObjectToCheck extends Object, Property extends PropertyKey>(objectToCheck: ObjectToCheck, name: Property) => objectToCheck is ObjectToCheck & Record<Property, Property extends keyof ObjectToCheck ? ObjectToCheck[Property] : unknown>;
60
60
  export declare type PlainObject = Record<number | string | symbol, unknown>;
61
61
  /**
62
62
  * Predefined sizes (in Bytes) of specific parts of JSON structure.
package/dist/misc.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"misc.js","sourceRoot":"","sources":["../src/misc.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,QAAQ;AACR,EAAE;;;AAoCF,EAAE;AACF,cAAc;AACd,EAAE;AAEF;;;;;;GAMG;AACH,SAAgB,eAAe,CAC7B,KAAgB;IAEhB,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AAClD,CAAC;AAJD,0CAIC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;AAC/C,CAAC;AAFD,8CAEC;AAED;;;;;;GAMG;AACH,SAAgB,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAFD,4BAEC;AAED,EAA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AAC,KAAK,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE;QACvD,IAAI,OAAO,CAAC,SAAS,CAAC,EAAE;YACtB,OAAO,KAAK,GAAG,CAAC,CAAC;SAClB;QACD,OAAO,KAAK,GAAG,CAAC,CAAC;IACnB,CAAC,EAAE,CAAC,CAAC,CAAC;IAEN,oDAAoD;IACpD,OAAO,IAAI,GAAG,CAAC,MAAA,KAAK,CAAC,KAAK,CAAC,gCAAwB,CAAC,mCAAI,EAAE,CAAC,CAAC,MAAM,CAAC;AACrE,CAAC;AAVD,kDAUC;AAED;;;;;GAKG;AACH,SAAgB,mBAAmB,CAAC,KAAa;IAC/C,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC;AACjC,CAAC;AAFD,kDAEC","sourcesContent":["//\n// Types\n//\n\n/**\n * Makes every specified property of the specified object type mutable.\n *\n * @template ObjectValue - The object whose readonly properties to make mutable.\n * @template TargetKey - The property key(s) to make mutable.\n */\nexport type Mutable<\n ObjectValue extends Record<string, unknown>,\n TargetKey extends keyof ObjectValue,\n> = {\n -readonly [Key in keyof Pick<ObjectValue, TargetKey>]: ObjectValue[Key];\n} & {\n [Key in keyof Omit<ObjectValue, TargetKey>]: ObjectValue[Key];\n};\n\n/**\n * Useful for representing some value that _might_ be present and / or complete.\n *\n * @template Value - The value that might be present or complete.\n */\nexport type PartialOrAbsent<Value> = Partial<Value> | null | undefined;\n\n/**\n * Like {@link Array}, but always non-empty.\n *\n * @template Element - The non-empty array member type.\n */\nexport type NonEmptyArray<Element> = [Element, ...Element[]];\n\n/**\n * A JavaScript object that is not `null`, a function, or an array.\n */\nexport type RuntimeObject = Record<PropertyKey, unknown>;\n\n//\n// Type Guards\n//\n\n/**\n * A {@link NonEmptyArray} type guard.\n *\n * @template Element - The non-empty array member type.\n * @param value - The value to check.\n * @returns Whether the value is a non-empty array.\n */\nexport function isNonEmptyArray<Element>(\n value: Element[],\n): value is NonEmptyArray<Element> {\n return Array.isArray(value) && value.length > 0;\n}\n\n/**\n * Type guard for \"nullishness\".\n *\n * @param value - Any value.\n * @returns `true` if the value is null or undefined, `false` otherwise.\n */\nexport function isNullOrUndefined(value: unknown): value is null | undefined {\n return value === null || value === undefined;\n}\n\n/**\n * A type guard for {@link RuntimeObject}.\n *\n * @param value - The value to check.\n * @returns Whether the specified value has a runtime type of `object` and is\n * neither `null` nor an `Array`.\n */\nexport function isObject(value: unknown): value is RuntimeObject {\n return Boolean(value) && typeof value === 'object' && !Array.isArray(value);\n}\n\n//\n// Other utility functions\n//\n\n/**\n * A type guard for ensuring an object has a property.\n *\n * @param objectToCheck - The object to check.\n * @param name - The property name to check for.\n * @returns Whether the specified object has an own property with the specified\n * name, regardless of whether it is enumerable or not.\n */\nexport const hasProperty = <\n // eslint-disable-next-line @typescript-eslint/ban-types\n ObjectToCheck extends Object,\n Property extends PropertyKey,\n>(\n objectToCheck: ObjectToCheck,\n name: Property,\n): objectToCheck is ObjectToCheck & Record<Property, unknown> =>\n Object.hasOwnProperty.call(objectToCheck, name);\n\nexport type PlainObject = Record<number | string | symbol, unknown>;\n\n/**\n * Predefined sizes (in Bytes) of specific parts of JSON structure.\n */\nexport enum JsonSize {\n Null = 4,\n Comma = 1,\n Wrapper = 1,\n True = 4,\n False = 5,\n Quote = 1,\n Colon = 1,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Date = 24,\n}\n\n/**\n * Regular expression with pattern matching for (special) escaped characters.\n */\nexport const ESCAPE_CHARACTERS_REGEXP = /\"|\\\\|\\n|\\r|\\t/gu;\n\n/**\n * Check if the value is plain object.\n *\n * @param value - Value to be checked.\n * @returns True if an object is the plain JavaScript object,\n * false if the object is not plain (e.g. function).\n */\nexport function isPlainObject(value: unknown): value is PlainObject {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n\n try {\n let proto = value;\n while (Object.getPrototypeOf(proto) !== null) {\n proto = Object.getPrototypeOf(proto);\n }\n\n return Object.getPrototypeOf(value) === proto;\n } catch (_) {\n return false;\n }\n}\n\n/**\n * Check if character is ASCII.\n *\n * @param character - Character.\n * @returns True if a character code is ASCII, false if not.\n */\nexport function isASCII(character: string) {\n return character.charCodeAt(0) <= 127;\n}\n\n/**\n * Calculate string size.\n *\n * @param value - String value to calculate size.\n * @returns Number of bytes used to store whole string value.\n */\nexport function calculateStringSize(value: string): number {\n const size = value.split('').reduce((total, character) => {\n if (isASCII(character)) {\n return total + 1;\n }\n return total + 2;\n }, 0);\n\n // Also detect characters that need backslash escape\n return size + (value.match(ESCAPE_CHARACTERS_REGEXP) ?? []).length;\n}\n\n/**\n * Calculate size of a number ofter JSON serialization.\n *\n * @param value - Number value to calculate size.\n * @returns Number of bytes used to store whole number in JSON.\n */\nexport function calculateNumberSize(value: number): number {\n return value.toString().length;\n}\n"]}
1
+ {"version":3,"file":"misc.js","sourceRoot":"","sources":["../src/misc.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,QAAQ;AACR,EAAE;;;AAoCF,EAAE;AACF,cAAc;AACd,EAAE;AAEF;;;;;;GAMG;AACH,SAAgB,eAAe,CAC7B,KAAgB;IAEhB,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AAClD,CAAC;AAJD,0CAIC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;AAC/C,CAAC;AAFD,8CAEC;AAED;;;;;;GAMG;AACH,SAAgB,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAFD,4BAEC;AAED,EAAE;AACF,0BAA0B;AAC1B,EAAE;AAEF;;;;;;;GAOG;AACI,MAAM,WAAW,GA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S,EAAE,EAAE;QACvD,IAAI,OAAO,CAAC,SAAS,CAAC,EAAE;YACtB,OAAO,KAAK,GAAG,CAAC,CAAC;SAClB;QACD,OAAO,KAAK,GAAG,CAAC,CAAC;IACnB,CAAC,EAAE,CAAC,CAAC,CAAC;IAEN,oDAAoD;IACpD,OAAO,IAAI,GAAG,CAAC,MAAA,KAAK,CAAC,KAAK,CAAC,gCAAwB,CAAC,mCAAI,EAAE,CAAC,CAAC,MAAM,CAAC;AACrE,CAAC;AAVD,kDAUC;AAED;;;;;GAKG;AACH,SAAgB,mBAAmB,CAAC,KAAa;IAC/C,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC;AACjC,CAAC;AAFD,kDAEC","sourcesContent":["//\n// Types\n//\n\n/**\n * Makes every specified property of the specified object type mutable.\n *\n * @template ObjectValue - The object whose readonly properties to make mutable.\n * @template TargetKey - The property key(s) to make mutable.\n */\nexport type Mutable<\n ObjectValue extends Record<string, unknown>,\n TargetKey extends keyof ObjectValue,\n> = {\n -readonly [Key in keyof Pick<ObjectValue, TargetKey>]: ObjectValue[Key];\n} & {\n [Key in keyof Omit<ObjectValue, TargetKey>]: ObjectValue[Key];\n};\n\n/**\n * Useful for representing some value that _might_ be present and / or complete.\n *\n * @template Value - The value that might be present or complete.\n */\nexport type PartialOrAbsent<Value> = Partial<Value> | null | undefined;\n\n/**\n * Like {@link Array}, but always non-empty.\n *\n * @template Element - The non-empty array member type.\n */\nexport type NonEmptyArray<Element> = [Element, ...Element[]];\n\n/**\n * A JavaScript object that is not `null`, a function, or an array.\n */\nexport type RuntimeObject = Record<PropertyKey, unknown>;\n\n//\n// Type Guards\n//\n\n/**\n * A {@link NonEmptyArray} type guard.\n *\n * @template Element - The non-empty array member type.\n * @param value - The value to check.\n * @returns Whether the value is a non-empty array.\n */\nexport function isNonEmptyArray<Element>(\n value: Element[],\n): value is NonEmptyArray<Element> {\n return Array.isArray(value) && value.length > 0;\n}\n\n/**\n * Type guard for \"nullishness\".\n *\n * @param value - Any value.\n * @returns `true` if the value is null or undefined, `false` otherwise.\n */\nexport function isNullOrUndefined(value: unknown): value is null | undefined {\n return value === null || value === undefined;\n}\n\n/**\n * A type guard for {@link RuntimeObject}.\n *\n * @param value - The value to check.\n * @returns Whether the specified value has a runtime type of `object` and is\n * neither `null` nor an `Array`.\n */\nexport function isObject(value: unknown): value is RuntimeObject {\n return Boolean(value) && typeof value === 'object' && !Array.isArray(value);\n}\n\n//\n// Other utility functions\n//\n\n/**\n * A type guard for ensuring an object has a property.\n *\n * @param objectToCheck - The object to check.\n * @param name - The property name to check for.\n * @returns Whether the specified object has an own property with the specified\n * name, regardless of whether it is enumerable or not.\n */\nexport const hasProperty = <\n // eslint-disable-next-line @typescript-eslint/ban-types\n ObjectToCheck extends Object,\n Property extends PropertyKey,\n>(\n objectToCheck: ObjectToCheck,\n name: Property,\n): objectToCheck is ObjectToCheck &\n Record<\n Property,\n Property extends keyof ObjectToCheck ? ObjectToCheck[Property] : unknown\n > => Object.hasOwnProperty.call(objectToCheck, name);\n\nexport type PlainObject = Record<number | string | symbol, unknown>;\n\n/**\n * Predefined sizes (in Bytes) of specific parts of JSON structure.\n */\nexport enum JsonSize {\n Null = 4,\n Comma = 1,\n Wrapper = 1,\n True = 4,\n False = 5,\n Quote = 1,\n Colon = 1,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Date = 24,\n}\n\n/**\n * Regular expression with pattern matching for (special) escaped characters.\n */\nexport const ESCAPE_CHARACTERS_REGEXP = /\"|\\\\|\\n|\\r|\\t/gu;\n\n/**\n * Check if the value is plain object.\n *\n * @param value - Value to be checked.\n * @returns True if an object is the plain JavaScript object,\n * false if the object is not plain (e.g. function).\n */\nexport function isPlainObject(value: unknown): value is PlainObject {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n\n try {\n let proto = value;\n while (Object.getPrototypeOf(proto) !== null) {\n proto = Object.getPrototypeOf(proto);\n }\n\n return Object.getPrototypeOf(value) === proto;\n } catch (_) {\n return false;\n }\n}\n\n/**\n * Check if character is ASCII.\n *\n * @param character - Character.\n * @returns True if a character code is ASCII, false if not.\n */\nexport function isASCII(character: string) {\n return character.charCodeAt(0) <= 127;\n}\n\n/**\n * Calculate string size.\n *\n * @param value - String value to calculate size.\n * @returns Number of bytes used to store whole string value.\n */\nexport function calculateStringSize(value: string): number {\n const size = value.split('').reduce((total, character) => {\n if (isASCII(character)) {\n return total + 1;\n }\n return total + 2;\n }, 0);\n\n // Also detect characters that need backslash escape\n return size + (value.match(ESCAPE_CHARACTERS_REGEXP) ?? []).length;\n}\n\n/**\n * Calculate size of a number ofter JSON serialization.\n *\n * @param value - Number value to calculate size.\n * @returns Number of bytes used to store whole number in JSON.\n */\nexport function calculateNumberSize(value: number): number {\n return value.toString().length;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/utils",
3
- "version": "5.0.0",
3
+ "version": "5.0.1",
4
4
  "description": "Various JavaScript/TypeScript utilities of wide relevance to the MetaMask codebase.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -80,4 +80,4 @@
80
80
  "tsd": {
81
81
  "directory": "src"
82
82
  }
83
- }
83
+ }