fuyao-web-utils
Version:
JavaScript常用工具库
2 lines (1 loc) • 5.1 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["web-utils"]={})}(this,(function(t){"use strict";var e=function(){return e=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},e.apply(this,arguments)};function n(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}"function"==typeof SuppressedError&&SuppressedError;var r=Object.prototype.toString,o=function(t,e){return function(t){return r.call(t).slice(8,-1).toLowerCase()}(t)===e},i={isNull:function(t){return null===t},isEmpty:function(t,e){void 0===e&&(e=!1);var n=function(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}([null,void 0,"",NaN],e?["null","undefined","NaN"]:[],!0);return n.includes(t)},isEmptyObject:function(t){return i.isEmpty(t)||i.isArray(t)&&!t.length||i.isPlainObject(t)&&!Object.keys(t).length},isArray:function(t){return Array.isArray(t)},isPlainObject:function(t){return o(t,"object")}},u=function(t,e,n){return t>=Math.pow(10,e)&&Math.round(t/Math.pow(10,e-n))/Math.pow(10,n)},c={isNumber:function(t){return"number"==typeof t&&!isNaN(t)},toNumber:function(t,e){void 0===e&&(e=0);var n=Number(t);return isNaN(n)?e:n},fixedNumber:function(t,e){return void 0===e&&(e=0),(Math.round(Number(t)*Math.pow(10,e))/Math.pow(10,e)).toFixed(e)},parseThousands:function(t){if(i.isEmpty(t))return"";var e="".concat(t).split("."),n=e[0],r=void 0===n?"0":n,o=e[1];return[r.replace(/(\d)(?=(?:\d{3})+$)/g,"$1,"),o].filter((function(t){return!i.isEmpty(t)})).join(".")},format:function(t,e,n){void 0===e&&(e=2),void 0===n&&(n=!1);var r=u(t,8,e);return r?(n?r.toFixed(e):r)+"亿":(r=u(t,4,e))?(n?r.toFixed(e):r)+"万":"".concat(t)},cn:function(t,e){if(Number(t)!=Number(t))return"".concat(null!=t?t:"");var n,r=!0===e?{traditional:!0,money:!0}:e||{},o=r.traditional,i=void 0!==o&&o,u=r.money,c=void 0!==u&&u,a="".concat(t).split("."),f=a[0],p=a[1],l=void 0===p?"":p,s=f.replace(/(\d)(?=(?:\d{4})+$)/g,"$1_").split("_"),y=(void 0===(n=i)&&(n=!1),{units:["","万","亿","万亿","亿亿"],decimalisms:n?["","拾","佰","仟"]:["","十","百","千"],integers:n?"零壹贰叁肆伍陆柒捌玖":"零一二三四五六七八九"}),d=y.units,v=y.integers,b=y.decimalisms,m=function(t){return v.charAt(Number(t))},h=s.reverse().map((function(t,e){var n=t.split("").map((function(e,n){return"0"===e?"零":m(e)+b[t.length-n-1]})).join("").replace(/\u96f6+$/,"").replace(/\u96f6+/g,"零");return n.length?n+d[e]:d[e]?"零":""})).reverse().join("").replace(/\u96f6+$/,"").replace(/\u96f6+/g,"零");if(c){if(!l.length||/^0+$/.test(l))return"".concat(h,"圆整");var g=l.split(""),O=g[0],j=void 0===O?"0":O,N=g[1],K=void 0===N?"0":N;return"".concat(h,"圆").concat("0"===j?"零":m(j)+"角").concat("0"===K?"":m(K)+"分")}var E=/^0+$/.test(l)?"":l.split("").map((function(t){return v.charAt(Number(t))})).join("");return E?"".concat(h,"点").concat(E):h}},a={transToTree:function(t,r,o){var u,c=r||{},a=c.key,f=void 0===a?"key":a,p=c.pKey,l=void 0===p?"pKey":p,s=c.rootKey,y=c.childrenKey,d=void 0===y?"children":y,v=c.parentKey,b=void 0===v?"parent":v,m=c.copy,h=void 0===m||m,g=c.assign,O=void 0===g||g,j=c.transKey,N=void 0!==j&&j,K=n(c,["key","pKey","rootKey","childrenKey","parentKey","copy","assign","transKey"]),E=function(){var t,n=h?function(t){return e({},t)}:function(t){return t};return N&&"key"!==f&&(K.key=f),N&&"pKey"!==l&&(K.pKey=f),i.isEmptyObject(K)?n:(t=Object.keys(K),function(e){return t.forEach((function(t,n){e[t]=e[K[t]]})),n(e)})}(),A=O?"function"==typeof Object.defineProperty?function(t,e){Object.defineProperty(t,b,{get:function(){return e},configurable:!1,enumerable:!1})}:function(t,e){t.getParent=function(){return e}}:function(t,e){},w="__root__",P=((u={})[w]=[],u),k={},x={},M=function(t){return!t&&0!==t};(Array.isArray(t)?t:[]).map((function(t){var n=E(t),r=n[l],o=n[f];return k[o]=e({},n),x[o]=n,M(r)?P[w].push(n):P[r]?P[r].push(n):P[r]=[n],n})).forEach((function(t){t[d]=P[t[f]],delete P[t[f]];var e=t[l];A(t,M(e)?null:x[e])}));var $=P,T=w,_=$[T],S=n($,[T+""]),F="function"==typeof r?r:"function"==typeof o?o:void 0;return F&&F(x,k),i.isEmpty(s)?_.concat.apply(_,Object.values(S)):P[s]||[]},transItemProps:function(t,e){var n=e.keyMap,r=e.recursive,o=void 0===r||r,u=e.childrenKey,c=void 0===u?"children":u,f=e.delOriginalKey,p=void 0===f||f;return t.map((function(t){Object.keys(n).forEach((function(e){t[e]=t[n[e]],p&&delete t[n[e]]}));var r=t[c];return o&&i.isArray(r)&&(t[c]=a.transItemProps(r,e)),t}))}};t.ArrayUtil=a,t.NumberUtil=c,t.TypeUtil=i}));