reduce-precision 1.0.1 → 1.0.2

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 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.format(123456789));
44
+ console.log(formatter.toJson(123456789));
45
45
  console.log(formatter.toString(123456789));
46
46
  ```
47
47
 
@@ -55,7 +55,7 @@ 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.format(123456789));
58
+ console.log(formatter.toJson(123456789));
59
59
  console.log(formatter.toString(123456789));
60
60
  ```
61
61
 
@@ -97,20 +97,20 @@ const formatterWithOptions = new NumberFormatter({
97
97
  formatter.setLanguage('en');
98
98
 
99
99
  // Basic number formatting
100
- formatter.format(1234.5678); // Output: { value: '1,234.6', ... }
100
+ formatter.toJson(1234.5678); // Output: { value: '1,234.6', ... }
101
101
 
102
102
  // Formatting with medium precision
103
- formatter.setTemplate('number', 'medium').format(1234.5678); // Output: { value: '1.23K', ... }
103
+ formatter.setTemplate('number', 'medium').toJson(1234.5678); // Output: { value: '1.23K', ... }
104
104
 
105
105
  // Formatting as USD
106
- formatter.setTemplate('usd', 'high').format(1234.5678); // Output: { value: '$1,234.6', ... }
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
- formatterWithOptions.format(1234.5678);
109
+ formatterWithOptions.toJson(1234.5678);
110
110
  // Output: { value: 'مبلغ: ۱٫۲۳ هزار ت', ... }
111
111
 
112
112
  // Formatting as a percentage with low precision
113
- formatter.setTemplate('percent', 'low').format(0.1234); // Output: { value: '0.12%', ... }
113
+ formatter.setTemplate('percent', 'low').toJson(0.1234); // Output: { value: '0.12%', ... }
114
114
 
115
115
  // Formatting with HTML output and custom markers
116
116
 
@@ -119,7 +119,7 @@ formatter.setLanguage('en', { prefixMarker: 'strong', prefix: 'USD ' }).toHtmlSt
119
119
 
120
120
  // Formatting with string input for small or big numbers
121
121
 
122
- formatter.setTemplate('usd', 'medium').format("0.00000000000000000000005678521");
122
+ formatter.setTemplate('usd', 'medium').toJson("0.00000000000000000000005678521");
123
123
  // Output: { value: '$0.0₂₂5678', ... }
124
124
  ```
125
125
 
@@ -136,10 +136,6 @@ interface FormattedObject {
136
136
  postfix: string; // The postfix string
137
137
  sign: string; // The sign of the number (either an empty string or '-')
138
138
  wholeNumber: string; // The whole number part of the value
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
143
139
  }
144
140
  ```
145
141
 
@@ -173,7 +169,7 @@ Sets the template and precision for the formatter.
173
169
 
174
170
  Returns the `NumberFormatter` instance for method chaining.
175
171
 
176
- #### `format(input: string | number): FormattedObject`
172
+ #### `toJson(input: string | number): FormattedObject`
177
173
 
178
174
  Formats the input number and returns the formatted object.
179
175
 
@@ -200,3 +196,4 @@ Contributions are welcome! If you find a bug or have a feature request, please o
200
196
  ## License
201
197
 
202
198
  This project is licensed under the [MIT License](LICENSE).
199
+
@@ -18,10 +18,6 @@ interface FormattedObject {
18
18
  postfix: string;
19
19
  sign: string;
20
20
  wholeNumber: string;
21
- fractionalPart: string;
22
- fractionalNonZeros: string;
23
- fractionalZerosCount: number;
24
- unit?: string;
25
21
  }
26
22
  interface LanguageConfig {
27
23
  prefixMarker?: string;
@@ -267,6 +267,7 @@ class NumberFormatter {
267
267
  return eNotation.toFixed(precision);
268
268
  }
269
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;
270
271
  if (!numberString) {
271
272
  return {};
272
273
  }
@@ -297,7 +298,7 @@ class NumberFormatter {
297
298
  return {};
298
299
  }
299
300
  const sign = parts[1] || '';
300
- let wholeNumberStr = parts[2];
301
+ let nonFractionalStr = parts[2];
301
302
  let fractionalZeroStr = parts[3];
302
303
  let fractionalNonZeroStr = parts[4];
303
304
  let unitPrefix = '';
@@ -313,15 +314,15 @@ class NumberFormatter {
313
314
  if (nonZeroDigits < 1)
314
315
  nonZeroDigits = 1;
315
316
  }
316
- else if (wholeNumberStr.length > maxIntegerDigits) {
317
- wholeNumberStr = '0';
317
+ else if (nonFractionalStr.length > maxIntegerDigits) {
318
+ nonFractionalStr = '0';
318
319
  fractionalZeroStr = '';
319
320
  fractionalNonZeroStr = '';
320
321
  }
321
322
  // compress large numbers
322
- if (compress && wholeNumberStr.length >= 4) {
323
+ if (compress && nonFractionalStr.length >= 4) {
323
324
  const scaleUnitKeys = Object.keys(scaleUnits);
324
- let scaledWholeNumber = wholeNumberStr;
325
+ let scaledWholeNumber = nonFractionalStr;
325
326
  let unitIndex = 0;
326
327
  while (+scaledWholeNumber > 999 && unitIndex < scaleUnitKeys.length - 1) {
327
328
  scaledWholeNumber = (+scaledWholeNumber / 1000).toFixed(2);
@@ -333,7 +334,7 @@ class NumberFormatter {
333
334
  return {};
334
335
  }
335
336
  // sign = parts[1] || "";
336
- wholeNumberStr = parts[2];
337
+ nonFractionalStr = parts[2];
337
338
  fractionalZeroStr = parts[3];
338
339
  fractionalNonZeroStr = parts[4];
339
340
  }
@@ -355,14 +356,13 @@ class NumberFormatter {
355
356
  fractionalZeroStr = fractionalZeroStr.substring(0, fractionalZeroStr.length - 1);
356
357
  }
357
358
  else {
358
- wholeNumberStr = (Number(wholeNumberStr) + 1).toString();
359
+ nonFractionalStr = (Number(nonFractionalStr) + 1).toString();
359
360
  fractionalNonZeroStr = fractionalNonZeroStr.substring(1);
360
361
  }
361
362
  }
362
363
  }
363
364
  }
364
365
  }
365
- const orginalFractionalZeroStr = fractionalZeroStr;
366
366
  // Using dex style
367
367
  if (compress && fractionalZeroStr !== '' && unitPostfix === '') {
368
368
  fractionalZeroStr =
@@ -388,6 +388,8 @@ class NumberFormatter {
388
388
  // Output Formating, Prefix, Postfix
389
389
  if (template === 'usd') {
390
390
  unitPrefix = language === 'en' ? '$' : '';
391
+ if (!unitPostfix)
392
+ unitPostfix = language === 'fa' ? ' دلار' : '';
391
393
  }
392
394
  else if (template === 'irr') {
393
395
  if (!unitPostfix)
@@ -424,34 +426,41 @@ class NumberFormatter {
424
426
  ? '.'.padEnd(fixedDecimalZeros + 1, '0')
425
427
  : '';
426
428
  let out = '';
429
+ let wholeNumberStr;
427
430
  if (precision <= 0 || nonZeroDigits <= 0 || !fractionalNonZeroStr) {
428
- out = `${sign}${unitPrefix}${wholeNumberStr.replace(thousandSeparatorRegex, ',')}${fixedDecimalZeroStr}${unitPostfix}`;
431
+ wholeNumberStr = `${nonFractionalStr.replace(thousandSeparatorRegex, ',')}${fixedDecimalZeroStr}`;
429
432
  }
430
433
  else {
431
- out = `${sign}${unitPrefix}${wholeNumberStr.replace(thousandSeparatorRegex, ',')}.${fractionalPartStr}${unitPostfix}`;
434
+ wholeNumberStr = `${nonFractionalStr.replace(thousandSeparatorRegex, ',')}.${fractionalPartStr}`;
432
435
  }
436
+ out = `${sign}${unitPrefix}${wholeNumberStr}${unitPostfix}`;
437
+ const formattedObject = {
438
+ value: out,
439
+ prefix: unitPrefix,
440
+ postfix: unitPostfix,
441
+ sign: sign,
442
+ wholeNumber: wholeNumberStr,
443
+ };
433
444
  // Convert output to Persian numerals if language is "fa"
434
445
  if (language === 'fa') {
435
- out = out
446
+ formattedObject.value = ((_a = formattedObject === null || formattedObject === void 0 ? void 0 : formattedObject.value) !== null && _a !== void 0 ? _a : '')
436
447
  .replace(/[0-9]/g, c => String.fromCharCode(c.charCodeAt(0) + 1728))
437
448
  .replace(/,/g, '٬')
438
449
  .replace(/\./g, '٫')
439
450
  .replace(/(K|M|B|T|Qt|Qd)/g, function (c) {
440
451
  return String(scaleUnits[c]);
441
452
  });
442
- }
443
- const formattedObject = {
444
- value: out,
445
- prefix: unitPrefix,
446
- postfix: unitPostfix.replace(/(K|M|B|T|Qt|Qd)/g, function (c) {
453
+ formattedObject.postfix = formattedObject.postfix
454
+ .replace(/[0-9]/g, c => String.fromCharCode(c.charCodeAt(0) + 1728))
455
+ .replace(/(K|M|B|T|Qt|Qd)/g, function (c) {
447
456
  return String(scaleUnits[c]);
448
- }),
449
- sign: sign,
450
- wholeNumber: wholeNumberStr.replace(thousandSeparatorRegex, ','),
451
- fractionalPart: fractionalPartStr,
452
- fractionalNonZeros: fractionalNonZeroStr,
453
- fractionalZerosCount: orginalFractionalZeroStr.length,
454
- };
457
+ });
458
+ formattedObject.wholeNumber = formattedObject.wholeNumber
459
+ .replace(/[0-9]/g, c => String.fromCharCode(c.charCodeAt(0) + 1728))
460
+ .replace(/(K|M|B|T|Qt|Qd)/g, function (c) {
461
+ return String(scaleUnits[c]);
462
+ });
463
+ }
455
464
  return formattedObject;
456
465
  }
457
466
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reduce-precision",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "files": [