Skip to content

Commit db89b88

Browse files
committed
feat(core): mergeObject
1 parent f3f5491 commit db89b88

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

packages/.vitepress/cache/deps/_metadata.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"hash": "3e39ebdb",
3-
"browserHash": "1ccd550d",
2+
"hash": "95f455e2",
3+
"browserHash": "d5235caa",
44
"optimized": {
55
"vue": {
66
"src": "../../../../node_modules/.pnpm/[email protected]/node_modules/vue/dist/vue.runtime.esm-bundler.js",
77
"file": "vue.js",
8-
"fileHash": "31a5a212",
8+
"fileHash": "5d71ae07",
99
"needsInterop": false
1010
}
1111
},

packages/core/function/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 判断是否为空对象
3+
* @param obj
4+
* @returns {boolean}
5+
*/
6+
const isObject = (obj: any): boolean => {
7+
return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
8+
};
9+
/**
10+
* 深拷贝合并对象
11+
* @param {object} obj1 外部的参数对象
12+
* @param {object} obj2 默认参数对象
13+
* @returns {Object}
14+
*/
15+
export const mergeObejct = (obj1: any, obj2: Object): Object => {
16+
if (!isObject(obj1)) {
17+
return mergeObejct({}, obj2);
18+
}
19+
if (!isObject(obj2)) {
20+
return mergeObejct({}, obj1);
21+
}
22+
// 定义一个以默认值为基础的新对象
23+
let newObj = Object.assign({}, obj2);
24+
// 遍历传参对象
25+
Object.keys(obj1).forEach(function (key) {
26+
let val = obj1[key];
27+
if (key === '__proto__' || key === 'constructor') {
28+
return;
29+
}
30+
if (val === null) {
31+
return;
32+
}
33+
// 如果传参对象中的值为对象,则递归调用
34+
if (isObject(val) && isObject(newObj[key])) {
35+
newObj[key] = mergeObejct(val, newObj[key]);
36+
} else {
37+
newObj[key] = val;
38+
}
39+
});
40+
return newObj;
41+
};

packages/core/string/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
export const randomString = () => {};
1+
/**
2+
* 随机生成字符串
3+
* @param {number} length
4+
* @returns {string}
5+
*/
6+
export const randomString = (length: number): string => {
7+
let chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789';
8+
let charsLen = chars.length;
9+
let resultStr = '';
10+
for (let i = 0; i < length; i++) {
11+
resultStr += chars.charAt(Math.floor(Math.random() * charsLen));
12+
}
13+
return resultStr;
14+
};

0 commit comments

Comments
 (0)