-
Notifications
You must be signed in to change notification settings - Fork 26.2k
/
Copy pathassert.ts
149 lines (126 loc) Β· 4.56 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
// The functions in this file verify that the assumptions we are making
// about state in an instruction are correct before implementing any logic.
// They are meant only to be called in dev mode as sanity checks.
import {getActiveConsumer} from '../../primitives/signals';
import {stringify} from './stringify';
export function assertNumber(actual: any, msg: string): asserts actual is number {
if (!(typeof actual === 'number')) {
throwError(msg, typeof actual, 'number', '===');
}
}
export function assertNumberInRange(
actual: any,
minInclusive: number,
maxInclusive: number,
): asserts actual is number {
assertNumber(actual, 'Expected a number');
assertLessThanOrEqual(actual, maxInclusive, 'Expected number to be less than or equal to');
assertGreaterThanOrEqual(actual, minInclusive, 'Expected number to be greater than or equal to');
}
export function assertString(actual: any, msg: string): asserts actual is string {
if (!(typeof actual === 'string')) {
throwError(msg, actual === null ? 'null' : typeof actual, 'string', '===');
}
}
export function assertFunction(actual: any, msg: string): asserts actual is Function {
if (!(typeof actual === 'function')) {
throwError(msg, actual === null ? 'null' : typeof actual, 'function', '===');
}
}
export function assertEqual<T>(actual: T, expected: T, msg: string) {
if (!(actual == expected)) {
throwError(msg, actual, expected, '==');
}
}
export function assertNotEqual<T>(actual: T, expected: T, msg: string): asserts actual is T {
if (!(actual != expected)) {
throwError(msg, actual, expected, '!=');
}
}
export function assertSame<T>(actual: T, expected: T, msg: string): asserts actual is T {
if (!(actual === expected)) {
throwError(msg, actual, expected, '===');
}
}
export function assertNotSame<T>(actual: T, expected: T, msg: string) {
if (!(actual !== expected)) {
throwError(msg, actual, expected, '!==');
}
}
export function assertLessThan<T>(actual: T, expected: T, msg: string): asserts actual is T {
if (!(actual < expected)) {
throwError(msg, actual, expected, '<');
}
}
export function assertLessThanOrEqual<T>(actual: T, expected: T, msg: string): asserts actual is T {
if (!(actual <= expected)) {
throwError(msg, actual, expected, '<=');
}
}
export function assertGreaterThan<T>(actual: T, expected: T, msg: string): asserts actual is T {
if (!(actual > expected)) {
throwError(msg, actual, expected, '>');
}
}
export function assertGreaterThanOrEqual<T>(
actual: T,
expected: T,
msg: string,
): asserts actual is T {
if (!(actual >= expected)) {
throwError(msg, actual, expected, '>=');
}
}
export function assertNotDefined<T>(actual: T, msg: string) {
if (actual != null) {
throwError(msg, actual, null, '==');
}
}
export function assertDefined<T>(actual: T | null | undefined, msg: string): asserts actual is T {
if (actual == null) {
throwError(msg, actual, null, '!=');
}
}
export function throwError(msg: string): never;
export function throwError(msg: string, actual: any, expected: any, comparison: string): never;
export function throwError(msg: string, actual?: any, expected?: any, comparison?: string): never {
throw new Error(
`ASSERTION ERROR: ${msg}` +
(comparison == null ? '' : ` [Expected=> ${expected} ${comparison} ${actual} <=Actual]`),
);
}
export function assertDomNode(node: any): asserts node is Node {
if (!(node instanceof Node)) {
throwError(`The provided value must be an instance of a DOM Node but got ${stringify(node)}`);
}
}
export function assertElement(node: any): asserts node is Element {
if (!(node instanceof Element)) {
throwError(`The provided value must be an element but got ${stringify(node)}`);
}
}
export function assertIndexInRange(arr: any[], index: number) {
assertDefined(arr, 'Array must be defined.');
const maxLen = arr.length;
if (index < 0 || index >= maxLen) {
throwError(`Index expected to be less than ${maxLen} but got ${index}`);
}
}
export function assertOneOf(value: any, ...validValues: any[]) {
if (validValues.indexOf(value) !== -1) return true;
throwError(
`Expected value to be one of ${JSON.stringify(validValues)} but was ${JSON.stringify(value)}.`,
);
}
export function assertNotReactive(fn: string): void {
if (getActiveConsumer() !== null) {
throwError(`${fn}() should never be called in a reactive context.`);
}
}