Skip to content

Commit 728b43b

Browse files
committed
feat: tools
1 parent 24eb710 commit 728b43b

File tree

9 files changed

+1941
-1855
lines changed

9 files changed

+1941
-1855
lines changed

.gitpod.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This configuration file was automatically generated by Gitpod.
2+
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
3+
# and commit this file to your remote git repository to share the goodness with others.
4+
5+
# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart
6+
7+
tasks:
8+
- init: pnpm install && pnpm run build
9+
command: pnpm run dev
10+
11+

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
# tseuse
22

33
一款轻量级ts工具库
4+
5+
# Contribute
6+
## run doc when you develop it
7+
pnpm run docs:dev
8+
9+
# test
10+
pnpm run coverage

packages/core/function/index.ts

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1 @@
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-
};
1+
export * from './tools';

packages/core/function/tools.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ export const throttle = (fn: Function, wait: number = 300) => {
2424
*/
2525
export const debounce = (fn: Function, wait: number, immediate: boolean = false) => {
2626
let timer: any = null;
27-
return (this: unknown, ...args: any) => {
27+
let ctx = this;
28+
return (...args: any) => {
2829
if (timer) {
2930
clearTimeout(timer);
3031
timer = null;
3132
}
3233
if (immediate) {
33-
if (!timer) fn.apply(this, args); // 第一次调用时执行
34+
if (!timer) fn.apply(ctx, args); // 第一次调用时执行
3435
timer = setTimeout(() => {
3536
// n秒内多次触发事件,重新计算timer
3637
timer = null; // n秒内没有触发事件,timer设置为null,保证n秒后能重新触发事件
@@ -43,3 +44,16 @@ export const debounce = (fn: Function, wait: number, immediate: boolean = false)
4344
}
4445
};
4546
};
47+
48+
export const deepClone = (obj: Object,hash: any = new WeakMap) => {
49+
if(obj instanceof Date){
50+
return new Date(obj);
51+
}
52+
if(obj instanceof RegExp){
53+
return new RegExp(obj);
54+
}
55+
if(hash.has(obj)){
56+
return hash.get(obj);
57+
}
58+
59+
}

packages/core/objects/index.ts

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,3 @@
1-
/**
2-
* 判断是否为对象
3-
* @param {any} data
4-
* @returns {boolean}
5-
*/
6-
export function isObject(data: any): boolean {
7-
return Object.prototype.toString.call(data) === '[object Object]';
8-
}
9-
/**
10-
* 合并对象
11-
* @param objs
12-
* @returns {object}
13-
*/
14-
export function mergeObject(...objs): object {
15-
let result = {};
16-
objs.forEach((obj) => {
17-
Object.keys(obj).forEach((key) => {
18-
let value = obj[key];
19-
if (!result.hasOwnProperty(key)) {
20-
// 说明result中没有该属性
21-
result[key] = result;
22-
} else {
23-
// 说明result已经存在该属性,那么就把同名属性的值合并为一个数组
24-
result[key] = [].concat(result[key], value);
25-
}
26-
});
27-
});
28-
return result;
29-
}
1+
export * from './mergeObject';
2+
export * from './isObject';
3+
export * from './observe';

packages/core/objects/isObject.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* 判断是否为对象
3+
* @param {any} data
4+
* @returns {boolean}
5+
*/
6+
export function isObject(data: any): boolean {
7+
return Object.prototype.toString.call(data) === '[object Object]';
8+
}
9+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 判断是否为空对象
3+
* @param obj 参数
4+
* @returns {boolean}
5+
*/
6+
const isObj = (obj: any): boolean => {
7+
return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
8+
}
9+
/**
10+
* @func mergeObject
11+
* @param {object} obj1 外部的参数对象
12+
* @param {object} obj2 默认参数对象
13+
* @desc 深拷贝合并对象
14+
* @returns {Object}
15+
*/
16+
export function mergeObject(obj1: any, obj2: Object): Object {
17+
if(!isObj(obj1)){ // 如果没有传参,返回默认值
18+
return mergeObject({}, obj2);
19+
}
20+
if(!isObj(obj2)){ // 如果没有默认,返回传参
21+
return mergeObject({},obj1);
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 === 'construtor') return;
28+
if(val === null) return;
29+
if(isObj(val) && isObj(newObj[key as keyof typeof newObj])) { // 如果传参中的值为对象,则递归调用
30+
newObj[key] = mergeObject(val, newObj[key as keyof typeof newObj]);
31+
} else {
32+
newObj[key as keyof typeof newObj] = val;
33+
}
34+
})
35+
return newObj;
36+
}
37+

packages/core/objects/observe.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* 将一个对象转化为可观测对象
3+
* @param { Object } obj 对象
4+
* @param { String } key 对象的key
5+
* @param { Any } val 对象的某个key的值
6+
*/
7+
const defineReactive = (obj: Object, key: string, val: any): void => {
8+
Object.defineProperty(obj, key, {
9+
enumerable: true,
10+
configurable: true,
11+
get() {
12+
return val;
13+
},
14+
set(newVal) {
15+
val = newVal;
16+
}
17+
})
18+
}
19+
/**
20+
* 设计一个对象的观测者
21+
* @param { Object } obj 对象
22+
* @return { Object } 返回一个可观测对象
23+
* @example let obj = observerDef({name:'alex',age:18})
24+
*/
25+
export const observeDef = (obj: Object): Object | undefined => {
26+
if(!obj || typeof obj !== 'object'){
27+
return;
28+
}
29+
let keys = Object.keys(obj);
30+
keys.forEach(key => {
31+
defineReactive(obj,key, obj[key as keyof typeof obj]);
32+
})
33+
return obj;
34+
}
35+
36+
/**
37+
* 设计一个对象的观测者 - proxy方案
38+
* @param { Object } obj 对象
39+
* @return { cal } 观测对象回调方法
40+
* @example let obj = observeProxy({name:'alex',age:18},callback)
41+
*/
42+
export const observeProxy = (obj: Object, cal: (val: any) => void) => {
43+
return new Proxy(obj, {
44+
get: function(target,prop){
45+
return Reflect.get(target, prop);
46+
},
47+
set: function(target, prop, val) {
48+
cal(val);
49+
return Reflect.set(target, prop, val);
50+
},
51+
deleteProperty: function (target, prop){
52+
return Reflect.deleteProperty(target,prop);
53+
}
54+
})
55+
}

0 commit comments

Comments
 (0)