@formatjs/ecma376 0.4.2 → 0.5.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/index.js +20 -25
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1,29 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const NUMBER = 1e4;
|
|
2
|
+
const ALL_ZEROS = /^0+$/;
|
|
3
3
|
function partToPattern(part) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
.map(function (_, i, arr) { return (i === arr.length - 1 ? '0' : '#'); })
|
|
15
|
-
.join('');
|
|
16
|
-
}
|
|
17
|
-
return '#';
|
|
18
|
-
}
|
|
4
|
+
switch (part.type) {
|
|
5
|
+
default: return part.value;
|
|
6
|
+
case "currency":
|
|
7
|
+
case "compact": return `"${part.value}"`;
|
|
8
|
+
case "integer":
|
|
9
|
+
if (ALL_ZEROS.test(part.value)) {
|
|
10
|
+
return part.value.split("").map((_, i, arr) => i === arr.length - 1 ? "0" : "#").join("");
|
|
11
|
+
}
|
|
12
|
+
return "#";
|
|
13
|
+
}
|
|
19
14
|
}
|
|
20
15
|
export function generateNumFmtPattern(locales, opts) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
16
|
+
const nf = new Intl.NumberFormat(locales, opts);
|
|
17
|
+
const positivePattern = nf.formatToParts(NUMBER).reduce((pattern, part) => {
|
|
18
|
+
return pattern + partToPattern(part);
|
|
19
|
+
}, "");
|
|
20
|
+
const negativePattern = nf.formatToParts(-NUMBER).reduce((pattern, part) => {
|
|
21
|
+
return pattern + partToPattern(part);
|
|
22
|
+
}, "");
|
|
23
|
+
return positivePattern + ";" + negativePattern;
|
|
29
24
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/ecma376",
|
|
3
3
|
"description": "generate ecma376 numFmt in different locales & currencies",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.5.0",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "Long Ho <holevietlong@gmail.com>",
|
|
7
7
|
"type": "module",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
".": "./index.js"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"tslib": "^2.8.
|
|
13
|
+
"tslib": "^2.8.1"
|
|
14
14
|
},
|
|
15
15
|
"bugs": "https://github.com/formatjs/formatjs/issues",
|
|
16
16
|
"homepage": "https://github.com/formatjs/formatjs#readme",
|