axios 0.27.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of axios might be problematic. Click here for more details.
- package/CHANGELOG.md +178 -940
- package/LICENSE +4 -16
- package/README.md +292 -93
- package/SECURITY.md +4 -3
- package/UPGRADE_GUIDE.md +1 -166
- package/bin/ssl_hotfix.js +22 -0
- package/dist/axios.js +2537 -2211
- package/dist/axios.js.map +1 -0
- package/dist/axios.min.js +2 -3
- package/dist/axios.min.js.map +1 -0
- package/dist/esm/axios.js +2942 -0
- package/dist/esm/axios.js.map +1 -0
- package/dist/esm/axios.min.js +2 -0
- package/dist/esm/axios.min.js.map +1 -0
- package/dist/node/axios.cjs +3750 -0
- package/dist/node/axios.cjs.map +1 -0
- package/gulpfile.js +88 -0
- package/index.d.ts +293 -70
- package/index.js +2 -1
- package/karma.conf.cjs +250 -0
- package/lib/adapters/http.js +371 -212
- package/lib/adapters/index.js +33 -0
- package/lib/adapters/xhr.js +81 -57
- package/lib/axios.js +34 -22
- package/lib/cancel/CancelToken.js +91 -89
- package/lib/cancel/CanceledError.js +9 -6
- package/lib/cancel/isCancel.js +2 -2
- package/lib/core/Axios.js +127 -99
- package/lib/core/AxiosError.js +22 -8
- package/lib/core/AxiosHeaders.js +274 -0
- package/lib/core/InterceptorManager.js +62 -45
- package/lib/core/buildFullPath.js +5 -4
- package/lib/core/dispatchRequest.js +21 -32
- package/lib/core/mergeConfig.js +8 -7
- package/lib/core/settle.js +6 -4
- package/lib/core/transformData.js +15 -9
- package/lib/defaults/index.js +77 -38
- package/lib/defaults/transitional.js +1 -1
- package/lib/env/classes/FormData.js +2 -0
- package/lib/env/data.js +1 -3
- package/lib/helpers/AxiosTransformStream.js +191 -0
- package/lib/helpers/AxiosURLSearchParams.js +58 -0
- package/lib/helpers/bind.js +3 -7
- package/lib/helpers/buildURL.js +24 -38
- package/lib/helpers/combineURLs.js +3 -2
- package/lib/helpers/cookies.js +43 -44
- package/lib/helpers/deprecatedMethod.js +4 -2
- package/lib/helpers/formDataToJSON.js +92 -0
- package/lib/helpers/fromDataURI.js +53 -0
- package/lib/helpers/isAbsoluteURL.js +3 -2
- package/lib/helpers/isAxiosError.js +4 -3
- package/lib/helpers/isURLSameOrigin.js +44 -45
- package/lib/helpers/null.js +1 -1
- package/lib/helpers/parseHeaders.js +24 -22
- package/lib/helpers/parseProtocol.js +3 -3
- package/lib/helpers/speedometer.js +55 -0
- package/lib/helpers/spread.js +3 -2
- package/lib/helpers/throttle.js +33 -0
- package/lib/helpers/toFormData.js +193 -36
- package/lib/helpers/toURLEncodedForm.js +18 -0
- package/lib/helpers/validator.js +20 -15
- package/lib/platform/browser/classes/FormData.js +3 -0
- package/lib/platform/browser/classes/URLSearchParams.js +4 -0
- package/lib/platform/browser/index.js +43 -0
- package/lib/platform/index.js +3 -0
- package/lib/platform/node/classes/FormData.js +3 -0
- package/lib/platform/node/classes/URLSearchParams.js +4 -0
- package/lib/platform/node/index.js +12 -0
- package/lib/utils.js +320 -177
- package/package.json +69 -23
- package/rollup.config.js +90 -0
- package/dist/axios.map +0 -1
- package/dist/axios.min.map +0 -1
- package/lib/defaults/env/FormData.js +0 -2
- package/lib/helpers/normalizeHeaderName.js +0 -12
package/lib/utils.js
CHANGED
@@ -1,76 +1,73 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
|
3
|
+
import bind from './helpers/bind.js';
|
4
4
|
|
5
5
|
// utils is a library of generic helper functions non-specific to axios
|
6
6
|
|
7
|
-
|
7
|
+
const {toString} = Object.prototype;
|
8
|
+
const {getPrototypeOf} = Object;
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
// eslint-disable-next-line func-names
|
12
|
-
return function(thing) {
|
13
|
-
var str = toString.call(thing);
|
10
|
+
const kindOf = (cache => thing => {
|
11
|
+
const str = toString.call(thing);
|
14
12
|
return cache[str] || (cache[str] = str.slice(8, -1).toLowerCase());
|
15
|
-
};
|
16
13
|
})(Object.create(null));
|
17
14
|
|
18
|
-
|
15
|
+
const kindOfTest = (type) => {
|
19
16
|
type = type.toLowerCase();
|
20
|
-
return
|
21
|
-
return kindOf(thing) === type;
|
22
|
-
};
|
17
|
+
return (thing) => kindOf(thing) === type
|
23
18
|
}
|
24
19
|
|
20
|
+
const typeOfTest = type => thing => typeof thing === type;
|
21
|
+
|
25
22
|
/**
|
26
23
|
* Determine if a value is an Array
|
27
24
|
*
|
28
25
|
* @param {Object} val The value to test
|
26
|
+
*
|
29
27
|
* @returns {boolean} True if value is an Array, otherwise false
|
30
28
|
*/
|
31
|
-
|
32
|
-
return Array.isArray(val);
|
33
|
-
}
|
29
|
+
const {isArray} = Array;
|
34
30
|
|
35
31
|
/**
|
36
32
|
* Determine if a value is undefined
|
37
33
|
*
|
38
|
-
* @param {
|
34
|
+
* @param {*} val The value to test
|
35
|
+
*
|
39
36
|
* @returns {boolean} True if the value is undefined, otherwise false
|
40
37
|
*/
|
41
|
-
|
42
|
-
return typeof val === 'undefined';
|
43
|
-
}
|
38
|
+
const isUndefined = typeOfTest('undefined');
|
44
39
|
|
45
40
|
/**
|
46
41
|
* Determine if a value is a Buffer
|
47
42
|
*
|
48
|
-
* @param {
|
43
|
+
* @param {*} val The value to test
|
44
|
+
*
|
49
45
|
* @returns {boolean} True if value is a Buffer, otherwise false
|
50
46
|
*/
|
51
47
|
function isBuffer(val) {
|
52
48
|
return val !== null && !isUndefined(val) && val.constructor !== null && !isUndefined(val.constructor)
|
53
|
-
&&
|
49
|
+
&& isFunction(val.constructor.isBuffer) && val.constructor.isBuffer(val);
|
54
50
|
}
|
55
51
|
|
56
52
|
/**
|
57
53
|
* Determine if a value is an ArrayBuffer
|
58
54
|
*
|
59
|
-
* @
|
60
|
-
*
|
55
|
+
* @param {*} val The value to test
|
56
|
+
*
|
61
57
|
* @returns {boolean} True if value is an ArrayBuffer, otherwise false
|
62
58
|
*/
|
63
|
-
|
59
|
+
const isArrayBuffer = kindOfTest('ArrayBuffer');
|
64
60
|
|
65
61
|
|
66
62
|
/**
|
67
63
|
* Determine if a value is a view on an ArrayBuffer
|
68
64
|
*
|
69
|
-
* @param {
|
65
|
+
* @param {*} val The value to test
|
66
|
+
*
|
70
67
|
* @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
|
71
68
|
*/
|
72
69
|
function isArrayBufferView(val) {
|
73
|
-
|
70
|
+
let result;
|
74
71
|
if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
|
75
72
|
result = ArrayBuffer.isView(val);
|
76
73
|
} else {
|
@@ -82,112 +79,116 @@ function isArrayBufferView(val) {
|
|
82
79
|
/**
|
83
80
|
* Determine if a value is a String
|
84
81
|
*
|
85
|
-
* @param {
|
82
|
+
* @param {*} val The value to test
|
83
|
+
*
|
86
84
|
* @returns {boolean} True if value is a String, otherwise false
|
87
85
|
*/
|
88
|
-
|
89
|
-
|
90
|
-
|
86
|
+
const isString = typeOfTest('string');
|
87
|
+
|
88
|
+
/**
|
89
|
+
* Determine if a value is a Function
|
90
|
+
*
|
91
|
+
* @param {*} val The value to test
|
92
|
+
* @returns {boolean} True if value is a Function, otherwise false
|
93
|
+
*/
|
94
|
+
const isFunction = typeOfTest('function');
|
91
95
|
|
92
96
|
/**
|
93
97
|
* Determine if a value is a Number
|
94
98
|
*
|
95
|
-
* @param {
|
99
|
+
* @param {*} val The value to test
|
100
|
+
*
|
96
101
|
* @returns {boolean} True if value is a Number, otherwise false
|
97
102
|
*/
|
98
|
-
|
99
|
-
return typeof val === 'number';
|
100
|
-
}
|
103
|
+
const isNumber = typeOfTest('number');
|
101
104
|
|
102
105
|
/**
|
103
106
|
* Determine if a value is an Object
|
104
107
|
*
|
105
|
-
* @param {
|
108
|
+
* @param {*} thing The value to test
|
109
|
+
*
|
106
110
|
* @returns {boolean} True if value is an Object, otherwise false
|
107
111
|
*/
|
108
|
-
|
109
|
-
|
110
|
-
|
112
|
+
const isObject = (thing) => thing !== null && typeof thing === 'object';
|
113
|
+
|
114
|
+
/**
|
115
|
+
* Determine if a value is a Boolean
|
116
|
+
*
|
117
|
+
* @param {*} thing The value to test
|
118
|
+
* @returns {boolean} True if value is a Boolean, otherwise false
|
119
|
+
*/
|
120
|
+
const isBoolean = thing => thing === true || thing === false;
|
111
121
|
|
112
122
|
/**
|
113
123
|
* Determine if a value is a plain Object
|
114
124
|
*
|
115
|
-
* @param {
|
116
|
-
*
|
125
|
+
* @param {*} val The value to test
|
126
|
+
*
|
127
|
+
* @returns {boolean} True if value is a plain Object, otherwise false
|
117
128
|
*/
|
118
|
-
|
129
|
+
const isPlainObject = (val) => {
|
119
130
|
if (kindOf(val) !== 'object') {
|
120
131
|
return false;
|
121
132
|
}
|
122
133
|
|
123
|
-
|
134
|
+
const prototype = getPrototypeOf(val);
|
124
135
|
return prototype === null || prototype === Object.prototype;
|
125
136
|
}
|
126
137
|
|
127
138
|
/**
|
128
139
|
* Determine if a value is a Date
|
129
140
|
*
|
130
|
-
* @
|
131
|
-
*
|
141
|
+
* @param {*} val The value to test
|
142
|
+
*
|
132
143
|
* @returns {boolean} True if value is a Date, otherwise false
|
133
144
|
*/
|
134
|
-
|
145
|
+
const isDate = kindOfTest('Date');
|
135
146
|
|
136
147
|
/**
|
137
148
|
* Determine if a value is a File
|
138
149
|
*
|
139
|
-
* @
|
140
|
-
*
|
150
|
+
* @param {*} val The value to test
|
151
|
+
*
|
141
152
|
* @returns {boolean} True if value is a File, otherwise false
|
142
153
|
*/
|
143
|
-
|
154
|
+
const isFile = kindOfTest('File');
|
144
155
|
|
145
156
|
/**
|
146
157
|
* Determine if a value is a Blob
|
147
158
|
*
|
148
|
-
* @
|
149
|
-
*
|
159
|
+
* @param {*} val The value to test
|
160
|
+
*
|
150
161
|
* @returns {boolean} True if value is a Blob, otherwise false
|
151
162
|
*/
|
152
|
-
|
163
|
+
const isBlob = kindOfTest('Blob');
|
153
164
|
|
154
165
|
/**
|
155
166
|
* Determine if a value is a FileList
|
156
167
|
*
|
157
|
-
* @
|
158
|
-
* @param {Object} val The value to test
|
159
|
-
* @returns {boolean} True if value is a File, otherwise false
|
160
|
-
*/
|
161
|
-
var isFileList = kindOfTest('FileList');
|
162
|
-
|
163
|
-
/**
|
164
|
-
* Determine if a value is a Function
|
168
|
+
* @param {*} val The value to test
|
165
169
|
*
|
166
|
-
* @
|
167
|
-
* @returns {boolean} True if value is a Function, otherwise false
|
170
|
+
* @returns {boolean} True if value is a File, otherwise false
|
168
171
|
*/
|
169
|
-
|
170
|
-
return toString.call(val) === '[object Function]';
|
171
|
-
}
|
172
|
+
const isFileList = kindOfTest('FileList');
|
172
173
|
|
173
174
|
/**
|
174
175
|
* Determine if a value is a Stream
|
175
176
|
*
|
176
|
-
* @param {
|
177
|
+
* @param {*} val The value to test
|
178
|
+
*
|
177
179
|
* @returns {boolean} True if value is a Stream, otherwise false
|
178
180
|
*/
|
179
|
-
|
180
|
-
return isObject(val) && isFunction(val.pipe);
|
181
|
-
}
|
181
|
+
const isStream = (val) => isObject(val) && isFunction(val.pipe);
|
182
182
|
|
183
183
|
/**
|
184
184
|
* Determine if a value is a FormData
|
185
185
|
*
|
186
|
-
* @param {
|
186
|
+
* @param {*} thing The value to test
|
187
|
+
*
|
187
188
|
* @returns {boolean} True if value is an FormData, otherwise false
|
188
189
|
*/
|
189
|
-
|
190
|
-
|
190
|
+
const isFormData = (thing) => {
|
191
|
+
const pattern = '[object FormData]';
|
191
192
|
return thing && (
|
192
193
|
(typeof FormData === 'function' && thing instanceof FormData) ||
|
193
194
|
toString.call(thing) === pattern ||
|
@@ -197,48 +198,22 @@ function isFormData(thing) {
|
|
197
198
|
|
198
199
|
/**
|
199
200
|
* Determine if a value is a URLSearchParams object
|
200
|
-
*
|
201
|
-
* @param {
|
201
|
+
*
|
202
|
+
* @param {*} val The value to test
|
203
|
+
*
|
202
204
|
* @returns {boolean} True if value is a URLSearchParams object, otherwise false
|
203
205
|
*/
|
204
|
-
|
206
|
+
const isURLSearchParams = kindOfTest('URLSearchParams');
|
205
207
|
|
206
208
|
/**
|
207
209
|
* Trim excess whitespace off the beginning and end of a string
|
208
210
|
*
|
209
211
|
* @param {String} str The String to trim
|
212
|
+
*
|
210
213
|
* @returns {String} The String freed of excess whitespace
|
211
214
|
*/
|
212
|
-
|
213
|
-
|
214
|
-
}
|
215
|
-
|
216
|
-
/**
|
217
|
-
* Determine if we're running in a standard browser environment
|
218
|
-
*
|
219
|
-
* This allows axios to run in a web worker, and react-native.
|
220
|
-
* Both environments support XMLHttpRequest, but not fully standard globals.
|
221
|
-
*
|
222
|
-
* web workers:
|
223
|
-
* typeof window -> undefined
|
224
|
-
* typeof document -> undefined
|
225
|
-
*
|
226
|
-
* react-native:
|
227
|
-
* navigator.product -> 'ReactNative'
|
228
|
-
* nativescript
|
229
|
-
* navigator.product -> 'NativeScript' or 'NS'
|
230
|
-
*/
|
231
|
-
function isStandardBrowserEnv() {
|
232
|
-
if (typeof navigator !== 'undefined' && (navigator.product === 'ReactNative' ||
|
233
|
-
navigator.product === 'NativeScript' ||
|
234
|
-
navigator.product === 'NS')) {
|
235
|
-
return false;
|
236
|
-
}
|
237
|
-
return (
|
238
|
-
typeof window !== 'undefined' &&
|
239
|
-
typeof document !== 'undefined'
|
240
|
-
);
|
241
|
-
}
|
215
|
+
const trim = (str) => str.trim ?
|
216
|
+
str.trim() : str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
|
242
217
|
|
243
218
|
/**
|
244
219
|
* Iterate over an Array or an Object invoking a function for each item.
|
@@ -251,13 +226,19 @@ function isStandardBrowserEnv() {
|
|
251
226
|
*
|
252
227
|
* @param {Object|Array} obj The object to iterate
|
253
228
|
* @param {Function} fn The callback to invoke for each item
|
229
|
+
*
|
230
|
+
* @param {Boolean} [allOwnKeys = false]
|
231
|
+
* @returns {void}
|
254
232
|
*/
|
255
|
-
function forEach(obj, fn) {
|
233
|
+
function forEach(obj, fn, {allOwnKeys = false} = {}) {
|
256
234
|
// Don't bother if no value provided
|
257
235
|
if (obj === null || typeof obj === 'undefined') {
|
258
236
|
return;
|
259
237
|
}
|
260
238
|
|
239
|
+
let i;
|
240
|
+
let l;
|
241
|
+
|
261
242
|
// Force an array if not already something iterable
|
262
243
|
if (typeof obj !== 'object') {
|
263
244
|
/*eslint no-param-reassign:0*/
|
@@ -266,15 +247,18 @@ function forEach(obj, fn) {
|
|
266
247
|
|
267
248
|
if (isArray(obj)) {
|
268
249
|
// Iterate over array values
|
269
|
-
for (
|
250
|
+
for (i = 0, l = obj.length; i < l; i++) {
|
270
251
|
fn.call(null, obj[i], i, obj);
|
271
252
|
}
|
272
253
|
} else {
|
273
254
|
// Iterate over object keys
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
255
|
+
const keys = allOwnKeys ? Object.getOwnPropertyNames(obj) : Object.keys(obj);
|
256
|
+
const len = keys.length;
|
257
|
+
let key;
|
258
|
+
|
259
|
+
for (i = 0; i < len; i++) {
|
260
|
+
key = keys[i];
|
261
|
+
fn.call(null, obj[key], key, obj);
|
278
262
|
}
|
279
263
|
}
|
280
264
|
}
|
@@ -294,11 +278,12 @@ function forEach(obj, fn) {
|
|
294
278
|
* ```
|
295
279
|
*
|
296
280
|
* @param {Object} obj1 Object to merge
|
281
|
+
*
|
297
282
|
* @returns {Object} Result of all merge properties
|
298
283
|
*/
|
299
284
|
function merge(/* obj1, obj2, obj3, ... */) {
|
300
|
-
|
301
|
-
|
285
|
+
const result = {};
|
286
|
+
const assignValue = (val, key) => {
|
302
287
|
if (isPlainObject(result[key]) && isPlainObject(val)) {
|
303
288
|
result[key] = merge(result[key], val);
|
304
289
|
} else if (isPlainObject(val)) {
|
@@ -310,8 +295,8 @@ function merge(/* obj1, obj2, obj3, ... */) {
|
|
310
295
|
}
|
311
296
|
}
|
312
297
|
|
313
|
-
for (
|
314
|
-
forEach(arguments[i], assignValue);
|
298
|
+
for (let i = 0, l = arguments.length; i < l; i++) {
|
299
|
+
arguments[i] && forEach(arguments[i], assignValue);
|
315
300
|
}
|
316
301
|
return result;
|
317
302
|
}
|
@@ -322,16 +307,18 @@ function merge(/* obj1, obj2, obj3, ... */) {
|
|
322
307
|
* @param {Object} a The object to be extended
|
323
308
|
* @param {Object} b The object to copy properties from
|
324
309
|
* @param {Object} thisArg The object to bind function to
|
325
|
-
*
|
310
|
+
*
|
311
|
+
* @param {Boolean} [allOwnKeys]
|
312
|
+
* @returns {Object} The resulting value of object a
|
326
313
|
*/
|
327
|
-
|
328
|
-
forEach(b,
|
329
|
-
if (thisArg &&
|
314
|
+
const extend = (a, b, thisArg, {allOwnKeys}= {}) => {
|
315
|
+
forEach(b, (val, key) => {
|
316
|
+
if (thisArg && isFunction(val)) {
|
330
317
|
a[key] = bind(val, thisArg);
|
331
318
|
} else {
|
332
319
|
a[key] = val;
|
333
320
|
}
|
334
|
-
});
|
321
|
+
}, {allOwnKeys});
|
335
322
|
return a;
|
336
323
|
}
|
337
324
|
|
@@ -339,9 +326,10 @@ function extend(a, b, thisArg) {
|
|
339
326
|
* Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
|
340
327
|
*
|
341
328
|
* @param {string} content with BOM
|
342
|
-
*
|
329
|
+
*
|
330
|
+
* @returns {string} content value without BOM
|
343
331
|
*/
|
344
|
-
|
332
|
+
const stripBOM = (content) => {
|
345
333
|
if (content.charCodeAt(0) === 0xFEFF) {
|
346
334
|
content = content.slice(1);
|
347
335
|
}
|
@@ -354,11 +342,15 @@ function stripBOM(content) {
|
|
354
342
|
* @param {function} superConstructor
|
355
343
|
* @param {object} [props]
|
356
344
|
* @param {object} [descriptors]
|
345
|
+
*
|
346
|
+
* @returns {void}
|
357
347
|
*/
|
358
|
-
|
359
|
-
function inherits(constructor, superConstructor, props, descriptors) {
|
348
|
+
const inherits = (constructor, superConstructor, props, descriptors) => {
|
360
349
|
constructor.prototype = Object.create(superConstructor.prototype, descriptors);
|
361
350
|
constructor.prototype.constructor = constructor;
|
351
|
+
Object.defineProperty(constructor, 'super', {
|
352
|
+
value: superConstructor.prototype
|
353
|
+
});
|
362
354
|
props && Object.assign(constructor.prototype, props);
|
363
355
|
}
|
364
356
|
|
@@ -366,105 +358,256 @@ function inherits(constructor, superConstructor, props, descriptors) {
|
|
366
358
|
* Resolve object with deep prototype chain to a flat object
|
367
359
|
* @param {Object} sourceObj source object
|
368
360
|
* @param {Object} [destObj]
|
369
|
-
* @param {Function} [filter]
|
361
|
+
* @param {Function|Boolean} [filter]
|
362
|
+
* @param {Function} [propFilter]
|
363
|
+
*
|
370
364
|
* @returns {Object}
|
371
365
|
*/
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
var merged = {};
|
366
|
+
const toFlatObject = (sourceObj, destObj, filter, propFilter) => {
|
367
|
+
let props;
|
368
|
+
let i;
|
369
|
+
let prop;
|
370
|
+
const merged = {};
|
378
371
|
|
379
372
|
destObj = destObj || {};
|
373
|
+
// eslint-disable-next-line no-eq-null,eqeqeq
|
374
|
+
if (sourceObj == null) return destObj;
|
380
375
|
|
381
376
|
do {
|
382
377
|
props = Object.getOwnPropertyNames(sourceObj);
|
383
378
|
i = props.length;
|
384
379
|
while (i-- > 0) {
|
385
380
|
prop = props[i];
|
386
|
-
if (!merged[prop]) {
|
381
|
+
if ((!propFilter || propFilter(prop, sourceObj, destObj)) && !merged[prop]) {
|
387
382
|
destObj[prop] = sourceObj[prop];
|
388
383
|
merged[prop] = true;
|
389
384
|
}
|
390
385
|
}
|
391
|
-
sourceObj =
|
386
|
+
sourceObj = filter !== false && getPrototypeOf(sourceObj);
|
392
387
|
} while (sourceObj && (!filter || filter(sourceObj, destObj)) && sourceObj !== Object.prototype);
|
393
388
|
|
394
389
|
return destObj;
|
395
390
|
}
|
396
391
|
|
397
|
-
|
398
|
-
*
|
392
|
+
/**
|
393
|
+
* Determines whether a string ends with the characters of a specified string
|
394
|
+
*
|
399
395
|
* @param {String} str
|
400
396
|
* @param {String} searchString
|
401
397
|
* @param {Number} [position= 0]
|
398
|
+
*
|
402
399
|
* @returns {boolean}
|
403
400
|
*/
|
404
|
-
|
401
|
+
const endsWith = (str, searchString, position) => {
|
405
402
|
str = String(str);
|
406
403
|
if (position === undefined || position > str.length) {
|
407
404
|
position = str.length;
|
408
405
|
}
|
409
406
|
position -= searchString.length;
|
410
|
-
|
407
|
+
const lastIndex = str.indexOf(searchString, position);
|
411
408
|
return lastIndex !== -1 && lastIndex === position;
|
412
409
|
}
|
413
410
|
|
414
411
|
|
415
412
|
/**
|
416
|
-
* Returns new array from array like object
|
413
|
+
* Returns new array from array like object or null if failed
|
414
|
+
*
|
417
415
|
* @param {*} [thing]
|
418
|
-
*
|
416
|
+
*
|
417
|
+
* @returns {?Array}
|
419
418
|
*/
|
420
|
-
|
419
|
+
const toArray = (thing) => {
|
421
420
|
if (!thing) return null;
|
422
|
-
|
423
|
-
|
424
|
-
|
421
|
+
if (isArray(thing)) return thing;
|
422
|
+
let i = thing.length;
|
423
|
+
if (!isNumber(i)) return null;
|
424
|
+
const arr = new Array(i);
|
425
425
|
while (i-- > 0) {
|
426
426
|
arr[i] = thing[i];
|
427
427
|
}
|
428
428
|
return arr;
|
429
429
|
}
|
430
430
|
|
431
|
+
/**
|
432
|
+
* Checking if the Uint8Array exists and if it does, it returns a function that checks if the
|
433
|
+
* thing passed in is an instance of Uint8Array
|
434
|
+
*
|
435
|
+
* @param {TypedArray}
|
436
|
+
*
|
437
|
+
* @returns {Array}
|
438
|
+
*/
|
431
439
|
// eslint-disable-next-line func-names
|
432
|
-
|
440
|
+
const isTypedArray = (TypedArray => {
|
433
441
|
// eslint-disable-next-line func-names
|
434
|
-
return
|
442
|
+
return thing => {
|
435
443
|
return TypedArray && thing instanceof TypedArray;
|
436
444
|
};
|
437
|
-
})(typeof Uint8Array !== 'undefined' &&
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
445
|
+
})(typeof Uint8Array !== 'undefined' && getPrototypeOf(Uint8Array));
|
446
|
+
|
447
|
+
/**
|
448
|
+
* For each entry in the object, call the function with the key and value.
|
449
|
+
*
|
450
|
+
* @param {Object<any, any>} obj - The object to iterate over.
|
451
|
+
* @param {Function} fn - The function to call for each entry.
|
452
|
+
*
|
453
|
+
* @returns {void}
|
454
|
+
*/
|
455
|
+
const forEachEntry = (obj, fn) => {
|
456
|
+
const generator = obj && obj[Symbol.iterator];
|
457
|
+
|
458
|
+
const iterator = generator.call(obj);
|
459
|
+
|
460
|
+
let result;
|
461
|
+
|
462
|
+
while ((result = iterator.next()) && !result.done) {
|
463
|
+
const pair = result.value;
|
464
|
+
fn.call(obj, pair[0], pair[1]);
|
465
|
+
}
|
466
|
+
}
|
467
|
+
|
468
|
+
/**
|
469
|
+
* It takes a regular expression and a string, and returns an array of all the matches
|
470
|
+
*
|
471
|
+
* @param {string} regExp - The regular expression to match against.
|
472
|
+
* @param {string} str - The string to search.
|
473
|
+
*
|
474
|
+
* @returns {Array<boolean>}
|
475
|
+
*/
|
476
|
+
const matchAll = (regExp, str) => {
|
477
|
+
let matches;
|
478
|
+
const arr = [];
|
479
|
+
|
480
|
+
while ((matches = regExp.exec(str)) !== null) {
|
481
|
+
arr.push(matches);
|
482
|
+
}
|
483
|
+
|
484
|
+
return arr;
|
485
|
+
}
|
486
|
+
|
487
|
+
/* Checking if the kindOfTest function returns true when passed an HTMLFormElement. */
|
488
|
+
const isHTMLForm = kindOfTest('HTMLFormElement');
|
489
|
+
|
490
|
+
const toCamelCase = str => {
|
491
|
+
return str.toLowerCase().replace(/[_-\s]([a-z\d])(\w*)/g,
|
492
|
+
function replacer(m, p1, p2) {
|
493
|
+
return p1.toUpperCase() + p2;
|
494
|
+
}
|
495
|
+
);
|
496
|
+
};
|
497
|
+
|
498
|
+
/* Creating a function that will check if an object has a property. */
|
499
|
+
const hasOwnProperty = (({hasOwnProperty}) => (obj, prop) => hasOwnProperty.call(obj, prop))(Object.prototype);
|
500
|
+
|
501
|
+
/**
|
502
|
+
* Determine if a value is a RegExp object
|
503
|
+
*
|
504
|
+
* @param {*} val The value to test
|
505
|
+
*
|
506
|
+
* @returns {boolean} True if value is a RegExp object, otherwise false
|
507
|
+
*/
|
508
|
+
const isRegExp = kindOfTest('RegExp');
|
509
|
+
|
510
|
+
const reduceDescriptors = (obj, reducer) => {
|
511
|
+
const descriptors = Object.getOwnPropertyDescriptors(obj);
|
512
|
+
const reducedDescriptors = {};
|
513
|
+
|
514
|
+
forEach(descriptors, (descriptor, name) => {
|
515
|
+
if (reducer(descriptor, name, obj) !== false) {
|
516
|
+
reducedDescriptors[name] = descriptor;
|
517
|
+
}
|
518
|
+
});
|
519
|
+
|
520
|
+
Object.defineProperties(obj, reducedDescriptors);
|
521
|
+
}
|
522
|
+
|
523
|
+
/**
|
524
|
+
* Makes all methods read-only
|
525
|
+
* @param {Object} obj
|
526
|
+
*/
|
527
|
+
|
528
|
+
const freezeMethods = (obj) => {
|
529
|
+
reduceDescriptors(obj, (descriptor, name) => {
|
530
|
+
const value = obj[name];
|
531
|
+
|
532
|
+
if (!isFunction(value)) return;
|
533
|
+
|
534
|
+
descriptor.enumerable = false;
|
535
|
+
|
536
|
+
if ('writable' in descriptor) {
|
537
|
+
descriptor.writable = false;
|
538
|
+
return;
|
539
|
+
}
|
540
|
+
|
541
|
+
if (!descriptor.set) {
|
542
|
+
descriptor.set = () => {
|
543
|
+
throw Error('Can not read-only method \'' + name + '\'');
|
544
|
+
};
|
545
|
+
}
|
546
|
+
});
|
547
|
+
}
|
548
|
+
|
549
|
+
const toObjectSet = (arrayOrString, delimiter) => {
|
550
|
+
const obj = {};
|
551
|
+
|
552
|
+
const define = (arr) => {
|
553
|
+
arr.forEach(value => {
|
554
|
+
obj[value] = true;
|
555
|
+
});
|
556
|
+
}
|
557
|
+
|
558
|
+
isArray(arrayOrString) ? define(arrayOrString) : define(String(arrayOrString).split(delimiter));
|
559
|
+
|
560
|
+
return obj;
|
561
|
+
}
|
562
|
+
|
563
|
+
const noop = () => {}
|
564
|
+
|
565
|
+
const toFiniteNumber = (value, defaultValue) => {
|
566
|
+
value = +value;
|
567
|
+
return Number.isFinite(value) ? value : defaultValue;
|
568
|
+
}
|
569
|
+
|
570
|
+
export default {
|
571
|
+
isArray,
|
572
|
+
isArrayBuffer,
|
573
|
+
isBuffer,
|
574
|
+
isFormData,
|
575
|
+
isArrayBufferView,
|
576
|
+
isString,
|
577
|
+
isNumber,
|
578
|
+
isBoolean,
|
579
|
+
isObject,
|
580
|
+
isPlainObject,
|
581
|
+
isUndefined,
|
582
|
+
isDate,
|
583
|
+
isFile,
|
584
|
+
isBlob,
|
585
|
+
isRegExp,
|
586
|
+
isFunction,
|
587
|
+
isStream,
|
588
|
+
isURLSearchParams,
|
589
|
+
isTypedArray,
|
590
|
+
isFileList,
|
591
|
+
forEach,
|
592
|
+
merge,
|
593
|
+
extend,
|
594
|
+
trim,
|
595
|
+
stripBOM,
|
596
|
+
inherits,
|
597
|
+
toFlatObject,
|
598
|
+
kindOf,
|
599
|
+
kindOfTest,
|
600
|
+
endsWith,
|
601
|
+
toArray,
|
602
|
+
forEachEntry,
|
603
|
+
matchAll,
|
604
|
+
isHTMLForm,
|
605
|
+
hasOwnProperty,
|
606
|
+
hasOwnProp: hasOwnProperty, // an alias to avoid ESLint no-prototype-builtins detection
|
607
|
+
reduceDescriptors,
|
608
|
+
freezeMethods,
|
609
|
+
toObjectSet,
|
610
|
+
toCamelCase,
|
611
|
+
noop,
|
612
|
+
toFiniteNumber
|
470
613
|
};
|