deep-guards 1.0.1 → 1.0.3
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/README.md +79 -6
- package/dist/compound.d.ts +7 -1
- package/dist/compound.js +7 -1
- package/dist/compound.js.map +1 -1
- package/dist/errors.d.ts +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.js +6 -5
- package/dist/index.js.map +1 -1
- package/dist/macros.d.ts +4 -0
- package/dist/macros.js +4 -0
- package/dist/macros.js.map +1 -0
- package/dist/primitives.d.ts +1 -1
- package/dist/structures.d.ts +3 -2
- package/dist/structures.js +16 -8
- package/dist/structures.js.map +1 -1
- package/package.json +5 -5
- package/src/compound.ts +99 -81
- package/src/errors.ts +17 -17
- package/src/index.ts +6 -5
- package/src/macros.ts +11 -0
- package/src/primitives.ts +23 -23
- package/src/structures.ts +105 -83
- package/tests/compound.test.ts +152 -136
- package/tests/macros.test.ts +15 -0
- package/tests/primitives.test.ts +91 -91
- package/tests/structures.test.ts +128 -110
package/tests/primitives.test.ts
CHANGED
|
@@ -1,91 +1,91 @@
|
|
|
1
|
-
import {
|
|
2
|
-
isBoolean,
|
|
3
|
-
isFunction,
|
|
4
|
-
isInteger,
|
|
5
|
-
isNull,
|
|
6
|
-
isNumber,
|
|
7
|
-
isString,
|
|
8
|
-
isUndefined,
|
|
9
|
-
isUnknown,
|
|
10
|
-
} from "../src
|
|
11
|
-
|
|
12
|
-
describe("isUnknown", () => {
|
|
13
|
-
it("succeeds for any value", () => {
|
|
14
|
-
expect(isUnknown("unknown")).toBe(true);
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe("isAnyFunction", () => {
|
|
19
|
-
it("succeeds for a function", () => {
|
|
20
|
-
expect(isFunction(() => {})).toBe(true);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("fails for any other value", () => {
|
|
24
|
-
expect(isFunction(1)).toBe(false);
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
describe("isNull", () => {
|
|
29
|
-
it("succeeds for null", () => {
|
|
30
|
-
expect(isNull(null)).toBe(true);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("fails for any other value", () => {
|
|
34
|
-
expect(isNull(undefined)).toBe(false);
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
describe("isUndefined", () => {
|
|
39
|
-
it("succeeds for undefined", () => {
|
|
40
|
-
expect(isUndefined(undefined)).toBe(true);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it("fails for any other value", () => {
|
|
44
|
-
expect(isUndefined(null)).toBe(false);
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
describe("isNumber", () => {
|
|
49
|
-
it("succeeds for a number", () => {
|
|
50
|
-
expect(isNumber(1.23)).toBe(true);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("fails for any other type", () => {
|
|
54
|
-
expect(isNumber("foo")).toBe(false);
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
describe("isInteger", () => {
|
|
59
|
-
it("succeeds for an integer", () => {
|
|
60
|
-
expect(isInteger(1)).toBe(true);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it("fails for a non-integer", () => {
|
|
64
|
-
expect(isInteger(1.23)).toBe(false);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("fails for any other type", () => {
|
|
68
|
-
expect(isInteger("foo")).toBe(false);
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
describe("isString", () => {
|
|
73
|
-
it("succeeds for a string", () => {
|
|
74
|
-
expect(isString("Foo bar")).toBe(true);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("fails for other types", () => {
|
|
78
|
-
expect(isString(true)).toBe(false);
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
describe("isBoolean", () => {
|
|
83
|
-
it("succeeds for a boolean", () => {
|
|
84
|
-
expect(isBoolean(true)).toBe(true);
|
|
85
|
-
expect(isBoolean(false)).toBe(true);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("fails for other types", () => {
|
|
89
|
-
expect(isBoolean("foo")).toBe(false);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
1
|
+
import {
|
|
2
|
+
isBoolean,
|
|
3
|
+
isFunction,
|
|
4
|
+
isInteger,
|
|
5
|
+
isNull,
|
|
6
|
+
isNumber,
|
|
7
|
+
isString,
|
|
8
|
+
isUndefined,
|
|
9
|
+
isUnknown,
|
|
10
|
+
} from "../src";
|
|
11
|
+
|
|
12
|
+
describe("isUnknown", () => {
|
|
13
|
+
it("succeeds for any value", () => {
|
|
14
|
+
expect(isUnknown("unknown")).toBe(true);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("isAnyFunction", () => {
|
|
19
|
+
it("succeeds for a function", () => {
|
|
20
|
+
expect(isFunction(() => {})).toBe(true);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("fails for any other value", () => {
|
|
24
|
+
expect(isFunction(1)).toBe(false);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("isNull", () => {
|
|
29
|
+
it("succeeds for null", () => {
|
|
30
|
+
expect(isNull(null)).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("fails for any other value", () => {
|
|
34
|
+
expect(isNull(undefined)).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("isUndefined", () => {
|
|
39
|
+
it("succeeds for undefined", () => {
|
|
40
|
+
expect(isUndefined(undefined)).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("fails for any other value", () => {
|
|
44
|
+
expect(isUndefined(null)).toBe(false);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("isNumber", () => {
|
|
49
|
+
it("succeeds for a number", () => {
|
|
50
|
+
expect(isNumber(1.23)).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("fails for any other type", () => {
|
|
54
|
+
expect(isNumber("foo")).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("isInteger", () => {
|
|
59
|
+
it("succeeds for an integer", () => {
|
|
60
|
+
expect(isInteger(1)).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("fails for a non-integer", () => {
|
|
64
|
+
expect(isInteger(1.23)).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("fails for any other type", () => {
|
|
68
|
+
expect(isInteger("foo")).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe("isString", () => {
|
|
73
|
+
it("succeeds for a string", () => {
|
|
74
|
+
expect(isString("Foo bar")).toBe(true);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("fails for other types", () => {
|
|
78
|
+
expect(isString(true)).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("isBoolean", () => {
|
|
83
|
+
it("succeeds for a boolean", () => {
|
|
84
|
+
expect(isBoolean(true)).toBe(true);
|
|
85
|
+
expect(isBoolean(false)).toBe(true);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("fails for other types", () => {
|
|
89
|
+
expect(isBoolean("foo")).toBe(false);
|
|
90
|
+
});
|
|
91
|
+
});
|
package/tests/structures.test.ts
CHANGED
|
@@ -1,110 +1,128 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
expect(isAnyArray(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
expect(isAnyRecord(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
expect(guard([
|
|
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
|
-
expect(
|
|
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
|
-
expect(guard({
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
});
|
|
1
|
+
import {
|
|
2
|
+
isAnyArray,
|
|
3
|
+
isAnyRecord,
|
|
4
|
+
isArrayOf,
|
|
5
|
+
isBoolean,
|
|
6
|
+
isNumber,
|
|
7
|
+
isObjectOf,
|
|
8
|
+
isRecordOf,
|
|
9
|
+
isString,
|
|
10
|
+
isSymbol,
|
|
11
|
+
isTupleOf,
|
|
12
|
+
} from "../src";
|
|
13
|
+
|
|
14
|
+
describe("isAnyArray", () => {
|
|
15
|
+
it("succeeds for an array", () => {
|
|
16
|
+
expect(isAnyArray([])).toBe(true);
|
|
17
|
+
expect(isAnyArray(["foo", 1, true, null])).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("fails for any other value", () => {
|
|
21
|
+
expect(isAnyArray({})).toBe(false);
|
|
22
|
+
expect(isAnyArray(1)).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("isAnyRecord", () => {
|
|
27
|
+
it("succeeds for a record", () => {
|
|
28
|
+
expect(isAnyRecord({})).toBe(true);
|
|
29
|
+
expect(isAnyRecord({ foo: "bar", baz: 1 })).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("fails for any other value", () => {
|
|
33
|
+
expect(isAnyRecord([])).toBe(false);
|
|
34
|
+
expect(isAnyRecord(1)).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("isArrayOf", () => {
|
|
39
|
+
const guard = isArrayOf(isString);
|
|
40
|
+
|
|
41
|
+
it("succeeds for an array of the value", () => {
|
|
42
|
+
expect(guard([])).toBe(true);
|
|
43
|
+
expect(guard(["foo", "bar", "baz"])).toBe(true);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("fails for any other value", () => {
|
|
47
|
+
expect(guard([1, 2, 3])).toBe(false);
|
|
48
|
+
expect(guard(["foo", "bar", null])).toBe(false);
|
|
49
|
+
expect(guard(1)).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("isTupleOf", () => {
|
|
54
|
+
const guard = isTupleOf(isNumber, isString, isBoolean);
|
|
55
|
+
|
|
56
|
+
it("succeeds for an array of the value", () => {
|
|
57
|
+
expect(guard([1, "foo", true])).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("fails for any other value", () => {
|
|
61
|
+
expect(guard([1, "foo", true, null])).toBe(false);
|
|
62
|
+
expect(guard([1, "foo"])).toBe(false);
|
|
63
|
+
expect(guard(1)).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("isObjectOf", () => {
|
|
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("isRecordOf", () => {
|
|
101
|
+
describe("with valueGuard", () => {
|
|
102
|
+
const guard = isRecordOf(isString, isNumber);
|
|
103
|
+
|
|
104
|
+
it("succeeds for a record of the key/value types", () => {
|
|
105
|
+
expect(guard({})).toBe(true);
|
|
106
|
+
expect(guard({ foo: 1, bar: 2 })).toBe(true);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("fails for any other value", () => {
|
|
110
|
+
expect(guard({ foo: "bar", baz: 1 })).toBe(false);
|
|
111
|
+
expect(guard(1)).toBe(false);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("without valueGuard", () => {
|
|
116
|
+
const guard = isRecordOf(isString);
|
|
117
|
+
|
|
118
|
+
it("succeeds for a record with the key type", () => {
|
|
119
|
+
expect(guard({})).toBe(true);
|
|
120
|
+
expect(guard({ foo: 1, bar: "baz" })).toBe(true);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("fails for any other value", () => {
|
|
124
|
+
expect(guard({ 1: "foo", [Symbol("bar")]: 1 })).toBe(false);
|
|
125
|
+
expect(guard(1)).toBe(false);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
});
|