reduce-precision 1.0.2 → 1.0.4
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 +88 -73
- package/package.json +15 -7
- package/ts/lib/bundle.min.js +1 -0
- package/{lib → ts/lib}/format/index.d.ts +15 -14
- package/{lib → ts/lib}/format/index.js +55 -22
- /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,12 +33,20 @@ npm install reduce-precision
|
|
|
29
33
|
|
|
30
34
|
[](https://www.npmjs.com/package/reduce-precision)
|
|
31
35
|
|
|
36
|
+
### PHP
|
|
37
|
+
|
|
38
|
+
You can install the PHP version of `reduce-precision` via Composer:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
composer require arzdigitallabs/reduce-precision
|
|
42
|
+
```
|
|
43
|
+
|
|
32
44
|
## Usage
|
|
33
45
|
|
|
34
|
-
###
|
|
46
|
+
### Node.js / TypeScript
|
|
35
47
|
|
|
36
|
-
```
|
|
37
|
-
|
|
48
|
+
```typescript
|
|
49
|
+
import { NumberFormatter } from 'reduce-precision';
|
|
38
50
|
|
|
39
51
|
const formatter = new NumberFormatter();
|
|
40
52
|
|
|
@@ -45,54 +57,42 @@ console.log(formatter.toJson(123456789));
|
|
|
45
57
|
console.log(formatter.toString(123456789));
|
|
46
58
|
```
|
|
47
59
|
|
|
48
|
-
###
|
|
60
|
+
### PHP
|
|
49
61
|
|
|
50
|
-
```
|
|
51
|
-
|
|
62
|
+
```php
|
|
63
|
+
require 'vendor/autoload.php';
|
|
52
64
|
|
|
53
|
-
|
|
65
|
+
use NumberFormatter\NumberFormatter;
|
|
54
66
|
|
|
55
|
-
formatter
|
|
56
|
-
|
|
57
|
-
console.log(formatter.toHtmlString(123456789));
|
|
58
|
-
console.log(formatter.toJson(123456789));
|
|
59
|
-
console.log(formatter.toString(123456789));
|
|
67
|
+
$formatter = new NumberFormatter();
|
|
68
|
+
echo $formatter->toString(12345.678); // Default format
|
|
60
69
|
```
|
|
61
70
|
|
|
62
71
|
## Options
|
|
63
72
|
|
|
64
73
|
The `format` function accepts an optional `options` object with the following properties:
|
|
65
74
|
|
|
66
|
-
| Option
|
|
67
|
-
|
|
|
68
|
-
| `precision`
|
|
69
|
-
| `template`
|
|
70
|
-
| `language`
|
|
71
|
-
| `outputFormat`
|
|
72
|
-
| `prefixMarker`
|
|
73
|
-
| `postfixMarker` | `string`
|
|
74
|
-
| `prefix`
|
|
75
|
-
| `postfix`
|
|
75
|
+
| Option | Type | Default | Description |
|
|
76
|
+
| --------------- | ---------------------------------------------------------- | ---------- | ----------------------------------------------------- |
|
|
77
|
+
| `precision` | `'auto'` \| `'high'` \| `'medium'` \| `'low'` | `'high'` | Precision level for formatting |
|
|
78
|
+
| `template` | `'number'` \| `'usd'` \| `'irt'` \| `'irr'` \| `'percent'` | `'number'` | Template for formatting |
|
|
79
|
+
| `language` | `'en'` \| `'fa'` | `'en'` | Language for formatting (English or Persian) |
|
|
80
|
+
| `outputFormat` | `'plain'` \| `'html'` \| `'markdown'` | `'plain'` | Output format |
|
|
81
|
+
| `prefixMarker` | `string` | `'i'` | Prefix marker for HTML and Markdown output |
|
|
82
|
+
| `postfixMarker` | `string` | `'i'` | Postfix marker for HTML and Markdown output |
|
|
83
|
+
| `prefix` | `string` | `''` | Prefix string to be added before the formatted number |
|
|
84
|
+
| `postfix` | `string` | `''` | Postfix string to be added after the formatted number |
|
|
76
85
|
|
|
77
86
|
## Examples
|
|
78
87
|
|
|
88
|
+
### TypeScript/Node.js
|
|
89
|
+
|
|
79
90
|
```typescript
|
|
80
91
|
import { NumberFormatter } from 'reduce-precision';
|
|
81
92
|
|
|
82
93
|
// Create a formatter instance with default options
|
|
83
94
|
const formatter = new NumberFormatter();
|
|
84
95
|
|
|
85
|
-
// Create a formatter instance with custom options
|
|
86
|
-
const formatterWithOptions = new NumberFormatter({
|
|
87
|
-
language: 'fa',
|
|
88
|
-
template: 'irr',
|
|
89
|
-
precision: 'medium',
|
|
90
|
-
prefixMarker: 'strong',
|
|
91
|
-
postfixMarker: 'em',
|
|
92
|
-
prefix: 'مبلغ: ',
|
|
93
|
-
postfix: ' ریال'
|
|
94
|
-
});
|
|
95
|
-
|
|
96
96
|
// Basic usage
|
|
97
97
|
formatter.setLanguage('en');
|
|
98
98
|
|
|
@@ -106,88 +106,102 @@ formatter.setTemplate('number', 'medium').toJson(1234.5678); // Output: { value:
|
|
|
106
106
|
formatter.setTemplate('usd', 'high').toJson(1234.5678); // Output: { value: '$1,234.6', ... }
|
|
107
107
|
|
|
108
108
|
// Formatting as Iranian Rial with Persian numerals
|
|
109
|
-
|
|
110
|
-
// Output: { value: '
|
|
109
|
+
formatter.setLanguage('fa');
|
|
110
|
+
formatter.setTemplate('irr', 'medium').toJson(1234.5678); // Output: { value: '۱٫۲۳ هزار ریال', ... }
|
|
111
111
|
|
|
112
112
|
// Formatting as a percentage with low precision
|
|
113
113
|
formatter.setTemplate('percent', 'low').toJson(0.1234); // Output: { value: '0.12%', ... }
|
|
114
114
|
|
|
115
115
|
// Formatting with HTML output and custom markers
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
formatter
|
|
117
|
+
.setLanguage('en', { prefixMarker: 'strong', prefix: 'USD ' })
|
|
118
|
+
.toHtmlString(1234.5678);
|
|
118
119
|
// Output: <strong>USD </strong>1,234.6
|
|
119
120
|
|
|
120
121
|
// Formatting with string input for small or big numbers
|
|
121
|
-
|
|
122
|
-
formatter.setTemplate('usd', 'medium').toJson("0.00000000000000000000005678521");
|
|
122
|
+
formatter.setTemplate('usd', 'medium').toJson('0.00000000000000000000005678521');
|
|
123
123
|
// Output: { value: '$0.0₂₂5678', ... }
|
|
124
124
|
```
|
|
125
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
|
+
|
|
126
146
|
## API
|
|
127
147
|
|
|
128
|
-
### `FormattedObject` Interface
|
|
148
|
+
### `FormattedObject` Interface (TypeScript/Node.js)
|
|
129
149
|
|
|
130
150
|
The `FormattedObject` interface represents the structure of the formatted number object returned by the `format` method.
|
|
131
151
|
|
|
132
152
|
```typescript
|
|
133
153
|
interface FormattedObject {
|
|
134
|
-
value: string;
|
|
135
|
-
prefix: string;
|
|
136
|
-
postfix: string;
|
|
137
|
-
sign: string;
|
|
138
|
-
wholeNumber: string;
|
|
154
|
+
value: string; // The formatted value as a string
|
|
155
|
+
prefix: string; // The prefix string
|
|
156
|
+
postfix: string; // The postfix string
|
|
157
|
+
sign: string; // The sign of the number (either an empty string or '-')
|
|
158
|
+
wholeNumber: string; // The whole number part of the value
|
|
139
159
|
}
|
|
140
160
|
```
|
|
141
161
|
|
|
142
|
-
### `NumberFormatter` Class
|
|
162
|
+
### `NumberFormatter` Class (PHP)
|
|
143
163
|
|
|
144
|
-
#### `constructor
|
|
164
|
+
#### `constructor`
|
|
145
165
|
|
|
146
166
|
Creates a new instance of the `NumberFormatter` class with optional configuration options.
|
|
147
167
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
#### `setLanguage(lang: Language, config?: LanguageConfig): NumberFormatter`
|
|
168
|
+
#### `setLanguage`
|
|
151
169
|
|
|
152
170
|
Sets the language and optional language configuration for the formatter.
|
|
153
171
|
|
|
154
|
-
|
|
155
|
-
- `config` (optional): An object containing additional language configuration options.
|
|
156
|
-
- `prefixMarker` (optional): The marker for the prefix in HTML or Markdown output (default: `'i'`).
|
|
157
|
-
- `postfixMarker` (optional): The marker for the postfix in HTML or Markdown output (default: `'i'`).
|
|
158
|
-
- `prefix` (optional): The prefix string to be added before the formatted number.
|
|
159
|
-
- `postfix` (optional): The postfix string to be added after the formatted number.
|
|
172
|
+
#### `setTemplate`
|
|
160
173
|
|
|
161
|
-
|
|
174
|
+
Sets the template and precision for the formatter.
|
|
162
175
|
|
|
163
|
-
#### `
|
|
176
|
+
#### `toString`
|
|
164
177
|
|
|
165
|
-
|
|
178
|
+
Formats the input number as a string.
|
|
166
179
|
|
|
167
|
-
|
|
168
|
-
- `precision`: The precision level for formatting (`'high'`, `'medium'`, `'low'`, or `'auto'`).
|
|
180
|
+
#### `toPlainString`
|
|
169
181
|
|
|
170
|
-
|
|
182
|
+
Formats the input number as a plain text string.
|
|
171
183
|
|
|
172
|
-
#### `
|
|
184
|
+
#### `toHtmlString`
|
|
173
185
|
|
|
174
|
-
Formats the input number
|
|
186
|
+
Formats the input number as an HTML string.
|
|
175
187
|
|
|
176
|
-
|
|
188
|
+
#### `toMdString`
|
|
177
189
|
|
|
178
|
-
|
|
190
|
+
Formats the input number as a Markdown string.
|
|
179
191
|
|
|
180
|
-
|
|
192
|
+
## Testing
|
|
181
193
|
|
|
182
|
-
|
|
194
|
+
### Node.js / TypeScript
|
|
183
195
|
|
|
184
|
-
|
|
196
|
+
You can run tests using Jest or any other preferred testing framework for TypeScript.
|
|
185
197
|
|
|
186
|
-
|
|
198
|
+
### PHP
|
|
187
199
|
|
|
188
|
-
|
|
200
|
+
You can run tests using PHPUnit:
|
|
189
201
|
|
|
190
|
-
|
|
202
|
+
```bash
|
|
203
|
+
./vendor/bin/phpunit tests
|
|
204
|
+
```
|
|
191
205
|
|
|
192
206
|
## Contributing
|
|
193
207
|
|
|
@@ -197,3 +211,4 @@ Contributions are welcome! If you find a bug or have a feature request, please o
|
|
|
197
211
|
|
|
198
212
|
This project is licensed under the [MIT License](LICENSE).
|
|
199
213
|
|
|
214
|
+
---
|
package/package.json
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reduce-precision",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
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
|
-
"build": "tsc
|
|
11
|
-
"
|
|
12
|
-
"
|
|
10
|
+
"build": "npm run build:tsc && npm run build:webpack",
|
|
11
|
+
"build:tsc": "tsc --project tsconfig.json",
|
|
12
|
+
"build:webpack": "webpack --config webpack.config.js",
|
|
13
|
+
"clean": "rm -rf ./ts/lib/",
|
|
14
|
+
"lint": "eslint ./ts/src/ --fix",
|
|
13
15
|
"test:watch": "jest --watch",
|
|
14
16
|
"test": "jest --coverage",
|
|
15
17
|
"typecheck": "tsc --noEmit"
|
|
@@ -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"
|
|
@@ -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)})()));
|
|
@@ -1,21 +1,12 @@
|
|
|
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
12
|
}
|
|
@@ -24,8 +15,18 @@ interface LanguageConfig {
|
|
|
24
15
|
postfixMarker?: string;
|
|
25
16
|
prefix?: string;
|
|
26
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;
|
|
27
26
|
}
|
|
28
27
|
declare class NumberFormatter {
|
|
28
|
+
private readonly languageBaseConfig;
|
|
29
|
+
private defaultLanguageConfig;
|
|
29
30
|
private options;
|
|
30
31
|
constructor(options?: Options);
|
|
31
32
|
setLanguage(lang: Language, config?: LanguageConfig): NumberFormatter;
|
|
@@ -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,9 +68,10 @@ 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;
|
|
63
|
-
if (
|
|
71
|
+
const { language, outputFormat, prefixMarker, postfixMarker, prefix, postfix, thousandSeparator, decimalSeparator, } = this.options;
|
|
72
|
+
if (input === undefined || input === null || input === '') {
|
|
64
73
|
return {};
|
|
74
|
+
}
|
|
65
75
|
if (!(template === null || template === void 0 ? void 0 : template.match(/^(number|usd|irt|irr|percent)$/g)))
|
|
66
76
|
template = 'number';
|
|
67
77
|
if (this.isENotation(input.toString())) {
|
|
@@ -83,7 +93,7 @@ class NumberFormatter {
|
|
|
83
93
|
let f = 0;
|
|
84
94
|
// Auto precision selection
|
|
85
95
|
if (precision === 'auto') {
|
|
86
|
-
if (template.match(/^(usd|irt|irr)$/g)) {
|
|
96
|
+
if (template.match(/^(usd|irt|irr|number)$/g)) {
|
|
87
97
|
if (number >= 0.0001 && number < 100000000000) {
|
|
88
98
|
precision = 'high';
|
|
89
99
|
}
|
|
@@ -91,9 +101,6 @@ class NumberFormatter {
|
|
|
91
101
|
precision = 'medium';
|
|
92
102
|
}
|
|
93
103
|
}
|
|
94
|
-
else if (template === 'number') {
|
|
95
|
-
precision = 'medium';
|
|
96
|
-
}
|
|
97
104
|
else if (template === 'percent') {
|
|
98
105
|
precision = 'low';
|
|
99
106
|
}
|
|
@@ -255,7 +262,7 @@ class NumberFormatter {
|
|
|
255
262
|
c = false;
|
|
256
263
|
}
|
|
257
264
|
}
|
|
258
|
-
return this.reducePrecision(numberString, p, d, r, c, f, template, language, outputFormat, prefixMarker, postfixMarker, prefix, postfix);
|
|
265
|
+
return this.reducePrecision(numberString, p, d, r, c, f, template, language, outputFormat, prefixMarker, postfixMarker, prefix, postfix, thousandSeparator, decimalSeparator);
|
|
259
266
|
}
|
|
260
267
|
convertENotationToRegularNumber(eNotation) {
|
|
261
268
|
const [coefficientStr, exponentStr] = eNotation.toString().split('e');
|
|
@@ -266,9 +273,9 @@ class NumberFormatter {
|
|
|
266
273
|
const precision = Math.max(coefficientLength - exponent, 1);
|
|
267
274
|
return eNotation.toFixed(precision);
|
|
268
275
|
}
|
|
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 = '') {
|
|
270
|
-
var _a;
|
|
271
|
-
if (
|
|
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 = '.') {
|
|
277
|
+
var _a, _b;
|
|
278
|
+
if (numberString === undefined || numberString === null || numberString.trim() === '') {
|
|
272
279
|
return {};
|
|
273
280
|
}
|
|
274
281
|
numberString = numberString.toString();
|
|
@@ -293,6 +300,25 @@ class NumberFormatter {
|
|
|
293
300
|
Qd: ' هزار همت',
|
|
294
301
|
Qt: ' میلیون همت',
|
|
295
302
|
};
|
|
303
|
+
const fullScaleUnits = template.match(/^(number|percent)$/g)
|
|
304
|
+
? {
|
|
305
|
+
'': '',
|
|
306
|
+
K: ' هزار',
|
|
307
|
+
M: ' میلیون',
|
|
308
|
+
B: ' میلیارد',
|
|
309
|
+
T: ' تریلیون',
|
|
310
|
+
Qd: ' کادریلیون',
|
|
311
|
+
Qt: ' کنتیلیون',
|
|
312
|
+
}
|
|
313
|
+
: {
|
|
314
|
+
'': '',
|
|
315
|
+
K: ' هزار تومان',
|
|
316
|
+
M: ' میلیون تومان',
|
|
317
|
+
B: ' میلیارد تومان',
|
|
318
|
+
T: ' هزار میلیارد تومان',
|
|
319
|
+
Qd: ' کادریلیون تومان',
|
|
320
|
+
Qt: ' کنتیلیون تومان',
|
|
321
|
+
};
|
|
296
322
|
let parts = /^(-)?(\d+)\.?([0]*)(\d*)$/g.exec(numberString);
|
|
297
323
|
if (!parts) {
|
|
298
324
|
return {};
|
|
@@ -441,15 +467,22 @@ class NumberFormatter {
|
|
|
441
467
|
sign: sign,
|
|
442
468
|
wholeNumber: wholeNumberStr,
|
|
443
469
|
};
|
|
470
|
+
// replace custom config
|
|
471
|
+
formattedObject.value = ((_a = formattedObject === null || formattedObject === void 0 ? void 0 : formattedObject.value) !== null && _a !== void 0 ? _a : '')
|
|
472
|
+
.replace(/,/g, thousandSeparator)
|
|
473
|
+
.replace(/\./g, decimalSeparator);
|
|
444
474
|
// Convert output to Persian numerals if language is "fa"
|
|
445
475
|
if (language === 'fa') {
|
|
446
|
-
formattedObject.value = ((
|
|
476
|
+
formattedObject.value = ((_b = formattedObject === null || formattedObject === void 0 ? void 0 : formattedObject.value) !== null && _b !== void 0 ? _b : '')
|
|
447
477
|
.replace(/[0-9]/g, c => String.fromCharCode(c.charCodeAt(0) + 1728))
|
|
448
|
-
.replace(/,/g, '٬')
|
|
449
|
-
.replace(/\./g, '٫')
|
|
450
478
|
.replace(/(K|M|B|T|Qt|Qd)/g, function (c) {
|
|
451
479
|
return String(scaleUnits[c]);
|
|
452
480
|
});
|
|
481
|
+
formattedObject.fullPostfix = unitPostfix
|
|
482
|
+
.replace(/[0-9]/g, c => String.fromCharCode(c.charCodeAt(0) + 1728))
|
|
483
|
+
.replace(/(K|M|B|T|Qt|Qd)/g, function (c) {
|
|
484
|
+
return String(fullScaleUnits[c]);
|
|
485
|
+
});
|
|
453
486
|
formattedObject.postfix = formattedObject.postfix
|
|
454
487
|
.replace(/[0-9]/g, c => String.fromCharCode(c.charCodeAt(0) + 1728))
|
|
455
488
|
.replace(/(K|M|B|T|Qt|Qd)/g, function (c) {
|
|
File without changes
|
/package/{lib → ts/lib}/index.js
RENAMED
|
File without changes
|