@zlikemario/helper 0.0.16 → 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 CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const i=require("bignumber.js");i.config({EXPONENTIAL_AT:99});function s(e,t=!1){if(!["string","number","bigint"].includes(typeof e))return!1;try{e=i(e).toFixed()}catch{return!1}return t?/^-?\d+$/.test(e):/^-?\d+(\.\d+)?$/.test(e)}function l(e,t=3,n=i.ROUND_HALF_UP){if(!s(e))return String(e);const r=1e3;let o=i(e);if(o.abs().lt(r))return o.toFixed(t,n).replace(/\.0+$/,"");const u=["","K","M","B"];let a=0;for(;++a<u.length&&(o=o.div(r),!o.abs().lt(r)););return o.toFixed(1,n)+u[a]}function c(e){return s(e)?i(e).toFormat({decimalSeparator:".",groupSeparator:",",groupSize:3}):String(e)}function f(e,t=2,n=!1){if(!s(e))return String(e);const r=i(e).times(100).toFixed(t);return parseFloat(r)+(n?"":"%")}function g(e,t=4){return s(e)?i(e).dp(t).toString():String(e)}const d=(e,t=4)=>{if(!s(e))return String(e);const n=i(e).toFixed().match(/(-?)(\d+)\.(0+)(\d+)/);if(!n)return c(i(e).dp(t).toFixed());const[,r,o,u,a]=n;return u.length>3?`${r}${c(o)}.0{${u.length}}${a.slice(0,t-2).replace(/0+$/,"")}`:`${r}${c(o)}.${u}${a.slice(0,t-2).replace(/0+$/,"")}`},b=e=>e.reduce((t,n)=>t.plus(n),i(0)).toString();function p(e,t){const n=typeof t=="function"?r=>t(r):r=>r?.[t]??"0";return e.reduce((r,o)=>r.plus(n(o)),i(0)).toString()}exports.BigNumber=i;exports.default=i;exports.formatPrecision=g;exports.isNumber=s;exports.readabilityNumber=c;exports.readableNumber=d;exports.simplifyNumber=l;exports.sum=b;exports.sumBy=p;exports.toPercentage=f;
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 isNumber(num: any, isInt?: boolean): boolean;
28
+ export declare function isComputableNumber(num: unknown, isInt?: boolean): boolean;
15
29
  /**
16
30
  * 数字简化显示(如 1.2K, 3.4M)
17
31
  * @param num - 数字
18
- * @param decimal - 保留小数位
32
+ * @param decimals - 保留小数位
19
33
  * @param rm - 舍入模式
20
34
  * @returns 简化后的字符串
21
35
  */
22
- export declare function simplifyNumber(num: Numberish, decimal?: number, rm?: BigNumber.RoundingMode): string;
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 N, default as x } from "bignumber.js";
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 (!["string", "number", "bigint"].includes(typeof t)) return !1;
11
+ if (typeof t == "object" && t !== null) return !1;
6
12
  try {
7
- t = o(t).toFixed();
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 l(t, e = 3, i = o.ROUND_HALF_UP) {
19
+ function g(t, e = 3, r = o.ROUND_HALF_UP) {
14
20
  if (!a(t)) return String(t);
15
- const r = 1e3;
21
+ const i = 1e3;
16
22
  let n = o(t);
17
- if (n.abs().lt(r)) return n.toFixed(e, i).replace(/\.0+$/, "");
18
- const s = ["", "K", "M", "B"];
19
- let u = 0;
20
- for (; ++u < s.length && (n = n.div(r), !n.abs().lt(r)); )
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, i) + s[u];
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 g(t, e = 2, i = !1) {
37
+ function d(t, e = 2, r = !1) {
32
38
  if (!a(t)) return String(t);
33
- const r = o(t).times(100).toFixed(e);
34
- return parseFloat(r) + (i ? "" : "%");
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 i = o(t).toFixed().match(/(-?)(\d+)\.(0+)(\d+)/);
42
- if (!i)
44
+ const r = o(t).toFixed().match(/(-?)(\d+)\.(0+)(\d+)/);
45
+ if (!r)
43
46
  return c(o(t).dp(e).toFixed());
44
- const [, r, n, s, u] = i;
45
- return s.length > 3 ? `${r}${c(n)}.0{${s.length}}${u.slice(0, e - 2).replace(/0+$/, "")}` : `${r}${c(n)}.${s}${u.slice(0, e - 2).replace(/0+$/, "")}`;
46
- }, $ = (t) => t.reduce((e, i) => e.plus(i), o(0)).toString();
47
- function b(t, e) {
48
- const i = typeof e == "function" ? (r) => e(r) : (r) => r?.[e] ?? "0";
49
- return t.reduce((r, n) => r.plus(i(n)), o(0)).toString();
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
- N as BigNumber,
53
- x as default,
54
- d as formatPrecision,
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
- l as simplifyNumber,
59
- $ as sum,
60
- b as sumBy,
61
- g as toPercentage
60
+ b as sum,
61
+ $ as sumBy,
62
+ d as toPercentage,
63
+ l as trimFloatEndZero,
64
+ g as unitFormat
62
65
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zlikemario/helper",
3
- "version": "0.0.16",
3
+ "version": "1.0.1",
4
4
  "description": "A utility library with number operations and common helper functions",
5
5
  "keywords": [
6
6
  "utility",
@@ -88,7 +88,6 @@
88
88
  },
89
89
  "packageManager": "yarn@4.9.4+sha512.7b1cb0b62abba6a537b3a2ce00811a843bea02bcf53138581a6ae5b1bf563f734872bd47de49ce32a9ca9dcaff995aa789577ffb16811da7c603dcf69e73750b",
90
90
  "dependencies": {
91
- "bignumber.js": "^9.3.1",
92
- "dayjs": "^1.11.18"
91
+ "bignumber.js": "^9.3.1"
93
92
  }
94
93
  }