-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_has_properties.ts
More file actions
130 lines (112 loc) · 2.85 KB
/
object_has_properties.ts
File metadata and controls
130 lines (112 loc) · 2.85 KB
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
import { AssertionError } from "@std/assert";
import { assertIsObject, isObject } from "./is_object.ts";
/**
* Check if an object has a property
*/
function hasProperty<T>(
obj: T,
property: PropertyKey,
deep: boolean,
): boolean {
let currentProto = obj;
while (currentProto !== null && currentProto !== undefined) {
if (Object.hasOwn(currentProto, property)) {
return true;
}
const descriptor = Object.getOwnPropertyDescriptor(
currentProto,
property,
);
if (descriptor !== undefined) {
return true;
}
if (!deep) {
return false;
}
currentProto = Object.getPrototypeOf(currentProto);
}
return false;
}
export function getKeyDiff(
value: object,
keys: Array<PropertyKey>,
deep: boolean,
): Array<PropertyKey> {
const diff: PropertyKey[] = [];
for (const key of keys) {
if (!hasProperty(value, key, deep)) {
diff.push(key);
}
}
return diff;
}
/**
* Checks if an object has given property keys
*/
export function objectHasProperties<T extends PropertyKey = PropertyKey>(
value: unknown,
keys: Array<T>,
): value is Record<T, unknown> {
if (!isObject(value)) {
return false;
}
const diff = getKeyDiff(value, keys, false);
return diff.length < 1;
}
/**
* Asserts that an object has given property keys
*/
export function assertObjectHasProperties<T extends PropertyKey = PropertyKey>(
value: unknown,
keys: Array<T>,
msg?: string,
): asserts value is Record<T, unknown> {
assertIsObject(value);
const diff = getKeyDiff(value, keys, false);
if (diff.length > 0) {
const msgSuffix = msg ? `: ${msg}` : ".";
const message = `The object is missing the following keys: [${
keys.map(String).join(",")
}]${msgSuffix}`;
throw new AssertionError(message);
}
}
/**
* Checks deeply if an object has given property keys
*
* Use when wanting to check for getters and other prototype
* properties on multilevel inheritance
*/
export function objectHasPropertiesDeep<T extends PropertyKey = PropertyKey>(
value: unknown,
keys: Array<T>,
): value is Record<T, unknown> {
if (!isObject(value)) {
return false;
}
const diff = getKeyDiff(value, keys, true);
return diff.length < 1;
}
/**
* Asserts that an object has given property keys
*
* Use when wanting to check for getters and other prototype
* properties on multilevel inheritance
*/
export function assertObjectHasPropertiesDeep<
T extends PropertyKey = PropertyKey,
>(
value: unknown,
keys: Array<T>,
msg?: string,
): asserts value is Record<T, unknown> {
assertIsObject(value);
const diff = getKeyDiff(value, keys, true);
if (diff.length > 0) {
const msgSuffix = msg ? `: ${msg}` : ".";
const message = `The object is missing the following keys: [${
keys.map(String).join(",")
}]${msgSuffix}`;
throw new AssertionError(message);
}
}