utils.js
1.7 KB
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
"use strict";
const { toString } = Object.prototype;
function isArray(val) {
return toString.call(val) === "[object Array]";
}
function isObject(val) {
return val !== null && typeof val === "object";
}
function isDate(val) {
return toString.call(val) === "[object Date]";
}
function isURLSearchParams(val) {
return typeof URLSearchParams !== "undefined" && val instanceof URLSearchParams;
}
function forEach(obj, fn) {
if (obj === null || typeof obj === "undefined") {
return;
}
if (typeof obj !== "object") {
obj = [obj];
}
if (isArray(obj)) {
for (let i = 0, l = obj.length; i < l; i++) {
fn.call(null, obj[i], i, obj);
}
} else {
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
fn.call(null, obj[key], key, obj);
}
}
}
}
function isPlainObject(obj) {
return Object.prototype.toString.call(obj) === "[object Object]";
}
function deepMerge() {
const result = {};
function assignValue(val, key) {
if (typeof result[key] === "object" && typeof val === "object") {
result[key] = deepMerge(result[key], val);
} else if (typeof val === "object") {
result[key] = deepMerge({}, val);
} else {
result[key] = val;
}
}
for (let i = 0, l = arguments.length; i < l; i++) {
forEach(arguments[i], assignValue);
}
return result;
}
function isUndefined(val) {
return typeof val === "undefined";
}
exports.deepMerge = deepMerge;
exports.forEach = forEach;
exports.isArray = isArray;
exports.isDate = isDate;
exports.isObject = isObject;
exports.isPlainObject = isPlainObject;
exports.isURLSearchParams = isURLSearchParams;
exports.isUndefined = isUndefined;