/
assert.ts
63 lines (58 loc) · 1.39 KB
/
assert.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { MetaplexError } from '../errors';
/**
* Error indicating that an assertion failed.
* @group Errors
*/
export class AssertionError extends Error {
constructor(message: string) {
super(message);
this.name = 'AssertionError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
}
/**
* Assserts that the provided condition is true.
* @internal
*/
export default function assert(
condition: boolean,
message?: string
): asserts condition {
if (!condition) {
throw new AssertionError(message ?? 'Assertion failed');
}
}
/**
* Asserts that both values are strictly equal.
* @internal
*/
assert.equal = function assertEqual<T>(
actual: unknown,
expected: T,
message?: string
): asserts actual is T {
if (actual !== expected) {
throw new AssertionError((message ?? '') + ` ${actual} !== ${expected}`);
}
};
/**
* Asserts that a given object contains the specified
* keys such that their values are defined.
*/
export function assertObjectHasDefinedKeys<
T extends object,
K extends keyof T = keyof T
>(
input: T,
keys: K[],
onError: (missingKeys: K[]) => MetaplexError
): asserts input is { [key in keyof T]: T[key] } & { [key in K]-?: T[key] } {
const missingKeys = keys.filter(
(property) => input?.[property] === undefined
);
if (missingKeys.length > 0) {
throw onError(missingKeys);
}
}