deep-guards 1.3.0 → 1.4.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/compound.d.ts +2 -0
- package/dist/compound.js +3 -1
- package/dist/compound.js.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/helpers.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/macros.js.map +1 -1
- package/dist/primitives.js.map +1 -1
- package/dist/structures.js.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +44 -21
- package/.yarn.yml +0 -11
- package/.yarnrc.yml +0 -5
- package/eslint.config.js +0 -68
- package/scripts/bump-and-release.ps1 +0 -2
- package/scripts/release.ps1 +0 -10
- package/src/compound.test.ts +0 -254
- package/src/compound.ts +0 -197
- package/src/errors.ts +0 -17
- package/src/helpers.ts +0 -22
- package/src/index.ts +0 -6
- package/src/macros.test.ts +0 -42
- package/src/macros.ts +0 -30
- package/src/primitives.test.ts +0 -94
- package/src/primitives.ts +0 -25
- package/src/structures.test.ts +0 -172
- package/src/structures.ts +0 -110
- package/src/types.ts +0 -4
- package/tsconfig.common.json +0 -24
- package/tsconfig.json +0 -9
- package/tsconfig.test.json +0 -4
package/src/compound.ts
DELETED
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
import { objectKeys } from "./helpers.js";
|
|
2
|
-
|
|
3
|
-
import type { GuardSchemaOf, ObjectKey } from "./helpers.js";
|
|
4
|
-
import type { Guard } from "./types.js";
|
|
5
|
-
|
|
6
|
-
export function isOptional<T>(guard: Guard<T>): Guard<T | undefined> {
|
|
7
|
-
if (typeof guard !== "function") {
|
|
8
|
-
throw new TypeError(
|
|
9
|
-
`isOptional expects a guard parameter. Got instead: ${guard}`,
|
|
10
|
-
);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
return (value): value is T | undefined => value === undefined || guard(value);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function isNullable<T>(guard: Guard<T>): Guard<T | null | undefined> {
|
|
17
|
-
if (typeof guard !== "function") {
|
|
18
|
-
throw new TypeError(
|
|
19
|
-
`isNullable expects a guard parameter. Got instead: ${guard}`,
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return (value: unknown): value is T | null | undefined =>
|
|
24
|
-
value == null || guard(value);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function isNonNullable<T extends NonNullable<unknown>>(
|
|
28
|
-
value: T | null | undefined,
|
|
29
|
-
): value is T {
|
|
30
|
-
return value != null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function isNot<const N>(guard: Guard<N>) {
|
|
34
|
-
if (typeof guard !== "function") {
|
|
35
|
-
throw new TypeError(
|
|
36
|
-
`isNot expects a guard parameter. Got instead: ${guard}`,
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return <const T>(value: T | N): value is T => !guard(value);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function isOneOf<
|
|
44
|
-
const T extends (string | number | boolean | symbol | null | undefined)[],
|
|
45
|
-
>(...values: T): Guard<T[number]> {
|
|
46
|
-
const valueSet = new Set(values);
|
|
47
|
-
return (value) => (valueSet.has as Guard<T[number]>)(value);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function isUnionOf<T extends readonly unknown[]>(
|
|
51
|
-
...guards: GuardSchemaOf<T>
|
|
52
|
-
): Guard<T[number]> {
|
|
53
|
-
if (guards.every((guard) => typeof guard !== "function")) {
|
|
54
|
-
throw new TypeError(
|
|
55
|
-
`isUnionOf expects N guard parameters. Got instead: ${guards}`,
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return (value): value is T => guards.some((guard) => guard(value));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
type ArrayToIntersection<A extends readonly unknown[]> = A extends [
|
|
63
|
-
infer T,
|
|
64
|
-
...infer R,
|
|
65
|
-
]
|
|
66
|
-
? T & ArrayToIntersection<R>
|
|
67
|
-
: unknown;
|
|
68
|
-
|
|
69
|
-
export function isIntersectionOf<T extends readonly unknown[]>(
|
|
70
|
-
...guards: GuardSchemaOf<T>
|
|
71
|
-
): Guard<ArrayToIntersection<T>> {
|
|
72
|
-
if (guards.every((guard) => typeof guard !== "function")) {
|
|
73
|
-
throw new TypeError(
|
|
74
|
-
`isIntersectionOf expects N guard parameters. Got instead: ${guards}`,
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return (value): value is ArrayToIntersection<T> =>
|
|
79
|
-
guards.every((guard) => guard(value));
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export function isExact<const T>(expected: T): Guard<T> {
|
|
83
|
-
// Primitive checks
|
|
84
|
-
if (typeof expected !== "object" || expected === null) {
|
|
85
|
-
// NaN check
|
|
86
|
-
if (typeof expected === "number" && Number.isNaN(expected)) {
|
|
87
|
-
return (value): value is T =>
|
|
88
|
-
typeof value === "number" && Number.isNaN(value);
|
|
89
|
-
}
|
|
90
|
-
return (value): value is T => value === expected;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Date checks
|
|
94
|
-
if (expected instanceof Date) {
|
|
95
|
-
return (value): value is T =>
|
|
96
|
-
value instanceof Date && expected.getTime() === value.getTime();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// RegExp checks
|
|
100
|
-
if (expected instanceof RegExp) {
|
|
101
|
-
return (value): value is T =>
|
|
102
|
-
value instanceof RegExp &&
|
|
103
|
-
expected.source === value.source &&
|
|
104
|
-
expected.flags === value.flags;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Error checks
|
|
108
|
-
if (expected instanceof Error) {
|
|
109
|
-
return (value): value is T =>
|
|
110
|
-
value instanceof Error &&
|
|
111
|
-
expected.name === value.name &&
|
|
112
|
-
expected.message === value.message;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// Map checks
|
|
116
|
-
if (expected instanceof Map) {
|
|
117
|
-
const guards = Array.from(
|
|
118
|
-
expected
|
|
119
|
-
.entries()
|
|
120
|
-
.map(([k, v]) => [k, isExact(v)] as [string, Guard<unknown>]),
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
return (value): value is T =>
|
|
124
|
-
value instanceof Map &&
|
|
125
|
-
expected.size === value.size &&
|
|
126
|
-
guards.every(([k, guard]) => value.has(k) && guard(value.get(k)));
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Set checks
|
|
130
|
-
if (expected instanceof Set) {
|
|
131
|
-
const guards = new Map(
|
|
132
|
-
expected.values().map((v): [unknown, Guard<unknown>] => [v, isExact(v)]),
|
|
133
|
-
);
|
|
134
|
-
|
|
135
|
-
return (value): value is T =>
|
|
136
|
-
value instanceof Set &&
|
|
137
|
-
expected.size === value.size &&
|
|
138
|
-
value.values().every((v) => guards.get(v)?.(v));
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Typed Array checks
|
|
142
|
-
if (ArrayBuffer.isView(expected) && !(expected instanceof DataView)) {
|
|
143
|
-
const guards = Array.from(expected as unknown as ArrayLike<number>).map(
|
|
144
|
-
(v) => isExact(v),
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
return (value): value is T =>
|
|
148
|
-
ArrayBuffer.isView(value) &&
|
|
149
|
-
!(value instanceof DataView) &&
|
|
150
|
-
expected.constructor === value.constructor &&
|
|
151
|
-
(expected as unknown as ArrayLike<number>).length ===
|
|
152
|
-
(value as unknown as ArrayLike<number>).length &&
|
|
153
|
-
guards.every((guard, i) =>
|
|
154
|
-
guard((value as unknown as ArrayLike<number>)[i]),
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// Array checks
|
|
159
|
-
if (Array.isArray(expected)) {
|
|
160
|
-
const guards = expected.map((v) => isExact(v));
|
|
161
|
-
|
|
162
|
-
return (value): value is T =>
|
|
163
|
-
Array.isArray(value) &&
|
|
164
|
-
expected.length === value.length &&
|
|
165
|
-
guards.every((guard, i) => guard(value[i]));
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// Object checks
|
|
169
|
-
const guards = objectKeys(expected).map(
|
|
170
|
-
(k) => [k, isExact(expected[k])] as [ObjectKey, Guard<unknown>],
|
|
171
|
-
);
|
|
172
|
-
|
|
173
|
-
function objectEntriesChecks(value: object): value is T & object {
|
|
174
|
-
const valueKeys = new Set(objectKeys(value) as ObjectKey[]);
|
|
175
|
-
return (
|
|
176
|
-
guards.length === valueKeys.size &&
|
|
177
|
-
guards.every(
|
|
178
|
-
([k, guard]) =>
|
|
179
|
-
valueKeys.has(k) && guard((value as Record<ObjectKey, unknown>)[k]),
|
|
180
|
-
)
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return (value): value is T =>
|
|
185
|
-
typeof value === "object" &&
|
|
186
|
-
value != null &&
|
|
187
|
-
!Array.isArray(value) &&
|
|
188
|
-
objectKeys(value).length === guards.length &&
|
|
189
|
-
objectEntriesChecks(value);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
193
|
-
export function isInstance<C extends abstract new (...args: any) => unknown>(
|
|
194
|
-
cls: C,
|
|
195
|
-
): Guard<InstanceType<C>> {
|
|
196
|
-
return (value): value is InstanceType<C> => value instanceof cls;
|
|
197
|
-
}
|
package/src/errors.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { Guard } from "./types.js";
|
|
2
|
-
|
|
3
|
-
export class GuardError extends Error {
|
|
4
|
-
name = "GuardError";
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function guardOrThrow<T>(
|
|
8
|
-
value: unknown,
|
|
9
|
-
guard: Guard<T>,
|
|
10
|
-
hint?: string,
|
|
11
|
-
): T {
|
|
12
|
-
if (guard(value)) {
|
|
13
|
-
return value;
|
|
14
|
-
} else {
|
|
15
|
-
throw new GuardError(hint ?? "Guard error");
|
|
16
|
-
}
|
|
17
|
-
}
|
package/src/helpers.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { Guard } from "./types.js";
|
|
2
|
-
|
|
3
|
-
export type GuardSchemaOf<O extends object> = {
|
|
4
|
-
[K in keyof O]: Guard<O[K]>;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
export type ObjectKey = string | number | symbol;
|
|
8
|
-
|
|
9
|
-
export const objectKeys = <K extends ObjectKey>(obj: Record<K, unknown>): K[] =>
|
|
10
|
-
(Object.getOwnPropertyNames(obj) as K[]).concat(
|
|
11
|
-
Object.getOwnPropertySymbols(obj) as K[],
|
|
12
|
-
);
|
|
13
|
-
|
|
14
|
-
export function omit<O extends NonNullable<unknown>>(
|
|
15
|
-
obj: O,
|
|
16
|
-
key: keyof O,
|
|
17
|
-
): { [K in keyof O as K extends typeof key ? never : K]: O[K] } {
|
|
18
|
-
const omitted = { ...obj };
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
20
|
-
delete omitted[key];
|
|
21
|
-
return omitted;
|
|
22
|
-
}
|
package/src/index.ts
DELETED
package/src/macros.test.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { isDiscriminatedObjectOf } from "./macros.ts";
|
|
4
|
-
import { isString } from "./primitives.ts";
|
|
5
|
-
import { isObjectOf } from "./structures.ts";
|
|
6
|
-
|
|
7
|
-
describe("isDiscriminatedObjectOf", () => {
|
|
8
|
-
describe("no key override", () => {
|
|
9
|
-
const guard = isDiscriminatedObjectOf(
|
|
10
|
-
"foo",
|
|
11
|
-
isObjectOf({ bar: isString }, true),
|
|
12
|
-
);
|
|
13
|
-
|
|
14
|
-
it("succeeds for an object of the value", () => {
|
|
15
|
-
expect(guard({ type: "foo", bar: "baz" })).toBe(true);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("fails for any other value", () => {
|
|
19
|
-
expect(guard({ type: "foo" })).toBe(false);
|
|
20
|
-
expect(guard({ bar: "baz" })).toBe(false);
|
|
21
|
-
expect(guard(1)).toBe(false);
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
describe("overriding key", () => {
|
|
26
|
-
const guard = isDiscriminatedObjectOf(
|
|
27
|
-
"foo",
|
|
28
|
-
isObjectOf({ bar: isString }, true),
|
|
29
|
-
"test",
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
it("succeeds for an object of the value", () => {
|
|
33
|
-
expect(guard({ test: "foo", bar: "baz" })).toBe(true);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("fails for any other value", () => {
|
|
37
|
-
expect(guard({ test: "foo" })).toBe(false);
|
|
38
|
-
expect(guard({ bar: "baz" })).toBe(false);
|
|
39
|
-
expect(guard(1)).toBe(false);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
});
|
package/src/macros.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { isExact } from "./compound.js";
|
|
2
|
-
import { omit } from "./helpers.js";
|
|
3
|
-
import { isObjectOf } from "./structures.js";
|
|
4
|
-
|
|
5
|
-
import type { ObjectKey } from "./helpers.js";
|
|
6
|
-
import type { Guard } from "./types.js";
|
|
7
|
-
|
|
8
|
-
export function isDiscriminatedObjectOf<
|
|
9
|
-
const T extends string,
|
|
10
|
-
O extends object,
|
|
11
|
-
>(value: T, guard: Guard<O>): Guard<{ type: T } & O>;
|
|
12
|
-
export function isDiscriminatedObjectOf<
|
|
13
|
-
const T extends string,
|
|
14
|
-
O extends object,
|
|
15
|
-
const K extends ObjectKey,
|
|
16
|
-
>(value: T, guard: Guard<O>, key: K): Guard<{ [S in K]: T } & O>;
|
|
17
|
-
export function isDiscriminatedObjectOf<
|
|
18
|
-
const T extends string,
|
|
19
|
-
O extends object,
|
|
20
|
-
>(
|
|
21
|
-
value: T,
|
|
22
|
-
guard: Guard<O>,
|
|
23
|
-
key: ObjectKey = "type",
|
|
24
|
-
): Guard<Record<ObjectKey, T> & O> {
|
|
25
|
-
const discriminatorGuard = isObjectOf({ [key]: isExact(value) }) as Guard<
|
|
26
|
-
Record<ObjectKey, T>
|
|
27
|
-
>;
|
|
28
|
-
return (value): value is Record<ObjectKey, T> & O =>
|
|
29
|
-
discriminatorGuard(value) && guard(omit(value, key));
|
|
30
|
-
}
|
package/src/primitives.test.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
isBoolean,
|
|
5
|
-
isFunction,
|
|
6
|
-
isInteger,
|
|
7
|
-
isNull,
|
|
8
|
-
isNumber,
|
|
9
|
-
isString,
|
|
10
|
-
isUndefined,
|
|
11
|
-
isUnknown,
|
|
12
|
-
} from "./primitives.ts";
|
|
13
|
-
|
|
14
|
-
describe("isUnknown", () => {
|
|
15
|
-
it("succeeds for any value", () => {
|
|
16
|
-
expect(isUnknown("unknown")).toBe(true);
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe("isAnyFunction", () => {
|
|
21
|
-
it("succeeds for a function", () => {
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
23
|
-
expect(isFunction(() => {})).toBe(true);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it("fails for any other value", () => {
|
|
27
|
-
expect(isFunction(1)).toBe(false);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
describe("isNull", () => {
|
|
32
|
-
it("succeeds for null", () => {
|
|
33
|
-
expect(isNull(null)).toBe(true);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("fails for any other value", () => {
|
|
37
|
-
expect(isNull(undefined)).toBe(false);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
describe("isUndefined", () => {
|
|
42
|
-
it("succeeds for undefined", () => {
|
|
43
|
-
expect(isUndefined(undefined)).toBe(true);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("fails for any other value", () => {
|
|
47
|
-
expect(isUndefined(null)).toBe(false);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
describe("isNumber", () => {
|
|
52
|
-
it("succeeds for a number", () => {
|
|
53
|
-
expect(isNumber(1.23)).toBe(true);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it("fails for any other type", () => {
|
|
57
|
-
expect(isNumber("foo")).toBe(false);
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
describe("isInteger", () => {
|
|
62
|
-
it("succeeds for an integer", () => {
|
|
63
|
-
expect(isInteger(1)).toBe(true);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("fails for a non-integer", () => {
|
|
67
|
-
expect(isInteger(1.23)).toBe(false);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it("fails for any other type", () => {
|
|
71
|
-
expect(isInteger("foo")).toBe(false);
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
describe("isString", () => {
|
|
76
|
-
it("succeeds for a string", () => {
|
|
77
|
-
expect(isString("Foo bar")).toBe(true);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it("fails for other types", () => {
|
|
81
|
-
expect(isString(true)).toBe(false);
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
describe("isBoolean", () => {
|
|
86
|
-
it("succeeds for a boolean", () => {
|
|
87
|
-
expect(isBoolean(true)).toBe(true);
|
|
88
|
-
expect(isBoolean(false)).toBe(true);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it("fails for other types", () => {
|
|
92
|
-
expect(isBoolean("foo")).toBe(false);
|
|
93
|
-
});
|
|
94
|
-
});
|
package/src/primitives.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { Guard } from "./types.js";
|
|
2
|
-
|
|
3
|
-
export const isUnknown: Guard<unknown> = (_value): _value is unknown => true;
|
|
4
|
-
|
|
5
|
-
export const isNull: Guard<null> = (value) => value === null;
|
|
6
|
-
|
|
7
|
-
export const isUndefined: Guard<undefined> = (value) => value === undefined;
|
|
8
|
-
|
|
9
|
-
export const isNumber: Guard<number> = (value) => typeof value === "number";
|
|
10
|
-
|
|
11
|
-
export const isInteger: Guard<number> = (value): value is number =>
|
|
12
|
-
Number.isInteger(value);
|
|
13
|
-
|
|
14
|
-
export const isString: Guard<string> = (value) => typeof value === "string";
|
|
15
|
-
|
|
16
|
-
export const isSymbol: Guard<symbol> = (value) => typeof value === "symbol";
|
|
17
|
-
|
|
18
|
-
export const isBoolean: Guard<boolean> = (value) =>
|
|
19
|
-
value === true || value === false;
|
|
20
|
-
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
-
export const isFunction: Guard<(...args: any[]) => unknown> = (
|
|
23
|
-
value,
|
|
24
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
|
-
): value is (...args: any[]) => unknown => typeof value === "function";
|
package/src/structures.test.ts
DELETED
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { isBoolean, isNumber, isString, isSymbol } from "./primitives.ts";
|
|
4
|
-
import {
|
|
5
|
-
isAnyArray,
|
|
6
|
-
isAnyRecord,
|
|
7
|
-
isArrayOf,
|
|
8
|
-
isObjectOf,
|
|
9
|
-
isRecordOf,
|
|
10
|
-
isTupleOf,
|
|
11
|
-
} from "./structures.ts";
|
|
12
|
-
|
|
13
|
-
describe("isAnyArray", () => {
|
|
14
|
-
it("succeeds for an array", () => {
|
|
15
|
-
expect(isAnyArray([])).toBe(true);
|
|
16
|
-
expect(isAnyArray(["foo", 1, true, null])).toBe(true);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("fails for any other value", () => {
|
|
20
|
-
expect(isAnyArray({})).toBe(false);
|
|
21
|
-
expect(isAnyArray(1)).toBe(false);
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
describe("isAnyRecord", () => {
|
|
26
|
-
it("succeeds for a record", () => {
|
|
27
|
-
expect(isAnyRecord({})).toBe(true);
|
|
28
|
-
expect(isAnyRecord({ foo: "bar", baz: 1 })).toBe(true);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("fails for any other value", () => {
|
|
32
|
-
expect(isAnyRecord([])).toBe(false);
|
|
33
|
-
expect(isAnyRecord(1)).toBe(false);
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
describe("isArrayOf", () => {
|
|
38
|
-
const guard = isArrayOf(isString);
|
|
39
|
-
|
|
40
|
-
it("succeeds for an array of the value", () => {
|
|
41
|
-
expect(guard([])).toBe(true);
|
|
42
|
-
expect(guard(["foo", "bar", "baz"])).toBe(true);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it("fails for any other value", () => {
|
|
46
|
-
expect(guard([1, 2, 3])).toBe(false);
|
|
47
|
-
expect(guard(["foo", "bar", null])).toBe(false);
|
|
48
|
-
expect(guard(1)).toBe(false);
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
describe("isTupleOf", () => {
|
|
53
|
-
const guard = isTupleOf(isNumber, isString, isBoolean);
|
|
54
|
-
|
|
55
|
-
it("succeeds for an array of the value", () => {
|
|
56
|
-
expect(guard([1, "foo", true])).toBe(true);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("fails for any other value", () => {
|
|
60
|
-
expect(guard([1, "foo", true, null])).toBe(false);
|
|
61
|
-
expect(guard([1, "foo"])).toBe(false);
|
|
62
|
-
expect(guard(1)).toBe(false);
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
describe("isObjectOf", () => {
|
|
67
|
-
describe("leaky", () => {
|
|
68
|
-
const barSymbol = Symbol("bar");
|
|
69
|
-
const guard = isObjectOf({
|
|
70
|
-
foo: isString,
|
|
71
|
-
[barSymbol]: isNumber,
|
|
72
|
-
baz: isObjectOf({
|
|
73
|
-
qux: isSymbol,
|
|
74
|
-
}),
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("succeeds for an object of the value", () => {
|
|
78
|
-
expect(
|
|
79
|
-
guard({
|
|
80
|
-
foo: "hello",
|
|
81
|
-
[barSymbol]: 1,
|
|
82
|
-
baz: { qux: Symbol("world!") },
|
|
83
|
-
quux: "this should not be checked",
|
|
84
|
-
}),
|
|
85
|
-
).toBe(true);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("fails for any other value", () => {
|
|
89
|
-
expect(
|
|
90
|
-
guard({
|
|
91
|
-
foo: "hello",
|
|
92
|
-
[barSymbol]: "FAIL",
|
|
93
|
-
baz: { qux: Symbol("world!") },
|
|
94
|
-
}),
|
|
95
|
-
).toBe(false);
|
|
96
|
-
expect(guard(1)).toBe(false);
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
describe("exact keys", () => {
|
|
101
|
-
const barSymbol = Symbol("bar");
|
|
102
|
-
const guard = isObjectOf(
|
|
103
|
-
{
|
|
104
|
-
foo: isString,
|
|
105
|
-
[barSymbol]: isNumber,
|
|
106
|
-
baz: isObjectOf({
|
|
107
|
-
qux: isSymbol,
|
|
108
|
-
}),
|
|
109
|
-
},
|
|
110
|
-
true,
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
it("succeeds for an object of the value", () => {
|
|
114
|
-
expect(
|
|
115
|
-
guard({
|
|
116
|
-
foo: "hello",
|
|
117
|
-
[barSymbol]: 1,
|
|
118
|
-
baz: { qux: Symbol("world!") },
|
|
119
|
-
}),
|
|
120
|
-
).toBe(true);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it("fails for any other value", () => {
|
|
124
|
-
expect(
|
|
125
|
-
guard({
|
|
126
|
-
foo: "hello",
|
|
127
|
-
[barSymbol]: 1,
|
|
128
|
-
baz: { qux: Symbol("world!") },
|
|
129
|
-
quux: "this should be checked",
|
|
130
|
-
}),
|
|
131
|
-
).toBe(false);
|
|
132
|
-
expect(
|
|
133
|
-
guard({
|
|
134
|
-
foo: "hello",
|
|
135
|
-
[barSymbol]: "FAIL",
|
|
136
|
-
baz: { qux: Symbol("world!") },
|
|
137
|
-
}),
|
|
138
|
-
).toBe(false);
|
|
139
|
-
expect(guard(1)).toBe(false);
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
describe("isRecordOf", () => {
|
|
145
|
-
describe("with valueGuard", () => {
|
|
146
|
-
const guard = isRecordOf(isString, isNumber);
|
|
147
|
-
|
|
148
|
-
it("succeeds for a record of the key/value types", () => {
|
|
149
|
-
expect(guard({})).toBe(true);
|
|
150
|
-
expect(guard({ foo: 1, bar: 2 })).toBe(true);
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it("fails for any other value", () => {
|
|
154
|
-
expect(guard({ foo: "bar", baz: 1 })).toBe(false);
|
|
155
|
-
expect(guard(1)).toBe(false);
|
|
156
|
-
});
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
describe("without valueGuard", () => {
|
|
160
|
-
const guard = isRecordOf(isString);
|
|
161
|
-
|
|
162
|
-
it("succeeds for a record with the key type", () => {
|
|
163
|
-
expect(guard({})).toBe(true);
|
|
164
|
-
expect(guard({ foo: 1, bar: "baz" })).toBe(true);
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
it("fails for any other value", () => {
|
|
168
|
-
expect(guard({ 1: "foo", [Symbol("bar")]: 1 })).toBe(false);
|
|
169
|
-
expect(guard(1)).toBe(false);
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
});
|