@metamask-previews/type-utils 0.0.0-preview-55fcf3ef7 → 0.0.0-preview-520b89c2f

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 CHANGED
@@ -11,5 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
11
11
 
12
12
  - Add `Json`, `Mutable`, `NonEmptyArray`, `PartialOrAbsent`, and `RuntimeObject` types, moved from `@metamask/utils` ([#10908](https://github.com/MetaMask/core/pull/10908))
13
13
  - Add `getKnownPropertyNames`, `hasProperty`, `isNullOrUndefined`, `isObject`, and `isPlainObject` functions, moved from `@metamask/utils` ([#10908](https://github.com/MetaMask/core/pull/10908))
14
+ - Add `AssertionErrorConstructor` type, moved from `@metamask/utils` ([#10983](https://github.com/MetaMask/core/pull/10983))
15
+ - Add `AssertionError`, `assert`, `assertExhaustive`, and `getAssertionError` functions, moved from `@metamask/utils` ([#10983](https://github.com/MetaMask/core/pull/10983))
14
16
 
15
17
  [Unreleased]: https://github.com/MetaMask/core/
@@ -0,0 +1,57 @@
1
+ export type AssertionErrorConstructor = (new (args: {
2
+ message: string;
3
+ }) => Error) | ((args: {
4
+ message: string;
5
+ }) => Error);
6
+ /**
7
+ * Initialise an {@link AssertionErrorConstructor} error.
8
+ *
9
+ * @param ErrorWrapper - The error class to use.
10
+ * @param message - The error message.
11
+ * @returns The error object.
12
+ * @internal
13
+ */
14
+ export declare function getAssertionError(ErrorWrapper: AssertionErrorConstructor, message: string): Error;
15
+ /**
16
+ * The default error class that is thrown if an assertion fails.
17
+ */
18
+ export declare class AssertionError extends Error {
19
+ readonly code = "ERR_ASSERTION";
20
+ constructor(options: {
21
+ message: string;
22
+ });
23
+ }
24
+ /**
25
+ * Same as Node.js assert.
26
+ * If the value is falsy, throws an error, does nothing otherwise.
27
+ *
28
+ * @throws {@link AssertionError} If value is falsy.
29
+ * @param value - The test that should be truthy to pass.
30
+ * @param message - Message to be passed to {@link AssertionError} or an
31
+ * {@link Error} instance to throw.
32
+ * @param ErrorWrapper - The error class to throw if the assertion fails.
33
+ * Defaults to {@link AssertionError}. If a custom error class is provided for
34
+ * the `message` argument, this argument is ignored.
35
+ */
36
+ export declare function assert(value: unknown, message?: string | Error, ErrorWrapper?: AssertionErrorConstructor): asserts value;
37
+ /**
38
+ * Use in the default case of a switch that you want to be fully exhaustive.
39
+ * Using this function forces the compiler to enforce exhaustivity during
40
+ * compile-time.
41
+ *
42
+ * @example
43
+ * ```
44
+ * const number = 1;
45
+ * switch (number) {
46
+ * case 0:
47
+ * ...
48
+ * case 1:
49
+ * ...
50
+ * default:
51
+ * assertExhaustive(snapPrefix);
52
+ * }
53
+ * ```
54
+ * @param _object - The object on which the switch is being operated.
55
+ */
56
+ export declare function assertExhaustive(_object: never): never;
57
+ //# sourceMappingURL=assert.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,yBAAyB,GACjC,CAAC,KAAK,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,KAAK,CAAC,GAC1C,CAAC,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,KAAK,CAAC,CAAC;AAiB3C;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC/B,YAAY,EAAE,yBAAyB,EACvC,OAAO,EAAE,MAAM,GACd,KAAK,CAUP;AAED;;GAEG;AACH,qBAAa,cAAe,SAAQ,KAAK;IACvC,QAAQ,CAAC,IAAI,mBAAmB;IAEhC,YAAY,OAAO,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,EAEvC;CACF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CACpB,KAAK,EAAE,OAAO,EACd,OAAO,GAAE,MAAM,GAAG,KAA2B,EAC7C,YAAY,GAAE,yBAA0C,GACvD,OAAO,CAAC,KAAK,CAQf;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,KAAK,GAAG,KAAK,CAItD"}
package/dist/assert.js ADDED
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Check if a value is a constructor, i.e., a function that can be called with
3
+ * the `new` keyword.
4
+ *
5
+ * @param fn - The value to check.
6
+ * @returns `true` if the value is a constructor, or `false` otherwise.
7
+ */
8
+ function isConstructable(fn) {
9
+ /* istanbul ignore next */
10
+ // oxlint-disable-next-line typescript/no-unsafe-member-access
11
+ return Boolean(typeof fn?.prototype?.constructor?.name === 'string');
12
+ }
13
+ /**
14
+ * Initialise an {@link AssertionErrorConstructor} error.
15
+ *
16
+ * @param ErrorWrapper - The error class to use.
17
+ * @param message - The error message.
18
+ * @returns The error object.
19
+ * @internal
20
+ */
21
+ export function getAssertionError(ErrorWrapper, message) {
22
+ if (isConstructable(ErrorWrapper)) {
23
+ return new ErrorWrapper({
24
+ message,
25
+ });
26
+ }
27
+ return ErrorWrapper({
28
+ message,
29
+ });
30
+ }
31
+ /**
32
+ * The default error class that is thrown if an assertion fails.
33
+ */
34
+ export class AssertionError extends Error {
35
+ code = 'ERR_ASSERTION';
36
+ constructor(options) {
37
+ super(options.message);
38
+ }
39
+ }
40
+ /**
41
+ * Same as Node.js assert.
42
+ * If the value is falsy, throws an error, does nothing otherwise.
43
+ *
44
+ * @throws {@link AssertionError} If value is falsy.
45
+ * @param value - The test that should be truthy to pass.
46
+ * @param message - Message to be passed to {@link AssertionError} or an
47
+ * {@link Error} instance to throw.
48
+ * @param ErrorWrapper - The error class to throw if the assertion fails.
49
+ * Defaults to {@link AssertionError}. If a custom error class is provided for
50
+ * the `message` argument, this argument is ignored.
51
+ */
52
+ export function assert(value, message = 'Assertion failed.', ErrorWrapper = AssertionError) {
53
+ if (!value) {
54
+ if (message instanceof Error) {
55
+ throw message;
56
+ }
57
+ throw getAssertionError(ErrorWrapper, message);
58
+ }
59
+ }
60
+ /**
61
+ * Use in the default case of a switch that you want to be fully exhaustive.
62
+ * Using this function forces the compiler to enforce exhaustivity during
63
+ * compile-time.
64
+ *
65
+ * @example
66
+ * ```
67
+ * const number = 1;
68
+ * switch (number) {
69
+ * case 0:
70
+ * ...
71
+ * case 1:
72
+ * ...
73
+ * default:
74
+ * assertExhaustive(snapPrefix);
75
+ * }
76
+ * ```
77
+ * @param _object - The object on which the switch is being operated.
78
+ */
79
+ export function assertExhaustive(_object) {
80
+ throw new Error('Invalid branch reached. Should be detected during compilation.');
81
+ }
82
+ //# sourceMappingURL=assert.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert.js","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,SAAS,eAAe,CACtB,EAA6B;IAE7B,0BAA0B;IAC1B,8DAA8D;IAC9D,OAAO,OAAO,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,WAAW,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAC;AACvE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAC/B,YAAuC,EACvC,OAAe;IAEf,IAAI,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC;QAClC,OAAO,IAAI,YAAY,CAAC;YACtB,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IAED,OAAO,YAAY,CAAC;QAClB,OAAO;KACR,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,cAAe,SAAQ,KAAK;IAC9B,IAAI,GAAG,eAAe,CAAC;IAEhC,YAAY,OAA4B;QACtC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACzB,CAAC;CACF;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,MAAM,CACpB,KAAc,EACd,OAAO,GAAmB,mBAAmB,EAC7C,YAAY,GAA8B,cAAc;IAExD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,IAAI,OAAO,YAAY,KAAK,EAAE,CAAC;YAC7B,MAAM,OAAO,CAAC;QAChB,CAAC;QAED,MAAM,iBAAiB,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IACjD,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAc;IAC7C,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;AACJ,CAAC","sourcesContent":["export type AssertionErrorConstructor =\n | (new (args: { message: string }) => Error)\n | ((args: { message: string }) => Error);\n\n/**\n * Check if a value is a constructor, i.e., a function that can be called with\n * the `new` keyword.\n *\n * @param fn - The value to check.\n * @returns `true` if the value is a constructor, or `false` otherwise.\n */\nfunction isConstructable(\n fn: AssertionErrorConstructor,\n): fn is new (args: { message: string }) => Error {\n /* istanbul ignore next */\n // oxlint-disable-next-line typescript/no-unsafe-member-access\n return Boolean(typeof fn?.prototype?.constructor?.name === 'string');\n}\n\n/**\n * Initialise an {@link AssertionErrorConstructor} error.\n *\n * @param ErrorWrapper - The error class to use.\n * @param message - The error message.\n * @returns The error object.\n * @internal\n */\nexport function getAssertionError(\n ErrorWrapper: AssertionErrorConstructor,\n message: string,\n): Error {\n if (isConstructable(ErrorWrapper)) {\n return new ErrorWrapper({\n message,\n });\n }\n\n return ErrorWrapper({\n message,\n });\n}\n\n/**\n * The default error class that is thrown if an assertion fails.\n */\nexport class AssertionError extends Error {\n readonly code = 'ERR_ASSERTION';\n\n constructor(options: { message: string }) {\n super(options.message);\n }\n}\n\n/**\n * Same as Node.js assert.\n * If the value is falsy, throws an error, does nothing otherwise.\n *\n * @throws {@link AssertionError} If value is falsy.\n * @param value - The test that should be truthy to pass.\n * @param message - Message to be passed to {@link AssertionError} or an\n * {@link Error} instance to throw.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}. If a custom error class is provided for\n * the `message` argument, this argument is ignored.\n */\nexport function assert(\n value: unknown,\n message: string | Error = 'Assertion failed.',\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value {\n if (!value) {\n if (message instanceof Error) {\n throw message;\n }\n\n throw getAssertionError(ErrorWrapper, message);\n }\n}\n\n/**\n * Use in the default case of a switch that you want to be fully exhaustive.\n * Using this function forces the compiler to enforce exhaustivity during\n * compile-time.\n *\n * @example\n * ```\n * const number = 1;\n * switch (number) {\n * case 0:\n * ...\n * case 1:\n * ...\n * default:\n * assertExhaustive(snapPrefix);\n * }\n * ```\n * @param _object - The object on which the switch is being operated.\n */\nexport function assertExhaustive(_object: never): never {\n throw new Error(\n 'Invalid branch reached. Should be detected during compilation.',\n );\n}\n"]}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export type { NonEmptyArray } from './array.js';
2
2
  export { isNonEmptyArray } from './array.js';
3
+ export type { AssertionErrorConstructor } from './assert.js';
4
+ export { AssertionError, assert, assertExhaustive, getAssertionError, } from './assert.js';
3
5
  export type { Json } from './json.js';
4
6
  export type { Mutable, PartialOrAbsent, RuntimeObject } from './object.js';
5
7
  export { getKnownPropertyNames, hasProperty, isNullOrUndefined, isObject, isPlainObject, } from './object.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,YAAY,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACtC,YAAY,EAAE,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC3E,OAAO,EACL,qBAAqB,EACrB,WAAW,EACX,iBAAiB,EACjB,QAAQ,EACR,aAAa,GACd,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,YAAY,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AAC7D,OAAO,EACL,cAAc,EACd,MAAM,EACN,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACtC,YAAY,EAAE,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC3E,OAAO,EACL,qBAAqB,EACrB,WAAW,EACX,iBAAiB,EACjB,QAAQ,EACR,aAAa,GACd,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { isNonEmptyArray } from './array.js';
2
+ export { AssertionError, assert, assertExhaustive, getAssertionError, } from './assert.js';
2
3
  export { getKnownPropertyNames, hasProperty, isNullOrUndefined, isObject, isPlainObject, } from './object.js';
3
4
  //# 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":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAG7C,OAAO,EACL,qBAAqB,EACrB,WAAW,EACX,iBAAiB,EACjB,QAAQ,EACR,aAAa,GACd,MAAM,aAAa,CAAC","sourcesContent":["export type { NonEmptyArray } from './array.js';\nexport { isNonEmptyArray } from './array.js';\nexport type { Json } from './json.js';\nexport type { Mutable, PartialOrAbsent, RuntimeObject } from './object.js';\nexport {\n getKnownPropertyNames,\n hasProperty,\n isNullOrUndefined,\n isObject,\n isPlainObject,\n} from './object.js';\nexport type { PublicInterface } from './types.js';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C,OAAO,EACL,cAAc,EACd,MAAM,EACN,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AAGrB,OAAO,EACL,qBAAqB,EACrB,WAAW,EACX,iBAAiB,EACjB,QAAQ,EACR,aAAa,GACd,MAAM,aAAa,CAAC","sourcesContent":["export type { NonEmptyArray } from './array.js';\nexport { isNonEmptyArray } from './array.js';\nexport type { AssertionErrorConstructor } from './assert.js';\nexport {\n AssertionError,\n assert,\n assertExhaustive,\n getAssertionError,\n} from './assert.js';\nexport type { Json } from './json.js';\nexport type { Mutable, PartialOrAbsent, RuntimeObject } from './object.js';\nexport {\n getKnownPropertyNames,\n hasProperty,\n isNullOrUndefined,\n isObject,\n isPlainObject,\n} from './object.js';\nexport type { PublicInterface } from './types.js';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/type-utils",
3
- "version": "0.0.0-preview-55fcf3ef7",
3
+ "version": "0.0.0-preview-520b89c2f",
4
4
  "description": "Type guards and helper types for runtime type checking",
5
5
  "keywords": [
6
6
  "Ethereum",