reduce-precision 1.0.6 → 1.0.7
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/package.json +1 -1
- package/ts/lib/bundle.min.js +1 -1
- package/ts/lib/format/index.js +73 -17
package/package.json
CHANGED
package/ts/lib/bundle.min.js
CHANGED
|
@@ -1 +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:
|
|
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:n,prefixMarker:o,postfixMarker:a,prefix:s,postfix:l,thousandSeparator:u,decimalSeparator:g}=this.options;if(null==e||""===e)return{};(null==r?void 0:r.match(/^(number|usd|irt|irr|percent)$/g))||(r="number");const f=e.toString();this.isENotation(f)&&(e=this.convertENotationToRegularNumber(Number(e)));let p=e.toString().replace(/[\u0660-\u0669\u06F0-\u06F9]/g,(function(e){return String(15&e.charCodeAt(0))})).replace(/[^\d.-]/g,"");p=p.replace(/^0+(?=\d)/g,"");const c=Math.abs(Number(p));let h,d,m,b,x=0;if("auto"===t&&(r.match(/^(usd|irt|irr|number)$/g)?t=c>=1e-4&&c<1e11?"high":"medium":"percent"===r&&(t="low")),"medium"===t)if(c>=0&&c<1e-4)h=33,d=4,m=!1,b=!0;else if(c>=1e-4&&c<.001)h=7,d=4,m=!1,b=!1;else if(c>=.001&&c<.01)h=5,d=3,m=!1,b=!1;else if(c>=.001&&c<.1)h=3,d=2,m=!1,b=!1;else if(c>=.1&&c<1)h=1,d=1,m=!1,b=!1;else if(c>=1&&c<10)h=3,d=3,m=!1,b=!1;else if(c>=10&&c<100)h=2,d=2,m=!1,b=!1;else if(c>=100&&c<1e3)h=1,d=1,m=!1,b=!1;else if(c>=1e3){const e=Math.floor(Math.log10(c))%3;h=2-e,d=2-e,m=!0,b=!0}else h=0,d=0,m=!0,b=!0;else if("low"===t)if(c>=0&&c<.01)h=2,d=0,m=!0,b=!1,x=2;else if(c>=.01&&c<.1)h=2,d=1,m=!0,b=!1;else if(c>=.1&&c<1)h=2,d=2,m=!0,b=!1;else if(c>=1&&c<10)h=2,d=2,m=!0,b=!1,x=2;else if(c>=10&&c<100)h=1,d=1,m=!0,b=!1,x=1;else if(c>=100&&c<1e3)h=0,d=0,m=!0,b=!1;else if(c>=1e3){const e=Math.floor(Math.log10(c))%3;h=1-e,d=1-e,m=!0,b=!0}else h=0,d=0,m=!0,b=!0,x=2;else c>=0&&c<1?(h=33,d=4,m=!1,b=!1):c>=1&&c<10?(h=3,d=3,m=!0,b=!1):c>=10&&c<100||c>=100&&c<1e3?(h=2,d=2,m=!0,b=!1):c>=1e3&&c<1e4?(h=1,d=1,m=!0,b=!1):(h=0,d=0,m=!0,b=!1);return this.isENotation(f)&&(h=Math.max(h,20),m=!1),this.reducePrecision(p,h,d,m,b,x,r,i,n,o,a,s,l,u,g,f)}convertENotationToRegularNumber(e){if(Number.isInteger(e)&&e>=1e3)return e.toString();const t=e.toString().toLowerCase().split("e");if(2!==t.length)return e.toString();parseFloat(t[0]);const r=parseInt(t[1],10);if(r<0){const i=Math.abs(r)+(t[0].includes(".")?t[0].split(".")[1].length:0);return e.toFixed(i)}return e.toString()}reducePrecision(e,t=30,r=4,i=!1,n=!1,o=0,a="number",s="en",l="plain",u="span",g="span",f="",p="",c=",",h=".",d=""){var m,b;if(null==e||""===e.trim())return{};"-0"!==e&&"-0.0"!==e||(e=e.substring(1)),e=e.toString();const x=a.match(/^(number|percent)$/g)?{"":"",K:" هزار",M:" میلیون",B:" میلیارد",T:" تریلیون",Qd:" کادریلیون",Qt:" کنتیلیون"}:{"":"",K:" هزار ت",M:" میلیون ت",B:" میلیارد ت",T:" همت",Qd:" هزار همت",Qt:" میلیون همت"},S=a.match(/^(number|percent)$/g)?{"":"",K:" هزار",M:" میلیون",B:" میلیارد",T:" تریلیون",Qd:" کادریلیون",Qt:" کنتیلیون"}:{"":"",K:" هزار تومان",M:" میلیون تومان",B:" میلیارد تومان",T:" هزار میلیارد تومان",Qd:" کادریلیون تومان",Qt:" کنتیلیون تومان"};let $=/^(-)?(\d+)\.?([0]*)(\d*)$/g.exec(e);if(!$)return{};const M=$[1]||"";let v=$[2],C=$[3],N=$[4],Q="",j="";if(C.length>=30?(C="0".padEnd(29,"0"),N="1"):C.length+r>t?(r=t-C.length)<1&&(r=1):v.length>21&&(v="0",C="",N=""),n&&v.length>=4){const e=Object.keys(x);let t=v,r=0;for(;+t>999&&r<e.length-1;)t=(+t/1e3).toFixed(2),r++;if(j=e[r],$=/^(-)?(\d+)\.?([0]*)(\d*)$/g.exec(t.toString()),!$)return{};v=$[2],C=$[3],N=$[4]}N.length>r&&(i?parseInt(N[r])<5?N=N.substring(0,r):(N=(parseInt(N.substring(0,r))+1).toString(),N.length>r&&(C.length>0?C=C.substring(0,C.length-1):(v=(Number(v)+1).toString(),N=N.substring(1)))):N=N.substring(0,r)),n&&""!==C&&""===j&&(C="0"+C.length.toString().replace(/\d/g,(function(e){return["₀","₁","₂","₃","₄","₅","₆","₇","₈","₉"][parseInt(e,10)]})));let k=`${C}${N}`;if(k.length>t&&!d.includes("e")&&(k=k.substring(0,t)),d.includes("e")||d.includes("E"));else if(d.includes(".")){const e=d.split(".");if(2===e.length){const t=e[1];if(t.length>k.length&&t.endsWith("0")){let e=0;for(let r=t.length-1;r>=0&&"0"===t[r];r--)e++;e>0&&(k=k.padEnd(k.length+e,"0"))}}}"usd"===a?(Q="en"===s?"$":"",j||(j="fa"===s?" دلار":"")):"irr"===a?j||(j="fa"===s?" ر":" R"):"irt"===a?j||(j="fa"===s?" ت":" T"):"percent"===a&&(j+="en"===s?"%":j?" درصد":"٪"),Q=f+Q,j+=p,"html"===l?(Q&&(Q=`<${u}>${Q}</${u}>`),j&&(j=`<${g}>${j}</${g}>`)):"markdown"===l&&(Q&&(Q=`${u}${Q}${u}`),j&&(j=`${g}${j}${g}`));const B=/\B(?=(\d{3})+(?!\d))/g,F=o?".".padEnd(o+1,"0"):"";let T,E="";T=t<=0||r<=0||""===N&&""===C?`${v.replace(B,",")}${F}`:`${v.replace(B,",")}.${k}`,E=`${M}${Q}${T}${j}`;const L={value:E,prefix:Q,postfix:j,sign:M,wholeNumber:T};return L.value=(null!==(m=null==L?void 0:L.value)&&void 0!==m?m:"").replace(/,/g,c).replace(/\./g,h),"fa"===s&&(L.value=(null!==(b=null==L?void 0:L.value)&&void 0!==b?b:"").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])})),L.fullPostfix=j.replace(/[0-9]/g,(e=>String.fromCharCode(e.charCodeAt(0)+1728))).replace(/(K|M|B|T|Qt|Qd)/g,(function(e){return String(S[e])})),L.postfix=L.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])})),L.wholeNumber=L.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])}))),L}}},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 n=i(r(900));t.NumberFormatter=n.default}},t={};return function r(i){var n=t[i];if(void 0!==n)return n.exports;var o=t[i]={exports:{}};return e[i].call(o.exports,o,o.exports,r),o.exports}(986)})()));
|
package/ts/lib/format/index.js
CHANGED
|
@@ -64,7 +64,7 @@ class NumberFormatter {
|
|
|
64
64
|
}
|
|
65
65
|
// Private methods...
|
|
66
66
|
isENotation(input) {
|
|
67
|
-
return /^[-+]?[0-9]*\.?[0-9]+([eE][-+][0-9]+)$/.test(input);
|
|
67
|
+
return /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)$/.test(input);
|
|
68
68
|
}
|
|
69
69
|
format(input) {
|
|
70
70
|
let { precision, template } = this.options;
|
|
@@ -74,7 +74,9 @@ class NumberFormatter {
|
|
|
74
74
|
}
|
|
75
75
|
if (!(template === null || template === void 0 ? void 0 : template.match(/^(number|usd|irt|irr|percent)$/g)))
|
|
76
76
|
template = 'number';
|
|
77
|
-
|
|
77
|
+
// Store original input string to preserve format for trailing zeros
|
|
78
|
+
const originalInput = input.toString();
|
|
79
|
+
if (this.isENotation(originalInput)) {
|
|
78
80
|
input = this.convertENotationToRegularNumber(Number(input));
|
|
79
81
|
}
|
|
80
82
|
// Replace each Persian/Arabic numeral in the string with its English counterpart and strip all non-numeric chars
|
|
@@ -84,10 +86,9 @@ class NumberFormatter {
|
|
|
84
86
|
return String(match.charCodeAt(0) & 0xf);
|
|
85
87
|
})
|
|
86
88
|
.replace(/[^\d.-]/g, '');
|
|
87
|
-
// Stripping leading zeros
|
|
89
|
+
// Stripping leading zeros only, preserve trailing zeros
|
|
88
90
|
numberString = numberString
|
|
89
|
-
.replace(/^0+(?=\d)/g, '')
|
|
90
|
-
.replace(/\.\d*0+$|(\.\d)0+\b/g, '$1');
|
|
91
|
+
.replace(/^0+(?=\d)/g, '');
|
|
91
92
|
const number = Math.abs(Number(numberString));
|
|
92
93
|
let p, d, r, c;
|
|
93
94
|
let f = 0;
|
|
@@ -262,22 +263,43 @@ class NumberFormatter {
|
|
|
262
263
|
c = false;
|
|
263
264
|
}
|
|
264
265
|
}
|
|
265
|
-
|
|
266
|
+
// For scientific notation, increase precision to ensure correct representation
|
|
267
|
+
if (this.isENotation(originalInput)) {
|
|
268
|
+
p = Math.max(p, 20);
|
|
269
|
+
r = false;
|
|
270
|
+
}
|
|
271
|
+
return this.reducePrecision(numberString, p, d, r, c, f, template, language, outputFormat, prefixMarker, postfixMarker, prefix, postfix, thousandSeparator, decimalSeparator, originalInput);
|
|
266
272
|
}
|
|
267
273
|
convertENotationToRegularNumber(eNotation) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
.
|
|
271
|
-
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
|
|
274
|
+
// For simple cases like 1e3, directly use Number constructor
|
|
275
|
+
if (Number.isInteger(eNotation) && eNotation >= 1000) {
|
|
276
|
+
return eNotation.toString();
|
|
277
|
+
}
|
|
278
|
+
const parts = eNotation.toString().toLowerCase().split('e');
|
|
279
|
+
if (parts.length !== 2)
|
|
280
|
+
return eNotation.toString();
|
|
281
|
+
const coefficient = parseFloat(parts[0]);
|
|
282
|
+
const exponent = parseInt(parts[1], 10);
|
|
283
|
+
// Handle negative exponents (very small numbers)
|
|
284
|
+
if (exponent < 0) {
|
|
285
|
+
const absExponent = Math.abs(exponent);
|
|
286
|
+
// Determine precision needed to show all digits
|
|
287
|
+
const precision = absExponent +
|
|
288
|
+
(parts[0].includes('.') ? parts[0].split('.')[1].length : 0);
|
|
289
|
+
return eNotation.toFixed(precision);
|
|
290
|
+
}
|
|
291
|
+
// For positive exponents, let JavaScript do the conversion
|
|
292
|
+
return eNotation.toString();
|
|
275
293
|
}
|
|
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 = '.') {
|
|
294
|
+
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 = '.', originalInput = '') {
|
|
277
295
|
var _a, _b;
|
|
278
296
|
if (numberString === undefined || numberString === null || numberString.trim() === '') {
|
|
279
297
|
return {};
|
|
280
298
|
}
|
|
299
|
+
// Handle negative zero
|
|
300
|
+
if (numberString === '-0' || numberString === '-0.0') {
|
|
301
|
+
numberString = numberString.substring(1); // Remove negative sign for zero
|
|
302
|
+
}
|
|
281
303
|
numberString = numberString.toString();
|
|
282
304
|
const maxPrecision = 30;
|
|
283
305
|
const maxIntegerDigits = 21;
|
|
@@ -408,9 +430,40 @@ class NumberFormatter {
|
|
|
408
430
|
][parseInt(match, 10)];
|
|
409
431
|
});
|
|
410
432
|
}
|
|
433
|
+
// Check if the original input had trailing zeros
|
|
411
434
|
let fractionalPartStr = `${fractionalZeroStr}${fractionalNonZeroStr}`;
|
|
412
|
-
|
|
413
|
-
fractionalPartStr
|
|
435
|
+
// Don't truncate trailing zeros when they're in the original string
|
|
436
|
+
if (fractionalPartStr.length > precision && !originalInput.includes('e')) {
|
|
437
|
+
fractionalPartStr = fractionalPartStr.substring(0, precision);
|
|
438
|
+
}
|
|
439
|
+
// For scientific notation and numbers with trailing zeros, preserve the format
|
|
440
|
+
if (originalInput.includes('e') || originalInput.includes('E')) {
|
|
441
|
+
// For scientific notation, use the converted string
|
|
442
|
+
}
|
|
443
|
+
else if (originalInput.includes('.')) {
|
|
444
|
+
// For regular numbers with decimal point, check for trailing zeros
|
|
445
|
+
const originalParts = originalInput.split('.');
|
|
446
|
+
if (originalParts.length === 2) {
|
|
447
|
+
const originalDecimal = originalParts[1];
|
|
448
|
+
// If original has more digits than what we have now, preserve those trailing zeros
|
|
449
|
+
if (originalDecimal.length > fractionalPartStr.length && originalDecimal.endsWith('0')) {
|
|
450
|
+
// Count trailing zeros in original
|
|
451
|
+
let trailingZeros = 0;
|
|
452
|
+
for (let i = originalDecimal.length - 1; i >= 0; i--) {
|
|
453
|
+
if (originalDecimal[i] === '0') {
|
|
454
|
+
trailingZeros++;
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
// Add back trailing zeros if they were in the original
|
|
461
|
+
if (trailingZeros > 0) {
|
|
462
|
+
fractionalPartStr = fractionalPartStr.padEnd(fractionalPartStr.length + trailingZeros, '0');
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
414
467
|
// Output Formating, Prefix, Postfix
|
|
415
468
|
if (template === 'usd') {
|
|
416
469
|
unitPrefix = language === 'en' ? '$' : '';
|
|
@@ -453,7 +506,10 @@ class NumberFormatter {
|
|
|
453
506
|
: '';
|
|
454
507
|
let out = '';
|
|
455
508
|
let wholeNumberStr;
|
|
456
|
-
|
|
509
|
+
// FIXED: Changed condition to correctly handle numbers with trailing zeros
|
|
510
|
+
// Old condition: if (precision <= 0 || nonZeroDigits <= 0 || !fractionalNonZeroStr) {
|
|
511
|
+
// New condition checks if both fractional parts are empty
|
|
512
|
+
if (precision <= 0 || nonZeroDigits <= 0 || (fractionalNonZeroStr === '' && fractionalZeroStr === '')) {
|
|
457
513
|
wholeNumberStr = `${nonFractionalStr.replace(thousandSeparatorRegex, ',')}${fixedDecimalZeroStr}`;
|
|
458
514
|
}
|
|
459
515
|
else {
|