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/structures.ts
DELETED
|
@@ -1,110 +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 const isAnyArray: Guard<unknown[]> = (value) => Array.isArray(value);
|
|
7
|
-
|
|
8
|
-
export const isAnyRecord: Guard<Record<ObjectKey, unknown>> = (
|
|
9
|
-
value,
|
|
10
|
-
): value is Record<ObjectKey, unknown> =>
|
|
11
|
-
value != null && typeof value === "object" && !Array.isArray(value);
|
|
12
|
-
|
|
13
|
-
export function isArrayOf<T>(guard: Guard<T>): Guard<T[]> {
|
|
14
|
-
if (typeof guard !== "function") {
|
|
15
|
-
throw new TypeError(
|
|
16
|
-
`isArrayOf expects a guard parameter. Got instead: ${guard}`,
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return (value): value is T[] => Array.isArray(value) && value.every(guard);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function isTupleOf<T extends readonly unknown[]>(
|
|
24
|
-
...tupleGuards: GuardSchemaOf<T>
|
|
25
|
-
): Guard<T> {
|
|
26
|
-
if (tupleGuards.some((guard) => typeof guard !== "function")) {
|
|
27
|
-
throw new TypeError(
|
|
28
|
-
`isTupleOf expects guard parameters. Got instead: ${JSON.stringify(
|
|
29
|
-
tupleGuards,
|
|
30
|
-
)}`,
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return (value): value is T =>
|
|
35
|
-
Array.isArray(value) &&
|
|
36
|
-
value.length === tupleGuards.length &&
|
|
37
|
-
tupleGuards.every((guard, i) => guard(value[i]));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function isRecordOf<K extends ObjectKey>(
|
|
41
|
-
keyGuard: Guard<K>,
|
|
42
|
-
): Guard<Record<K, unknown>>;
|
|
43
|
-
export function isRecordOf<K extends ObjectKey, V>(
|
|
44
|
-
keyGuard: Guard<K>,
|
|
45
|
-
valueGuard: Guard<V>,
|
|
46
|
-
): Guard<Record<K, V>>;
|
|
47
|
-
export function isRecordOf<K extends ObjectKey, V>(
|
|
48
|
-
keyGuard: Guard<K>,
|
|
49
|
-
valueGuard?: Guard<V>,
|
|
50
|
-
): Guard<Record<K, V>> {
|
|
51
|
-
if (typeof keyGuard !== "function") {
|
|
52
|
-
throw new TypeError(
|
|
53
|
-
`isRecordOf keyGuard expects a guard parameter. Got instead: ${keyGuard}`,
|
|
54
|
-
);
|
|
55
|
-
} else if (valueGuard != null && typeof valueGuard !== "function") {
|
|
56
|
-
throw new TypeError(
|
|
57
|
-
`isRecordOf valueGuard expects an optional guard parameter. Got instead: ${valueGuard}`,
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return (value): value is Record<K, V> =>
|
|
62
|
-
value != null &&
|
|
63
|
-
typeof value === "object" &&
|
|
64
|
-
!Array.isArray(value) &&
|
|
65
|
-
objectKeys(value).every(
|
|
66
|
-
(key) => keyGuard(key) && (valueGuard?.(value[key]) ?? true),
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
type IsObjectOfGuard<O extends object> = O extends unknown[]
|
|
71
|
-
? never
|
|
72
|
-
: // eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
73
|
-
{} extends O
|
|
74
|
-
? never
|
|
75
|
-
: Guard<O>;
|
|
76
|
-
|
|
77
|
-
export function isObjectOf<O extends object>(
|
|
78
|
-
schema: GuardSchemaOf<O>,
|
|
79
|
-
exactKeys: boolean = false,
|
|
80
|
-
): IsObjectOfGuard<O> {
|
|
81
|
-
const schemaUnknown: unknown = schema;
|
|
82
|
-
if (schemaUnknown == null || typeof schemaUnknown !== "object") {
|
|
83
|
-
throw new TypeError(
|
|
84
|
-
`isObjectOf expects a guard schema object. Got instead: ${schemaUnknown}`,
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const schemaKeys = objectKeys(schema);
|
|
89
|
-
if (
|
|
90
|
-
Array.isArray(schema) ||
|
|
91
|
-
schemaKeys.some((key) => typeof schema[key] !== "function")
|
|
92
|
-
) {
|
|
93
|
-
throw new TypeError(
|
|
94
|
-
`isObjectOf expects a guard schema object. Got instead ${JSON.stringify(
|
|
95
|
-
schema,
|
|
96
|
-
)}`,
|
|
97
|
-
);
|
|
98
|
-
} else if (schemaKeys.length === 0) {
|
|
99
|
-
throw new Error("isObjectOf received an empty schema");
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return ((value): value is O =>
|
|
103
|
-
value != null &&
|
|
104
|
-
typeof value === "object" &&
|
|
105
|
-
!Array.isArray(value) &&
|
|
106
|
-
(!exactKeys || schemaKeys.length === objectKeys(value).length) &&
|
|
107
|
-
schemaKeys.every(
|
|
108
|
-
(key) => key in value && schema[key]((value as O)[key]),
|
|
109
|
-
)) as IsObjectOfGuard<O>;
|
|
110
|
-
}
|
package/src/types.ts
DELETED
package/tsconfig.common.json
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"allowJs": false,
|
|
4
|
-
"allowSyntheticDefaultImports": true,
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"downlevelIteration": true,
|
|
7
|
-
"esModuleInterop": true,
|
|
8
|
-
"forceConsistentCasingInFileNames": true,
|
|
9
|
-
"isolatedModules": true,
|
|
10
|
-
"lib": ["dom", "dom.iterable", "esnext"],
|
|
11
|
-
"module": "esnext",
|
|
12
|
-
"moduleResolution": "Bundler",
|
|
13
|
-
"noErrorTruncation": true,
|
|
14
|
-
"noFallthroughCasesInSwitch": true,
|
|
15
|
-
"noUncheckedIndexedAccess": true,
|
|
16
|
-
"resolveJsonModule": true,
|
|
17
|
-
"rewriteRelativeImportExtensions": true,
|
|
18
|
-
"skipLibCheck": true,
|
|
19
|
-
"sourceMap": true,
|
|
20
|
-
"strict": true,
|
|
21
|
-
"target": "esnext",
|
|
22
|
-
"types": ["node"]
|
|
23
|
-
}
|
|
24
|
-
}
|
package/tsconfig.json
DELETED
package/tsconfig.test.json
DELETED