index.es.js
5.19 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
function debounce(fn, delay) {
var timer = null;
return function () {
var self = this;
var args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(self, args);
}, delay);
};
}
function throttle(fn, wait, delay) {
var timer = null;
var previous = null;
return function () {
var self = this;
var args = arguments;
var now = Date.now();
if (!previous) previous = now;
if (now - previous > wait) {
fn.apply(self, args);
previous = now;
} else if (delay) {
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(self, args);
}, delay);
}
};
}
function set(target, path, value) {
if (!path) return;
var targetTemp = target;
var pathArr = path.split('.');
pathArr.forEach(function (item, index) {
if (index === pathArr.length - 1) {
targetTemp[item] = value;
} else {
if (!targetTemp[item]) targetTemp[item] = {};
targetTemp = targetTemp[item];
}
});
}
function get(target, path, defaultValue) {
if (!path) return target;
var pathArr = path.split('.');
var targetTemp = target;
pathArr.some(function (item, index) {
if (targetTemp[item] === undefined) {
targetTemp = defaultValue;
return true;
} else {
targetTemp = targetTemp[item];
}
});
return targetTemp;
}
function getStore(name) {
try {
return JSON.parse(window.localStorage.getItem(name));
} catch (e) {}
}
function setStore(name, data) {
try {
window.localStorage.setItem(name, JSON.stringify(data));
} catch (e) {}
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
function getType(v) {
return Object.prototype.toString.call(v);
}
function getTypeof(v) {
return typeof v === 'undefined' ? 'undefined' : _typeof(v);
}
function isObject(v) {
return getType(v) === '[object Object]';
}
function isArray(v) {
return getType(v) === '[object Array]';
}
function isFunction(v) {
return getType(v) === '[object Function]';
}
function isString(v) {
return getType(v) === '[object String]';
}
function isBoolean(v) {
return getType(v) === '[object Boolean]';
}
function isEmptyObj(v) {
return isObject(v) && !Object.keys(v).length;
}
function isNumber(v) {
return getType(v) === '[object Number]';
}
function clone(v) {
if (isObject(v)) return Object.assign({}, v);
if (isArray(v)) return v.slice();
}
function cloneDeep(v) {
return JSON.parse(JSON.stringify(v));
}
function kebabToCamel(s) {
return s.replace(/-(\w)/g, function (_, c) {
return c.toUpperCase();
});
}
function camelToKebab(s) {
return s.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}
function unique(arr) {
var result = [];
arr.forEach(function (item) {
if (!~result.indexOf(item)) result.push(item);
});
return result;
}
function getLinearValue(x1, y1, x2, y2, x3) {
var k = (y2 - y1) / (x2 - x1);
var b = y1 - x1 * k;
if (x3 == null) {
return { k: k, b: b };
} else {
return x3 * k + b;
}
}
function getFnAndObjValue(target, key) {
return isFunction(target) ? target(key) : !isObject(target) ? key : target[key] != null ? target[key] : key;
}
function arrDelItem(arr, diffItem) {
return arr.filter(function (item) {
return diffItem !== item;
});
}
var arrDelArrItem = function arrDelArrItem(arr, diffArr) {
return arr.filter(function (item) {
return !~diffArr.indexOf(item);
});
};
function getArrMin(arr) {
return Math.min.apply(null, arr);
}
function getArrMax(arr) {
return Math.max.apply(null, arr);
}
function toArray(v) {
return Array.prototype.slice.call(v);
}
function noop() {}
function hasOwn(source, target) {
return Object.prototype.hasOwnProperty.call(source, target);
}
var extend = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (hasOwn(source, key)) target[key] = source[key];
}
}
return target;
};
function isEqual(alice, bob) {
if (alice === bob) return true;
if (alice === null || bob === null || getTypeof(alice) !== 'object' || getTypeof(bob) !== 'object') {
return alice === bob;
}
for (var key in alice) {
if (!hasOwn(alice, key)) continue;
var aliceValue = alice[key];
var bobValue = bob[key];
var aliceType = getTypeof(aliceValue);
if (getTypeof(bobValue) === 'undefined') {
return false;
} else if (aliceType === 'object') {
if (!isEqual(aliceValue, bobValue)) return false;
} else if (aliceValue !== bobValue) {
return false;
}
}
for (var _key in bob) {
if (!hasOwn(bob, _key)) continue;
if (getTypeof(alice)[_key] === 'undefined') return false;
}
return true;
}
export { debounce, throttle, set, get, getStore, setStore, clone, cloneDeep, getType, getTypeof, isObject, isArray, isFunction, isString, isBoolean, isEmptyObj, isNumber, kebabToCamel, camelToKebab, unique, getLinearValue, getFnAndObjValue, arrDelItem, arrDelArrItem, getArrMin, getArrMax, toArray, noop, extend, isEqual, hasOwn };