commonly-used-utils
Advanced tools
Comparing version 1.0.0-beat10 to 1.0.0-beat11
{ | ||
"name": "commonly-used-utils", | ||
"version": "1.0.0-beat10", | ||
"version": "1.0.0-beat11", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import { isArray, isNil, isObject, type } from './utils/basics' | ||
/** | ||
* 查找数组符合属性的数据 | ||
* @param array 数组 | ||
* @param value value | ||
* @param attr 属性值(默认值'id') | ||
* @param returnData 返回数据(对象或value) | ||
* @param isStrict 严格模式(== 与 === 的区别) | ||
* @example | ||
* const array = [{ value: '小明', id: 1 }, { value: '小红', id: 2}] | ||
* findArrayAttr(array, 1, 'id', {}) // { value: '小明', id: 1 } | ||
* findArrayAttr(array, 2, 'id', 'value') // value: '小红' | ||
*/ | ||
export const findArrayAttr = ( | ||
@@ -54,2 +66,13 @@ array: any[], | ||
} | ||
/** | ||
* 添加不重复数据(string、number) | ||
* @param value string | number | ||
* @return this | ||
* @example | ||
* const array = [] | ||
* add.call(array, 1) // [1] | ||
* add.call(array, 1) // [1] | ||
* add.call(array, 2) // [1, 2] | ||
*/ | ||
function add(value: number | string): void { | ||
@@ -71,2 +94,12 @@ if (type(value) !== 'number' && type(value) !== 'string') { | ||
/** | ||
* 删除数组数据 | ||
* @param value string | number | ||
* @return this | ||
* @example | ||
* const array = [1, 2] | ||
* remove.call(array, 1) // [2] | ||
* remove.call(array, 1) // [2] | ||
* remove.call(array, 2) // [] | ||
*/ | ||
function remove(value: number | string): void { | ||
@@ -89,2 +122,10 @@ if (type(value) !== 'number' && type(value) !== 'string') { | ||
/** | ||
* 添加不重复数据(多参数) | ||
* @param args | ||
* @return this | ||
* @example | ||
* const array = [] | ||
* addItem.call(array, 1, 1, 2, 3) // [1, 2, 3] | ||
*/ | ||
export const addItem = function (...args) { | ||
@@ -98,2 +139,10 @@ for (let i in args) { | ||
/** | ||
* 删除数组数据(多参数) | ||
* @param args | ||
* @return this | ||
* @example | ||
* const array = [1, 2] | ||
* removeItem.call(array, 1, 2, 3) // [] | ||
*/ | ||
export const removeItem = function (...args) { | ||
@@ -100,0 +149,0 @@ for (let i in args) { |
@@ -1,1 +0,6 @@ | ||
export const sleep = (time) => new Promise(resolve => setTimeout(resolve, time)) | ||
/** | ||
* 睡眠(由于 Event Loop 机制,无法保证睡眠结束后代码立即执行) | ||
* @param time | ||
* @return void | ||
*/ | ||
export const sleep = (time: 0) => new Promise(resolve => setTimeout(resolve, time)) |
import { isObject, type } from './utils/basics' | ||
/** | ||
* 多维嵌套数组转一维数组 | ||
* @param array treeData | ||
* @param children 数组子类字段(默认值'children') | ||
* @returns array|[] | ||
* @example | ||
* oneDimensional(treeData, 'children') | ||
*/ | ||
export const oneDimensional: (array?: any[], children?: string | 'children') => (any[]) = ( | ||
@@ -14,2 +22,15 @@ array: any[] = [], | ||
/** | ||
* 通过 ID 查找祖父节点 | ||
* @param array treeData | ||
* @param value value | ||
* @param attr 属性值(默认值'id') | ||
* @param children 数组子类字段(默认值'children') | ||
* @param descend 返回数组是否为正序 | ||
* @param self 返回数组是否包含自身 | ||
* @param firstLayer | ||
* @return array|[] | ||
* @example | ||
* treeFindParents(treeData, value, 'id', 'children', true, true) | ||
*/ | ||
export const treeFindParents = ( | ||
@@ -43,2 +64,20 @@ array = [], | ||
/** | ||
* 递归树每个对象 | ||
* @param array treeData | ||
* @param callback 每个节点执行的方法 | ||
* @param children 数组子类字段(默认值'children') | ||
* @param parents | ||
* @return void | ||
* @example | ||
* setTreeAttr( | ||
* treeData, | ||
* (item, parents) => { | ||
* item['parentId'] = parents[parents.length - 1]?.['id'] // 父节点 | ||
* item['parentIds'] = parents.map((i) => i?.['id']) || [] // 所有父级链id | ||
* ... | ||
* }, | ||
* 'children' | ||
* ) | ||
*/ | ||
export const setTreeAttr = ( | ||
@@ -57,2 +96,24 @@ array = [], | ||
/** | ||
* 删除不符合条件的某个节点 | ||
* @param array treeData | ||
* @param callback 每个对象执行的方法 | ||
* @param children 数组子类字段(默认值'children') | ||
* @example | ||
* deleteRecursively( | ||
* treeData, | ||
* (item) => { | ||
* if (item['disable']) { | ||
* return false | ||
* } | ||
* | ||
* // 添加数据等 | ||
* // item['disableStatus'] = 1 | ||
* // ... | ||
* | ||
* return true | ||
* }, | ||
* 'children' | ||
* ) | ||
*/ | ||
export const deleteRecursively = ( | ||
@@ -81,2 +142,24 @@ array = [], | ||
/** | ||
* 删除不符合条件的某个节点(倒叙执行) | ||
* @param array treeData | ||
* @param callback 每个对象执行的方法 | ||
* @param children 数组子类字段(默认值'children') | ||
* @example | ||
* deleteRecursively( | ||
* treeData, | ||
* (item) => { | ||
* if (item['disable']) { | ||
* return false | ||
* } | ||
* | ||
* // 添加数据等 | ||
* // item['disableStatus'] = 1 | ||
* // ... | ||
* | ||
* return true | ||
* }, | ||
* 'children' | ||
* ) | ||
*/ | ||
export const deleteRecursivelyReverse = ( | ||
@@ -103,2 +186,10 @@ array = [], | ||
/** | ||
* 实验方法,不推荐使用 | ||
* 递归树每个对象(通过每个对象执行) | ||
* @param array treeData | ||
* @param callback 回调 | ||
* @param children 数组子类字段(默认值'children') | ||
* @param parents | ||
*/ | ||
export const setTreeAttr_copy = ( | ||
@@ -105,0 +196,0 @@ array, |
@@ -20,5 +20,9 @@ const class2type = new Map([ | ||
// 是否为对象 | ||
export const isObject = (param) => type(param) === 'object' | ||
// 是否为方法 | ||
export const isFunction = (param) => type(param) === 'function' | ||
// 是否为数组 | ||
export const isArray = (param) => type(param) === 'array' | ||
// 是否为 NULL | ||
export const isNil = (param) => param == null |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20974
17
494
3