Skip to content

Commit aa548e9

Browse files
committed
chore: lint
1 parent 0af2aa4 commit aa548e9

File tree

2 files changed

+287
-255
lines changed

2 files changed

+287
-255
lines changed

packages/fetch-mock/src/IsSubsetOf.ts

+176-160
Original file line numberDiff line numberDiff line change
@@ -1,173 +1,189 @@
11
/** Copied from deprecated https://www.npmjs.com/package/is-subset-of **/
22
import { Type } from './TypeDescriptor.js';
33

4-
const allowedTypes = new Set([ 'array', 'object', 'function', 'null' ]);
4+
const allowedTypes = new Set(['array', 'object', 'function', 'null']);
55

66
const isSubsetOf = function (
7-
subset: any[] | Record<string, any | undefined> | null,
8-
superset: any[] | Record<string, any | undefined> | null,
9-
visited: string[] = []
7+
subset: any[] | Record<string, any | undefined> | null,
8+
superset: any[] | Record<string, any | undefined> | null,
9+
visited: string[] = [],
1010
): boolean {
11-
const subsetType = Type.of(subset);
12-
const supersetType = Type.of(superset);
13-
14-
if (!allowedTypes.has(subsetType)) {
15-
throw new Error(`Type '${subsetType}' is not supported.`);
16-
}
17-
if (!allowedTypes.has(supersetType)) {
18-
throw new Error(`Type '${supersetType}' is not supported.`);
19-
}
20-
21-
if (Type.isFunction(subset)) {
22-
if (!Type.isFunction(superset)) {
23-
throw new Error(`Types '${subsetType}' and '${supersetType}' do not match.`);
24-
}
25-
26-
return subset.toString() === superset.toString();
27-
}
28-
29-
if (Type.isArray(subset)) {
30-
if (!Type.isArray(superset)) {
31-
throw new Error(`Types '${subsetType}' and '${supersetType}' do not match.`);
32-
}
33-
if (subset.length > superset.length) {
34-
return false;
35-
}
36-
37-
for (const subsetItem of subset) {
38-
const subsetItemType = Type.of(subsetItem);
39-
40-
let isItemInSuperset;
41-
42-
switch (subsetItemType) {
43-
case 'array':
44-
case 'object':
45-
case 'function': {
46-
if (visited.includes(subsetItem)) {
47-
continue;
48-
}
49-
50-
visited.push(subsetItem);
51-
52-
isItemInSuperset = superset.some((supersetItem: any): boolean => {
53-
try {
54-
return isSubsetOf(subsetItem, supersetItem, visited);
55-
} catch {
56-
return false;
57-
}
58-
});
59-
break;
60-
}
61-
default: {
62-
isItemInSuperset = superset.includes(subsetItem);
63-
}
64-
}
65-
66-
if (!isItemInSuperset) {
67-
return false;
68-
}
69-
}
70-
71-
return true;
72-
}
73-
74-
if (Type.isObject(subset)) {
75-
if (!Type.isObject(superset) || Type.isArray(superset)) {
76-
throw new Error(`Types '${subsetType}' and '${supersetType}' do not match.`);
77-
}
78-
if (Object.keys(subset).length > Object.keys(superset).length) {
79-
return false;
80-
}
81-
82-
for (const [ subsetKey, subsetValue ] of Object.entries(subset)) {
83-
const supersetValue = superset[subsetKey];
84-
85-
const subsetValueType = Type.of(subsetValue);
86-
87-
switch (subsetValueType) {
88-
case 'array':
89-
case 'object':
90-
case 'function': {
91-
if (visited.includes(subsetValue)) {
92-
continue;
93-
}
94-
95-
visited.push(subsetValue);
96-
97-
try {
98-
const isInSuperset = isSubsetOf(subsetValue, supersetValue, visited);
99-
100-
if (!isInSuperset) {
101-
return false;
102-
}
103-
} catch {
104-
return false;
105-
}
106-
break;
107-
}
108-
default: {
109-
if (subsetValue !== supersetValue) {
110-
return false;
111-
}
112-
}
113-
}
114-
}
115-
116-
return true;
117-
}
118-
119-
if (Type.isNull(subset)) {
120-
if (!Type.isNull(superset)) {
121-
throw new Error(`Types '${subsetType}' and '${supersetType}' do not match.`);
122-
}
123-
124-
return true;
125-
}
126-
127-
throw new Error('Invalid operation.');
11+
const subsetType = Type.of(subset);
12+
const supersetType = Type.of(superset);
13+
14+
if (!allowedTypes.has(subsetType)) {
15+
throw new Error(`Type '${subsetType}' is not supported.`);
16+
}
17+
if (!allowedTypes.has(supersetType)) {
18+
throw new Error(`Type '${supersetType}' is not supported.`);
19+
}
20+
21+
if (Type.isFunction(subset)) {
22+
if (!Type.isFunction(superset)) {
23+
throw new Error(
24+
`Types '${subsetType}' and '${supersetType}' do not match.`,
25+
);
26+
}
27+
28+
return subset.toString() === superset.toString();
29+
}
30+
31+
if (Type.isArray(subset)) {
32+
if (!Type.isArray(superset)) {
33+
throw new Error(
34+
`Types '${subsetType}' and '${supersetType}' do not match.`,
35+
);
36+
}
37+
if (subset.length > superset.length) {
38+
return false;
39+
}
40+
41+
for (const subsetItem of subset) {
42+
const subsetItemType = Type.of(subsetItem);
43+
44+
let isItemInSuperset;
45+
46+
switch (subsetItemType) {
47+
case 'array':
48+
case 'object':
49+
case 'function': {
50+
if (visited.includes(subsetItem)) {
51+
continue;
52+
}
53+
54+
visited.push(subsetItem);
55+
56+
isItemInSuperset = superset.some((supersetItem: any): boolean => {
57+
try {
58+
return isSubsetOf(subsetItem, supersetItem, visited);
59+
} catch {
60+
return false;
61+
}
62+
});
63+
break;
64+
}
65+
default: {
66+
isItemInSuperset = superset.includes(subsetItem);
67+
}
68+
}
69+
70+
if (!isItemInSuperset) {
71+
return false;
72+
}
73+
}
74+
75+
return true;
76+
}
77+
78+
if (Type.isObject(subset)) {
79+
if (!Type.isObject(superset) || Type.isArray(superset)) {
80+
throw new Error(
81+
`Types '${subsetType}' and '${supersetType}' do not match.`,
82+
);
83+
}
84+
if (Object.keys(subset).length > Object.keys(superset).length) {
85+
return false;
86+
}
87+
88+
for (const [subsetKey, subsetValue] of Object.entries(subset)) {
89+
const supersetValue = superset[subsetKey];
90+
91+
const subsetValueType = Type.of(subsetValue);
92+
93+
switch (subsetValueType) {
94+
case 'array':
95+
case 'object':
96+
case 'function': {
97+
if (visited.includes(subsetValue)) {
98+
continue;
99+
}
100+
101+
visited.push(subsetValue);
102+
103+
try {
104+
const isInSuperset = isSubsetOf(
105+
subsetValue,
106+
supersetValue,
107+
visited,
108+
);
109+
110+
if (!isInSuperset) {
111+
return false;
112+
}
113+
} catch {
114+
return false;
115+
}
116+
break;
117+
}
118+
default: {
119+
if (subsetValue !== supersetValue) {
120+
return false;
121+
}
122+
}
123+
}
124+
}
125+
126+
return true;
127+
}
128+
129+
if (Type.isNull(subset)) {
130+
if (!Type.isNull(superset)) {
131+
throw new Error(
132+
`Types '${subsetType}' and '${supersetType}' do not match.`,
133+
);
134+
}
135+
136+
return true;
137+
}
138+
139+
throw new Error('Invalid operation.');
128140
};
129141

130142
isSubsetOf.structural = function (
131-
subset: Record<string, any> | null,
132-
superset: Record<string, any> | null,
133-
visited: string[] = []
143+
subset: Record<string, any> | null,
144+
superset: Record<string, any> | null,
145+
visited: string[] = [],
134146
): boolean {
135-
if (!Type.isObject(subset)) {
136-
throw new Error(`Type '${Type.of(subset)}' is not supported.`);
137-
}
138-
139-
if (!Type.isObject(superset)) {
140-
throw new Error(`Type '${Type.of(superset)}' is not supported.`);
141-
}
142-
143-
for (const [ subsetKey, subsetValue ] of Object.entries(subset)) {
144-
if (superset[subsetKey] === undefined) {
145-
return false;
146-
}
147-
148-
const subsetValueType = Type.of(subsetValue);
149-
const supersetValue = superset[subsetKey];
150-
151-
if (subsetValueType === 'object') {
152-
if (visited.includes(subsetValue)) {
153-
continue;
154-
}
155-
156-
visited.push(subsetValue);
157-
158-
try {
159-
const isInSuperset = isSubsetOf.structural(subsetValue, supersetValue, visited);
160-
161-
if (!isInSuperset) {
162-
return false;
163-
}
164-
} catch {
165-
return false;
166-
}
167-
}
168-
}
169-
170-
return true;
147+
if (!Type.isObject(subset)) {
148+
throw new Error(`Type '${Type.of(subset)}' is not supported.`);
149+
}
150+
151+
if (!Type.isObject(superset)) {
152+
throw new Error(`Type '${Type.of(superset)}' is not supported.`);
153+
}
154+
155+
for (const [subsetKey, subsetValue] of Object.entries(subset)) {
156+
if (superset[subsetKey] === undefined) {
157+
return false;
158+
}
159+
160+
const subsetValueType = Type.of(subsetValue);
161+
const supersetValue = superset[subsetKey];
162+
163+
if (subsetValueType === 'object') {
164+
if (visited.includes(subsetValue)) {
165+
continue;
166+
}
167+
168+
visited.push(subsetValue);
169+
170+
try {
171+
const isInSuperset = isSubsetOf.structural(
172+
subsetValue,
173+
supersetValue,
174+
visited,
175+
);
176+
177+
if (!isInSuperset) {
178+
return false;
179+
}
180+
} catch {
181+
return false;
182+
}
183+
}
184+
}
185+
186+
return true;
171187
};
172188

173189
export { isSubsetOf };

0 commit comments

Comments
 (0)