reduce-precision 1.0.3 → 1.0.5
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/LICENSE +1 -1
- package/README.md +69 -63
- package/package.json +7 -7
- package/ts/lib/bundle.min.js +1 -0
- package/{lib → ts/lib}/format/index.js +3 -2
- package/lib/bundle.min.js +0 -1
- /package/{lib → ts/lib}/format/index.d.ts +0 -0
- /package/{lib → ts/lib}/index.d.ts +0 -0
- /package/{lib → ts/lib}/index.js +0 -0
package/LICENSE
CHANGED
|
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
1
3
|
# reduce-precision
|
|
2
4
|
|
|
3
5
|
[](https://snyk.io/test/github/ArzDigitalLabs/reduce-precision?targetFile=package.json)
|
|
@@ -6,7 +8,7 @@
|
|
|
6
8
|
[](https://codeclimate.com/github/ArzDigitalLabs/reduce-precision)
|
|
7
9
|
[](https://npmjs.org/package/reduce-precision)
|
|
8
10
|
|
|
9
|
-
`reduce-precision` is a versatile
|
|
11
|
+
`reduce-precision` is a versatile package for formatting and reducing the precision of numbers, currencies, and percentages. It supports various templates, precision levels, languages, and output formats, making it easy to generate formatted strings for different use cases.
|
|
10
12
|
|
|
11
13
|
## Features
|
|
12
14
|
|
|
@@ -21,6 +23,8 @@
|
|
|
21
23
|
|
|
22
24
|
## Installation
|
|
23
25
|
|
|
26
|
+
### Node.js / TypeScript
|
|
27
|
+
|
|
24
28
|
You can install `reduce-precision` using npm:
|
|
25
29
|
|
|
26
30
|
```bash
|
|
@@ -29,23 +33,17 @@ npm install reduce-precision
|
|
|
29
33
|
|
|
30
34
|
[](https://www.npmjs.com/package/reduce-precision)
|
|
31
35
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
### JavaScript (CommonJS)
|
|
36
|
+
### PHP
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
const { NumberFormatter } = require('reduce-precision');
|
|
38
|
-
|
|
39
|
-
const formatter = new NumberFormatter();
|
|
40
|
-
|
|
41
|
-
formatter.setLanguage('en', { prefixMarker: 'strong', prefix: 'USD ' });
|
|
38
|
+
You can install the PHP version of `reduce-precision` via Composer:
|
|
42
39
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
console.log(formatter.toString(123456789));
|
|
40
|
+
```bash
|
|
41
|
+
composer require arzdigitallabs/reduce-precision
|
|
46
42
|
```
|
|
47
43
|
|
|
48
|
-
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
### Node.js / TypeScript
|
|
49
47
|
|
|
50
48
|
```typescript
|
|
51
49
|
import { NumberFormatter } from 'reduce-precision';
|
|
@@ -59,10 +57,15 @@ console.log(formatter.toJson(123456789));
|
|
|
59
57
|
console.log(formatter.toString(123456789));
|
|
60
58
|
```
|
|
61
59
|
|
|
62
|
-
###
|
|
60
|
+
### PHP
|
|
61
|
+
|
|
62
|
+
```php
|
|
63
|
+
require 'vendor/autoload.php';
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
|
|
65
|
+
use NumberFormatter\NumberFormatter;
|
|
66
|
+
|
|
67
|
+
$formatter = new NumberFormatter();
|
|
68
|
+
echo $formatter->toString(12345.678); // Default format
|
|
66
69
|
```
|
|
67
70
|
|
|
68
71
|
## Options
|
|
@@ -82,23 +85,14 @@ The `format` function accepts an optional `options` object with the following pr
|
|
|
82
85
|
|
|
83
86
|
## Examples
|
|
84
87
|
|
|
88
|
+
### TypeScript/Node.js
|
|
89
|
+
|
|
85
90
|
```typescript
|
|
86
91
|
import { NumberFormatter } from 'reduce-precision';
|
|
87
92
|
|
|
88
93
|
// Create a formatter instance with default options
|
|
89
94
|
const formatter = new NumberFormatter();
|
|
90
95
|
|
|
91
|
-
// Create a formatter instance with custom options
|
|
92
|
-
const formatterWithOptions = new NumberFormatter({
|
|
93
|
-
language: 'fa',
|
|
94
|
-
template: 'irr',
|
|
95
|
-
precision: 'medium',
|
|
96
|
-
prefixMarker: 'strong',
|
|
97
|
-
postfixMarker: 'em',
|
|
98
|
-
prefix: 'مبلغ: ',
|
|
99
|
-
postfix: ' ریال',
|
|
100
|
-
});
|
|
101
|
-
|
|
102
96
|
// Basic usage
|
|
103
97
|
formatter.setLanguage('en');
|
|
104
98
|
|
|
@@ -112,30 +106,46 @@ formatter.setTemplate('number', 'medium').toJson(1234.5678); // Output: { value:
|
|
|
112
106
|
formatter.setTemplate('usd', 'high').toJson(1234.5678); // Output: { value: '$1,234.6', ... }
|
|
113
107
|
|
|
114
108
|
// Formatting as Iranian Rial with Persian numerals
|
|
115
|
-
|
|
116
|
-
// Output: { value: '
|
|
109
|
+
formatter.setLanguage('fa');
|
|
110
|
+
formatter.setTemplate('irr', 'medium').toJson(1234.5678); // Output: { value: '۱٫۲۳ هزار ریال', ... }
|
|
117
111
|
|
|
118
112
|
// Formatting as a percentage with low precision
|
|
119
113
|
formatter.setTemplate('percent', 'low').toJson(0.1234); // Output: { value: '0.12%', ... }
|
|
120
114
|
|
|
121
115
|
// Formatting with HTML output and custom markers
|
|
122
|
-
|
|
123
116
|
formatter
|
|
124
117
|
.setLanguage('en', { prefixMarker: 'strong', prefix: 'USD ' })
|
|
125
118
|
.toHtmlString(1234.5678);
|
|
126
119
|
// Output: <strong>USD </strong>1,234.6
|
|
127
120
|
|
|
128
121
|
// Formatting with string input for small or big numbers
|
|
129
|
-
|
|
130
|
-
formatter
|
|
131
|
-
.setTemplate('usd', 'medium')
|
|
132
|
-
.toJson('0.00000000000000000000005678521');
|
|
122
|
+
formatter.setTemplate('usd', 'medium').toJson('0.00000000000000000000005678521');
|
|
133
123
|
// Output: { value: '$0.0₂₂5678', ... }
|
|
134
124
|
```
|
|
135
125
|
|
|
126
|
+
### PHP
|
|
127
|
+
|
|
128
|
+
```php
|
|
129
|
+
require 'vendor/autoload.php';
|
|
130
|
+
|
|
131
|
+
use NumberFormatter\NumberFormatter;
|
|
132
|
+
|
|
133
|
+
$formatter = new NumberFormatter();
|
|
134
|
+
echo $formatter->toString(12345.678); // Default format
|
|
135
|
+
|
|
136
|
+
$formatter->setLanguage('fa');
|
|
137
|
+
echo $formatter->toString(12345.678); // Output in Persian
|
|
138
|
+
|
|
139
|
+
$formatter->setTemplate('usd', 'high');
|
|
140
|
+
echo $formatter->toString(12345.678); // Output in USD format with high precision
|
|
141
|
+
|
|
142
|
+
echo $formatter->toHtmlString(12345.678); // HTML formatted output
|
|
143
|
+
echo $formatter->toMdString(12345.678); // Markdown formatted output
|
|
144
|
+
```
|
|
145
|
+
|
|
136
146
|
## API
|
|
137
147
|
|
|
138
|
-
### `FormattedObject` Interface
|
|
148
|
+
### `FormattedObject` Interface (TypeScript/Node.js)
|
|
139
149
|
|
|
140
150
|
The `FormattedObject` interface represents the structure of the formatted number object returned by the `format` method.
|
|
141
151
|
|
|
@@ -149,55 +159,49 @@ interface FormattedObject {
|
|
|
149
159
|
}
|
|
150
160
|
```
|
|
151
161
|
|
|
152
|
-
### `NumberFormatter` Class
|
|
162
|
+
### `NumberFormatter` Class (PHP)
|
|
153
163
|
|
|
154
|
-
#### `constructor
|
|
164
|
+
#### `constructor`
|
|
155
165
|
|
|
156
166
|
Creates a new instance of the `NumberFormatter` class with optional configuration options.
|
|
157
167
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
#### `setLanguage(lang: Language, config?: LanguageConfig): NumberFormatter`
|
|
168
|
+
#### `setLanguage`
|
|
161
169
|
|
|
162
170
|
Sets the language and optional language configuration for the formatter.
|
|
163
171
|
|
|
164
|
-
|
|
165
|
-
- `config` (optional): An object containing additional language configuration options.
|
|
166
|
-
- `prefixMarker` (optional): The marker for the prefix in HTML or Markdown output (default: `'i'`).
|
|
167
|
-
- `postfixMarker` (optional): The marker for the postfix in HTML or Markdown output (default: `'i'`).
|
|
168
|
-
- `prefix` (optional): The prefix string to be added before the formatted number.
|
|
169
|
-
- `postfix` (optional): The postfix string to be added after the formatted number.
|
|
172
|
+
#### `setTemplate`
|
|
170
173
|
|
|
171
|
-
|
|
174
|
+
Sets the template and precision for the formatter.
|
|
172
175
|
|
|
173
|
-
#### `
|
|
176
|
+
#### `toString`
|
|
174
177
|
|
|
175
|
-
|
|
178
|
+
Formats the input number as a string.
|
|
176
179
|
|
|
177
|
-
|
|
178
|
-
- `precision`: The precision level for formatting (`'high'`, `'medium'`, `'low'`, or `'auto'`).
|
|
180
|
+
#### `toPlainString`
|
|
179
181
|
|
|
180
|
-
|
|
182
|
+
Formats the input number as a plain text string.
|
|
181
183
|
|
|
182
|
-
#### `
|
|
184
|
+
#### `toHtmlString`
|
|
183
185
|
|
|
184
|
-
Formats the input number
|
|
186
|
+
Formats the input number as an HTML string.
|
|
185
187
|
|
|
186
|
-
|
|
188
|
+
#### `toMdString`
|
|
187
189
|
|
|
188
|
-
|
|
190
|
+
Formats the input number as a Markdown string.
|
|
189
191
|
|
|
190
|
-
|
|
192
|
+
## Testing
|
|
191
193
|
|
|
192
|
-
|
|
194
|
+
### Node.js / TypeScript
|
|
193
195
|
|
|
194
|
-
|
|
196
|
+
You can run tests using Jest or any other preferred testing framework for TypeScript.
|
|
195
197
|
|
|
196
|
-
|
|
198
|
+
### PHP
|
|
197
199
|
|
|
198
|
-
|
|
200
|
+
You can run tests using PHPUnit:
|
|
199
201
|
|
|
200
|
-
|
|
202
|
+
```bash
|
|
203
|
+
./vendor/bin/phpunit tests
|
|
204
|
+
```
|
|
201
205
|
|
|
202
206
|
## Contributing
|
|
203
207
|
|
|
@@ -206,3 +210,5 @@ Contributions are welcome! If you find a bug or have a feature request, please o
|
|
|
206
210
|
## License
|
|
207
211
|
|
|
208
212
|
This project is licensed under the [MIT License](LICENSE).
|
|
213
|
+
|
|
214
|
+
---
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reduce-precision",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "./lib/index.js",
|
|
5
|
+
"main": "./ts/lib/index.js",
|
|
6
6
|
"files": [
|
|
7
|
-
"lib/**/*"
|
|
7
|
+
"ts/lib/**/*"
|
|
8
8
|
],
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "npm run build:tsc && npm run build:webpack",
|
|
11
|
-
"build:tsc": "tsc --project tsconfig.
|
|
11
|
+
"build:tsc": "tsc --project tsconfig.json",
|
|
12
12
|
"build:webpack": "webpack --config webpack.config.js",
|
|
13
|
-
"clean": "rm -rf ./lib/",
|
|
14
|
-
"lint": "eslint ./src/ --fix",
|
|
13
|
+
"clean": "rm -rf ./ts/lib/",
|
|
14
|
+
"lint": "eslint ./ts/src/ --fix",
|
|
15
15
|
"test:watch": "jest --watch",
|
|
16
16
|
"test": "jest --coverage",
|
|
17
17
|
"typecheck": "tsc --noEmit"
|
|
@@ -60,4 +60,4 @@
|
|
|
60
60
|
"main"
|
|
61
61
|
]
|
|
62
62
|
}
|
|
63
|
-
}
|
|
63
|
+
}
|
|
@@ -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={900:(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(null==e||""===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(null==e||""===e.trim())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}}},986: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(900));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}(986)})()));
|
|
@@ -69,8 +69,9 @@ class NumberFormatter {
|
|
|
69
69
|
format(input) {
|
|
70
70
|
let { precision, template } = this.options;
|
|
71
71
|
const { language, outputFormat, prefixMarker, postfixMarker, prefix, postfix, thousandSeparator, decimalSeparator, } = this.options;
|
|
72
|
-
if (
|
|
72
|
+
if (input === undefined || input === null || input === '') {
|
|
73
73
|
return {};
|
|
74
|
+
}
|
|
74
75
|
if (!(template === null || template === void 0 ? void 0 : template.match(/^(number|usd|irt|irr|percent)$/g)))
|
|
75
76
|
template = 'number';
|
|
76
77
|
if (this.isENotation(input.toString())) {
|
|
@@ -274,7 +275,7 @@ class NumberFormatter {
|
|
|
274
275
|
}
|
|
275
276
|
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
277
|
var _a, _b;
|
|
277
|
-
if (
|
|
278
|
+
if (numberString === undefined || numberString === null || numberString.trim() === '') {
|
|
278
279
|
return {};
|
|
279
280
|
}
|
|
280
281
|
numberString = numberString.toString();
|
package/lib/bundle.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
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)})()));
|
|
File without changes
|
|
File without changes
|
/package/{lib → ts/lib}/index.js
RENAMED
|
File without changes
|