-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24eb710
commit 728b43b
Showing
9 changed files
with
1,941 additions
and
1,855 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]'; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}) | ||
} |
Oops, something went wrong.