@zlikemario/helper 1.0.0 → 1.0.1
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/dist/number.cjs +1 -1
- package/dist/number.d.ts +17 -10
- package/dist/number.js +36 -33
- package/package.json +1 -1
package/dist/number.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const n=require("bignumber.js");n.config({EXPONENTIAL_AT:99});function c(t){const e=String(t);if(!e.includes(".")||/e/i.test(e))return e;const r=e.replace(/(\.\d*?)0+$/,"$1");return r.endsWith(".")?r.slice(0,-1):r}function s(t,e=!1){if(typeof t=="object"&&t!==null)return!1;try{const r=n(t);return e?r.isInteger():r.isFinite()}catch{return!1}}function f(t,e=3,r=n.ROUND_HALF_UP){if(!s(t))return String(t);const i=1e3;let o=n(t);if(o.abs().lt(i))return c(o.toFixed(e,r));const u=["","K","M","B"];let a=0;for(;++a<u.length&&(o=o.div(i),!o.abs().lt(i)););return c(o.toFixed(1,r))+u[a]}function l(t){return s(t)?n(t).toFormat({decimalSeparator:".",groupSeparator:",",groupSize:3}):String(t)}function d(t,e=2,r=!1){if(!s(t))return String(t);const i=n(t).times(100).toFixed(e);return parseFloat(i)+(r?"":"%")}const g=(t,e=4)=>{if(!s(t))return String(t);const r=n(t).toFixed().match(/(-?)(\d+)\.(0+)(\d+)/);if(!r)return l(n(t).dp(e).toFixed());const[,i,o,u,a]=r;return u.length>3?`${i}${l(o)}.0{${u.length}}${a.slice(0,e-2).replace(/0+$/,"")}`:`${i}${l(o)}.${u}${a.slice(0,e-2).replace(/0+$/,"")}`},b=t=>t.reduce((e,r)=>e.plus(r),n(0)).toString();function p(t,e){const r=typeof e=="function"?i=>e(i):i=>i?.[e]??"0";return t.reduce((i,o)=>i.plus(r(o)),n(0)).toString()}exports.BigNumber=n;exports.default=n;exports.isComputableNumber=s;exports.readabilityNumber=l;exports.readableNumber=g;exports.sum=b;exports.sumBy=p;exports.toPercentage=d;exports.trimFloatEndZero=c;exports.unitFormat=f;
|
package/dist/number.d.ts
CHANGED
|
@@ -5,21 +5,35 @@ import BigNumber from "bignumber.js";
|
|
|
5
5
|
import type { Numberish, NumberString, MaybeUndefined } from "./types";
|
|
6
6
|
export { BigNumber };
|
|
7
7
|
export default BigNumber;
|
|
8
|
+
/**
|
|
9
|
+
* 删除结尾不该出现的 0
|
|
10
|
+
* @returns {string}
|
|
11
|
+
* @eg [
|
|
12
|
+
* 1234.0000 => 1234
|
|
13
|
+
* 1234.00001 => 1234.00001
|
|
14
|
+
* 1234.100100 => 1234.1001
|
|
15
|
+
* 1234. => 1234
|
|
16
|
+
* ]
|
|
17
|
+
*/
|
|
18
|
+
export declare function trimFloatEndZero(num: Numberish): string;
|
|
8
19
|
/**
|
|
9
20
|
* 判断是否为有效数字
|
|
10
21
|
* @param num - 待判断的值
|
|
11
22
|
* @param isInt - 是否要求为整数
|
|
12
23
|
* @returns 是否为数字
|
|
24
|
+
*
|
|
25
|
+
* false:Infinity, -Infinity, NaN
|
|
26
|
+
* true: "1e10000"(非常大但有限)
|
|
13
27
|
*/
|
|
14
|
-
export declare function
|
|
28
|
+
export declare function isComputableNumber(num: unknown, isInt?: boolean): boolean;
|
|
15
29
|
/**
|
|
16
30
|
* 数字简化显示(如 1.2K, 3.4M)
|
|
17
31
|
* @param num - 数字
|
|
18
|
-
* @param
|
|
32
|
+
* @param decimals - 保留小数位
|
|
19
33
|
* @param rm - 舍入模式
|
|
20
34
|
* @returns 简化后的字符串
|
|
21
35
|
*/
|
|
22
|
-
export declare function
|
|
36
|
+
export declare function unitFormat(num: Numberish, decimals?: number, rm?: BigNumber.RoundingMode): string;
|
|
23
37
|
/**
|
|
24
38
|
* 数字千分位分隔显示
|
|
25
39
|
* @param num - 数字
|
|
@@ -34,13 +48,6 @@ export declare function readabilityNumber(num: Numberish): string;
|
|
|
34
48
|
* @returns 百分比字符串
|
|
35
49
|
*/
|
|
36
50
|
export declare function toPercentage(num: Numberish, precision?: number, isHiddenUnit?: boolean): string;
|
|
37
|
-
/**
|
|
38
|
-
* 保留指定精度
|
|
39
|
-
* @param num - 数字
|
|
40
|
-
* @param precision - 保留小数位
|
|
41
|
-
* @returns 格式化后的字符串
|
|
42
|
-
*/
|
|
43
|
-
export declare function formatPrecision(num: Numberish, precision?: number): string;
|
|
44
51
|
/**
|
|
45
52
|
* 高级可读性数字格式化(如 1,234.0{4}1)
|
|
46
53
|
* @param number - 数字
|
package/dist/number.js
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
1
1
|
import o from "bignumber.js";
|
|
2
|
-
import { default as
|
|
2
|
+
import { default as h, default as m } from "bignumber.js";
|
|
3
3
|
o.config({ EXPONENTIAL_AT: 99 });
|
|
4
|
+
function l(t) {
|
|
5
|
+
const e = String(t);
|
|
6
|
+
if (!e.includes(".") || /e/i.test(e)) return e;
|
|
7
|
+
const r = e.replace(/(\.\d*?)0+$/, "$1");
|
|
8
|
+
return r.endsWith(".") ? r.slice(0, -1) : r;
|
|
9
|
+
}
|
|
4
10
|
function a(t, e = !1) {
|
|
5
|
-
if (
|
|
11
|
+
if (typeof t == "object" && t !== null) return !1;
|
|
6
12
|
try {
|
|
7
|
-
|
|
13
|
+
const r = o(t);
|
|
14
|
+
return e ? r.isInteger() : r.isFinite();
|
|
8
15
|
} catch {
|
|
9
16
|
return !1;
|
|
10
17
|
}
|
|
11
|
-
return e ? /^-?\d+$/.test(t) : /^-?\d+(\.\d+)?$/.test(t);
|
|
12
18
|
}
|
|
13
|
-
function
|
|
19
|
+
function g(t, e = 3, r = o.ROUND_HALF_UP) {
|
|
14
20
|
if (!a(t)) return String(t);
|
|
15
|
-
const
|
|
21
|
+
const i = 1e3;
|
|
16
22
|
let n = o(t);
|
|
17
|
-
if (n.abs().lt(
|
|
18
|
-
const
|
|
19
|
-
let
|
|
20
|
-
for (; ++
|
|
23
|
+
if (n.abs().lt(i)) return l(n.toFixed(e, r));
|
|
24
|
+
const u = ["", "K", "M", "B"];
|
|
25
|
+
let s = 0;
|
|
26
|
+
for (; ++s < u.length && (n = n.div(i), !n.abs().lt(i)); )
|
|
21
27
|
;
|
|
22
|
-
return n.toFixed(1,
|
|
28
|
+
return l(n.toFixed(1, r)) + u[s];
|
|
23
29
|
}
|
|
24
30
|
function c(t) {
|
|
25
31
|
return a(t) ? o(t).toFormat({
|
|
@@ -28,35 +34,32 @@ function c(t) {
|
|
|
28
34
|
groupSize: 3
|
|
29
35
|
}) : String(t);
|
|
30
36
|
}
|
|
31
|
-
function
|
|
37
|
+
function d(t, e = 2, r = !1) {
|
|
32
38
|
if (!a(t)) return String(t);
|
|
33
|
-
const
|
|
34
|
-
return parseFloat(
|
|
35
|
-
}
|
|
36
|
-
function d(t, e = 4) {
|
|
37
|
-
return a(t) ? o(t).dp(e).toString() : String(t);
|
|
39
|
+
const i = o(t).times(100).toFixed(e);
|
|
40
|
+
return parseFloat(i) + (r ? "" : "%");
|
|
38
41
|
}
|
|
39
42
|
const p = (t, e = 4) => {
|
|
40
43
|
if (!a(t)) return String(t);
|
|
41
|
-
const
|
|
42
|
-
if (!
|
|
44
|
+
const r = o(t).toFixed().match(/(-?)(\d+)\.(0+)(\d+)/);
|
|
45
|
+
if (!r)
|
|
43
46
|
return c(o(t).dp(e).toFixed());
|
|
44
|
-
const [,
|
|
45
|
-
return
|
|
46
|
-
},
|
|
47
|
-
function
|
|
48
|
-
const
|
|
49
|
-
return t.reduce((
|
|
47
|
+
const [, i, n, u, s] = r;
|
|
48
|
+
return u.length > 3 ? `${i}${c(n)}.0{${u.length}}${s.slice(0, e - 2).replace(/0+$/, "")}` : `${i}${c(n)}.${u}${s.slice(0, e - 2).replace(/0+$/, "")}`;
|
|
49
|
+
}, b = (t) => t.reduce((e, r) => e.plus(r), o(0)).toString();
|
|
50
|
+
function $(t, e) {
|
|
51
|
+
const r = typeof e == "function" ? (i) => e(i) : (i) => i?.[e] ?? "0";
|
|
52
|
+
return t.reduce((i, n) => i.plus(r(n)), o(0)).toString();
|
|
50
53
|
}
|
|
51
54
|
export {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
a as isNumber,
|
|
55
|
+
h as BigNumber,
|
|
56
|
+
m as default,
|
|
57
|
+
a as isComputableNumber,
|
|
56
58
|
c as readabilityNumber,
|
|
57
59
|
p as readableNumber,
|
|
58
|
-
|
|
59
|
-
$ as
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
b as sum,
|
|
61
|
+
$ as sumBy,
|
|
62
|
+
d as toPercentage,
|
|
63
|
+
l as trimFloatEndZero,
|
|
64
|
+
g as unitFormat
|
|
62
65
|
};
|