@lntvow/utils 1.7.1 → 1.7.2
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.
- package/dist/index.cjs +141 -2
- package/dist/index.mjs +122 -88
- package/package.json +1 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,141 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
function getRandomColor() {
|
|
4
|
+
return `#${Math.random().toString(16).slice(2, 8).padEnd(6, "0")}`;
|
|
5
|
+
}
|
|
6
|
+
function getRandomString(number) {
|
|
7
|
+
return number > 10 ? getRandomString(10) + getRandomString(number - 10) : Math.random().toString(36).padEnd(12, "0").slice(2, number + 2);
|
|
8
|
+
}
|
|
9
|
+
const extend = Object.assign;
|
|
10
|
+
const objectToString = Object.prototype.toString;
|
|
11
|
+
const toTypeString = (value) => objectToString.call(value);
|
|
12
|
+
const log = (...arg) => console.log(
|
|
13
|
+
`%c ${arg[0]}: `,
|
|
14
|
+
"padding: 2px 1px; border-radius: 3px 3px 3px 3px; color: #fff; background: #000; font-weight: bold;",
|
|
15
|
+
arg.slice(1)
|
|
16
|
+
);
|
|
17
|
+
const warn = (msg) => console.warn(msg);
|
|
18
|
+
const error = (msg) => console.error(msg);
|
|
19
|
+
const isMap = (val) => toTypeString(val) === "[object Map]";
|
|
20
|
+
const isSet = (val) => toTypeString(val) === "[object Set]";
|
|
21
|
+
const isDate = (val) => toTypeString(val) === "[object Date]";
|
|
22
|
+
const isRegExp = (val) => toTypeString(val) === "[object RegExp]";
|
|
23
|
+
const isFunction = (val) => typeof val === "function";
|
|
24
|
+
const isString = (val) => typeof val === "string";
|
|
25
|
+
const isSymbol = (val) => typeof val === "symbol";
|
|
26
|
+
const isObject = (val) => val !== null && typeof val === "object";
|
|
27
|
+
const isPromise = (val) => isObject(val) && isFunction(val.then) && isFunction(val.catch);
|
|
28
|
+
const isUndef = (val) => val === void 0 || val === null;
|
|
29
|
+
const isNull = (val) => val === null;
|
|
30
|
+
function isDef(v) {
|
|
31
|
+
return v !== void 0 && v !== null;
|
|
32
|
+
}
|
|
33
|
+
const isArray = Array.isArray;
|
|
34
|
+
const hasOwn = (target, key) => Object.prototype.hasOwnProperty.call(target, key);
|
|
35
|
+
const hasChanged = (oldValue, newValue) => oldValue !== newValue;
|
|
36
|
+
function deepClone(target) {
|
|
37
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
38
|
+
function _deepClone(value) {
|
|
39
|
+
if (isDate(value))
|
|
40
|
+
return new Date(value);
|
|
41
|
+
if (isRegExp(value))
|
|
42
|
+
return new RegExp(value);
|
|
43
|
+
if (isObject(value)) {
|
|
44
|
+
if (map.has(value))
|
|
45
|
+
return map.get(value);
|
|
46
|
+
let clone = {};
|
|
47
|
+
if (isArray(value)) {
|
|
48
|
+
clone = [];
|
|
49
|
+
map.set(value, clone);
|
|
50
|
+
for (let i = 0; i < value.length; i++) {
|
|
51
|
+
clone[i] = _deepClone(value[i]);
|
|
52
|
+
}
|
|
53
|
+
return clone;
|
|
54
|
+
}
|
|
55
|
+
map.set(value, clone);
|
|
56
|
+
for (const key in value) {
|
|
57
|
+
if (hasOwn(value, key)) {
|
|
58
|
+
clone[key] = _deepClone(value[key]);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return clone;
|
|
62
|
+
} else {
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return _deepClone(target);
|
|
67
|
+
}
|
|
68
|
+
function deepMerge(template, source, options) {
|
|
69
|
+
return isArray(template) ? arrayMerge(template, source, options) : mergeObject(template, source, options);
|
|
70
|
+
}
|
|
71
|
+
function mergeObject(template, source, options) {
|
|
72
|
+
const target = process(source, options);
|
|
73
|
+
const templateKeys = Object.keys(template);
|
|
74
|
+
templateKeys.forEach((key) => {
|
|
75
|
+
if (isUndef(source[key])) {
|
|
76
|
+
target[key] = process(template[key], options);
|
|
77
|
+
} else {
|
|
78
|
+
target[key] = isObject(template[key]) && isObject(source[key]) ? deepMerge(template[key], source[key], options) : process(source[key], options);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
return target;
|
|
82
|
+
}
|
|
83
|
+
function arrayMerge(template, source, options) {
|
|
84
|
+
return isArray(source) ? process(template, options).concat(process(source, options)) : process(source, options);
|
|
85
|
+
}
|
|
86
|
+
function process(source, options) {
|
|
87
|
+
const { isDeepClone = true } = options || {};
|
|
88
|
+
return isDeepClone ? deepClone(source) : source;
|
|
89
|
+
}
|
|
90
|
+
function debounce(target, wait = 1e3) {
|
|
91
|
+
let timer = null;
|
|
92
|
+
return function(...args) {
|
|
93
|
+
if (timer)
|
|
94
|
+
clearTimeout(timer);
|
|
95
|
+
timer = setTimeout(() => {
|
|
96
|
+
target.apply(this, ...args);
|
|
97
|
+
}, wait);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function throttle(target, wait = 1e3) {
|
|
101
|
+
let timer = null;
|
|
102
|
+
return function(...args) {
|
|
103
|
+
if (isNull(timer)) {
|
|
104
|
+
timer = Date.now();
|
|
105
|
+
return target.apply(this, ...args);
|
|
106
|
+
} else {
|
|
107
|
+
const now = Date.now();
|
|
108
|
+
if (now - timer >= wait) {
|
|
109
|
+
timer = now;
|
|
110
|
+
return target.apply(this, ...args);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
exports.debounce = debounce;
|
|
116
|
+
exports.deepClone = deepClone;
|
|
117
|
+
exports.deepMerge = deepMerge;
|
|
118
|
+
exports.error = error;
|
|
119
|
+
exports.extend = extend;
|
|
120
|
+
exports.getRandomColor = getRandomColor;
|
|
121
|
+
exports.getRandomString = getRandomString;
|
|
122
|
+
exports.hasChanged = hasChanged;
|
|
123
|
+
exports.hasOwn = hasOwn;
|
|
124
|
+
exports.isArray = isArray;
|
|
125
|
+
exports.isDate = isDate;
|
|
126
|
+
exports.isDef = isDef;
|
|
127
|
+
exports.isFunction = isFunction;
|
|
128
|
+
exports.isMap = isMap;
|
|
129
|
+
exports.isNull = isNull;
|
|
130
|
+
exports.isObject = isObject;
|
|
131
|
+
exports.isPromise = isPromise;
|
|
132
|
+
exports.isRegExp = isRegExp;
|
|
133
|
+
exports.isSet = isSet;
|
|
134
|
+
exports.isString = isString;
|
|
135
|
+
exports.isSymbol = isSymbol;
|
|
136
|
+
exports.isUndef = isUndef;
|
|
137
|
+
exports.log = log;
|
|
138
|
+
exports.objectToString = objectToString;
|
|
139
|
+
exports.throttle = throttle;
|
|
140
|
+
exports.toTypeString = toTypeString;
|
|
141
|
+
exports.warn = warn;
|
package/dist/index.mjs
CHANGED
|
@@ -1,107 +1,141 @@
|
|
|
1
|
-
function
|
|
1
|
+
function getRandomColor() {
|
|
2
2
|
return `#${Math.random().toString(16).slice(2, 8).padEnd(6, "0")}`;
|
|
3
3
|
}
|
|
4
|
-
function
|
|
5
|
-
return
|
|
4
|
+
function getRandomString(number) {
|
|
5
|
+
return number > 10 ? getRandomString(10) + getRandomString(number - 10) : Math.random().toString(36).padEnd(12, "0").slice(2, number + 2);
|
|
6
6
|
}
|
|
7
|
-
const
|
|
8
|
-
|
|
7
|
+
const extend = Object.assign;
|
|
8
|
+
const objectToString = Object.prototype.toString;
|
|
9
|
+
const toTypeString = (value) => objectToString.call(value);
|
|
10
|
+
const log = (...arg) => console.log(
|
|
11
|
+
`%c ${arg[0]}: `,
|
|
9
12
|
"padding: 2px 1px; border-radius: 3px 3px 3px 3px; color: #fff; background: #000; font-weight: bold;",
|
|
10
|
-
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
arg.slice(1)
|
|
14
|
+
);
|
|
15
|
+
const warn = (msg) => console.warn(msg);
|
|
16
|
+
const error = (msg) => console.error(msg);
|
|
17
|
+
const isMap = (val) => toTypeString(val) === "[object Map]";
|
|
18
|
+
const isSet = (val) => toTypeString(val) === "[object Set]";
|
|
19
|
+
const isDate = (val) => toTypeString(val) === "[object Date]";
|
|
20
|
+
const isRegExp = (val) => toTypeString(val) === "[object RegExp]";
|
|
21
|
+
const isFunction = (val) => typeof val === "function";
|
|
22
|
+
const isString = (val) => typeof val === "string";
|
|
23
|
+
const isSymbol = (val) => typeof val === "symbol";
|
|
24
|
+
const isObject = (val) => val !== null && typeof val === "object";
|
|
25
|
+
const isPromise = (val) => isObject(val) && isFunction(val.then) && isFunction(val.catch);
|
|
26
|
+
const isUndef = (val) => val === void 0 || val === null;
|
|
27
|
+
const isNull = (val) => val === null;
|
|
28
|
+
function isDef(v) {
|
|
29
|
+
return v !== void 0 && v !== null;
|
|
14
30
|
}
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (
|
|
22
|
-
return new
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
const isArray = Array.isArray;
|
|
32
|
+
const hasOwn = (target, key) => Object.prototype.hasOwnProperty.call(target, key);
|
|
33
|
+
const hasChanged = (oldValue, newValue) => oldValue !== newValue;
|
|
34
|
+
function deepClone(target) {
|
|
35
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
36
|
+
function _deepClone(value) {
|
|
37
|
+
if (isDate(value))
|
|
38
|
+
return new Date(value);
|
|
39
|
+
if (isRegExp(value))
|
|
40
|
+
return new RegExp(value);
|
|
41
|
+
if (isObject(value)) {
|
|
42
|
+
if (map.has(value))
|
|
43
|
+
return map.get(value);
|
|
44
|
+
let clone = {};
|
|
45
|
+
if (isArray(value)) {
|
|
46
|
+
clone = [];
|
|
47
|
+
map.set(value, clone);
|
|
48
|
+
for (let i = 0; i < value.length; i++) {
|
|
49
|
+
clone[i] = _deepClone(value[i]);
|
|
50
|
+
}
|
|
51
|
+
return clone;
|
|
32
52
|
}
|
|
33
|
-
|
|
34
|
-
for (const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
53
|
+
map.set(value, clone);
|
|
54
|
+
for (const key in value) {
|
|
55
|
+
if (hasOwn(value, key)) {
|
|
56
|
+
clone[key] = _deepClone(value[key]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return clone;
|
|
60
|
+
} else {
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
39
63
|
}
|
|
40
|
-
return
|
|
64
|
+
return _deepClone(target);
|
|
41
65
|
}
|
|
42
|
-
function
|
|
43
|
-
return
|
|
66
|
+
function deepMerge(template, source, options) {
|
|
67
|
+
return isArray(template) ? arrayMerge(template, source, options) : mergeObject(template, source, options);
|
|
44
68
|
}
|
|
45
|
-
function
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
69
|
+
function mergeObject(template, source, options) {
|
|
70
|
+
const target = process(source, options);
|
|
71
|
+
const templateKeys = Object.keys(template);
|
|
72
|
+
templateKeys.forEach((key) => {
|
|
73
|
+
if (isUndef(source[key])) {
|
|
74
|
+
target[key] = process(template[key], options);
|
|
75
|
+
} else {
|
|
76
|
+
target[key] = isObject(template[key]) && isObject(source[key]) ? deepMerge(template[key], source[key], options) : process(source[key], options);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return target;
|
|
50
80
|
}
|
|
51
|
-
function
|
|
52
|
-
return
|
|
81
|
+
function arrayMerge(template, source, options) {
|
|
82
|
+
return isArray(source) ? process(template, options).concat(process(source, options)) : process(source, options);
|
|
53
83
|
}
|
|
54
|
-
function
|
|
55
|
-
const { isDeepClone
|
|
56
|
-
return
|
|
84
|
+
function process(source, options) {
|
|
85
|
+
const { isDeepClone = true } = options || {};
|
|
86
|
+
return isDeepClone ? deepClone(source) : source;
|
|
57
87
|
}
|
|
58
|
-
function
|
|
59
|
-
let
|
|
60
|
-
return function(...
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
88
|
+
function debounce(target, wait = 1e3) {
|
|
89
|
+
let timer = null;
|
|
90
|
+
return function(...args) {
|
|
91
|
+
if (timer)
|
|
92
|
+
clearTimeout(timer);
|
|
93
|
+
timer = setTimeout(() => {
|
|
94
|
+
target.apply(this, ...args);
|
|
95
|
+
}, wait);
|
|
64
96
|
};
|
|
65
97
|
}
|
|
66
|
-
function
|
|
67
|
-
let
|
|
68
|
-
return function(...
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
98
|
+
function throttle(target, wait = 1e3) {
|
|
99
|
+
let timer = null;
|
|
100
|
+
return function(...args) {
|
|
101
|
+
if (isNull(timer)) {
|
|
102
|
+
timer = Date.now();
|
|
103
|
+
return target.apply(this, ...args);
|
|
104
|
+
} else {
|
|
105
|
+
const now = Date.now();
|
|
106
|
+
if (now - timer >= wait) {
|
|
107
|
+
timer = now;
|
|
108
|
+
return target.apply(this, ...args);
|
|
109
|
+
}
|
|
75
110
|
}
|
|
76
111
|
};
|
|
77
112
|
}
|
|
78
113
|
export {
|
|
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
|
-
|
|
114
|
+
debounce,
|
|
115
|
+
deepClone,
|
|
116
|
+
deepMerge,
|
|
117
|
+
error,
|
|
118
|
+
extend,
|
|
119
|
+
getRandomColor,
|
|
120
|
+
getRandomString,
|
|
121
|
+
hasChanged,
|
|
122
|
+
hasOwn,
|
|
123
|
+
isArray,
|
|
124
|
+
isDate,
|
|
125
|
+
isDef,
|
|
126
|
+
isFunction,
|
|
127
|
+
isMap,
|
|
128
|
+
isNull,
|
|
129
|
+
isObject,
|
|
130
|
+
isPromise,
|
|
131
|
+
isRegExp,
|
|
132
|
+
isSet,
|
|
133
|
+
isString,
|
|
134
|
+
isSymbol,
|
|
135
|
+
isUndef,
|
|
136
|
+
log,
|
|
137
|
+
objectToString,
|
|
138
|
+
throttle,
|
|
139
|
+
toTypeString,
|
|
140
|
+
warn
|
|
106
141
|
};
|
|
107
|
-
//# sourceMappingURL=index.mjs.map
|
package/package.json
CHANGED
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/utils/getRandom.ts","../src/utils/type.ts","../src/utils/deepClone.ts","../src/utils/deepMerge.ts","../src/utils/debounce.ts","../src/utils/throttle.ts"],"sourcesContent":["/**\n * @description 获取随机颜色\n * @return {String} 随机颜色\n *\n * @example getRandomColor() => '#f36a38'\n * */\nexport function getRandomColor(): string {\n return `#${Math.random().toString(16).slice(2, 8).padEnd(6, '0')}`\n}\n\n/**\n * @description 获取随机字符串\n * @param {Number} number 长度\n * @return {String} 随机字符串\n *\n * @example getRandomString(20) => 'tlw491y5ha357l59980n'\n * */\nexport function getRandomString(number: number): string {\n return number > 10\n ? getRandomString(10) + getRandomString(number - 10)\n : Math.random()\n .toString(36)\n .padEnd(12, '0')\n .slice(2, number + 2)\n}\n","export const extend = Object.assign\n\nexport type AnyObj = Record<string, any>\n\nexport const objectToString = Object.prototype.toString\n\nexport const toTypeString = (value: unknown): string =>\n objectToString.call(value)\n\nexport const log = (...arg: unknown[]) =>\n console.log(\n `%c ${arg[0]}: `,\n 'padding: 2px 1px; border-radius: 3px 3px 3px 3px; color: #fff; background: #000; font-weight: bold;',\n arg.slice(1)\n )\n\nexport const warn = (msg: string) => console.warn(msg)\n\nexport const error = (msg: string) => console.error(msg)\n\nexport const isMap = (val: unknown): val is Map<any, any> =>\n toTypeString(val) === '[object Map]'\n\nexport const isSet = (val: unknown): val is Set<any> =>\n toTypeString(val) === '[object Set]'\n\nexport const isDate = (val: unknown): val is Date =>\n toTypeString(val) === '[object Date]'\n\nexport const isRegExp = (val: unknown): val is RegExp =>\n toTypeString(val) === '[object RegExp]'\n\nexport const isFunction = (val: unknown): val is Function =>\n typeof val === 'function'\n\nexport const isString = (val: unknown): val is string => typeof val === 'string'\n\nexport const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'\n\nexport const isObject = (val: unknown): val is Record<any, any> =>\n val !== null && typeof val === 'object'\n\nexport const isPromise = <T = any>(val: unknown): val is Promise<T> =>\n isObject(val) && isFunction(val.then) && isFunction(val.catch)\n\nexport const isUndef = (val: unknown): val is undefined | null =>\n val === undefined || val === null\n\nexport const isNull = (val: unknown): val is null => val === null\n\nexport function isDef<T>(v: T): v is NonNullable<T> {\n return v !== undefined && v !== null\n}\n\nexport const isArray = Array.isArray\n\nexport const hasOwn = <T extends object, U extends keyof T>(\n target: T,\n key: U\n) => Object.prototype.hasOwnProperty.call(target, key)\n\nexport const hasChanged = (oldValue: unknown, newValue: unknown) =>\n oldValue !== newValue\n","import { hasOwn, isArray, isDate, isObject, isRegExp } from './type'\n\n/**\n * @description 深拷贝\n * @param target 拷贝对象\n * @example deepClone({ name: 1, obj: { name: 1 }, arr: [1, 2, 3] }) => { name: 1, obj: { name: 1 }, arr: [1, 2, 3] }\n */\nexport function deepClone<T = any>(target: T): T {\n const map = new WeakMap()\n\n function _deepClone(value: any) {\n if (isDate(value)) return new Date(value)\n if (isRegExp(value)) return new RegExp(value)\n\n if (isObject(value)) {\n // 取出 WeakMap 中缓存的 clone 对象\n if (map.has(value)) return map.get(value)\n let clone: Record<any, any> = {}\n\n // 处理数组\n if (isArray(value)) {\n clone = []\n map.set(value, clone)\n for (let i = 0; i < value.length; i++) {\n clone[i] = _deepClone(value[i])\n }\n\n return clone\n }\n\n // 设置缓存\n map.set(value, clone)\n\n for (const key in value) {\n if (hasOwn(value, key)) {\n clone[key] = _deepClone(value[key])\n }\n }\n\n return clone\n } else {\n return value\n }\n }\n\n return _deepClone(target)\n}\n","import { AnyObj, isArray, isObject, isUndef } from './type'\nimport { deepClone } from './deepClone'\n\ninterface Options {\n /**\n * @description 是否开启深拷贝 默认开启\n * @default true\n */\n isDeepClone: boolean\n}\n\n/**\n * @description 深度合并两对象\n * @param template 模板对象 就是默认值\n * @param source 源对象 就是用户传入的值\n * @param options 配置对象\n * @example deepMerge({ name: 1, obj: { name: '默认' , text: '旧值' }, arr: [1, 2, 3] }, { name: 1, obj: { name: '修改', age: '新增' }, test: 'test' }) => { name: 1, obj: { name: '修改', text: '旧值', age: '新增' }, arr: [1, 2, 3], test: 'test' }\n */\n\nexport function deepMerge(\n template: AnyObj,\n source: AnyObj,\n options?: Options\n): any {\n return isArray(template)\n ? arrayMerge(template, source, options)\n : mergeObject(template, source, options)\n}\n\nfunction mergeObject(template: AnyObj, source: AnyObj, options?: Options) {\n const target = process(source, options)\n const templateKeys = Object.keys(template)\n templateKeys.forEach(key => {\n if (isUndef(source[key])) {\n target[key] = process(template[key], options)\n } else {\n target[key] =\n isObject(template[key]) && isObject(source[key])\n ? deepMerge(template[key], source[key], options)\n : process(source[key], options)\n }\n })\n\n return target\n}\n\n// 数组暂时没有深拷贝的需求\nfunction arrayMerge(template: any[], source: AnyObj, options?: Options) {\n return isArray(source)\n ? process(template, options).concat(process(source, options))\n : process(source, options)\n}\n\nfunction process<T = any>(source: T, options?: Options) {\n const { isDeepClone = true } = options || {}\n\n return isDeepClone ? deepClone(source) : source\n}\n","/**\n * @description 防抖函数\n * @param target 目标函数\n * @param wait 等待时间 默认 1000ms\n * @example debounce(() => console.log('debounce'),500)\n */\nexport function debounce(target: Function, wait = 1000) {\n let timer: number | null = null\n\n return function (...args: any[]) {\n if (timer) clearTimeout(timer)\n timer = setTimeout(() => {\n target.apply(this, ...args)\n }, wait) as any\n }\n}\n","import { isNull } from './type'\n\n/**\n * @description 节流函数\n * @param target 目标函数\n * @param wait 等待时间 默认 1000ms\n * @example throttle(() =>\n */\nexport function throttle(target: Function, wait = 1000) {\n let timer: number | null = null\n\n return function (...args: any[]) {\n if (isNull(timer)) {\n timer = Date.now()\n\n return target.apply(this, ...args)\n } else {\n const now = Date.now()\n if (now - timer >= wait) {\n timer = now\n\n return target.apply(this, ...args)\n }\n }\n }\n}\n"],"names":["getRandomColor","getRandomString","number","extend","objectToString","toTypeString","value","log","arg","warn","msg","error","isMap","val","isSet","isDate","isRegExp","isFunction","isString","isSymbol","isObject","isPromise","isUndef","isNull","isDef","v","isArray","hasOwn","target","key","hasChanged","oldValue","newValue","deepClone","map","_deepClone","clone","i","deepMerge","template","source","options","arrayMerge","mergeObject","process","isDeepClone","debounce","wait","timer","args","throttle","now"],"mappings":"gFAMO,SAASA,GAAyB,CACvC,MAAO,IAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,MAAM,EAAG,CAAC,EAAE,OAAO,EAAG,GAAG,CAAC,EAClE,CASO,SAASC,EAAgBC,EAAwB,CAC/C,OAAAA,EAAS,GACZD,EAAgB,EAAE,EAAIA,EAAgBC,EAAS,EAAE,EACjD,KAAK,OAAA,EACF,SAAS,EAAE,EACX,OAAO,GAAI,GAAG,EACd,MAAM,EAAGA,EAAS,CAAC,CAC5B,CCxBO,MAAMC,EAAS,OAAO,OAIhBC,EAAiB,OAAO,UAAU,SAElCC,EAAgBC,GAC3BF,EAAe,KAAKE,CAAK,EAEdC,EAAM,IAAIC,IACrB,QAAQ,IACN,MAAMA,EAAI,CAAC,CAAC,KACZ,sGACAA,EAAI,MAAM,CAAC,CACb,EAEWC,EAAQC,GAAgB,QAAQ,KAAKA,CAAG,EAExCC,EAASD,GAAgB,QAAQ,MAAMA,CAAG,EAE1CE,EAASC,GACpBR,EAAaQ,CAAG,IAAM,eAEXC,EAASD,GACpBR,EAAaQ,CAAG,IAAM,eAEXE,EA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SAAAG,EAAStB,EAAkBmB,EAAO,IAAM,CACtD,IAAIC,EAAuB,KAE3B,OAAO,YAAaC,EAAa,CAC3B,GAAA1B,EAAOyB,CAAK,EACd,OAAAA,EAAQ,KAAK,MAENpB,EAAO,MAAM,KAAM,GAAGqB,CAAI,EAC5B,CACC,MAAAE,EAAM,KAAK,MACb,GAAAA,EAAMH,GAASD,EACT,OAAAC,EAAAG,EAEDvB,EAAO,MAAM,KAAM,GAAGqB,CAAI,CAErC,CAAA,CAEJ"}
|
package/dist/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/utils/getRandom.ts","../src/utils/type.ts","../src/utils/deepClone.ts","../src/utils/deepMerge.ts","../src/utils/debounce.ts","../src/utils/throttle.ts"],"sourcesContent":["/**\n * @description 获取随机颜色\n * @return {String} 随机颜色\n *\n * @example getRandomColor() => '#f36a38'\n * */\nexport function getRandomColor(): string {\n return `#${Math.random().toString(16).slice(2, 8).padEnd(6, '0')}`\n}\n\n/**\n * @description 获取随机字符串\n * @param {Number} number 长度\n * @return {String} 随机字符串\n *\n * @example getRandomString(20) => 'tlw491y5ha357l59980n'\n * */\nexport function getRandomString(number: number): string {\n return number > 10\n ? getRandomString(10) + getRandomString(number - 10)\n : Math.random()\n .toString(36)\n .padEnd(12, '0')\n .slice(2, number + 2)\n}\n","export const extend = Object.assign\n\nexport type AnyObj = Record<string, any>\n\nexport const objectToString = Object.prototype.toString\n\nexport const toTypeString = (value: unknown): string =>\n objectToString.call(value)\n\nexport const log = (...arg: unknown[]) =>\n console.log(\n `%c ${arg[0]}: `,\n 'padding: 2px 1px; border-radius: 3px 3px 3px 3px; color: #fff; background: #000; font-weight: bold;',\n arg.slice(1)\n )\n\nexport const warn = (msg: string) => console.warn(msg)\n\nexport const error = (msg: string) => console.error(msg)\n\nexport const isMap = (val: unknown): val is Map<any, any> =>\n toTypeString(val) === '[object Map]'\n\nexport const isSet = (val: unknown): val is Set<any> =>\n toTypeString(val) === '[object Set]'\n\nexport const isDate = (val: unknown): val is Date =>\n toTypeString(val) === '[object Date]'\n\nexport const isRegExp = (val: unknown): val is RegExp =>\n toTypeString(val) === '[object RegExp]'\n\nexport const isFunction = (val: unknown): val is Function =>\n typeof val === 'function'\n\nexport const isString = (val: unknown): val is string => typeof val === 'string'\n\nexport const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'\n\nexport const isObject = (val: unknown): val is Record<any, any> =>\n val !== null && typeof val === 'object'\n\nexport const isPromise = <T = any>(val: unknown): val is Promise<T> =>\n isObject(val) && isFunction(val.then) && isFunction(val.catch)\n\nexport const isUndef = (val: unknown): val is undefined | null =>\n val === undefined || val === null\n\nexport const isNull = (val: unknown): val is null => val === null\n\nexport function isDef<T>(v: T): v is NonNullable<T> {\n return v !== undefined && v !== null\n}\n\nexport const isArray = Array.isArray\n\nexport const hasOwn = <T extends object, U extends keyof T>(\n target: T,\n key: U\n) => Object.prototype.hasOwnProperty.call(target, key)\n\nexport const hasChanged = (oldValue: unknown, newValue: unknown) =>\n oldValue !== newValue\n","import { hasOwn, isArray, isDate, isObject, isRegExp } from './type'\n\n/**\n * @description 深拷贝\n * @param target 拷贝对象\n * @example deepClone({ name: 1, obj: { name: 1 }, arr: [1, 2, 3] }) => { name: 1, obj: { name: 1 }, arr: [1, 2, 3] }\n */\nexport function deepClone<T = any>(target: T): T {\n const map = new WeakMap()\n\n function _deepClone(value: any) {\n if (isDate(value)) return new Date(value)\n if (isRegExp(value)) return new RegExp(value)\n\n if (isObject(value)) {\n // 取出 WeakMap 中缓存的 clone 对象\n if (map.has(value)) return map.get(value)\n let clone: Record<any, any> = {}\n\n // 处理数组\n if (isArray(value)) {\n clone = []\n map.set(value, clone)\n for (let i = 0; i < value.length; i++) {\n clone[i] = _deepClone(value[i])\n }\n\n return clone\n }\n\n // 设置缓存\n map.set(value, clone)\n\n for (const key in value) {\n if (hasOwn(value, key)) {\n clone[key] = _deepClone(value[key])\n }\n }\n\n return clone\n } else {\n return value\n }\n }\n\n return _deepClone(target)\n}\n","import { AnyObj, isArray, isObject, isUndef } from './type'\nimport { deepClone } from './deepClone'\n\ninterface Options {\n /**\n * @description 是否开启深拷贝 默认开启\n * @default true\n */\n isDeepClone: boolean\n}\n\n/**\n * @description 深度合并两对象\n * @param template 模板对象 就是默认值\n * @param source 源对象 就是用户传入的值\n * @param options 配置对象\n * @example deepMerge({ name: 1, obj: { name: '默认' , text: '旧值' }, arr: [1, 2, 3] }, { name: 1, obj: { name: '修改', age: '新增' }, test: 'test' }) => { name: 1, obj: { name: '修改', text: '旧值', age: '新增' }, arr: [1, 2, 3], test: 'test' }\n */\n\nexport function deepMerge(\n template: AnyObj,\n source: AnyObj,\n options?: Options\n): any {\n return isArray(template)\n ? arrayMerge(template, source, options)\n : mergeObject(template, source, options)\n}\n\nfunction mergeObject(template: AnyObj, source: AnyObj, options?: Options) {\n const target = process(source, options)\n const templateKeys = Object.keys(template)\n templateKeys.forEach(key => {\n if (isUndef(source[key])) {\n target[key] = process(template[key], options)\n } else {\n target[key] =\n isObject(template[key]) && isObject(source[key])\n ? deepMerge(template[key], source[key], options)\n : process(source[key], options)\n }\n })\n\n return target\n}\n\n// 数组暂时没有深拷贝的需求\nfunction arrayMerge(template: any[], source: AnyObj, options?: Options) {\n return isArray(source)\n ? process(template, options).concat(process(source, options))\n : process(source, options)\n}\n\nfunction process<T = any>(source: T, options?: Options) {\n const { isDeepClone = true } = options || {}\n\n return isDeepClone ? deepClone(source) : source\n}\n","/**\n * @description 防抖函数\n * @param target 目标函数\n * @param wait 等待时间 默认 1000ms\n * @example debounce(() => console.log('debounce'),500)\n */\nexport function debounce(target: Function, wait = 1000) {\n let timer: number | null = null\n\n return function (...args: any[]) {\n if (timer) clearTimeout(timer)\n timer = setTimeout(() => {\n target.apply(this, ...args)\n }, wait) as any\n }\n}\n","import { isNull } from './type'\n\n/**\n * @description 节流函数\n * @param target 目标函数\n * @param wait 等待时间 默认 1000ms\n * @example throttle(() =>\n */\nexport function throttle(target: Function, wait = 1000) {\n let timer: number | null = null\n\n return function (...args: any[]) {\n if (isNull(timer)) {\n timer = Date.now()\n\n return target.apply(this, ...args)\n } else {\n const now = Date.now()\n if (now - timer >= wait) {\n timer = now\n\n return target.apply(this, ...args)\n }\n }\n }\n}\n"],"names":["getRandomColor","getRandomString","number","extend","objectToString","toTypeString","value","log","arg","warn","msg","error","isMap","val","isSet","isDate","isRegExp","isFunction","isString","isSymbol","isObject","isPromise","isUndef","isNull","isDef","v","isArray","hasOwn","target","key","hasChanged","oldValue","newValue","deepClone","map","_deepClone","clone","i","deepMerge","template","source","options","arrayMerge","mergeObject","process","isDeepClone","debounce","wait","timer","args","throttle","now"],"mappings":"AAMO,SAASA,IAAyB;AACvC,SAAO,IAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,MAAM,GAAG,CAAC,EAAE,OAAO,GAAG,GAAG,CAAC;AAClE;AASO,SAASC,EAAgBC,GAAwB;AAC/C,SAAAA,IAAS,KACZD,EAAgB,EAAE,IAAIA,EAAgBC,IAAS,EAAE,IACjD,KAAK,OAAA,EACF,SAAS,EAAE,EACX,OAAO,IAAI,GAAG,EACd,MAAM,GAAGA,IAAS,CAAC;AAC5B;ACxBO,MAAMC,IAAS,OAAO,QAIhBC,IAAiB,OAAO,UAAU,UAElCC,IAAe,CAACC,MAC3BF,EAAe,KAAKE,CAAK,GAEdC,IAAM,IAAIC,MACrB,QAAQ;AAAA,EACN,MAAMA,EAAI,CAAC,CAAC;AAAA,EACZ;AAAA,EACAA,EAAI,MAAM,CAAC;AACb,GAEWC,IAAO,CAACC,MAAgB,QAAQ,KAAKA,CAAG,GAExCC,IAAQ,CAACD,MAAgB,QAAQ,MAAMA,CAAG,GAE1CE,IAAQ,CAACC,MACpBR,EAAaQ,CAAG,MAAM,gBAEXC,IAAQ,CAACD,MACpBR,EAAaQ,CAAG,MAAM,gBAEXE,IAAS,CAACF,MACrBR,EAAaQ,CAAG,MAAM,iBAEXG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