@fnmain/number 1.6.1 → 1.8.0
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/index.d.ts +9 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +28 -5
- package/dist/index.js.map +1 -1
- package/dist/test.js +17 -11
- package/dist/test.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +46 -11
- package/src/test.ts +17 -11
package/dist/index.d.ts
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
type Options = {
|
2
2
|
fixed?: number;
|
3
|
+
wanFixed?: number;
|
4
|
+
yiFixed?: number;
|
5
|
+
wanYiFixed?: number;
|
3
6
|
trimEndZeros?: boolean;
|
7
|
+
nanString?: string;
|
4
8
|
space?: string;
|
5
9
|
};
|
6
10
|
export declare function trimTrailingZeros(numberString: string): string;
|
7
|
-
export declare function toWan(value:
|
8
|
-
export declare function toYi(value:
|
9
|
-
export declare function toWanYi(value:
|
10
|
-
export declare function toAuto(value:
|
11
|
-
export declare function toPercent(value:
|
11
|
+
export declare function toWan(value: any, { fixed, trimEndZeros, space, nanString }?: Options): string;
|
12
|
+
export declare function toYi(value: any, { fixed, trimEndZeros, space, nanString }?: Options): string;
|
13
|
+
export declare function toWanYi(value: any, { fixed, trimEndZeros, space, nanString }?: Options): string;
|
14
|
+
export declare function toAuto(value: any, { fixed, wanFixed, yiFixed, wanYiFixed, trimEndZeros, space, nanString, }?: Options): string;
|
15
|
+
export declare function toPercent(value: any, { fixed, trimEndZeros, space, nanString }?: Options): string;
|
12
16
|
export declare function parseNumber(input: any): number;
|
13
17
|
export declare function isNumber(value: any): boolean;
|
14
18
|
export declare function isNotNumber(value: any): boolean;
|
package/dist/index.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,KAAK,OAAO,GAAG;IACb,KAAK,CAAC,EAAE,MAA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GAAG,EACV,EAAE,KAAS,EAAE,YAAoB,EAAE,KAAU,EAAE,SAAiB,EAAE,GAAE,OAAY,GAC/E,MAAM,CAQR;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,GAAG,GAAG,MAAM,CAwD9C;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAE5C;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAE/C;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,MAAM,EAAE;;CAAoB,GAAG,MAAM,CAMnF"}
|
package/dist/index.js
CHANGED
@@ -11,25 +11,41 @@ function trimTrailingZeros(numberString) {
|
|
11
11
|
return numberString;
|
12
12
|
}
|
13
13
|
exports.trimTrailingZeros = trimTrailingZeros;
|
14
|
-
function toWan(value, { fixed = 0, trimEndZeros = false, space = "" } = {}) {
|
14
|
+
function toWan(value, { fixed = 0, trimEndZeros = false, space = "", nanString = "NaN" } = {}) {
|
15
|
+
value = parseNumber(value);
|
16
|
+
if (isNaN(value)) {
|
17
|
+
return nanString;
|
18
|
+
}
|
15
19
|
let ret = (value / 10000).toFixed(fixed);
|
16
20
|
ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
|
17
21
|
return ret + space + "万";
|
18
22
|
}
|
19
23
|
exports.toWan = toWan;
|
20
|
-
function toYi(value, { fixed = 0, trimEndZeros = false, space = "" } = {}) {
|
24
|
+
function toYi(value, { fixed = 0, trimEndZeros = false, space = "", nanString = "NaN" } = {}) {
|
25
|
+
value = parseNumber(value);
|
26
|
+
if (isNaN(value)) {
|
27
|
+
return nanString;
|
28
|
+
}
|
21
29
|
let ret = (value / 100000000).toFixed(fixed);
|
22
30
|
ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
|
23
31
|
return ret + space + "亿";
|
24
32
|
}
|
25
33
|
exports.toYi = toYi;
|
26
|
-
function toWanYi(value, { fixed = 0, trimEndZeros = false, space = "" } = {}) {
|
34
|
+
function toWanYi(value, { fixed = 0, trimEndZeros = false, space = "", nanString = "NaN" } = {}) {
|
35
|
+
value = parseNumber(value);
|
36
|
+
if (isNaN(value)) {
|
37
|
+
return nanString;
|
38
|
+
}
|
27
39
|
let ret = (value / 1000000000000).toFixed(fixed);
|
28
40
|
ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
|
29
41
|
return ret + space + "万亿";
|
30
42
|
}
|
31
43
|
exports.toWanYi = toWanYi;
|
32
|
-
function toAuto(value, { fixed = 0, trimEndZeros = false, space = "" } = {}) {
|
44
|
+
function toAuto(value, { fixed = 0, wanFixed, yiFixed, wanYiFixed, trimEndZeros = false, space = "", nanString = "NaN", } = {}) {
|
45
|
+
value = parseNumber(value);
|
46
|
+
if (isNaN(value)) {
|
47
|
+
return nanString;
|
48
|
+
}
|
33
49
|
const abs = Math.abs(value);
|
34
50
|
if (abs < 10000) {
|
35
51
|
let ret = value.toFixed(fixed);
|
@@ -37,15 +53,22 @@ function toAuto(value, { fixed = 0, trimEndZeros = false, space = "" } = {}) {
|
|
37
53
|
return ret;
|
38
54
|
}
|
39
55
|
if (abs < 100000000) {
|
56
|
+
fixed = (isNumber(wanFixed) && wanFixed) || fixed;
|
40
57
|
return toWan(value, { fixed, trimEndZeros, space });
|
41
58
|
}
|
42
59
|
if (abs < 1000000000000) {
|
60
|
+
fixed = (isNumber(yiFixed) && yiFixed) || fixed;
|
43
61
|
return toYi(value, { fixed, trimEndZeros, space });
|
44
62
|
}
|
63
|
+
fixed = (isNumber(wanYiFixed) && wanYiFixed) || fixed;
|
45
64
|
return toWanYi(value, { fixed, trimEndZeros, space });
|
46
65
|
}
|
47
66
|
exports.toAuto = toAuto;
|
48
|
-
function toPercent(value, { fixed = 0, trimEndZeros = false, space = "" } = {}) {
|
67
|
+
function toPercent(value, { fixed = 0, trimEndZeros = false, space = "", nanString = "NaN" } = {}) {
|
68
|
+
value = parseNumber(value);
|
69
|
+
if (isNaN(value)) {
|
70
|
+
return nanString;
|
71
|
+
}
|
49
72
|
let ret = (value * 100).toFixed(fixed);
|
50
73
|
ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
|
51
74
|
return ret + space + "%";
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAUA,SAAgB,iBAAiB,CAAC,YAAoB;IACpD,+CAA+C;IAC/C,IAAI,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;QAC9B,iFAAiF;QACjF,OAAO,YAAY,CAAC,OAAO,CAAC,wBAAwB,EAAE,IAAI,CAAC,CAAC;KAC7D;IACD,0DAA0D;IAC1D,OAAO,YAAY,CAAC;AACtB,CAAC;AARD,8CAQC;AAED,SAAgB,KAAK,CACnB,KAAU,EACV,EAAE,KAAK,GAAG,CAAC,EAAE,YAAY,GAAG,KAAK,EAAE,KAAK,GAAG,EAAE,EAAE,SAAS,GAAG,KAAK,KAAc,EAAE;IAEhF,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;IAC3B,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;QAChB,OAAO,SAAS,CAAC;KAClB;IACD,IAAI,GAAG,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACzC,GAAG,GAAG,YAAY,CAAC,CAAC,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;IAClD,OAAO,GAAG,GAAG,KAAK,GAAG,GAAG,CAAC;AAC3B,CAAC;AAXD,sBAWC;AAED,SAAgB,IAAI,CAClB,KAAU,EACV,EAAE,KAAK,GAAG,CAAC,EAAE,YAAY,GAAG,KAAK,EAAE,KAAK,GAAG,EAAE,EAAE,SAAS,GAAG,KAAK,KAAc,EAAE;IAE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AE,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE;IACzE,MAAM,GAAG,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;IAC/B,IAAI,KAAK,CAAC,GAAG,CAAC,EAAE;QACd,OAAO,MAAM,CAAC;KACf;IACD,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,uBAAuB,EAAE,GAAG,CAAC,CAAC;AAC9D,CAAC;AAND,4CAMC"}
|
package/dist/test.js
CHANGED
@@ -1,17 +1,23 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
const index_1 = require("./index");
|
4
|
-
console.log((0, index_1.toAuto)(
|
5
|
-
console.log((0, index_1.toAuto)(
|
6
|
-
console.log((0, index_1.toAuto)(
|
7
|
-
console.log((0, index_1.toAuto)(
|
8
|
-
console.log((0, index_1.toAuto)(
|
9
|
-
console.log((0, index_1.toAuto)(
|
10
|
-
console.log((0, index_1.toAuto)(
|
11
|
-
console.log((0, index_1.toAuto)(
|
12
|
-
console.log((0, index_1.
|
13
|
-
console.log((0, index_1.
|
14
|
-
console.log((0, index_1.
|
4
|
+
console.log((0, index_1.toAuto)(NaN)); // NaN
|
5
|
+
console.log((0, index_1.toAuto)(NaN, { nanString: "invalid" })); // invalid
|
6
|
+
console.log((0, index_1.toAuto)(1234)); // 1234
|
7
|
+
console.log((0, index_1.toAuto)(12345)); // 1万
|
8
|
+
console.log((0, index_1.toAuto)(123456789)); // 1亿
|
9
|
+
console.log((0, index_1.toAuto)(1234, { fixed: 1 })); // 1234.0
|
10
|
+
console.log((0, index_1.toAuto)(12345, { fixed: 1 })); // 1.2万
|
11
|
+
console.log((0, index_1.toAuto)(12345, { wanFixed: 2 })); // 1.23万
|
12
|
+
console.log((0, index_1.toAuto)(123456789, { fixed: 2, space: " " })); // 1.23 亿
|
13
|
+
console.log((0, index_1.toAuto)(123456789, { yiFixed: 3, space: " " })); // 1.235 亿
|
14
|
+
console.log((0, index_1.toAuto)(12.3 * 10000 * 10000 * 10000, { fixed: 1, space: " " })); // 12.3 万亿
|
15
|
+
console.log((0, index_1.toAuto)(1.00002, { fixed: 1, trimEndZeros: true })); // 1
|
16
|
+
console.log((0, index_1.toPercent)(0.1234, { fixed: 1, space: " " })); // 12.3 %
|
17
|
+
console.log((0, index_1.toPercent)(1.23456, { trimEndZeros: true })); // 123%
|
18
|
+
console.log((0, index_1.toPercent)(1.9, { fixed: 4, trimEndZeros: true })); // 190%
|
19
|
+
console.log((0, index_1.toPercent)("1.2%", { fixed: 2, trimEndZeros: true })); // 1.2%
|
20
|
+
console.log((0, index_1.toPercent)("1.2%", { fixed: 2 })); // 1.20%
|
15
21
|
// Example usage
|
16
22
|
console.log((0, index_1.parseNumber)("50%")); // 0.5
|
17
23
|
console.log((0, index_1.parseNumber)("1e3")); // 1000
|
package/dist/test.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAAkG;AAElG,OAAO,CAAC,GAAG,CAAC,IAAA,cAAM,EAAC,IAAI,CAAC,CAAC,CAAC;
|
1
|
+
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAAkG;AAElG,OAAO,CAAC,GAAG,CAAC,IAAA,cAAM,EAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM;AAChC,OAAO,CAAC,GAAG,CAAC,IAAA,cAAM,EAAC,GAAG,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC,UAAU;AAC9D,OAAO,CAAC,GAAG,CAAC,IAAA,cAAM,EAAC,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO;AAClC,OAAO,CAAC,GAAG,CAAC,IAAA,cAAM,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK;AACjC,OAAO,CAAC,GAAG,CAAC,IAAA,c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: string;
|
4
8
|
space?: string;
|
5
9
|
};
|
6
10
|
|
@@ -15,36 +19,60 @@ export function trimTrailingZeros(numberString: string): string {
|
|
15
19
|
}
|
16
20
|
|
17
21
|
export function toWan(
|
18
|
-
value:
|
19
|
-
{ fixed = 0, trimEndZeros = false, space = "" }: Options = {}
|
22
|
+
value: any,
|
23
|
+
{ fixed = 0, trimEndZeros = false, space = "", nanString = "NaN" }: Options = {}
|
20
24
|
): string {
|
25
|
+
value = parseNumber(value);
|
26
|
+
if (isNaN(value)) {
|
27
|
+
return nanString;
|
28
|
+
}
|
21
29
|
let ret = (value / 10000).toFixed(fixed);
|
22
30
|
ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
|
23
31
|
return ret + space + "万";
|
24
32
|
}
|
25
33
|
|
26
34
|
export function toYi(
|
27
|
-
value:
|
28
|
-
{ fixed = 0, trimEndZeros = false, space = "" }: Options = {}
|
35
|
+
value: any,
|
36
|
+
{ fixed = 0, trimEndZeros = false, space = "", nanString = "NaN" }: Options = {}
|
29
37
|
): string {
|
38
|
+
value = parseNumber(value);
|
39
|
+
if (isNaN(value)) {
|
40
|
+
return nanString;
|
41
|
+
}
|
30
42
|
let ret = (value / 100000000).toFixed(fixed);
|
31
43
|
ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
|
32
44
|
return ret + space + "亿";
|
33
45
|
}
|
34
46
|
|
35
47
|
export function toWanYi(
|
36
|
-
value:
|
37
|
-
{ fixed = 0, trimEndZeros = false, space = "" }: Options = {}
|
48
|
+
value: any,
|
49
|
+
{ fixed = 0, trimEndZeros = false, space = "", nanString = "NaN" }: Options = {}
|
38
50
|
): string {
|
51
|
+
value = parseNumber(value);
|
52
|
+
if (isNaN(value)) {
|
53
|
+
return nanString;
|
54
|
+
}
|
39
55
|
let ret = (value / 1000000000000).toFixed(fixed);
|
40
56
|
ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
|
41
57
|
return ret + space + "万亿";
|
42
58
|
}
|
43
59
|
|
44
60
|
export function toAuto(
|
45
|
-
value:
|
46
|
-
{
|
61
|
+
value: any,
|
62
|
+
{
|
63
|
+
fixed = 0,
|
64
|
+
wanFixed,
|
65
|
+
yiFixed,
|
66
|
+
wanYiFixed,
|
67
|
+
trimEndZeros = false,
|
68
|
+
space = "",
|
69
|
+
nanString = "NaN",
|
70
|
+
}: Options = {}
|
47
71
|
): string {
|
72
|
+
value = parseNumber(value);
|
73
|
+
if (isNaN(value)) {
|
74
|
+
return nanString;
|
75
|
+
}
|
48
76
|
const abs = Math.abs(value);
|
49
77
|
if (abs < 10000) {
|
50
78
|
let ret = value.toFixed(fixed);
|
@@ -52,18 +80,25 @@ export function toAuto(
|
|
52
80
|
return ret;
|
53
81
|
}
|
54
82
|
if (abs < 100000000) {
|
83
|
+
fixed = (isNumber(wanFixed) && wanFixed) || fixed;
|
55
84
|
return toWan(value, { fixed, trimEndZeros, space });
|
56
85
|
}
|
57
86
|
if (abs < 1000000000000) {
|
87
|
+
fixed = (isNumber(yiFixed) && yiFixed) || fixed;
|
58
88
|
return toYi(value, { fixed, trimEndZeros, space });
|
59
89
|
}
|
90
|
+
fixed = (isNumber(wanYiFixed) && wanYiFixed) || fixed;
|
60
91
|
return toWanYi(value, { fixed, trimEndZeros, space });
|
61
92
|
}
|
62
93
|
|
63
94
|
export function toPercent(
|
64
|
-
value:
|
65
|
-
{ fixed = 0, trimEndZeros = false, space = "" }: Options = {}
|
66
|
-
) {
|
95
|
+
value: any,
|
96
|
+
{ fixed = 0, trimEndZeros = false, space = "", nanString = "NaN" }: Options = {}
|
97
|
+
): string {
|
98
|
+
value = parseNumber(value);
|
99
|
+
if (isNaN(value)) {
|
100
|
+
return nanString;
|
101
|
+
}
|
67
102
|
let ret = (value * 100).toFixed(fixed);
|
68
103
|
ret = trimEndZeros ? trimTrailingZeros(ret) : ret;
|
69
104
|
return ret + space + "%";
|
package/src/test.ts
CHANGED
@@ -1,18 +1,24 @@
|
|
1
1
|
import { formatWithCommas, isNotNumber, isNumber, parseNumber, toAuto, toPercent } from "./index";
|
2
2
|
|
3
|
-
console.log(toAuto(
|
4
|
-
console.log(toAuto(
|
5
|
-
console.log(toAuto(
|
3
|
+
console.log(toAuto(NaN)); // NaN
|
4
|
+
console.log(toAuto(NaN, { nanString: "invalid" })); // invalid
|
5
|
+
console.log(toAuto(1234)); // 1234
|
6
|
+
console.log(toAuto(12345)); // 1万
|
7
|
+
console.log(toAuto(123456789)); // 1亿
|
6
8
|
|
7
|
-
console.log(toAuto(1234, { fixed: 1 }));
|
8
|
-
console.log(toAuto(12345, { fixed: 1 }));
|
9
|
-
console.log(toAuto(
|
10
|
-
console.log(toAuto(
|
11
|
-
console.log(toAuto(
|
9
|
+
console.log(toAuto(1234, { fixed: 1 })); // 1234.0
|
10
|
+
console.log(toAuto(12345, { fixed: 1 })); // 1.2万
|
11
|
+
console.log(toAuto(12345, { wanFixed: 2 })); // 1.23万
|
12
|
+
console.log(toAuto(123456789, { fixed: 2, space: " " })); // 1.23 亿
|
13
|
+
console.log(toAuto(123456789, { yiFixed: 3, space: " " })); // 1.235 亿
|
14
|
+
console.log(toAuto(12.3 * 10000 * 10000 * 10000, { fixed: 1, space: " " })); // 12.3 万亿
|
15
|
+
console.log(toAuto(1.00002, { fixed: 1, trimEndZeros: true })); // 1
|
12
16
|
|
13
|
-
console.log(toPercent(0.1234, { fixed: 1, space: " " }));
|
14
|
-
console.log(toPercent(1.23456, { trimEndZeros: true }));
|
15
|
-
console.log(toPercent(1.9, { fixed: 4, trimEndZeros: true }));
|
17
|
+
console.log(toPercent(0.1234, { fixed: 1, space: " " })); // 12.3 %
|
18
|
+
console.log(toPercent(1.23456, { trimEndZeros: true })); // 123%
|
19
|
+
console.log(toPercent(1.9, { fixed: 4, trimEndZeros: true })); // 190%
|
20
|
+
console.log(toPercent("1.2%", { fixed: 2, trimEndZeros: true })); // 1.2%
|
21
|
+
console.log(toPercent("1.2%", { fixed: 2 })); // 1.20%
|
16
22
|
|
17
23
|
// Example usage
|
18
24
|
console.log(parseNumber("50%")); // 0.5
|