forked from web-std/io
-
Notifications
You must be signed in to change notification settings - Fork 23
/
is.js
119 lines (110 loc) · 2.87 KB
/
is.js
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
import Stream from "stream";
/**
* Is.js
*
* Object type checks.
*/
const NAME = Symbol.toStringTag;
/**
* Check if `obj` is a URLSearchParams object
* ref: https://github.com/node-fetch/node-fetch/issues/296#issuecomment-307598143
*
* @param {any} object
* @return {obj is URLSearchParams}
*/
export const isURLSearchParameters = (object) => {
return (
typeof object === "object" &&
typeof object.append === "function" &&
typeof object.delete === "function" &&
typeof object.get === "function" &&
typeof object.getAll === "function" &&
typeof object.has === "function" &&
typeof object.set === "function" &&
typeof object.sort === "function" &&
object[NAME] === "URLSearchParams"
);
};
/**
* Check if `object` is a W3C `Blob` object (which `File` inherits from)
*
* @param {*} object
* @return {object is Blob}
*/
export const isBlob = (object) => {
return (
typeof object === "object" &&
typeof object.arrayBuffer === "function" &&
typeof object.type === "string" &&
typeof object.stream === "function" &&
typeof object.constructor === "function" &&
/^(Blob|File)$/.test(object[NAME])
);
};
/**
* Check if `obj` is a spec-compliant `FormData` object
*
* @param {*} object
* @return {object is FormData}
*/
export function isFormData(object) {
return (
typeof object === "object" &&
typeof object.append === "function" &&
typeof object.set === "function" &&
typeof object.get === "function" &&
typeof object.getAll === "function" &&
typeof object.delete === "function" &&
typeof object.keys === "function" &&
typeof object.values === "function" &&
typeof object.entries === "function" &&
typeof object.constructor === "function" &&
object[NAME] === "FormData"
);
}
/**
* Detect form data input from form-data module
*
* @param {any} value
* @returns {value is Stream & {getBoundary():string, hasKnownLength():boolean, getLengthSync():number|null}}
*/
export const isMultipartFormDataStream = (value) => {
return (
value instanceof Stream === true &&
typeof value.getBoundary === "function" &&
typeof value.hasKnownLength === "function" &&
typeof value.getLengthSync === "function"
);
};
/**
* Check if `obj` is an instance of AbortSignal.
*
* @param {any} object
* @return {obj is AbortSignal}
*/
export const isAbortSignal = (object) => {
return (
typeof object === "object" &&
(object[NAME] === "AbortSignal" || object[NAME] === "EventTarget")
);
};
/**
* Check if `value` is a ReadableStream.
*
* @param {*} value
* @returns {value is ReadableStream}
*/
export const isReadableStream = (value) => {
return (
typeof value === "object" &&
typeof value.getReader === "function" &&
typeof value.cancel === "function" &&
typeof value.tee === "function"
);
};
/**
*
* @param {any} value
* @returns {value is Iterable<unknown>}
*/
export const isIterable = (value) => value && Symbol.iterator in value;