Skip to content

Commit

Permalink
feat: tools
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-fang committed Jun 11, 2023
1 parent 24eb710 commit 728b43b
Show file tree
Hide file tree
Showing 9 changed files with 1,941 additions and 1,855 deletions.
11 changes: 11 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
# and commit this file to your remote git repository to share the goodness with others.

# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart

tasks:
- init: pnpm install && pnpm run build
command: pnpm run dev


7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# tseuse

一款轻量级ts工具库

# Contribute
## run doc when you develop it
pnpm run docs:dev

# test
pnpm run coverage
42 changes: 1 addition & 41 deletions packages/core/function/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1 @@
/**
* 判断是否为空对象
* @param obj
* @returns {boolean}
*/
const isObject = (obj: any): boolean => {
return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
};
/**
* 深拷贝合并对象
* @param {object} obj1 外部的参数对象
* @param {object} obj2 默认参数对象
* @returns {Object}
*/
export const mergeObejct = (obj1: any, obj2: Object): Object => {
if (!isObject(obj1)) {
return mergeObejct({}, obj2);
}
if (!isObject(obj2)) {
return mergeObejct({}, obj1);
}
// 定义一个以默认值为基础的新对象
let newObj = Object.assign({}, obj2);
// 遍历传参对象
Object.keys(obj1).forEach(function (key) {
let val = obj1[key];
if (key === '__proto__' || key === 'constructor') {
return;
}
if (val === null) {
return;
}
// 如果传参对象中的值为对象,则递归调用
if (isObject(val) && isObject(newObj[key])) {
newObj[key] = mergeObejct(val, newObj[key]);
} else {
newObj[key] = val;
}
});
return newObj;
};
export * from './tools';
18 changes: 16 additions & 2 deletions packages/core/function/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ export const throttle = (fn: Function, wait: number = 300) => {
*/
export const debounce = (fn: Function, wait: number, immediate: boolean = false) => {
let timer: any = null;
return (this: unknown, ...args: any) => {
let ctx = this;
return (...args: any) => {
if (timer) {
clearTimeout(timer);
timer = null;
}
if (immediate) {
if (!timer) fn.apply(this, args); // 第一次调用时执行
if (!timer) fn.apply(ctx, args); // 第一次调用时执行
timer = setTimeout(() => {
// n秒内多次触发事件,重新计算timer
timer = null; // n秒内没有触发事件,timer设置为null,保证n秒后能重新触发事件
Expand All @@ -43,3 +44,16 @@ export const debounce = (fn: Function, wait: number, immediate: boolean = false)
}
};
};

export const deepClone = (obj: Object,hash: any = new WeakMap) => {
if(obj instanceof Date){
return new Date(obj);
}
if(obj instanceof RegExp){
return new RegExp(obj);
}
if(hash.has(obj)){
return hash.get(obj);
}

}
32 changes: 3 additions & 29 deletions packages/core/objects/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
/**
* 判断是否为对象
* @param {any} data
* @returns {boolean}
*/
export function isObject(data: any): boolean {
return Object.prototype.toString.call(data) === '[object Object]';
}
/**
* 合并对象
* @param objs
* @returns {object}
*/
export function mergeObject(...objs): object {
let result = {};
objs.forEach((obj) => {
Object.keys(obj).forEach((key) => {
let value = obj[key];
if (!result.hasOwnProperty(key)) {
// 说明result中没有该属性
result[key] = result;
} else {
// 说明result已经存在该属性,那么就把同名属性的值合并为一个数组
result[key] = [].concat(result[key], value);
}
});
});
return result;
}
export * from './mergeObject';
export * from './isObject';
export * from './observe';
9 changes: 9 additions & 0 deletions packages/core/objects/isObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 判断是否为对象
* @param {any} data
* @returns {boolean}
*/
export function isObject(data: any): boolean {
return Object.prototype.toString.call(data) === '[object Object]';
}

37 changes: 37 additions & 0 deletions packages/core/objects/mergeObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 判断是否为空对象
* @param obj 参数
* @returns {boolean}
*/
const isObj = (obj: any): boolean => {
return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
}
/**
* @func mergeObject
* @param {object} obj1 外部的参数对象
* @param {object} obj2 默认参数对象
* @desc 深拷贝合并对象
* @returns {Object}
*/
export function mergeObject(obj1: any, obj2: Object): Object {
if(!isObj(obj1)){ // 如果没有传参,返回默认值
return mergeObject({}, obj2);
}
if(!isObj(obj2)){ // 如果没有默认,返回传参
return mergeObject({},obj1);
}
let newObj = Object.assign({},obj2);
// 遍历传参对象
Object.keys(obj1).forEach(function(key){
let val = obj1[key]
if(key === '__proto__' || key === 'construtor') return;
if(val === null) return;
if(isObj(val) && isObj(newObj[key as keyof typeof newObj])) { // 如果传参中的值为对象,则递归调用
newObj[key] = mergeObject(val, newObj[key as keyof typeof newObj]);
} else {
newObj[key as keyof typeof newObj] = val;
}
})
return newObj;
}

55 changes: 55 additions & 0 deletions packages/core/objects/observe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 将一个对象转化为可观测对象
* @param { Object } obj 对象
* @param { String } key 对象的key
* @param { Any } val 对象的某个key的值
*/
const defineReactive = (obj: Object, key: string, val: any): void => {
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get() {
return val;
},
set(newVal) {
val = newVal;
}
})
}
/**
* 设计一个对象的观测者
* @param { Object } obj 对象
* @return { Object } 返回一个可观测对象
* @example let obj = observerDef({name:'alex',age:18})
*/
export const observeDef = (obj: Object): Object | undefined => {
if(!obj || typeof obj !== 'object'){
return;
}
let keys = Object.keys(obj);
keys.forEach(key => {
defineReactive(obj,key, obj[key as keyof typeof obj]);
})
return obj;
}

/**
* 设计一个对象的观测者 - proxy方案
* @param { Object } obj 对象
* @return { cal } 观测对象回调方法
* @example let obj = observeProxy({name:'alex',age:18},callback)
*/
export const observeProxy = (obj: Object, cal: (val: any) => void) => {
return new Proxy(obj, {
get: function(target,prop){
return Reflect.get(target, prop);
},
set: function(target, prop, val) {
cal(val);
return Reflect.set(target, prop, val);
},
deleteProperty: function (target, prop){
return Reflect.deleteProperty(target,prop);
}
})
}
Loading

0 comments on commit 728b43b

Please sign in to comment.