Skip to content

Commit a2546ef

Browse files
committed
fix: remove deprecated is-subset-of
1 parent c9d7eb2 commit a2546ef

File tree

5 files changed

+303
-20
lines changed

5 files changed

+303
-20
lines changed

package-lock.json

-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/fetch-mock/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"@types/glob-to-regexp": "^0.4.4",
2525
"dequal": "^2.0.3",
2626
"glob-to-regexp": "^0.4.1",
27-
"is-subset-of": "^3.1.10",
2827
"regexparam": "^3.0.0"
2928
},
3029
"repository": {

packages/fetch-mock/src/IsSubsetOf.ts

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/** Copied from deprecated https://www.npmjs.com/package/is-subset-of **/
2+
import { Type } from './TypeDescriptor.js';
3+
4+
const allowedTypes = new Set([ 'array', 'object', 'function', 'null' ]);
5+
6+
const isSubsetOf = function (
7+
subset: any[] | Record<string, any | undefined> | null,
8+
superset: any[] | Record<string, any | undefined> | null,
9+
visited: string[] = []
10+
): 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.');
128+
};
129+
130+
isSubsetOf.structural = function (
131+
subset: Record<string, any> | null,
132+
superset: Record<string, any> | null,
133+
visited: string[] = []
134+
): 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;
171+
};
172+
173+
export { isSubsetOf };

packages/fetch-mock/src/Matchers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { RouteConfig } from './Route.js';
22
import { CallLog } from './CallHistory.js';
33
import glob from 'glob-to-regexp';
44
import * as regexparam from 'regexparam';
5-
import { isSubsetOf } from 'is-subset-of';
5+
import { isSubsetOf } from './IsSubsetOf.js';
66
import { dequal as isEqual } from 'dequal';
77
import { normalizeHeaders, getPath, normalizeUrl } from './RequestUtils.js';
88

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
const valueTypes = new Set([ 'boolean', 'number', 'null', 'string', 'undefined' ]);
2+
const referenceTypes = new Set([ 'array', 'function', 'object', 'symbol' ]);
3+
4+
const detectableTypes = new Set([ 'boolean', 'function', 'number', 'string', 'symbol' ]);
5+
const typeConstructors = new Set([ Boolean, Number, String ]);
6+
7+
class TypeDescriptor {
8+
public name: string;
9+
10+
public isValueType: boolean;
11+
12+
public isReferenceType: boolean;
13+
14+
public isArray: boolean;
15+
16+
public isBoolean: boolean;
17+
18+
public isFunction: boolean;
19+
20+
public isNull: boolean;
21+
22+
public isNumber: boolean;
23+
24+
public isObject: boolean;
25+
26+
public isString: boolean;
27+
28+
public isSymbol: boolean;
29+
30+
public isUndefined: boolean;
31+
32+
protected constructor (value: any) {
33+
this.name = TypeDescriptor.of(value);
34+
35+
this.isValueType = TypeDescriptor.isValueType(value);
36+
this.isReferenceType = TypeDescriptor.isReferenceType(value);
37+
this.isArray = TypeDescriptor.isArray(value);
38+
this.isBoolean = TypeDescriptor.isBoolean(value);
39+
this.isFunction = TypeDescriptor.isFunction(value);
40+
this.isNull = TypeDescriptor.isNull(value);
41+
this.isNumber = TypeDescriptor.isNumber(value);
42+
this.isObject = TypeDescriptor.isObject(value);
43+
this.isString = TypeDescriptor.isString(value);
44+
this.isSymbol = TypeDescriptor.isSymbol(value);
45+
this.isUndefined = TypeDescriptor.isUndefined(value);
46+
}
47+
48+
public static of (value: any): string {
49+
if (value === null) {
50+
return 'null';
51+
}
52+
if (value === undefined) {
53+
return 'undefined';
54+
}
55+
56+
const detectedType = typeof value;
57+
58+
if (detectableTypes.has(detectedType)) {
59+
return detectedType;
60+
}
61+
62+
if (detectedType === 'object') {
63+
if (Array.isArray(value)) {
64+
return 'array';
65+
}
66+
67+
if (typeConstructors.has(value.constructor)) {
68+
return value.constructor.name.toLowerCase();
69+
}
70+
71+
return detectedType;
72+
}
73+
74+
throw new Error('Failed due to an unknown type.');
75+
}
76+
77+
public static from (value: any): TypeDescriptor {
78+
return new TypeDescriptor(value);
79+
}
80+
81+
public static isValueType (value: any): value is boolean | number | null | string | undefined {
82+
return valueTypes.has(TypeDescriptor.of(value));
83+
}
84+
85+
// eslint-disable-next-line @typescript-eslint/ban-types
86+
public static isReferenceType (value: any): value is any[] | Function | object | symbol {
87+
return referenceTypes.has(TypeDescriptor.of(value));
88+
}
89+
90+
public static isArray (value: any): value is any[] {
91+
return TypeDescriptor.of(value) === 'array';
92+
}
93+
94+
public static isBoolean (value: any): value is boolean {
95+
return TypeDescriptor.of(value) === 'boolean';
96+
}
97+
98+
// eslint-disable-next-line @typescript-eslint/ban-types
99+
public static isFunction (value: any): value is Function {
100+
return TypeDescriptor.of(value) === 'function';
101+
}
102+
103+
public static isNull (value: any): value is null {
104+
return TypeDescriptor.of(value) === 'null';
105+
}
106+
107+
public static isNumber (value: any): value is number {
108+
return TypeDescriptor.of(value) === 'number';
109+
}
110+
111+
// eslint-disable-next-line @typescript-eslint/ban-types
112+
public static isObject (value: any): value is object {
113+
return TypeDescriptor.of(value) === 'object';
114+
}
115+
116+
public static isString (value: any): value is string {
117+
return TypeDescriptor.of(value) === 'string';
118+
}
119+
120+
public static isSymbol (value: any): value is symbol {
121+
return TypeDescriptor.of(value) === 'symbol';
122+
}
123+
124+
public static isUndefined (value: any): value is undefined {
125+
return TypeDescriptor.of(value) === 'undefined';
126+
}
127+
}
128+
129+
export { TypeDescriptor as Type };

0 commit comments

Comments
 (0)