reduce-precision 1.0.1 → 1.0.3
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/README.md +36 -30
- package/lib/bundle.min.js +1 -0
- package/lib/format/index.d.ts +15 -18
- package/lib/format/index.js +82 -41
- package/package.json +11 -3
package/README.md
CHANGED
|
@@ -41,7 +41,7 @@ const formatter = new NumberFormatter();
|
|
|
41
41
|
formatter.setLanguage('en', { prefixMarker: 'strong', prefix: 'USD ' });
|
|
42
42
|
|
|
43
43
|
console.log(formatter.toHtmlString(123456789));
|
|
44
|
-
console.log(formatter.
|
|
44
|
+
console.log(formatter.toJson(123456789));
|
|
45
45
|
console.log(formatter.toString(123456789));
|
|
46
46
|
```
|
|
47
47
|
|
|
@@ -55,24 +55,30 @@ const formatter = new NumberFormatter();
|
|
|
55
55
|
formatter.setLanguage('en', { prefixMarker: 'strong', prefix: 'USD ' });
|
|
56
56
|
|
|
57
57
|
console.log(formatter.toHtmlString(123456789));
|
|
58
|
-
console.log(formatter.
|
|
58
|
+
console.log(formatter.toJson(123456789));
|
|
59
59
|
console.log(formatter.toString(123456789));
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
+
### Browser
|
|
63
|
+
|
|
64
|
+
```html
|
|
65
|
+
<script src="https://cdn.jsdelivr.net/npm/reduce-precision/lib/bundle.min.js"></script>
|
|
66
|
+
```
|
|
67
|
+
|
|
62
68
|
## Options
|
|
63
69
|
|
|
64
70
|
The `format` function accepts an optional `options` object with the following properties:
|
|
65
71
|
|
|
66
|
-
| Option
|
|
67
|
-
|
|
|
68
|
-
| `precision`
|
|
69
|
-
| `template`
|
|
70
|
-
| `language`
|
|
71
|
-
| `outputFormat`
|
|
72
|
-
| `prefixMarker`
|
|
73
|
-
| `postfixMarker` | `string`
|
|
74
|
-
| `prefix`
|
|
75
|
-
| `postfix`
|
|
72
|
+
| Option | Type | Default | Description |
|
|
73
|
+
| --------------- | ---------------------------------------------------------- | ---------- | ----------------------------------------------------- |
|
|
74
|
+
| `precision` | `'auto'` \| `'high'` \| `'medium'` \| `'low'` | `'high'` | Precision level for formatting |
|
|
75
|
+
| `template` | `'number'` \| `'usd'` \| `'irt'` \| `'irr'` \| `'percent'` | `'number'` | Template for formatting |
|
|
76
|
+
| `language` | `'en'` \| `'fa'` | `'en'` | Language for formatting (English or Persian) |
|
|
77
|
+
| `outputFormat` | `'plain'` \| `'html'` \| `'markdown'` | `'plain'` | Output format |
|
|
78
|
+
| `prefixMarker` | `string` | `'i'` | Prefix marker for HTML and Markdown output |
|
|
79
|
+
| `postfixMarker` | `string` | `'i'` | Postfix marker for HTML and Markdown output |
|
|
80
|
+
| `prefix` | `string` | `''` | Prefix string to be added before the formatted number |
|
|
81
|
+
| `postfix` | `string` | `''` | Postfix string to be added after the formatted number |
|
|
76
82
|
|
|
77
83
|
## Examples
|
|
78
84
|
|
|
@@ -90,36 +96,40 @@ const formatterWithOptions = new NumberFormatter({
|
|
|
90
96
|
prefixMarker: 'strong',
|
|
91
97
|
postfixMarker: 'em',
|
|
92
98
|
prefix: 'مبلغ: ',
|
|
93
|
-
postfix: ' ریال'
|
|
99
|
+
postfix: ' ریال',
|
|
94
100
|
});
|
|
95
101
|
|
|
96
102
|
// Basic usage
|
|
97
103
|
formatter.setLanguage('en');
|
|
98
104
|
|
|
99
105
|
// Basic number formatting
|
|
100
|
-
formatter.
|
|
106
|
+
formatter.toJson(1234.5678); // Output: { value: '1,234.6', ... }
|
|
101
107
|
|
|
102
108
|
// Formatting with medium precision
|
|
103
|
-
formatter.setTemplate('number', 'medium').
|
|
109
|
+
formatter.setTemplate('number', 'medium').toJson(1234.5678); // Output: { value: '1.23K', ... }
|
|
104
110
|
|
|
105
111
|
// Formatting as USD
|
|
106
|
-
formatter.setTemplate('usd', 'high').
|
|
112
|
+
formatter.setTemplate('usd', 'high').toJson(1234.5678); // Output: { value: '$1,234.6', ... }
|
|
107
113
|
|
|
108
114
|
// Formatting as Iranian Rial with Persian numerals
|
|
109
|
-
formatterWithOptions.
|
|
115
|
+
formatterWithOptions.toJson(1234.5678);
|
|
110
116
|
// Output: { value: 'مبلغ: ۱٫۲۳ هزار ت', ... }
|
|
111
117
|
|
|
112
118
|
// Formatting as a percentage with low precision
|
|
113
|
-
formatter.setTemplate('percent', 'low').
|
|
119
|
+
formatter.setTemplate('percent', 'low').toJson(0.1234); // Output: { value: '0.12%', ... }
|
|
114
120
|
|
|
115
121
|
// Formatting with HTML output and custom markers
|
|
116
122
|
|
|
117
|
-
formatter
|
|
123
|
+
formatter
|
|
124
|
+
.setLanguage('en', { prefixMarker: 'strong', prefix: 'USD ' })
|
|
125
|
+
.toHtmlString(1234.5678);
|
|
118
126
|
// Output: <strong>USD </strong>1,234.6
|
|
119
127
|
|
|
120
128
|
// Formatting with string input for small or big numbers
|
|
121
129
|
|
|
122
|
-
formatter
|
|
130
|
+
formatter
|
|
131
|
+
.setTemplate('usd', 'medium')
|
|
132
|
+
.toJson('0.00000000000000000000005678521');
|
|
123
133
|
// Output: { value: '$0.0₂₂5678', ... }
|
|
124
134
|
```
|
|
125
135
|
|
|
@@ -131,15 +141,11 @@ The `FormattedObject` interface represents the structure of the formatted number
|
|
|
131
141
|
|
|
132
142
|
```typescript
|
|
133
143
|
interface FormattedObject {
|
|
134
|
-
value: string;
|
|
135
|
-
prefix: string;
|
|
136
|
-
postfix: string;
|
|
137
|
-
sign: string;
|
|
138
|
-
wholeNumber: string;
|
|
139
|
-
fractionalPart: string; // The complete fractional part of the value
|
|
140
|
-
fractionalNonZeros: string; // The non-zero digits in the fractional part
|
|
141
|
-
fractionalZerosCount: number; // The count of zeros in the fractional part
|
|
142
|
-
unit: string; // The unit postfix
|
|
144
|
+
value: string; // The formatted value as a string
|
|
145
|
+
prefix: string; // The prefix string
|
|
146
|
+
postfix: string; // The postfix string
|
|
147
|
+
sign: string; // The sign of the number (either an empty string or '-')
|
|
148
|
+
wholeNumber: string; // The whole number part of the value
|
|
143
149
|
}
|
|
144
150
|
```
|
|
145
151
|
|
|
@@ -173,7 +179,7 @@ Sets the template and precision for the formatter.
|
|
|
173
179
|
|
|
174
180
|
Returns the `NumberFormatter` instance for method chaining.
|
|
175
181
|
|
|
176
|
-
#### `
|
|
182
|
+
#### `toJson(input: string | number): FormattedObject`
|
|
177
183
|
|
|
178
184
|
Formats the input number and returns the formatted object.
|
|
179
185
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.ReducePrecision=t():e.ReducePrecision=t()}(self,(()=>(()=>{"use strict";var e={898:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=class{constructor(e={}){this.languageBaseConfig={prefixMarker:"i",postfixMarker:"i",prefix:"",postfix:""},this.defaultLanguageConfig={en:Object.assign(Object.assign({},this.languageBaseConfig),{thousandSeparator:",",decimalSeparator:"."}),fa:Object.assign(Object.assign({},this.languageBaseConfig),{thousandSeparator:"٫",decimalSeparator:"٬"})},this.options=Object.assign({language:"en",template:"number",precision:"high",outputFormat:"plain"},this.defaultLanguageConfig.en),this.options=Object.assign(Object.assign({},this.options),e)}setLanguage(e,t={}){return this.options.language=e,this.options.prefixMarker=t.prefixMarker||this.defaultLanguageConfig[e].prefixMarker,this.options.postfixMarker=t.postfixMarker||this.defaultLanguageConfig[e].postfixMarker,this.options.prefix=t.prefix||this.defaultLanguageConfig[e].prefix,this.options.postfix=t.postfix||this.defaultLanguageConfig[e].postfix,this.options.thousandSeparator=t.thousandSeparator||this.defaultLanguageConfig[e].thousandSeparator,this.options.decimalSeparator=t.decimalSeparator||this.defaultLanguageConfig[e].decimalSeparator,this}setTemplate(e,t){return this.options.template=e,this.options.precision=t,this}toJson(e){const t=this.format(e);return delete t.value,t}toString(e){return this.format(e).value||""}toPlainString(e){return this.options.outputFormat="plain",this.format(e).value||""}toHtmlString(e){return this.options.outputFormat="html",this.format(e).value||""}toMdString(e){return this.options.outputFormat="markdown",this.format(e).value||""}isENotation(e){return/^[-+]?[0-9]*\.?[0-9]+([eE][-+][0-9]+)$/.test(e)}format(e){let{precision:t,template:r}=this.options;const{language:i,outputFormat:o,prefixMarker:n,postfixMarker:a,prefix:s,postfix:u,thousandSeparator:l,decimalSeparator:p}=this.options;if(!e)return{};(null==r?void 0:r.match(/^(number|usd|irt|irr|percent)$/g))||(r="number"),this.isENotation(e.toString())&&(e=this.convertENotationToRegularNumber(Number(e)));let g=e.toString().replace(/[\u0660-\u0669\u06F0-\u06F9]/g,(function(e){return String(15&e.charCodeAt(0))})).replace(/[^\d.-]/g,"");g=g.replace(/^0+(?=\d)/g,"").replace(/(?<=\.\d*)0+$|(?<=\.\d)0+\b/g,"");const f=Math.abs(Number(g));let c,d,h,m,x=0;if("auto"===t&&(r.match(/^(usd|irt|irr|number)$/g)?t=f>=1e-4&&f<1e11?"high":"medium":"percent"===r&&(t="low")),"medium"===t)if(f>=0&&f<1e-4)c=33,d=4,h=!1,m=!0;else if(f>=1e-4&&f<.001)c=7,d=4,h=!1,m=!1;else if(f>=.001&&f<.01)c=5,d=3,h=!1,m=!1;else if(f>=.001&&f<.1)c=3,d=2,h=!1,m=!1;else if(f>=.1&&f<1)c=1,d=1,h=!1,m=!1;else if(f>=1&&f<10)c=3,d=3,h=!1,m=!1;else if(f>=10&&f<100)c=2,d=2,h=!1,m=!1;else if(f>=100&&f<1e3)c=1,d=1,h=!1,m=!1;else if(f>=1e3){const e=Math.floor(Math.log10(f))%3;c=2-e,d=2-e,h=!0,m=!0}else c=0,d=0,h=!0,m=!0;else if("low"===t)if(f>=0&&f<.01)c=2,d=0,h=!0,m=!1,x=2;else if(f>=.01&&f<.1)c=2,d=1,h=!0,m=!1;else if(f>=.1&&f<1)c=2,d=2,h=!0,m=!1;else if(f>=1&&f<10)c=2,d=2,h=!0,m=!1,x=2;else if(f>=10&&f<100)c=1,d=1,h=!0,m=!1,x=1;else if(f>=100&&f<1e3)c=0,d=0,h=!0,m=!1;else if(f>=1e3){const e=Math.floor(Math.log10(f))%3;c=1-e,d=1-e,h=!0,m=!0}else c=0,d=0,h=!0,m=!0,x=2;else f>=0&&f<1?(c=33,d=4,h=!1,m=!1):f>=1&&f<10?(c=3,d=3,h=!0,m=!1):f>=10&&f<100||f>=100&&f<1e3?(c=2,d=2,h=!0,m=!1):f>=1e3&&f<1e4?(c=1,d=1,h=!0,m=!1):(c=0,d=0,h=!0,m=!1);return this.reducePrecision(g,c,d,h,m,x,r,i,o,n,a,s,u,l,p)}convertENotationToRegularNumber(e){const[t,r]=e.toString().split("e"),i=t.replace(".","").replace("-","").length,o=parseFloat(r),n=Math.max(i-o,1);return e.toFixed(n)}reducePrecision(e,t=30,r=4,i=!1,o=!1,n=0,a="number",s="en",u="plain",l="span",p="span",g="",f="",c=",",d="."){var h,m;if(!e)return{};e=e.toString();const x=a.match(/^(number|percent)$/g)?{"":"",K:" هزار",M:" میلیون",B:" میلیارد",T:" تریلیون",Qd:" کادریلیون",Qt:" کنتیلیون"}:{"":"",K:" هزار ت",M:" میلیون ت",B:" میلیارد ت",T:" همت",Qd:" هزار همت",Qt:" میلیون همت"},b=a.match(/^(number|percent)$/g)?{"":"",K:" هزار",M:" میلیون",B:" میلیارد",T:" تریلیون",Qd:" کادریلیون",Qt:" کنتیلیون"}:{"":"",K:" هزار تومان",M:" میلیون تومان",B:" میلیارد تومان",T:" هزار میلیارد تومان",Qd:" کادریلیون تومان",Qt:" کنتیلیون تومان"};let S=/^(-)?(\d+)\.?([0]*)(\d*)$/g.exec(e);if(!S)return{};const $=S[1]||"";let M=S[2],v=S[3],C=S[4],Q="",N="";if(v.length>=30?(v="0".padEnd(29,"0"),C="1"):v.length+r>t?(r=t-v.length)<1&&(r=1):M.length>21&&(M="0",v="",C=""),o&&M.length>=4){const e=Object.keys(x);let t=M,r=0;for(;+t>999&&r<e.length-1;)t=(+t/1e3).toFixed(2),r++;if(N=e[r],S=/^(-)?(\d+)\.?([0]*)(\d*)$/g.exec(t.toString()),!S)return{};M=S[2],v=S[3],C=S[4]}C.length>r&&(i?parseInt(C[r])<5?C=C.substring(0,r):(C=(parseInt(C.substring(0,r))+1).toString(),C.length>r&&(v.length>0?v=v.substring(0,v.length-1):(M=(Number(M)+1).toString(),C=C.substring(1)))):C=C.substring(0,r)),o&&""!==v&&""===N&&(v="0"+v.length.toString().replace(/\d/g,(function(e){return["₀","₁","₂","₃","₄","₅","₆","₇","₈","₉"][parseInt(e,10)]})));let j=`${v}${C}`;j=j.substring(0,t),j=j.replace(/^(\d*[1-9])0+$/g,"$1"),"usd"===a?(Q="en"===s?"$":"",N||(N="fa"===s?" دلار":"")):"irr"===a?N||(N="fa"===s?" ر":" R"):"irt"===a?N||(N="fa"===s?" ت":" T"):"percent"===a&&(N+="en"===s?"%":N?" درصد":"٪"),Q=g+Q,N+=f,"html"===u?(Q&&(Q=`<${l}>${Q}</${l}>`),N&&(N=`<${p}>${N}</${p}>`)):"markdown"===u&&(Q&&(Q=`${l}${Q}${l}`),N&&(N=`${p}${N}${p}`));const k=/\B(?=(\d{3})+(?!\d))/g,B=n?".".padEnd(n+1,"0"):"";let F,T="";F=t<=0||r<=0||!C?`${M.replace(k,",")}${B}`:`${M.replace(k,",")}.${j}`,T=`${$}${Q}${F}${N}`;const O={value:T,prefix:Q,postfix:N,sign:$,wholeNumber:F};return O.value=(null!==(h=null==O?void 0:O.value)&&void 0!==h?h:"").replace(/,/g,c).replace(/\./g,d),"fa"===s&&(O.value=(null!==(m=null==O?void 0:O.value)&&void 0!==m?m:"").replace(/[0-9]/g,(e=>String.fromCharCode(e.charCodeAt(0)+1728))).replace(/(K|M|B|T|Qt|Qd)/g,(function(e){return String(x[e])})),O.fullPostfix=N.replace(/[0-9]/g,(e=>String.fromCharCode(e.charCodeAt(0)+1728))).replace(/(K|M|B|T|Qt|Qd)/g,(function(e){return String(b[e])})),O.postfix=O.postfix.replace(/[0-9]/g,(e=>String.fromCharCode(e.charCodeAt(0)+1728))).replace(/(K|M|B|T|Qt|Qd)/g,(function(e){return String(x[e])})),O.wholeNumber=O.wholeNumber.replace(/[0-9]/g,(e=>String.fromCharCode(e.charCodeAt(0)+1728))).replace(/(K|M|B|T|Qt|Qd)/g,(function(e){return String(x[e])}))),O}}},156:function(e,t,r){var i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.NumberFormatter=void 0;const o=i(r(898));t.NumberFormatter=o.default}},t={};return function r(i){var o=t[i];if(void 0!==o)return o.exports;var n=t[i]={exports:{}};return e[i].call(n.exports,n,n.exports,r),n.exports}(156)})()));
|
package/lib/format/index.d.ts
CHANGED
|
@@ -1,35 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
interface Options {
|
|
6
|
-
precision?: Precision;
|
|
7
|
-
template?: Template;
|
|
8
|
-
language?: Language;
|
|
9
|
-
outputFormat?: OutputFormat;
|
|
10
|
-
prefixMarker?: string;
|
|
11
|
-
postfixMarker?: string;
|
|
12
|
-
prefix?: string;
|
|
13
|
-
postfix?: string;
|
|
14
|
-
}
|
|
1
|
+
type Template = 'number' | 'usd' | 'irt' | 'irr' | 'percent';
|
|
2
|
+
type Precision = 'auto' | 'high' | 'medium' | 'low';
|
|
3
|
+
type Language = 'en' | 'fa';
|
|
4
|
+
type OutputFormat = 'plain' | 'html' | 'markdown';
|
|
15
5
|
interface FormattedObject {
|
|
16
6
|
value?: string;
|
|
17
7
|
prefix: string;
|
|
18
8
|
postfix: string;
|
|
9
|
+
fullPostfix?: string;
|
|
19
10
|
sign: string;
|
|
20
11
|
wholeNumber: string;
|
|
21
|
-
fractionalPart: string;
|
|
22
|
-
fractionalNonZeros: string;
|
|
23
|
-
fractionalZerosCount: number;
|
|
24
|
-
unit?: string;
|
|
25
12
|
}
|
|
26
13
|
interface LanguageConfig {
|
|
27
14
|
prefixMarker?: string;
|
|
28
15
|
postfixMarker?: string;
|
|
29
16
|
prefix?: string;
|
|
30
17
|
postfix?: string;
|
|
18
|
+
thousandSeparator?: string;
|
|
19
|
+
decimalSeparator?: string;
|
|
20
|
+
}
|
|
21
|
+
interface Options extends LanguageConfig {
|
|
22
|
+
precision?: Precision;
|
|
23
|
+
template?: Template;
|
|
24
|
+
language?: Language;
|
|
25
|
+
outputFormat?: OutputFormat;
|
|
31
26
|
}
|
|
32
27
|
declare class NumberFormatter {
|
|
28
|
+
private readonly languageBaseConfig;
|
|
29
|
+
private defaultLanguageConfig;
|
|
33
30
|
private options;
|
|
34
31
|
constructor(options?: Options);
|
|
35
32
|
setLanguage(lang: Language, config?: LanguageConfig): NumberFormatter;
|
package/lib/format/index.js
CHANGED
|
@@ -2,26 +2,35 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
class NumberFormatter {
|
|
4
4
|
constructor(options = {}) {
|
|
5
|
-
this.
|
|
6
|
-
language: 'en',
|
|
7
|
-
template: 'number',
|
|
8
|
-
precision: 'high',
|
|
9
|
-
outputFormat: 'plain',
|
|
5
|
+
this.languageBaseConfig = {
|
|
10
6
|
prefixMarker: 'i',
|
|
11
7
|
postfixMarker: 'i',
|
|
12
8
|
prefix: '',
|
|
13
9
|
postfix: '',
|
|
14
10
|
};
|
|
11
|
+
this.defaultLanguageConfig = {
|
|
12
|
+
en: Object.assign(Object.assign({}, this.languageBaseConfig), { thousandSeparator: ',', decimalSeparator: '.' }),
|
|
13
|
+
fa: Object.assign(Object.assign({}, this.languageBaseConfig), { thousandSeparator: '٫', decimalSeparator: '٬' }),
|
|
14
|
+
};
|
|
15
|
+
this.options = Object.assign({ language: 'en', template: 'number', precision: 'high', outputFormat: 'plain' }, this.defaultLanguageConfig['en']);
|
|
15
16
|
this.options = Object.assign(Object.assign({}, this.options), options);
|
|
16
17
|
}
|
|
17
18
|
setLanguage(lang, config = {}) {
|
|
18
19
|
this.options.language = lang;
|
|
19
20
|
this.options.prefixMarker =
|
|
20
|
-
config.prefixMarker || this.
|
|
21
|
+
config.prefixMarker || this.defaultLanguageConfig[lang].prefixMarker;
|
|
21
22
|
this.options.postfixMarker =
|
|
22
|
-
config.postfixMarker || this.
|
|
23
|
-
this.options.prefix =
|
|
24
|
-
|
|
23
|
+
config.postfixMarker || this.defaultLanguageConfig[lang].postfixMarker;
|
|
24
|
+
this.options.prefix =
|
|
25
|
+
config.prefix || this.defaultLanguageConfig[lang].prefix;
|
|
26
|
+
this.options.postfix =
|
|
27
|
+
config.postfix || this.defaultLanguageConfig[lang].postfix;
|
|
28
|
+
this.options.thousandSeparator =
|
|
29
|
+
config.thousandSeparator ||
|
|
30
|
+
this.defaultLanguageConfig[lang].thousandSeparator;
|
|
31
|
+
this.options.decimalSeparator =
|
|
32
|
+
config.decimalSeparator ||
|
|
33
|
+
this.defaultLanguageConfig[lang].decimalSeparator;
|
|
25
34
|
return this;
|
|
26
35
|
}
|
|
27
36
|
setTemplate(template, precision) {
|
|
@@ -59,7 +68,7 @@ class NumberFormatter {
|
|
|
59
68
|
}
|
|
60
69
|
format(input) {
|
|
61
70
|
let { precision, template } = this.options;
|
|
62
|
-
const { language, outputFormat, prefixMarker, postfixMarker, prefix, postfix, } = this.options;
|
|
71
|
+
const { language, outputFormat, prefixMarker, postfixMarker, prefix, postfix, thousandSeparator, decimalSeparator, } = this.options;
|
|
63
72
|
if (!input)
|
|
64
73
|
return {};
|
|
65
74
|
if (!(template === null || template === void 0 ? void 0 : template.match(/^(number|usd|irt|irr|percent)$/g)))
|
|
@@ -83,7 +92,7 @@ class NumberFormatter {
|
|
|
83
92
|
let f = 0;
|
|
84
93
|
// Auto precision selection
|
|
85
94
|
if (precision === 'auto') {
|
|
86
|
-
if (template.match(/^(usd|irt|irr)$/g)) {
|
|
95
|
+
if (template.match(/^(usd|irt|irr|number)$/g)) {
|
|
87
96
|
if (number >= 0.0001 && number < 100000000000) {
|
|
88
97
|
precision = 'high';
|
|
89
98
|
}
|
|
@@ -91,9 +100,6 @@ class NumberFormatter {
|
|
|
91
100
|
precision = 'medium';
|
|
92
101
|
}
|
|
93
102
|
}
|
|
94
|
-
else if (template === 'number') {
|
|
95
|
-
precision = 'medium';
|
|
96
|
-
}
|
|
97
103
|
else if (template === 'percent') {
|
|
98
104
|
precision = 'low';
|
|
99
105
|
}
|
|
@@ -255,7 +261,7 @@ class NumberFormatter {
|
|
|
255
261
|
c = false;
|
|
256
262
|
}
|
|
257
263
|
}
|
|
258
|
-
return this.reducePrecision(numberString, p, d, r, c, f, template, language, outputFormat, prefixMarker, postfixMarker, prefix, postfix);
|
|
264
|
+
return this.reducePrecision(numberString, p, d, r, c, f, template, language, outputFormat, prefixMarker, postfixMarker, prefix, postfix, thousandSeparator, decimalSeparator);
|
|
259
265
|
}
|
|
260
266
|
convertENotationToRegularNumber(eNotation) {
|
|
261
267
|
const [coefficientStr, exponentStr] = eNotation.toString().split('e');
|
|
@@ -266,7 +272,8 @@ class NumberFormatter {
|
|
|
266
272
|
const precision = Math.max(coefficientLength - exponent, 1);
|
|
267
273
|
return eNotation.toFixed(precision);
|
|
268
274
|
}
|
|
269
|
-
reducePrecision(numberString, precision = 30, nonZeroDigits = 4, round = false, compress = false, fixedDecimalZeros = 0, template = 'number', language = 'en', outputFormat = 'plain', prefixMarker = 'span', postfixMarker = 'span', prefix = '', postfix = '') {
|
|
275
|
+
reducePrecision(numberString, precision = 30, nonZeroDigits = 4, round = false, compress = false, fixedDecimalZeros = 0, template = 'number', language = 'en', outputFormat = 'plain', prefixMarker = 'span', postfixMarker = 'span', prefix = '', postfix = '', thousandSeparator = ',', decimalSeparator = '.') {
|
|
276
|
+
var _a, _b;
|
|
270
277
|
if (!numberString) {
|
|
271
278
|
return {};
|
|
272
279
|
}
|
|
@@ -292,12 +299,31 @@ class NumberFormatter {
|
|
|
292
299
|
Qd: ' هزار همت',
|
|
293
300
|
Qt: ' میلیون همت',
|
|
294
301
|
};
|
|
302
|
+
const fullScaleUnits = template.match(/^(number|percent)$/g)
|
|
303
|
+
? {
|
|
304
|
+
'': '',
|
|
305
|
+
K: ' هزار',
|
|
306
|
+
M: ' میلیون',
|
|
307
|
+
B: ' میلیارد',
|
|
308
|
+
T: ' تریلیون',
|
|
309
|
+
Qd: ' کادریلیون',
|
|
310
|
+
Qt: ' کنتیلیون',
|
|
311
|
+
}
|
|
312
|
+
: {
|
|
313
|
+
'': '',
|
|
314
|
+
K: ' هزار تومان',
|
|
315
|
+
M: ' میلیون تومان',
|
|
316
|
+
B: ' میلیارد تومان',
|
|
317
|
+
T: ' هزار میلیارد تومان',
|
|
318
|
+
Qd: ' کادریلیون تومان',
|
|
319
|
+
Qt: ' کنتیلیون تومان',
|
|
320
|
+
};
|
|
295
321
|
let parts = /^(-)?(\d+)\.?([0]*)(\d*)$/g.exec(numberString);
|
|
296
322
|
if (!parts) {
|
|
297
323
|
return {};
|
|
298
324
|
}
|
|
299
325
|
const sign = parts[1] || '';
|
|
300
|
-
let
|
|
326
|
+
let nonFractionalStr = parts[2];
|
|
301
327
|
let fractionalZeroStr = parts[3];
|
|
302
328
|
let fractionalNonZeroStr = parts[4];
|
|
303
329
|
let unitPrefix = '';
|
|
@@ -313,15 +339,15 @@ class NumberFormatter {
|
|
|
313
339
|
if (nonZeroDigits < 1)
|
|
314
340
|
nonZeroDigits = 1;
|
|
315
341
|
}
|
|
316
|
-
else if (
|
|
317
|
-
|
|
342
|
+
else if (nonFractionalStr.length > maxIntegerDigits) {
|
|
343
|
+
nonFractionalStr = '0';
|
|
318
344
|
fractionalZeroStr = '';
|
|
319
345
|
fractionalNonZeroStr = '';
|
|
320
346
|
}
|
|
321
347
|
// compress large numbers
|
|
322
|
-
if (compress &&
|
|
348
|
+
if (compress && nonFractionalStr.length >= 4) {
|
|
323
349
|
const scaleUnitKeys = Object.keys(scaleUnits);
|
|
324
|
-
let scaledWholeNumber =
|
|
350
|
+
let scaledWholeNumber = nonFractionalStr;
|
|
325
351
|
let unitIndex = 0;
|
|
326
352
|
while (+scaledWholeNumber > 999 && unitIndex < scaleUnitKeys.length - 1) {
|
|
327
353
|
scaledWholeNumber = (+scaledWholeNumber / 1000).toFixed(2);
|
|
@@ -333,7 +359,7 @@ class NumberFormatter {
|
|
|
333
359
|
return {};
|
|
334
360
|
}
|
|
335
361
|
// sign = parts[1] || "";
|
|
336
|
-
|
|
362
|
+
nonFractionalStr = parts[2];
|
|
337
363
|
fractionalZeroStr = parts[3];
|
|
338
364
|
fractionalNonZeroStr = parts[4];
|
|
339
365
|
}
|
|
@@ -355,14 +381,13 @@ class NumberFormatter {
|
|
|
355
381
|
fractionalZeroStr = fractionalZeroStr.substring(0, fractionalZeroStr.length - 1);
|
|
356
382
|
}
|
|
357
383
|
else {
|
|
358
|
-
|
|
384
|
+
nonFractionalStr = (Number(nonFractionalStr) + 1).toString();
|
|
359
385
|
fractionalNonZeroStr = fractionalNonZeroStr.substring(1);
|
|
360
386
|
}
|
|
361
387
|
}
|
|
362
388
|
}
|
|
363
389
|
}
|
|
364
390
|
}
|
|
365
|
-
const orginalFractionalZeroStr = fractionalZeroStr;
|
|
366
391
|
// Using dex style
|
|
367
392
|
if (compress && fractionalZeroStr !== '' && unitPostfix === '') {
|
|
368
393
|
fractionalZeroStr =
|
|
@@ -388,6 +413,8 @@ class NumberFormatter {
|
|
|
388
413
|
// Output Formating, Prefix, Postfix
|
|
389
414
|
if (template === 'usd') {
|
|
390
415
|
unitPrefix = language === 'en' ? '$' : '';
|
|
416
|
+
if (!unitPostfix)
|
|
417
|
+
unitPostfix = language === 'fa' ? ' دلار' : '';
|
|
391
418
|
}
|
|
392
419
|
else if (template === 'irr') {
|
|
393
420
|
if (!unitPostfix)
|
|
@@ -424,34 +451,48 @@ class NumberFormatter {
|
|
|
424
451
|
? '.'.padEnd(fixedDecimalZeros + 1, '0')
|
|
425
452
|
: '';
|
|
426
453
|
let out = '';
|
|
454
|
+
let wholeNumberStr;
|
|
427
455
|
if (precision <= 0 || nonZeroDigits <= 0 || !fractionalNonZeroStr) {
|
|
428
|
-
|
|
456
|
+
wholeNumberStr = `${nonFractionalStr.replace(thousandSeparatorRegex, ',')}${fixedDecimalZeroStr}`;
|
|
429
457
|
}
|
|
430
458
|
else {
|
|
431
|
-
|
|
459
|
+
wholeNumberStr = `${nonFractionalStr.replace(thousandSeparatorRegex, ',')}.${fractionalPartStr}`;
|
|
432
460
|
}
|
|
461
|
+
out = `${sign}${unitPrefix}${wholeNumberStr}${unitPostfix}`;
|
|
462
|
+
const formattedObject = {
|
|
463
|
+
value: out,
|
|
464
|
+
prefix: unitPrefix,
|
|
465
|
+
postfix: unitPostfix,
|
|
466
|
+
sign: sign,
|
|
467
|
+
wholeNumber: wholeNumberStr,
|
|
468
|
+
};
|
|
469
|
+
// replace custom config
|
|
470
|
+
formattedObject.value = ((_a = formattedObject === null || formattedObject === void 0 ? void 0 : formattedObject.value) !== null && _a !== void 0 ? _a : '')
|
|
471
|
+
.replace(/,/g, thousandSeparator)
|
|
472
|
+
.replace(/\./g, decimalSeparator);
|
|
433
473
|
// Convert output to Persian numerals if language is "fa"
|
|
434
474
|
if (language === 'fa') {
|
|
435
|
-
|
|
475
|
+
formattedObject.value = ((_b = formattedObject === null || formattedObject === void 0 ? void 0 : formattedObject.value) !== null && _b !== void 0 ? _b : '')
|
|
436
476
|
.replace(/[0-9]/g, c => String.fromCharCode(c.charCodeAt(0) + 1728))
|
|
437
|
-
.replace(/,/g, '٬')
|
|
438
|
-
.replace(/\./g, '٫')
|
|
439
477
|
.replace(/(K|M|B|T|Qt|Qd)/g, function (c) {
|
|
440
478
|
return String(scaleUnits[c]);
|
|
441
479
|
});
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
480
|
+
formattedObject.fullPostfix = unitPostfix
|
|
481
|
+
.replace(/[0-9]/g, c => String.fromCharCode(c.charCodeAt(0) + 1728))
|
|
482
|
+
.replace(/(K|M|B|T|Qt|Qd)/g, function (c) {
|
|
483
|
+
return String(fullScaleUnits[c]);
|
|
484
|
+
});
|
|
485
|
+
formattedObject.postfix = formattedObject.postfix
|
|
486
|
+
.replace(/[0-9]/g, c => String.fromCharCode(c.charCodeAt(0) + 1728))
|
|
487
|
+
.replace(/(K|M|B|T|Qt|Qd)/g, function (c) {
|
|
447
488
|
return String(scaleUnits[c]);
|
|
448
|
-
})
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
}
|
|
489
|
+
});
|
|
490
|
+
formattedObject.wholeNumber = formattedObject.wholeNumber
|
|
491
|
+
.replace(/[0-9]/g, c => String.fromCharCode(c.charCodeAt(0) + 1728))
|
|
492
|
+
.replace(/(K|M|B|T|Qt|Qd)/g, function (c) {
|
|
493
|
+
return String(scaleUnits[c]);
|
|
494
|
+
});
|
|
495
|
+
}
|
|
455
496
|
return formattedObject;
|
|
456
497
|
}
|
|
457
498
|
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reduce-precision",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"lib/**/*"
|
|
8
8
|
],
|
|
9
9
|
"scripts": {
|
|
10
|
-
"build": "tsc
|
|
10
|
+
"build": "npm run build:tsc && npm run build:webpack",
|
|
11
|
+
"build:tsc": "tsc --project tsconfig.build.json",
|
|
12
|
+
"build:webpack": "webpack --config webpack.config.js",
|
|
11
13
|
"clean": "rm -rf ./lib/",
|
|
12
14
|
"lint": "eslint ./src/ --fix",
|
|
13
15
|
"test:watch": "jest --watch",
|
|
@@ -29,10 +31,13 @@
|
|
|
29
31
|
},
|
|
30
32
|
"homepage": "https://github.com/ArzDigitalLabs/reduce-precision#readme",
|
|
31
33
|
"devDependencies": {
|
|
34
|
+
"@babel/core": "^7.24.7",
|
|
35
|
+
"@babel/preset-env": "^7.24.7",
|
|
32
36
|
"@types/jest": "^27.5.2",
|
|
33
37
|
"@types/node": "^12.20.11",
|
|
34
38
|
"@typescript-eslint/eslint-plugin": "^4.22.0",
|
|
35
39
|
"@typescript-eslint/parser": "^4.22.0",
|
|
40
|
+
"babel-loader": "^9.1.3",
|
|
36
41
|
"eslint": "^7.25.0",
|
|
37
42
|
"eslint-config-prettier": "^8.3.0",
|
|
38
43
|
"eslint-plugin-node": "^11.1.0",
|
|
@@ -41,8 +46,11 @@
|
|
|
41
46
|
"lint-staged": "^13.2.1",
|
|
42
47
|
"prettier": "^2.2.1",
|
|
43
48
|
"ts-jest": "^27.0.5",
|
|
49
|
+
"ts-loader": "^9.5.1",
|
|
44
50
|
"ts-node": "^10.2.1",
|
|
45
|
-
"typescript": "^4.
|
|
51
|
+
"typescript": "^4.9.5",
|
|
52
|
+
"webpack": "^5.92.1",
|
|
53
|
+
"webpack-cli": "^5.1.4"
|
|
46
54
|
},
|
|
47
55
|
"lint-staged": {
|
|
48
56
|
"*.ts": "eslint --cache --cache-location .eslintcache --fix"
|