commonly-used-utils
Advanced tools
Comparing version 1.0.0-beat22 to 1.0.0-beat23
@@ -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 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=-1,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=-1,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})())); | ||
!function(r,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var e in n)("object"==typeof exports?exports:r)[e]=n[e]}}(self,(()=>(()=>{"use strict";var r={d:(t,n)=>{for(var e in n)r.o(n,e)&&!r.o(t,e)&&Object.defineProperty(t,e,{enumerable:!0,get:n[e]})},o:(r,t)=>Object.prototype.hasOwnProperty.call(r,t),r:r=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(r,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(r,"__esModule",{value:!0})}},t={};r.r(t),r.d(t,{NotRepeatedArray:()=>T,addItem:()=>P,areObjectArraysEqual:()=>p,deleteRecursively:()=>m,deleteRecursivelyReverse:()=>w,findArrayAttr:()=>A,findNestArrayAttr:()=>x,isArray:()=>l,isEmptyObject:()=>v,isFunction:()=>u,isNil:()=>f,isNumber:()=>s,isObject:()=>c,isObjectEqual:()=>h,isString:()=>a,mb:()=>d,oneDimensional:()=>b,removeItem:()=>k,setTreeAttr:()=>j,setTreeAttr_copy:()=>O,sleep:()=>C,treeFind:()=>E,treeFindParents:()=>g});var n,e=function(){return e=Object.assign||function(r){for(var t,n=1,e=arguments.length;n<e;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(r[o]=t[o]);return r},e.apply(this,arguments)},o=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"]]),i=function(r){return null==r?r+"":"object"==typeof r||"function"==typeof r?o.get(Object.prototype.toString.call(r))||"object":typeof r},c=function(r){return"object"===i(r)},u=function(r){return"function"===i(r)},l=function(r){return"array"===i(r)},a=function(r){return"string"===i(r)},f=function(r){return null==r},s=function(r){return null!==r&&""!==r&&void 0!==r&&!isNaN(Number(r))},v=function(r){if(!c(r))return!1;for(var t in r)if(r.hasOwnProperty(t))return!1;return!0},d=function(r){return function(t){return r.map((function(r){t=(t||{})[r]})),t}},h=function(r,t){var n=Object.keys(r),e=Object.keys(t);if(n.length!==e.length)return!1;for(var o=0,i=n;o<i.length;o++){var c=i[o];if(r[c]!==t[c])return!1}return!0},p=function(r,t){if(r.some((function(r){return l(r)}))||t.some((function(r){return l(r)})))throw new Error("暂不支持二维数组比对");if(r.length!==t.length)return!1;if(c(r[0])){for(var n=r.map((function(r){return Object.keys(r).sort().reduce((function(t,n){var o;return e(e({},t),((o={})[n]=r[n],o))}),{})})).sort((function(r,t){return JSON.stringify(r).localeCompare(JSON.stringify(t))})),o=t.map((function(r){return Object.keys(r).sort().reduce((function(t,n){var o;return e(e({},t),((o={})[n]=r[n],o))}),{})})).sort((function(r,t){return JSON.stringify(r).localeCompare(JSON.stringify(t))})),i=0;i<n.length;i++)if(!h(n[i],o[i]))return!1}else for(n=r.slice().sort(),o=t.slice().sort(),i=0;i<n.length;i++)if(n[i]!==o[i])return!1;return!0},y=function(r,t,n){if(n||2===arguments.length)for(var e,o=0,i=t.length;o<i;o++)!e&&o in t||(e||(e=Array.prototype.slice.call(t,0,o)),e[o]=t[o]);return r.concat(e||Array.prototype.slice.call(t))},b=function(r,t){return void 0===r&&(r=[]),void 0===t&&(t="children"),r&&r.length?r.reduce((function(r,n){var e;return r.concat(n,(null===(e=null==n?void 0:n[t])||void 0===e?void 0:e.length)?b(n[t],t):[])}),[]):[]},g=function(r,t,n,e,o,i,u){var l;if(void 0===r&&(r=[]),void 0===n&&(n="id"),void 0===e&&(e="children"),void 0===o&&(o=!0),void 0===i&&(i=!0),void 0===u&&(u=!0),!r.length)return[];for(var a=0,f=r.length;a<f;a++){var s=r[a];if(s[n]===t)return i?[s]:u?[]:s;var v=(null===(l=null==s?void 0:s[e])||void 0===l?void 0:l.length)&&g(s[e],t,n,e,o,i,!1);if(c(v))return[s];if(null==v?void 0:v.length)return o?[s].concat(v):v.concat(s)}},j=function(r,t,n,e){var o;void 0===r&&(r=[]),void 0===n&&(n="children"),void 0===e&&(e=[]);for(var i=0,c=r.length;i<c;i++){var u=r[i];t(u,e,i,r),(null===(o=null==u?void 0:u[n])||void 0===o?void 0:o.length)&&j(u[n],t,n,y(y([],e,!0),[u],!1))}},m=function(r,t,n,e){var o;void 0===r&&(r=[]),void 0===n&&(n="children"),void 0===e&&(e=[]);for(var c=0,u=r.length;c<u;c++){var l=r[c],a=t(l,e,c,r);if("boolean"!==i(a)){new Error("callback返回值:".concat(a,"(返回值只能是布尔值)"));break}a?(null===(o=null==l?void 0:l[n])||void 0===o?void 0:o.length)&&m(l[n],t,n,y(y([],e,!0),[l],!1)):(r.splice(c,1),u--,c--)}},w=function(r,t,n,e){var o;void 0===r&&(r=[]),void 0===n&&(n="children"),void 0===e&&(e=[]);for(var c=r.length-1;c>=0;c--){var u=r[c],l=t(u,e,c,r);if("boolean"!==i(l)){new Error("callback返回值:".concat(l,"(返回值只能是布尔值)"));break}l?(null===(o=null==u?void 0:u[n])||void 0===o?void 0:o.length)&&m(u[n],t,n,y(y([],e,!0),[u],!1)):r.splice(c,1)}},O=function(r,t,n,e){return void 0===n&&(n="children"),void 0===e&&(e=[]),r.length?r.reduce((function(r,o,i,u){var l=t(o,e,i,u);if(!c(l))throw new Error("函数返回值必须为对象");return l[n]&&(l[n]=O(l[n],t,n,y(y([],e,!0),[o],!1))),0===Object.keys(l).length?r:r.concat(l)}),[]):[]},E=function(r,t,n,e,o){void 0===n&&(n="{}"),void 0===e&&(e="children"),void 0===o&&(o=[]);for(var i=0,u=r.length;i<u;i++){var l=r[i];if(t(l,o,i,r))return"{}"===n||"Object"===n||c(n)?l:l[n];var a=l[e]&&E(l[e],t,n,e,y(y([],o,!0),[l],!1));if(a)return a}},S=(n=function(r,t){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(r,t){r.__proto__=t}||function(r,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])},n(r,t)},function(r,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function e(){this.constructor=r}n(r,t),r.prototype=null===t?Object.create(t):(e.prototype=t.prototype,new e)}),A=function(r,t,n,e,o){if(void 0===o&&(o=!1),!l(r)||!n||f(t)||o&&!r.length||"boolean"!=typeof o)throw new Error("请检查参数");var i=-1,u=r.find((function(r,e){var u=!!c(r)&&(o?r[n]===t:""+r[n]==""+t);return u&&(i=e),u}));if("{}"===e||"string"==typeof e&&"object"===e.toLowerCase()||c(e)){if(o&&!c(u))throw new Error("严格模式(对象)未找到, value: ".concat(t,", attr:").concat(n,", url: ").concat(location.href));return u}if(o&&(c(u)&&e in u&&"$index"!==e?f(u[e]):-1===i))throw new Error("严格模式(属性)未找到, value: ".concat(t,", attr:").concat(n,", url: ").concat(location.href));return"$index"===e?i:null==u?void 0:u[e]},x=function(r,t,n,e,o){if(void 0===o&&(o=!1),!l(r)||!l(n)||f(t)||o&&!r.length||"boolean"!=typeof o)throw new Error("请检查参数");var i=-1,u=r.find((function(r,e){var c=o?d(n)(r)===t:""+d(n)(r)==""+t;return c&&(i=e),c}));if("{}"===e||"string"==typeof e&&"object"===e.toLowerCase()||c(e)){if(o&&!c(u))throw new Error("严格模式(对象)未找到, value: ".concat(t,", attr:").concat(n,", url: ").concat(location.href));return u}if(o&&(c(u)&&e in u&&"$index"!==e?f(u[e]):-1===i))throw new Error("严格模式(属性)未找到, value: ".concat(t,", attr:").concat(n,", url: ").concat(location.href));return"$index"===e?i:null==u?void 0:u[e]};function N(r){if("number"!==i(r)&&"string"!==i(r))throw new Error("只支持string、number类型的数据");!l(this)||this.includes(r)?this instanceof Set&&this.add(r):this.push(r)}function _(r){if("number"!==i(r)&&"string"!==i(r))throw new Error("只支持string、number类型的数据");if(l(this)){var t=this.indexOf(r);-1!==t&&this.splice(t,1)}else this instanceof Set&&this.delete(r)}var P=function(){for(var r=this,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];for(var e in t){var o=t[e];l(o)?o.forEach((function(t){N.call(r,t)})):N.call(this,o)}return this},k=function(){for(var r=this,t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];for(var e in t){var o=t[e];l(o)?o.forEach((function(t){_.call(r,t)})):_.call(this,o)}return this},T=function(r){function t(t){return(null==t?void 0:t.length)?r.apply(this,t)||this:r.call(this)||this}return S(t,r),t.prototype.addItem=function(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];return P.call(this,r),this},t.prototype.removeItem=function(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];return k.call(this,r),this},t}(Array),C=function(r){return new Promise((function(t){return setTimeout(t,r)}))};return t})())); |
{ | ||
"name": "commonly-used-utils", | ||
"version": "1.0.0-beat22", | ||
"version": "1.0.0-beat23", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -38,1 +38,7 @@ ### 1.0.0-beat11 | ||
1. findArrayAttr、findNestArrayAttr 查找下标模式与数组 findIndex,返回值统一 | ||
### 1.0.0-beat23 | ||
#### 2024/6/17 | ||
1. 比对对象(内容) | ||
2. 比对数组(内容) | ||
3. 递归循环 |
@@ -10,3 +10,5 @@ // 常用工具 | ||
isString, | ||
mb | ||
mb, | ||
isObjectEqual, | ||
areObjectArraysEqual | ||
} from './utils/basics' | ||
@@ -13,0 +15,0 @@ |
@@ -252,2 +252,15 @@ import { isObject, type } from './utils/basics' | ||
} | ||
} | ||
} | ||
export const traverse = ( | ||
array = [], | ||
callback: (item: any, i: number, array: any[]) => void, | ||
children = 'children', | ||
) => { | ||
for (let i = 0, len = array.length; i < len; i++) { | ||
let item = array[i] | ||
item?.[children]?.length && traverse(item[children], callback, children) | ||
callback(item, i, array) | ||
} | ||
} | ||
@@ -67,1 +67,65 @@ const class2type = new Map([ | ||
// 比对对象 | ||
export const isObjectEqual = (obj1: {}, obj2: {}): boolean => { | ||
// 获取对象的键数组 | ||
const keys1 = Object.keys(obj1) | ||
const keys2 = Object.keys(obj2) | ||
// 检查键的数量是否相同 | ||
if (keys1.length !== keys2.length) { | ||
return false | ||
} | ||
// 检查键值对是否相同 | ||
for (const key of keys1) { | ||
if (obj1[key] !== obj2[key]) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
// 比对数组 | ||
export const areObjectArraysEqual = (arr1: any[], arr2: any[]) => { | ||
if (arr1.some((item) => isArray(item)) || arr2.some((item) => isArray(item))) { | ||
throw new Error('暂不支持二维数组比对') | ||
} | ||
// 判断数组内数据相同 | ||
if (arr1.length !== arr2.length) { | ||
return false | ||
} | ||
// 对象 | ||
if (isObject(arr1[0])) { | ||
// 对数组进行深度复制并排序 | ||
const sortedArr1 = arr1 | ||
.map((item) => Object.keys(item).sort().reduce((obj, key) => ({...obj, [key]: item[key]}), {})) | ||
.sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b))) | ||
const sortedArr2 = arr2 | ||
.map((item) => Object.keys(item).sort().reduce((obj, key) => ({...obj, [key]: item[key]}), {})) | ||
.sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b))) | ||
// 比较排序后的数组 | ||
for (let i = 0; i < sortedArr1.length; i++) { | ||
if (!isObjectEqual(sortedArr1[i], sortedArr2[i])) { | ||
return false | ||
} | ||
} | ||
} else { | ||
// 其他类型 | ||
const sortedArr1 = arr1.slice().sort() | ||
const sortedArr2 = arr2.slice().sort() | ||
for (let i = 0; i < sortedArr1.length; i++) { | ||
if (sortedArr1[i] !== sortedArr2[i]) { | ||
return false | ||
} | ||
} | ||
} | ||
// 如果所有对象都相同,则返回true | ||
return true | ||
} |
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
35845
850
44