lgutils 1.3.31 → 1.3.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -0
- package/README.md +1 -1
- package/index.d.ts +23 -1
- package/lib/lgutils.cjs.js +92 -17
- package/lib/lgutils.cjs.prod.js +1 -1
- package/lib/lgutils.esm-bundler.js +90 -18
- package/package.json +2 -9
package/LICENSE.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# lgutils
|
package/README.md
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
#
|
|
1
|
+
# lgutils
|
package/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { parse } from 'qs'
|
|
2
2
|
import { stringify } from 'qs'
|
|
3
3
|
|
|
4
|
-
export declare
|
|
4
|
+
export declare function accAdd(a: number | string, b: number | string): number
|
|
5
5
|
|
|
6
6
|
export declare const accDiv: (arg1: number, arg2: number) => number
|
|
7
7
|
|
|
@@ -84,6 +84,21 @@ export declare function formatDuration(period: string): string
|
|
|
84
84
|
|
|
85
85
|
export declare const formatDurationNil: (d: string | null | undefined) => string
|
|
86
86
|
|
|
87
|
+
/**
|
|
88
|
+
* 金额格式化为万元,带千分位,最多保留6位小数
|
|
89
|
+
* @param amount 金额(元)
|
|
90
|
+
* @returns 格式化后的金额(万元)
|
|
91
|
+
*/
|
|
92
|
+
export declare function formatNumWithUnit(
|
|
93
|
+
num: number | string,
|
|
94
|
+
min?: number,
|
|
95
|
+
max?: number,
|
|
96
|
+
unit?: string | boolean,
|
|
97
|
+
useOrg?: boolean,
|
|
98
|
+
showNull?: boolean,
|
|
99
|
+
type?: number,
|
|
100
|
+
): string | undefined
|
|
101
|
+
|
|
87
102
|
export declare const formatRatio: (
|
|
88
103
|
n:
|
|
89
104
|
| number
|
|
@@ -185,6 +200,8 @@ export declare const random: (min: number, max: number) => number
|
|
|
185
200
|
|
|
186
201
|
export declare const randomString: (len?: number) => string
|
|
187
202
|
|
|
203
|
+
export declare const randomWithCrypto: () => string
|
|
204
|
+
|
|
188
205
|
export declare const removeAll: () => any
|
|
189
206
|
|
|
190
207
|
export declare const removeItem: (key: string) => any
|
|
@@ -193,6 +210,11 @@ export declare const setItem: (key: string, value: any) => any
|
|
|
193
210
|
|
|
194
211
|
export declare const setProxyObj: (data: object, getFn: () => void, setFn: () => void) => object
|
|
195
212
|
|
|
213
|
+
export declare const sOptions: {
|
|
214
|
+
key: number
|
|
215
|
+
value: number
|
|
216
|
+
}[]
|
|
217
|
+
|
|
196
218
|
export declare const sortObj: <
|
|
197
219
|
T extends keyof D,
|
|
198
220
|
D extends {
|
package/lib/lgutils.cjs.js
CHANGED
|
@@ -35,6 +35,7 @@ const isSMSCode = (code) => {
|
|
|
35
35
|
return code.match(/^\d{6}$/);
|
|
36
36
|
};
|
|
37
37
|
const random = (min, max) => Math.round(Math.random() * (max - min) + min);
|
|
38
|
+
const randomWithCrypto = () => crypto.randomUUID();
|
|
38
39
|
const playAudio = (mp3Id) => {
|
|
39
40
|
const audioArr = document.querySelectorAll('audio');
|
|
40
41
|
Array.from(audioArr).forEach((mp3) => {
|
|
@@ -161,23 +162,20 @@ const accDiv = (arg1, arg2) => {
|
|
|
161
162
|
return (r1 / r2) * Math.pow(10, t2 - t1);
|
|
162
163
|
};
|
|
163
164
|
// 加
|
|
164
|
-
|
|
165
|
-
let
|
|
166
|
-
|
|
167
|
-
|
|
165
|
+
function accAdd(a, b) {
|
|
166
|
+
let aStr = a.toString();
|
|
167
|
+
let bStr = b.toString();
|
|
168
|
+
let aDecimal = 0, bDecimal = 0;
|
|
169
|
+
if (aStr.indexOf('.') > -1) {
|
|
170
|
+
aDecimal = aStr.split('.')[1].length;
|
|
168
171
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
try {
|
|
173
|
-
r2 = arg2.toString().split('.')[1].length;
|
|
172
|
+
if (bStr.indexOf('.') > -1) {
|
|
173
|
+
bDecimal = bStr.split('.')[1].length;
|
|
174
174
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
return (arg1 * m + arg2 * m) / m;
|
|
180
|
-
};
|
|
175
|
+
const base = Math.pow(10, Math.max(aDecimal, bDecimal));
|
|
176
|
+
// 使用 Number 防止 string 拼接
|
|
177
|
+
return Number(+a * base + +b * base) / base;
|
|
178
|
+
}
|
|
181
179
|
// 减
|
|
182
180
|
const accSub = function (arg1, arg2) {
|
|
183
181
|
let r1, r2, m, n;
|
|
@@ -234,7 +232,80 @@ const loopData = (arr, childkey = 'children') => {
|
|
|
234
232
|
}
|
|
235
233
|
}
|
|
236
234
|
return arr;
|
|
237
|
-
};
|
|
235
|
+
};
|
|
236
|
+
/**
|
|
237
|
+
* 金额格式化为万元,带千分位,最多保留6位小数
|
|
238
|
+
* @param amount 金额(元)
|
|
239
|
+
* @returns 格式化后的金额(万元)
|
|
240
|
+
*/
|
|
241
|
+
function formatNumWithUnit(num, min = 2, max = 2, unit = '', useOrg = false, showNull = false, type = 2) {
|
|
242
|
+
if ((!num || num == '' || num == '-') && num !== 0)
|
|
243
|
+
return showNull ? undefined : '-';
|
|
244
|
+
// 转换为数字
|
|
245
|
+
let _num = typeof num === 'string' ? parseFloat(num) : num;
|
|
246
|
+
const isLt0 = +_num < 0;
|
|
247
|
+
if (isLt0)
|
|
248
|
+
_num = 0 - _num;
|
|
249
|
+
if (!isNumber(_num) || isNaN(+_num))
|
|
250
|
+
return _num;
|
|
251
|
+
// const isBillion = num / (10000 * 10000) >= 1
|
|
252
|
+
const _obj = sOptions === null || sOptions === void 0 ? void 0 : sOptions[type];
|
|
253
|
+
// 转换为万元
|
|
254
|
+
let wan = _num;
|
|
255
|
+
if (!useOrg) {
|
|
256
|
+
wan = (isLt0 ? 0 - _num : _num) / _obj.key; //_num / 10000
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
wan = isLt0 ? 0 - _num : _num;
|
|
260
|
+
}
|
|
261
|
+
if (unit === true)
|
|
262
|
+
unit = _obj === null || _obj === void 0 ? void 0 : _obj.label;
|
|
263
|
+
// 处理小数位数
|
|
264
|
+
let result = wan;
|
|
265
|
+
const decimalStr = wan.toString().split('.')[1];
|
|
266
|
+
if (decimalStr && decimalStr.length > max) {
|
|
267
|
+
result = Number(wan.toFixed(max));
|
|
268
|
+
}
|
|
269
|
+
// 添加千分位
|
|
270
|
+
return (result.toLocaleString('en-US', {
|
|
271
|
+
minimumFractionDigits: min,
|
|
272
|
+
maximumFractionDigits: max,
|
|
273
|
+
}) +
|
|
274
|
+
' ' +
|
|
275
|
+
unit);
|
|
276
|
+
}
|
|
277
|
+
const sOptions = [
|
|
278
|
+
{
|
|
279
|
+
// label: '元',
|
|
280
|
+
key: 1,
|
|
281
|
+
value: 0,
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
// label: '千元',
|
|
285
|
+
key: 1000,
|
|
286
|
+
value: 1,
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
// label: '万元',
|
|
290
|
+
key: 10000,
|
|
291
|
+
value: 2,
|
|
292
|
+
},
|
|
293
|
+
{
|
|
294
|
+
// label: '百万元',
|
|
295
|
+
key: 1000000,
|
|
296
|
+
value: 3,
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
// label: '亿元',
|
|
300
|
+
key: 100000000,
|
|
301
|
+
value: 4,
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
// label: '十亿元',
|
|
305
|
+
key: 1000000000,
|
|
306
|
+
value: 5,
|
|
307
|
+
},
|
|
308
|
+
];
|
|
238
309
|
|
|
239
310
|
const storage = inBrowser ? localStorage : {};
|
|
240
311
|
const prefix = '';
|
|
@@ -316,7 +387,8 @@ function digitCnUppercase(n) {
|
|
|
316
387
|
.replace(/^整$/, '零元整'));
|
|
317
388
|
}
|
|
318
389
|
function formatDate(d, formatType = SERVER_DATE_FROMAT) {
|
|
319
|
-
return dayjs__default(
|
|
390
|
+
return dayjs__default(d).format(formatType);
|
|
391
|
+
// return dayjs(new Date(d)).format(formatType)
|
|
320
392
|
}
|
|
321
393
|
const formatDateNil = withDefaultNil(formatDate);
|
|
322
394
|
const unit = {
|
|
@@ -599,6 +671,7 @@ exports.formatDate = formatDate;
|
|
|
599
671
|
exports.formatDateNil = formatDateNil;
|
|
600
672
|
exports.formatDuration = formatDuration;
|
|
601
673
|
exports.formatDurationNil = formatDurationNil;
|
|
674
|
+
exports.formatNumWithUnit = formatNumWithUnit;
|
|
602
675
|
exports.formatRatio = formatRatio;
|
|
603
676
|
exports.formatRatioNil = formatRatioNil;
|
|
604
677
|
exports.getBase64 = getBase64;
|
|
@@ -629,8 +702,10 @@ exports.os = os;
|
|
|
629
702
|
exports.playAudio = playAudio;
|
|
630
703
|
exports.random = random;
|
|
631
704
|
exports.randomString = randomString;
|
|
705
|
+
exports.randomWithCrypto = randomWithCrypto;
|
|
632
706
|
exports.removeAll = removeAll;
|
|
633
707
|
exports.removeItem = removeItem;
|
|
708
|
+
exports.sOptions = sOptions;
|
|
634
709
|
exports.setItem = setItem;
|
|
635
710
|
exports.setProxyObj = setProxyObj;
|
|
636
711
|
exports.sortObj = sortObj;
|
package/lib/lgutils.cjs.prod.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("qs"),t=require("dayjs"),o=require("d3-format");function r(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var s=r(t);const n=(...e)=>{console.group("====================="),console.info(...e),console.groupEnd()},i="undefined"!=typeof window,a=e=>Array.isArray(e),c=e=>e&&"number"==typeof e.size&&"string"==typeof e.type&&"function"==typeof e.slice
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("qs"),t=require("dayjs"),o=require("d3-format");function r(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var s=r(t);const n=(...e)=>{console.group("====================="),console.info(...e),console.groupEnd()},i="undefined"!=typeof window,a=e=>/^[0-9]*(.[0-9]*)?$/.test(e+""),l=e=>Array.isArray(e),c=e=>e&&"number"==typeof e.size&&"string"==typeof e.type&&"function"==typeof e.slice;const d=(e,t="children")=>{if(!l(e))return[];for(const o of e)o[t]&&o[t].length&&(o.children=[...o[t]],null==o||delete o.childList,d(o.children,t));return e};const p=[{key:1,value:0},{key:1e3,value:1},{key:1e4,value:2},{key:1e6,value:3},{key:1e8,value:4},{key:1e9,value:5}],u=i?localStorage:{};let m={};const h=o.format(",.2f"),f=o.format(","),x=o.format(".2%"),g=v(h),y=v(f),w=v(x);function v(e){return t=>S(t)?k:e(t)}const b=e=>h(e/1e4),M=v(b),S=e=>null==e||""===e,k="——";function D(e,t="YYYY-MM-DD"){return s(e).format(t)}const N=v(D),T={d:"天",m:"个月",y:"年"};function A(e){return e?`${parseInt(e,10)}${T[e.slice(-1).toLowerCase()]}`:e}const I=v(A);exports.parse=e.parse,exports.stringify=e.stringify,exports.DATE_FORMAT="YYYY/MM/DD",exports.FORMAT_DATE="YYYY-MM-DD",exports.FORMAT_TIME="YYYY-MM-DD HH:mm:ss",exports.NIL_STRING=k,exports.WS=class{constructor(e,t){this.socket=null,this.timer=null,this.ioUrl="",this.heartTime=1e4;this.ioUrl=e,this.socket=t?new WebSocket(e,t):new WebSocket(e)}init(e){const t=this;t.heartInfo=(null==e?void 0:e.heartInfo)?JSON.stringify(null==e?void 0:e.heartInfo):"ws HeartBeat!",t.socket.addEventListener("open",(()=>{n("ws connect!"),t.start(t.heartInfo),e.open&&e.open()}),!1),t.socket.addEventListener("disconnect",(()=>{console.info("[ws info]: disconnect and reconnect... "),e.disconnect&&e.disconnect(),t.socket=new WebSocket(t.ioUrl)}),!1),t.socket.addEventListener("error",(t=>{console.info("---------------"),console.info("[ws error]: "),console.info(t),console.info("---------------"),e.error&&e.error()}),!1),t.socket.addEventListener("close",e.close,!1),t.socket.addEventListener("message",(o=>{t.reset(),e.msg(o)}),!1)}close(){this.timer&&clearTimeout(this.timer),console.info("[ws close]: close!"),this.socket.close()}start(e){const t=this;t.timer=setTimeout((()=>{t.socket.send(e)}),t.heartTime)}send(e){this.socket.send(e)}reset(){const e=this;clearTimeout(e.timer),e.start(e.heartInfo)}getSocketState(){return this.socket.readyState}setHeartTime(e){this.heartTime=e,this.reset()}},exports.accAdd=function(e,t){let o=e.toString(),r=t.toString(),s=0,n=0;o.indexOf(".")>-1&&(s=o.split(".")[1].length),r.indexOf(".")>-1&&(n=r.split(".")[1].length);const i=Math.pow(10,Math.max(s,n));return Number(+e*i+ +t*i)/i},exports.accDiv=(e,t)=>{let o,r,s=0,n=0;try{s=e.toString().split(".")[1].length}catch(i){}try{n=t.toString().split(".")[1].length}catch(i){}return o=Number(e.toString().replace(".","")),r=Number(t.toString().replace(".","")),o/r*Math.pow(10,n-s)},exports.accMul=(e,t)=>{let o=0,r=e.toString(),s=t.toString();try{o+=r.split(".")[1].length}catch(n){}try{o+=s.split(".")[1].length}catch(n){}return Number(r.replace(".",""))*Number(s.replace(".",""))/Math.pow(10,o)},exports.accSub=function(e,t){let o,r,s,n;try{o=e.toString().split(".")[1].length}catch(i){o=0}try{r=t.toString().split(".")[1].length}catch(i){r=0}return s=Math.pow(10,Math.max(o,r)),n=o>=r?o:r,((e*s-t*s)/s).toFixed(n)},exports.copyToClipboard=function(e){var t=document.createElement("textarea");t.value=e,document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t)},exports.cutTimeFn=e=>`00‘${0===e?"00":"0"+Math.floor(e/60)}’${0===e?"00":e%60>=10?e%60:"0"+e%60}`,exports.digitCnUppercase=function(e){var t=["角","分"],o=["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"],r=[["元","万","亿"],["","拾","佰","仟"]],s=e<0?"欠":"";e=Math.abs(e);for(var n="",i=0;i<t.length;i++)n+=(o[Math.floor(10*e*Math.pow(10,i))%10]+t[i]).replace(/零./,"");for(n=n||"整",e=Math.floor(e),i=0;i<r[0].length&&e>0;i++){for(var a="",l=0;l<r[1].length&&e>0;l++)a=o[e%10]+r[1][l]+a,e=Math.floor(e/10);n=a.replace(/(零.)*零$/,"").replace(/^$/,"零")+r[0][i]+n}return s+n.replace(/(零.)*零元/,"元").replace(/(零.)+/g,"零").replace(/^整$/,"零元整")},exports.disableBrowserZoom=function(){document.addEventListener("keydown",(function(e){!0!==e.ctrlKey&&!0!==e.metaKey||61!==e.which&&107!==e.which&&173!==e.which&&109!==e.which&&187!==e.which&&189!==e.which||e.preventDefault()}),!1),window.addEventListener("mousewheel",(function(e){(!0===e.ctrlKey||e.metaKey)&&e.preventDefault()}),{passive:!1}),window.addEventListener("DOMMouseScroll",(function(e){(!0===e.ctrlKey||e.metaKey)&&e.preventDefault()}),{passive:!1})},exports.download=(e,t="download",o="application/octet-stream",r)=>{const s=window;let n=o,i=e;const a=document,l=a.createElement("a"),c=e=>String(e),d=s.Blob||c(l),p=t;let u=null,m=null;const h=(t,o)=>{if("download"in l)return l.href=r?e:t,l.setAttribute("download",p),l.innerHTML="downloading...",a.body.appendChild(l),setTimeout((()=>{l.click(),a.body.removeChild(l),!0===o&&setTimeout((()=>{s.URL.revokeObjectURL(l.href)}),250)}),66),!0;const n=a.createElement("iframe");a.body.appendChild(n),o||(t="data:"+t.replace(/^data:([\w\/\-\+]+)/,"application/octet-stream")),n.src=t,setTimeout((()=>{a.body.removeChild(n)}),333)};if(String(i).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/))return h(i);try{u=i instanceof d?i:new d([i],{type:n})}catch(f){}if(s.URL)h(s.URL.createObjectURL(u),!0);else{if("string"==typeof u||u.constructor===c(l))try{return h("data:"+n+";base64,"+s.btoa(u))}catch(f){return h("data:"+n+","+encodeURIComponent(u))}m=new FileReader,m.onload=function(){h(this.result)},m.readAsDataURL(u)}return!0},exports.fileReaderToBase64=e=>t=>{const o=new FileReader;o.onload=e=>{t&&t(e.target.result)},o.readAsDataURL(e)},exports.filterNil=e=>{const t={};return Object.entries(e).forEach((([e,o])=>{o&&0!==o&&(t[e]=o)})),t},exports.format10k=b,exports.format10kNil=M,exports.formatBillion=e=>{const t=e/1e8>=1,o=t?"亿":"万";return`${h(t?e/1e8:e/1e4)}${o}`},exports.formatCash=h,exports.formatCash2=e=>(Math.floor(100*e)/100).toFixed(2),exports.formatCashInt=f,exports.formatCashIntNil=y,exports.formatCashNil=g,exports.formatDate=D,exports.formatDateNil=N,exports.formatDuration=A,exports.formatDurationNil=I,exports.formatNumWithUnit=function(e,t=2,o=2,r="",s=!1,n=!1,i=2){if((!e||""==e||"-"==e)&&0!==e)return n?void 0:"-";let l="string"==typeof e?parseFloat(e):e;const c=+l<0;if(c&&(l=0-l),!a(l)||isNaN(+l))return l;const d=null==p?void 0:p[i];let u=l;u=s?c?0-l:l:(c?0-l:l)/d.key,!0===r&&(r=null==d?void 0:d.label);let m=u;const h=u.toString().split(".")[1];return h&&h.length>o&&(m=Number(u.toFixed(o))),m.toLocaleString("en-US",{minimumFractionDigits:t,maximumFractionDigits:o})+" "+r},exports.formatRatio=x,exports.formatRatioNil=w,exports.getBase64=e=>new Promise(((t,o)=>{const r=new FileReader;r.readAsDataURL(e),r.onload=()=>t(r.result),r.onerror=e=>o(e)})),exports.getItem=e=>((m[e]||u.getItem(""+e))&&(m[e]=u.getItem(""+e)?JSON.parse(u.getItem(""+e)||"null"):""),m[e]),exports.getQueryObject=(t=window.location.href)=>{var o;const r=null===(o=t.split("?"))||void 0===o?void 0:o[1];return e.parse(r||"",{decoder:e=>e})},exports.getSearchParams=e=>new URLSearchParams(e),exports.getValOfArr=e=>{const t=e.length,o=e.reduce(((e,t)=>e+t),0),r=e.reduce(((e,t)=>e*t),1);return{min:Math.min.apply(null,e),max:Math.max.apply(null,e),sum:o,average:o/t,mull:r}},exports.getValueWithNil=e=>S(e)?k:e,exports.inBrowser=i,exports.isArray=l,exports.isBlob=c,exports.isCardNo=e=>/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/.test(e),exports.isDate=e=>!!e&&s(e).isValid(),exports.isEmail=e=>/^(.+)@(.+)\.(.+)$/.test(e),exports.isFile=e=>c(e)&&"string"==typeof e.name&&("object"==typeof e.lastModifiedDate||"number"==typeof e.lastModified),exports.isMinigram=()=>!!(null===window||void 0===window?void 0:window.__wxjs_environment),exports.isNil=S,exports.isNull=e=>null===e,exports.isNumber=a,exports.isObject=e=>e===Object(e),exports.isPhone=e=>/^[1](\d{10})$/.test(e),exports.isSMSCode=e=>e.match(/^\d{6}$/),exports.isUndefined=e=>void 0===e,exports.isWX=()=>{const e=navigator.userAgent.toLowerCase();return/MicroMessenger/i.test(e)},exports.log=n,exports.loopData=d,exports.os=()=>{if(!i)return{isTablet:!1,isPhone:!1,isAndroid:!1,isPc:!1,isIos:!1,isPad:!1};const e=navigator.userAgent,t=/(?:Windows Phone)/.test(e),o=/(?:SymbianOS)/.test(e)||t,r=/(?:Android)/.test(e),s=/(?:Firefox)/.test(e),n=/(?:iPad|PlayBook)/.test(e)||r&&!/(?:Mobile)/.test(e)||s&&/(?:Tablet)/.test(e),a=/(?:iPhone)/.test(e)&&!n,l=/(?:iPad)/.test(e)&&!n;return{isTablet:n,isPhone:a,isAndroid:r,isPc:!a&&!r&&!o,isPad:l,isIos:a||l}},exports.playAudio=e=>{const t=document.querySelectorAll("audio");Array.from(t).forEach((e=>{e.pause(),e.currentTime=0})),document.querySelector(`#${e}`).play()},exports.random=(e,t)=>Math.round(Math.random()*(t-e)+e),exports.randomString=(e=32)=>{const t="ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",o=t.length;let r="";for(let s=0;s<e;s++)r+=t.charAt(Math.floor(Math.random()*o));return r},exports.randomWithCrypto=()=>crypto.randomUUID(),exports.removeAll=()=>(m={},u.clear()),exports.removeItem=e=>(delete m[e],u.removeItem(""+e)),exports.sOptions=p,exports.setItem=(e,t)=>(m[e]=t,u.setItem(""+e,JSON.stringify(t))),exports.setProxyObj=(e,t,o)=>new Proxy(e,{get:(e,o)=>(t(),Reflect.get(e,o)),set:(e,t,r)=>(o(),Reflect.set(e,t,r))}),exports.sortObj=(e,t,o="asc")=>e.sort(((e,r)=>"asc"===o?e[t]-r[t]:r[t]-e[t])),exports.sumObj=(e,t)=>e.reduce(((e,o)=>o[t]+e),0),exports.trimString=e=>e.replace(/(^\s*)|(\s*$)/g,"");
|
|
@@ -28,6 +28,7 @@ const isSMSCode = (code) => {
|
|
|
28
28
|
return code.match(/^\d{6}$/);
|
|
29
29
|
};
|
|
30
30
|
const random = (min, max) => Math.round(Math.random() * (max - min) + min);
|
|
31
|
+
const randomWithCrypto = () => crypto.randomUUID();
|
|
31
32
|
const playAudio = (mp3Id) => {
|
|
32
33
|
const audioArr = document.querySelectorAll('audio');
|
|
33
34
|
Array.from(audioArr).forEach((mp3) => {
|
|
@@ -154,23 +155,20 @@ const accDiv = (arg1, arg2) => {
|
|
|
154
155
|
return (r1 / r2) * Math.pow(10, t2 - t1);
|
|
155
156
|
};
|
|
156
157
|
// 加
|
|
157
|
-
|
|
158
|
-
let
|
|
159
|
-
|
|
160
|
-
|
|
158
|
+
function accAdd(a, b) {
|
|
159
|
+
let aStr = a.toString();
|
|
160
|
+
let bStr = b.toString();
|
|
161
|
+
let aDecimal = 0, bDecimal = 0;
|
|
162
|
+
if (aStr.indexOf('.') > -1) {
|
|
163
|
+
aDecimal = aStr.split('.')[1].length;
|
|
161
164
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
try {
|
|
166
|
-
r2 = arg2.toString().split('.')[1].length;
|
|
165
|
+
if (bStr.indexOf('.') > -1) {
|
|
166
|
+
bDecimal = bStr.split('.')[1].length;
|
|
167
167
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return (arg1 * m + arg2 * m) / m;
|
|
173
|
-
};
|
|
168
|
+
const base = Math.pow(10, Math.max(aDecimal, bDecimal));
|
|
169
|
+
// 使用 Number 防止 string 拼接
|
|
170
|
+
return Number(+a * base + +b * base) / base;
|
|
171
|
+
}
|
|
174
172
|
// 减
|
|
175
173
|
const accSub = function (arg1, arg2) {
|
|
176
174
|
let r1, r2, m, n;
|
|
@@ -227,7 +225,80 @@ const loopData = (arr, childkey = 'children') => {
|
|
|
227
225
|
}
|
|
228
226
|
}
|
|
229
227
|
return arr;
|
|
230
|
-
};
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* 金额格式化为万元,带千分位,最多保留6位小数
|
|
231
|
+
* @param amount 金额(元)
|
|
232
|
+
* @returns 格式化后的金额(万元)
|
|
233
|
+
*/
|
|
234
|
+
function formatNumWithUnit(num, min = 2, max = 2, unit = '', useOrg = false, showNull = false, type = 2) {
|
|
235
|
+
if ((!num || num == '' || num == '-') && num !== 0)
|
|
236
|
+
return showNull ? undefined : '-';
|
|
237
|
+
// 转换为数字
|
|
238
|
+
let _num = typeof num === 'string' ? parseFloat(num) : num;
|
|
239
|
+
const isLt0 = +_num < 0;
|
|
240
|
+
if (isLt0)
|
|
241
|
+
_num = 0 - _num;
|
|
242
|
+
if (!isNumber(_num) || isNaN(+_num))
|
|
243
|
+
return _num;
|
|
244
|
+
// const isBillion = num / (10000 * 10000) >= 1
|
|
245
|
+
const _obj = sOptions === null || sOptions === void 0 ? void 0 : sOptions[type];
|
|
246
|
+
// 转换为万元
|
|
247
|
+
let wan = _num;
|
|
248
|
+
if (!useOrg) {
|
|
249
|
+
wan = (isLt0 ? 0 - _num : _num) / _obj.key; //_num / 10000
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
wan = isLt0 ? 0 - _num : _num;
|
|
253
|
+
}
|
|
254
|
+
if (unit === true)
|
|
255
|
+
unit = _obj === null || _obj === void 0 ? void 0 : _obj.label;
|
|
256
|
+
// 处理小数位数
|
|
257
|
+
let result = wan;
|
|
258
|
+
const decimalStr = wan.toString().split('.')[1];
|
|
259
|
+
if (decimalStr && decimalStr.length > max) {
|
|
260
|
+
result = Number(wan.toFixed(max));
|
|
261
|
+
}
|
|
262
|
+
// 添加千分位
|
|
263
|
+
return (result.toLocaleString('en-US', {
|
|
264
|
+
minimumFractionDigits: min,
|
|
265
|
+
maximumFractionDigits: max,
|
|
266
|
+
}) +
|
|
267
|
+
' ' +
|
|
268
|
+
unit);
|
|
269
|
+
}
|
|
270
|
+
const sOptions = [
|
|
271
|
+
{
|
|
272
|
+
// label: '元',
|
|
273
|
+
key: 1,
|
|
274
|
+
value: 0,
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
// label: '千元',
|
|
278
|
+
key: 1000,
|
|
279
|
+
value: 1,
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
// label: '万元',
|
|
283
|
+
key: 10000,
|
|
284
|
+
value: 2,
|
|
285
|
+
},
|
|
286
|
+
{
|
|
287
|
+
// label: '百万元',
|
|
288
|
+
key: 1000000,
|
|
289
|
+
value: 3,
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
// label: '亿元',
|
|
293
|
+
key: 100000000,
|
|
294
|
+
value: 4,
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
// label: '十亿元',
|
|
298
|
+
key: 1000000000,
|
|
299
|
+
value: 5,
|
|
300
|
+
},
|
|
301
|
+
];
|
|
231
302
|
|
|
232
303
|
const storage = inBrowser ? localStorage : {};
|
|
233
304
|
const prefix = '';
|
|
@@ -309,7 +380,8 @@ function digitCnUppercase(n) {
|
|
|
309
380
|
.replace(/^整$/, '零元整'));
|
|
310
381
|
}
|
|
311
382
|
function formatDate(d, formatType = SERVER_DATE_FROMAT) {
|
|
312
|
-
return dayjs(
|
|
383
|
+
return dayjs(d).format(formatType);
|
|
384
|
+
// return dayjs(new Date(d)).format(formatType)
|
|
313
385
|
}
|
|
314
386
|
const formatDateNil = withDefaultNil(formatDate);
|
|
315
387
|
const unit = {
|
|
@@ -562,4 +634,4 @@ function copyToClipboard(txt) {
|
|
|
562
634
|
document.body.removeChild(textarea);
|
|
563
635
|
}
|
|
564
636
|
|
|
565
|
-
export { DATE_FORMAT, FORMAT_DATE, FORMAT_TIME, NIL_STRING, WS, accAdd, accDiv, accMul, accSub, copyToClipboard, cutTimeFn, digitCnUppercase, disableBrowserZoom, download, fileReaderToBase64, filterNil, format10k, format10kNil, formatBillion, formatCash, formatCash2, formatCashInt, formatCashIntNil, formatCashNil, formatDate, formatDateNil, formatDuration, formatDurationNil, formatRatio, formatRatioNil, getBase64, getItem, getQueryObject, getSearchParams, getValOfArr, getValueWithNil, inBrowser, isArray, isBlob, isCardNo, isDate, isEmail, isFile, isMinigram, isNil, isNull, isNumber, isObject, isPhone, isSMSCode, isUndefined, isWX, log, loopData, os, playAudio, random, randomString, removeAll, removeItem, setItem, setProxyObj, sortObj, sumObj, trimString };
|
|
637
|
+
export { DATE_FORMAT, FORMAT_DATE, FORMAT_TIME, NIL_STRING, WS, accAdd, accDiv, accMul, accSub, copyToClipboard, cutTimeFn, digitCnUppercase, disableBrowserZoom, download, fileReaderToBase64, filterNil, format10k, format10kNil, formatBillion, formatCash, formatCash2, formatCashInt, formatCashIntNil, formatCashNil, formatDate, formatDateNil, formatDuration, formatDurationNil, formatNumWithUnit, formatRatio, formatRatioNil, getBase64, getItem, getQueryObject, getSearchParams, getValOfArr, getValueWithNil, inBrowser, isArray, isBlob, isCardNo, isDate, isEmail, isFile, isMinigram, isNil, isNull, isNumber, isObject, isPhone, isSMSCode, isUndefined, isWX, log, loopData, os, playAudio, random, randomString, randomWithCrypto, removeAll, removeItem, sOptions, setItem, setProxyObj, sortObj, sumObj, trimString };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lgutils",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.51",
|
|
4
4
|
"description": "lgutils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "lib/lgutils.cjs.prod.js",
|
|
@@ -10,18 +10,11 @@
|
|
|
10
10
|
"lib"
|
|
11
11
|
],
|
|
12
12
|
"types": "index.d.ts",
|
|
13
|
-
"repository": {
|
|
14
|
-
"type": "git",
|
|
15
|
-
"url": "git+https://gitee.com/create_prj/lerna.git"
|
|
16
|
-
},
|
|
17
13
|
"keywords": [
|
|
18
14
|
"index"
|
|
19
15
|
],
|
|
20
16
|
"author": "lego",
|
|
21
17
|
"license": "MIT",
|
|
22
|
-
"bugs": {
|
|
23
|
-
"url": "https://gitee.com/create_prj/lerna/issues"
|
|
24
|
-
},
|
|
25
18
|
"homepage": "",
|
|
26
19
|
"dependencies": {
|
|
27
20
|
"d3-format": "^1.4.1",
|
|
@@ -32,5 +25,5 @@
|
|
|
32
25
|
"@types/d3-format": "^1.3.1",
|
|
33
26
|
"@types/qs": "^6.9.0"
|
|
34
27
|
},
|
|
35
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "396e5ab1ff893e6073a89e36326bea1b64248da6"
|
|
36
29
|
}
|