commonly-used-utils
Advanced tools
Comparing version 1.0.0-beat20 to 1.0.0-beat21
@@ -1,1 +0,1 @@ | ||
!function(t,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{var e=r();for(var n in e)("object"==typeof exports?exports:t)[n]=e[n]}}(self,(()=>(()=>{"use strict";var t={d:(r,e)=>{for(var n in e)t.o(e,n)&&!t.o(r,n)&&Object.defineProperty(r,n,{enumerable:!0,get:e[n]})},o:(t,r)=>Object.prototype.hasOwnProperty.call(t,r),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},r={};t.r(r),t.d(r,{NotRepeatedArray:()=>_,addItem:()=>E,deleteRecursively:()=>p,deleteRecursivelyReverse:()=>b,findArrayAttr:()=>m,isArray:()=>l,isEmptyObject:()=>f,isFunction:()=>c,isNil:()=>u,isNumber:()=>a,isObject:()=>i,oneDimensional:()=>d,removeItem:()=>A,setTreeAttr:()=>h,setTreeAttr_copy:()=>y,sleep:()=>S,treeFind:()=>g,treeFindParents:()=>s});var e,n=new Map([["[object Boolean]","boolean"],["[object Number]","number"],["[object String]","string"],["[object Function]","function"],["[object Array]","array"],["[object Date]","date"],["[object RegExp]","regExp"],["[object Object]","object"],["[object Error]","error"]]),o=function(t){return null==t?t+"":"object"==typeof t||"function"==typeof t?n.get(Object.prototype.toString.call(t))||"object":typeof t},i=function(t){return"object"===o(t)},c=function(t){return"function"===o(t)},l=function(t){return"array"===o(t)},u=function(t){return null==t},a=function(t){return null!==t&&""!==t&&void 0!==t&&!isNaN(Number(t))},f=function(t){if(!i(t))return!1;for(var r in t)if(t.hasOwnProperty(r))return!1;return!0},v=function(t,r,e){if(e||2===arguments.length)for(var n,o=0,i=r.length;o<i;o++)!n&&o in r||(n||(n=Array.prototype.slice.call(r,0,o)),n[o]=r[o]);return t.concat(n||Array.prototype.slice.call(r))},d=function(t,r){return void 0===t&&(t=[]),void 0===r&&(r="children"),t&&t.length?t.reduce((function(t,e){var n;return t.concat(e,(null===(n=null==e?void 0:e[r])||void 0===n?void 0:n.length)?d(e[r],r):[])}),[]):[]},s=function(t,r,e,n,o,c,l){var u;if(void 0===t&&(t=[]),void 0===e&&(e="id"),void 0===n&&(n="children"),void 0===o&&(o=!0),void 0===c&&(c=!0),void 0===l&&(l=!0),!t.length)return[];for(var a=0,f=t.length;a<f;a++){var v=t[a];if(v[e]===r)return c?[v]:l?[]:v;var d=(null===(u=null==v?void 0:v[n])||void 0===u?void 0:u.length)&&s(v[n],r,e,n,o,c,!1);if(i(d))return[v];if(null==d?void 0:d.length)return o?[v].concat(d):d.concat(v)}},h=function(t,r,e,n){var o;void 0===t&&(t=[]),void 0===e&&(e="children"),void 0===n&&(n=[]);for(var i=0,c=t.length;i<c;i++){var l=t[i];r(l,n,i,t),(null===(o=null==l?void 0:l[e])||void 0===o?void 0:o.length)&&h(l[e],r,e,v(v([],n,!0),[l],!1))}},p=function(t,r,e,n){var i;void 0===t&&(t=[]),void 0===e&&(e="children"),void 0===n&&(n=[]);for(var c=0,l=t.length;c<l;c++){var u=t[c],a=r(u,n,c,t);if("boolean"!==o(a)){new Error("callback返回值:".concat(a,"(返回值只能是布尔值)"));break}a?(null===(i=null==u?void 0:u[e])||void 0===i?void 0:i.length)&&p(u[e],r,e,v(v([],n,!0),[u],!1)):(t.splice(c,1),l--,c--)}},b=function(t,r,e,n){var i;void 0===t&&(t=[]),void 0===e&&(e="children"),void 0===n&&(n=[]);for(var c=t.length-1;c>=0;c--){var l=t[c],u=r(l,n,c,t);if("boolean"!==o(u)){new Error("callback返回值:".concat(u,"(返回值只能是布尔值)"));break}u?(null===(i=null==l?void 0:l[e])||void 0===i?void 0:i.length)&&p(l[e],r,e,v(v([],n,!0),[l],!1)):t.splice(c,1)}},y=function(t,r,e,n){return void 0===e&&(e="children"),void 0===n&&(n=[]),t.length?t.reduce((function(t,o,c,l){var u=r(o,n,c,l);if(!i(u))throw new Error("函数返回值必须为对象");return u[e]&&(u[e]=y(u[e],r,e,v(v([],n,!0),[o],!1))),0===Object.keys(u).length?t:t.concat(u)}),[]):[]},g=function(t,r,e,n,o){void 0===e&&(e="{}"),void 0===n&&(n="children"),void 0===o&&(o=[]);for(var c=0,l=t.length;c<l;c++){var u=t[c];if(r(u,o,c,t))return"{}"===e||"Object"===e||i(e)?u:u[e];var a=u[n]&&g(u[n],r,e,n,v(v([],o,!0),[u],!1));if(a)return a}},j=(e=function(t,r){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])},e(t,r)},function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}),m=function(t,r,e,n,o){if(void 0===o&&(o=!1),!l(t)||!e||u(r)||o&&!t.length||"boolean"!=typeof o)throw new Error("请检查参数");var c=t.find((function(t){return!!i(t)&&(o?t[e]===r:""+t[e]==""+r)}));if("{}"===n||"string"==typeof n&&"object"===n.toLowerCase()||i(n)){if(o&&!i(c))throw new Error("严格模式(对象)未找到, value: ".concat(r,", attr:").concat(e,", url: ").concat(location.href));return c}if(o&&(!i(c)||!(n in c)||u(c[n])))throw new Error("严格模式(属性)未找到, value: ".concat(r,", attr:").concat(e,", url: ").concat(location.href));return null==c?void 0:c[n]};function w(t){if("number"!==o(t)&&"string"!==o(t))throw new Error("只支持string、number类型的数据");!l(this)||this.includes(t)?this instanceof Set&&this.add(t):this.push(t)}function O(t){if("number"!==o(t)&&"string"!==o(t))throw new Error("只支持string、number类型的数据");if(l(this)){var r=this.indexOf(t);-1!==r&&this.splice(r,1)}else this instanceof Set&&this.delete(t)}var E=function(){for(var t=this,r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];for(var n in r){var o=r[n];l(o)?o.forEach((function(r){w.call(t,r)})):w.call(this,o)}return this},A=function(){for(var t=this,r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];for(var n in r){var o=r[n];l(o)?o.forEach((function(r){O.call(t,r)})):O.call(this,o)}return this},_=function(t){function r(r){return(null==r?void 0:r.length)?t.apply(this,r)||this:t.call(this)||this}return j(r,t),r.prototype.addItem=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return E.call(this,t),this},r.prototype.removeItem=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return A.call(this,t),this},r}(Array),S=function(t){return new Promise((function(r){return setTimeout(r,t)}))};return r})())); | ||
!function(t,r){if("object"==typeof exports&&"object"==typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{var n=r();for(var e in n)("object"==typeof exports?exports:t)[e]=n[e]}}(self,(()=>(()=>{"use strict";var t={d:(r,n)=>{for(var e in n)t.o(n,e)&&!t.o(r,e)&&Object.defineProperty(r,e,{enumerable:!0,get:n[e]})},o:(t,r)=>Object.prototype.hasOwnProperty.call(t,r),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},r={};t.r(r),t.d(r,{NotRepeatedArray:()=>P,addItem:()=>S,deleteRecursively:()=>y,deleteRecursivelyReverse:()=>g,findArrayAttr:()=>E,findNestArrayAttr:()=>O,isArray:()=>l,isEmptyObject:()=>v,isFunction:()=>c,isNil:()=>a,isNumber:()=>f,isObject:()=>i,isString:()=>u,mb:()=>d,oneDimensional:()=>h,removeItem:()=>_,setTreeAttr:()=>b,setTreeAttr_copy:()=>j,sleep:()=>N,treeFind:()=>w,treeFindParents:()=>p});var n,e=new Map([["[object Boolean]","boolean"],["[object Number]","number"],["[object String]","string"],["[object Function]","function"],["[object Array]","array"],["[object Date]","date"],["[object RegExp]","regExp"],["[object Object]","object"],["[object Error]","error"]]),o=function(t){return null==t?t+"":"object"==typeof t||"function"==typeof t?e.get(Object.prototype.toString.call(t))||"object":typeof t},i=function(t){return"object"===o(t)},c=function(t){return"function"===o(t)},l=function(t){return"array"===o(t)},u=function(t){return"string"===o(t)},a=function(t){return null==t},f=function(t){return null!==t&&""!==t&&void 0!==t&&!isNaN(Number(t))},v=function(t){if(!i(t))return!1;for(var r in t)if(t.hasOwnProperty(r))return!1;return!0},d=function(t){return function(r){return t.map((function(t){r=(r||{})[t]})),r}},s=function(t,r,n){if(n||2===arguments.length)for(var e,o=0,i=r.length;o<i;o++)!e&&o in r||(e||(e=Array.prototype.slice.call(r,0,o)),e[o]=r[o]);return t.concat(e||Array.prototype.slice.call(r))},h=function(t,r){return void 0===t&&(t=[]),void 0===r&&(r="children"),t&&t.length?t.reduce((function(t,n){var e;return t.concat(n,(null===(e=null==n?void 0:n[r])||void 0===e?void 0:e.length)?h(n[r],r):[])}),[]):[]},p=function(t,r,n,e,o,c,l){var u;if(void 0===t&&(t=[]),void 0===n&&(n="id"),void 0===e&&(e="children"),void 0===o&&(o=!0),void 0===c&&(c=!0),void 0===l&&(l=!0),!t.length)return[];for(var a=0,f=t.length;a<f;a++){var v=t[a];if(v[n]===r)return c?[v]:l?[]:v;var d=(null===(u=null==v?void 0:v[e])||void 0===u?void 0:u.length)&&p(v[e],r,n,e,o,c,!1);if(i(d))return[v];if(null==d?void 0:d.length)return o?[v].concat(d):d.concat(v)}},b=function(t,r,n,e){var o;void 0===t&&(t=[]),void 0===n&&(n="children"),void 0===e&&(e=[]);for(var i=0,c=t.length;i<c;i++){var l=t[i];r(l,e,i,t),(null===(o=null==l?void 0:l[n])||void 0===o?void 0:o.length)&&b(l[n],r,n,s(s([],e,!0),[l],!1))}},y=function(t,r,n,e){var i;void 0===t&&(t=[]),void 0===n&&(n="children"),void 0===e&&(e=[]);for(var c=0,l=t.length;c<l;c++){var u=t[c],a=r(u,e,c,t);if("boolean"!==o(a)){new Error("callback返回值:".concat(a,"(返回值只能是布尔值)"));break}a?(null===(i=null==u?void 0:u[n])||void 0===i?void 0:i.length)&&y(u[n],r,n,s(s([],e,!0),[u],!1)):(t.splice(c,1),l--,c--)}},g=function(t,r,n,e){var i;void 0===t&&(t=[]),void 0===n&&(n="children"),void 0===e&&(e=[]);for(var c=t.length-1;c>=0;c--){var l=t[c],u=r(l,e,c,t);if("boolean"!==o(u)){new Error("callback返回值:".concat(u,"(返回值只能是布尔值)"));break}u?(null===(i=null==l?void 0:l[n])||void 0===i?void 0:i.length)&&y(l[n],r,n,s(s([],e,!0),[l],!1)):t.splice(c,1)}},j=function(t,r,n,e){return void 0===n&&(n="children"),void 0===e&&(e=[]),t.length?t.reduce((function(t,o,c,l){var u=r(o,e,c,l);if(!i(u))throw new Error("函数返回值必须为对象");return u[n]&&(u[n]=j(u[n],r,n,s(s([],e,!0),[o],!1))),0===Object.keys(u).length?t:t.concat(u)}),[]):[]},w=function(t,r,n,e,o){void 0===n&&(n="{}"),void 0===e&&(e="children"),void 0===o&&(o=[]);for(var c=0,l=t.length;c<l;c++){var u=t[c];if(r(u,o,c,t))return"{}"===n||"Object"===n||i(n)?u:u[n];var a=u[e]&&w(u[e],r,n,e,s(s([],o,!0),[u],!1));if(a)return a}},m=(n=function(t,r){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])},n(t,r)},function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function e(){this.constructor=t}n(t,r),t.prototype=null===r?Object.create(r):(e.prototype=r.prototype,new e)}),E=function(t,r,n,e,o){if(void 0===o&&(o=!1),!l(t)||!n||a(r)||o&&!t.length||"boolean"!=typeof o)throw new Error("请检查参数");var c,u=t.find((function(t,e){var l=!!i(t)&&(o?t[n]===r:""+t[n]==""+r);return l&&(c=e),l}));if("{}"===e||"string"==typeof e&&"object"===e.toLowerCase()||i(e)){if(o&&!i(u))throw new Error("严格模式(对象)未找到, value: ".concat(r,", attr:").concat(n,", url: ").concat(location.href));return u}if(o&&(i(u)&&e in u&&"$index"!==e?a(u[e]):-1===c))throw new Error("严格模式(属性)未找到, value: ".concat(r,", attr:").concat(n,", url: ").concat(location.href));return"$index"===e?c:null==u?void 0:u[e]},O=function(t,r,n,e,o){if(void 0===o&&(o=!1),!l(t)||!l(n)||a(r)||o&&!t.length||"boolean"!=typeof o)throw new Error("请检查参数");var c,u=t.find((function(t,e){var i=o?d(n)(t)===r:""+d(n)(t)==""+r;return i&&(c=e),i}));if("{}"===e||"string"==typeof e&&"object"===e.toLowerCase()||i(e)){if(o&&!i(u))throw new Error("严格模式(对象)未找到, value: ".concat(r,", attr:").concat(n,", url: ").concat(location.href));return u}if(o&&(i(u)&&e in u&&"$index"!==e?a(u[e]):-1===c))throw new Error("严格模式(属性)未找到, value: ".concat(r,", attr:").concat(n,", url: ").concat(location.href));return"$index"===e?c:null==u?void 0:u[e]};function A(t){if("number"!==o(t)&&"string"!==o(t))throw new Error("只支持string、number类型的数据");!l(this)||this.includes(t)?this instanceof Set&&this.add(t):this.push(t)}function x(t){if("number"!==o(t)&&"string"!==o(t))throw new Error("只支持string、number类型的数据");if(l(this)){var r=this.indexOf(t);-1!==r&&this.splice(r,1)}else this instanceof Set&&this.delete(t)}var S=function(){for(var t=this,r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];for(var e in r){var o=r[e];l(o)?o.forEach((function(r){A.call(t,r)})):A.call(this,o)}return this},_=function(){for(var t=this,r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];for(var e in r){var o=r[e];l(o)?o.forEach((function(r){x.call(t,r)})):x.call(this,o)}return this},P=function(t){function r(r){return(null==r?void 0:r.length)?t.apply(this,r)||this:t.call(this)||this}return m(r,t),r.prototype.addItem=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return S.call(this,t),this},r.prototype.removeItem=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return _.call(this,t),this},r}(Array),N=function(t){return new Promise((function(r){return setTimeout(r,t)}))};return r})())); |
{ | ||
"name": "commonly-used-utils", | ||
"version": "1.0.0-beat20", | ||
"version": "1.0.0-beat21", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -29,1 +29,6 @@ ### 1.0.0-beat11 | ||
1. isEmptyObject 判断空对象方法新增 | ||
### 1.0.0-beat21 | ||
#### 2024/5/13 | ||
1. 扩展 findArrayAttr 查找下标的能力 | ||
2. 新增 findNestArrayAttr 获取嵌套属性 |
@@ -1,2 +0,2 @@ | ||
import { isArray, isNil, isObject, type } from './utils/basics' | ||
import { isArray, isNil, isObject, type, mb } from './utils/basics' | ||
@@ -8,3 +8,3 @@ /** | ||
* @param attr 属性值(默认值'id') | ||
* @param returnData 返回数据(对象或value) | ||
* @param returnData 返回数据(对象或 value 或下标('$index')) | ||
* @param isStrict 严格模式(== 与 === 的区别) | ||
@@ -20,3 +20,3 @@ * @example | ||
attr: 'id' | string | number, | ||
returnData: string | number | object | '{}' | 'object', | ||
returnData: string | number | object | '{}' | 'object' | '$index', | ||
isStrict = false | ||
@@ -34,7 +34,12 @@ ): number | string | undefined | object => { | ||
const result = array.find(i => | ||
isObject(i) | ||
let _index | ||
const result = array.find((i, index) => { | ||
const _result = isObject(i) ? isStrict ? i[attr] === value : '' + i[attr] === '' + value : false | ||
_result && (_index = index) | ||
return _result | ||
/*return isObject(i) | ||
? isStrict ? i[attr] === value : '' + i[attr] === '' + value | ||
: false | ||
) | ||
: false*/ | ||
}) | ||
@@ -55,2 +60,3 @@ // 对象模式 | ||
// 属性模式(严格) | ||
// 不是对象 or 不存在属性 or 属性[返回值如果是下标,未找到的情况下] [其他返回值,不为 null、undefined] | ||
if ( | ||
@@ -61,3 +67,3 @@ isStrict && | ||
!(<string>returnData in result) || | ||
isNil(result[<string>returnData]) | ||
returnData === '$index' ? _index === -1 : isNil(result[<string>returnData]) | ||
) | ||
@@ -70,5 +76,61 @@ ) { | ||
return result?.[<string>returnData] // 宽松 | ||
return returnData === '$index' ? _index : result?.[<string>returnData] | ||
// return result?.[<string>returnData] // 宽松 | ||
} | ||
export const findNestArrayAttr = ( | ||
array: any[], | ||
value: string | number | undefined, | ||
attr: (string | number)[], | ||
returnData: string | number | object | '{}' | 'object', | ||
isStrict = false | ||
): number | string | undefined | object => { | ||
if ( | ||
!isArray(array) || | ||
!isArray(attr) || | ||
isNil(value) || | ||
(isStrict && !array.length) || | ||
typeof isStrict !== 'boolean' | ||
) { | ||
throw new Error('请检查参数') | ||
} | ||
let _index | ||
const result = array.find((item, index) => { | ||
const _result = isStrict ? mb(attr)(item) === value : '' + mb(attr)(item) === '' + value | ||
_result && (_index = index) | ||
return _result | ||
}) | ||
// 对象模式 | ||
if ( | ||
returnData === '{}' || | ||
(typeof returnData === 'string' && returnData.toLowerCase() === 'object') || | ||
isObject(returnData) | ||
) { | ||
// 严格 | ||
if (isStrict && !isObject(result)) { | ||
throw new Error(`严格模式(对象)未找到, value: ${value}, attr:${attr}, url: ${location.href}`) | ||
} | ||
return result // 宽松 | ||
} | ||
// 属性模式(严格) | ||
// 不是对象 or 不存在属性 or 属性[返回值如果是下标,未找到的情况下] [其他返回值,不为 null、undefined] | ||
if ( | ||
isStrict && | ||
( | ||
!isObject(result) || | ||
!(<string>returnData in result) || | ||
returnData === '$index' ? _index === -1 : isNil(result[<string>returnData]) | ||
) | ||
) { | ||
// 对象 -> 属性 -> 属性不为null、undefined | ||
// 严格 | ||
throw new Error(`严格模式(属性)未找到, value: ${value}, attr:${attr}, url: ${location.href}`) | ||
} | ||
return returnData === '$index' ? _index : result?.[<string>returnData] | ||
} | ||
/** | ||
@@ -173,2 +235,2 @@ * 添加不重复数据(string、number) | ||
} | ||
} | ||
} |
@@ -8,3 +8,5 @@ // 常用工具 | ||
isNumber, | ||
isEmptyObject | ||
isEmptyObject, | ||
isString, | ||
mb | ||
} from './utils/basics' | ||
@@ -26,2 +28,3 @@ | ||
findArrayAttr, | ||
findNestArrayAttr, | ||
NotRepeatedArray, | ||
@@ -28,0 +31,0 @@ addItem, |
@@ -26,3 +26,3 @@ const class2type = new Map([ | ||
export const isArray = (param) => type(param) === 'array' | ||
export const isString = (param) => type(param) === 'string' | ||
/** | ||
@@ -59,1 +59,10 @@ * 是否为 null 或 undefined | ||
} | ||
// 获取对象嵌套属性 | ||
export const mb = (p: (string | number)[]) => (o: string | number | undefined) => { | ||
p.map(c => { | ||
o = (o || {})[c] | ||
}) | ||
return o | ||
} | ||
@@ -24,5 +24,10 @@ // 是否为对象 | ||
export function isString (param: any): param is boolean | ||
// 是否为空对象 | ||
export function isEmptyObject (param: any): param is boolean | ||
// 获取嵌套对象 | ||
export function mb (param: (string | number)[]): string | number | undefined | ||
/** | ||
@@ -69,7 +74,10 @@ * 多维嵌套数组转一维数组 | ||
* @param returnData 返回数据类型(返回匹配的对象或匹配对象的某个属性) | ||
* key or '{}' or 'object' or '$index' | ||
* @param isStrict 严格模式(== 与 === 的区别) | ||
* @example | ||
* const array = [{ value: '小明', id: 1 }, { value: '小红', id: 2}] | ||
* const array = [{ value: '小明', id: 1 }, { value: '小红', id: 2 }] | ||
* findArrayAttr(array, 1, 'id', {}) // { value: '小明', id: 1 } | ||
* findArrayAttr(array, 2, 'id', 'value') // value: '小红' | ||
* findArrayAttr(array, 2, 'id', 'value') // '小红' | ||
* findArrayAttr(array, '小明’, 'value', 'id') // 1 | ||
* findArrayAttr(array, '小明’, 'value', '$index') // 0 | ||
*/ | ||
@@ -76,0 +84,0 @@ export function findArrayAttr <T> ( |
32256
782
34