-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
161 lines (145 loc) · 4.3 KB
/
utils.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import type { Buffer } from "https://deno.land/[email protected]/node/buffer.ts";
// the following is from axios library with a bit of touch up for ts
const toString = Object.prototype.toString;
/**
* Determine if a value is an Array
*
* @param val The value to test
* @returns {boolean} True if value is an Array, otherwise false
*/
export function isArray(val: unknown): val is Array<unknown> {
return toString.call(val) === "[object Array]";
}
/**
* Determine if a value is a FormData
*
* @param val The value to test
* @returns {boolean} True if value is an FormData, otherwise false
*/
export function isFormData(val: unknown): val is FormData {
return (typeof FormData !== "undefined") && (val instanceof FormData);
}
/**
* Determine if a value is an Object
*
* @param val The value to test
* @returns {boolean} True if value is an Object, otherwise false
*/
export function isObject(val: unknown): val is Record<string, unknown> {
return !!val && typeof val === "object";
}
/**
* Determine if a value is undefined
*
* @param val The value to test
* @returns {boolean} True if the value is undefined, otherwise false
*/
export function isUndefined(val: unknown): val is undefined {
return typeof val === "undefined";
}
/**
* Determine if a value is a Buffer
*
* @param val The value to test
* @returns {boolean} True if value is a Buffer, otherwise false
*/
export function isBuffer(val: unknown): val is Buffer {
// deno-lint-ignore ban-ts-comment
// @ts-ignore
return val !== null && !isUndefined(val) && val.constructor !== null &&
// deno-lint-ignore ban-ts-comment
// @ts-ignore
!isUndefined(val.constructor) &&
// deno-lint-ignore ban-ts-comment
// @ts-ignore
typeof val.constructor.isBuffer === "function" &&
// deno-lint-ignore ban-ts-comment
// @ts-ignore
val.constructor.isBuffer(val);
}
/**
* Determine if a value is an ArrayBuffer
*
* @param val The value to test
* @returns {boolean} True if value is an ArrayBuffer, otherwise false
*/
export function isArrayBuffer(val: unknown): val is ArrayBuffer {
return toString.call(val) === "[object ArrayBuffer]";
}
/**
* Determine if a value is a view on an ArrayBuffer
*
* @param val The value to test
* @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
*/
export function isArrayBufferView(val: unknown): val is ArrayBufferView {
let result;
if ((typeof ArrayBuffer !== "undefined") && (ArrayBuffer.isView)) {
result = ArrayBuffer.isView(val);
} else {
// deno-lint-ignore ban-ts-comment
// @ts-ignore
result = (val) && (val.buffer) && (val.buffer instanceof ArrayBuffer);
}
return result;
}
/**
* Determine if a value is a File
*
* @param val The value to test
* @returns {boolean} True if value is a File, otherwise false
*/
export function isFile(val: unknown): val is File {
return toString.call(val) === "[object File]";
}
/**
* Determine if a value is a Blob
*
* @param val The value to test
* @returns {boolean} True if value is a Blob, otherwise false
*/
export function isBlob(val: unknown): val is Blob {
return toString.call(val) === "[object Blob]";
}
/**
* Determine if a value is a Function
*
* @param val The value to test
* @returns {boolean} True if value is a Function, otherwise false
*/
// deno-lint-ignore ban-types
export function isFunction(val: unknown): val is Function {
return toString.call(val) === "[object Function]";
}
/**
* Determine if a value is a Stream
*
* @param val The value to test
* @returns {boolean} True if value is a Stream, otherwise false
*/
export function isStream(val: unknown): val is ReadableStream {
return isObject(val) && isFunction(val.pipe);
}
/**
* Determine if a value is a URLSearchParams object
*
* @param val The value to test
* @returns {boolean} True if value is a URLSearchParams object, otherwise false
*/
export function isURLSearchParams(val: unknown): val is URLSearchParams {
return typeof URLSearchParams !== "undefined" &&
val instanceof URLSearchParams;
}
export function delay(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
export function getFirstDefined<T>(...values: [...T[], NonNullable<T>]) {
for (const val of values) {
if (val !== undefined) {
return val;
}
}
return values[values.length - 1] as NonNullable<T>;
}