@sankhyalabs/core
Advanced tools
Comparing version 1.0.15 to 1.0.16
@@ -74,9 +74,12 @@ const NUMBERINPUTS_REGEX_SCIENTIFIC_PARTS = /^([+-])?(\d+).?(\d*)[eE]([-+]?\d+)$/; | ||
let newValue = NumberUtils.stringToNumber(value); | ||
if (newValue === undefined || newValue === NaN) { | ||
if (newValue === NaN) { | ||
return NaN.toString(); | ||
} | ||
; | ||
//Validation "precision" | ||
if (Math.abs(Math.round(precision * 1) / 1) !== precision) { | ||
throw new Error(`O argumento "precision" precisa ser um número inteiro positivo.`); | ||
//Validation "precision": | ||
// Case1: precision < 0 => does not use precision | ||
// Case2: presicion not int => does not use precision | ||
if (precision < 0 || Math.abs(Math.round(precision * 1) / 1) !== precision) { | ||
//Once stringToNumber returns number format, we need to change to pt-br | ||
return NumberUtils.changeFormat(newValue.toString()); | ||
} | ||
@@ -86,4 +89,6 @@ ; | ||
let prettyPrecisionInternal = 0; | ||
if (prettyPrecision === NaN) { | ||
prettyPrecisionInternal = 0; | ||
// Case1: prettyPrecision < 0 => prettyPrecision does not change de format that means: prettyPrecisionInternal = precision; | ||
// Case2: prettyPrecision not int => prettyPrecision does not change de format that means: prettyPrecisionInternal = precision; | ||
if (prettyPrecision === NaN || Math.abs(Math.round(prettyPrecision * 1) / 1) !== prettyPrecision) { | ||
prettyPrecisionInternal = precision; | ||
} | ||
@@ -93,16 +98,10 @@ else { | ||
} | ||
if (Math.abs(Math.round(prettyPrecisionInternal * 1) / 1) !== prettyPrecisionInternal) { | ||
throw new Error(`O argumento "prettyPrecision" precisa ser um número inteiro positivo.`); | ||
} | ||
; | ||
let newValueStr; | ||
newValueStr = (Math.round(newValue * Math.pow(10, precision)) / Math.pow(10, precision)).toLocaleString('pt-br', { minimumFractionDigits: precision }); | ||
//prettyPrecision | ||
if (prettyPrecision !== NaN) { | ||
const varSettingPP = precision - prettyPrecisionInternal; | ||
if (varSettingPP > 0) { | ||
for (let i = 0; i < varSettingPP; i++) { | ||
if (newValueStr.substring(newValueStr.length - 1) == "0" && precision > 0) { | ||
newValueStr = newValueStr.substring(0, newValueStr.length - 1); | ||
} | ||
const varSettingPP = precision - prettyPrecisionInternal; | ||
if (varSettingPP > 0) { | ||
for (let i = 0; i < varSettingPP; i++) { | ||
if (newValueStr.substring(newValueStr.length - 1) == "0" && precision > 0) { | ||
newValueStr = newValueStr.substring(0, newValueStr.length - 1); | ||
} | ||
@@ -109,0 +108,0 @@ } |
{ | ||
"name": "@sankhyalabs/core", | ||
"version": "1.0.15", | ||
"version": "1.0.16", | ||
"description": "Modulo core JavaScript da Sankhya.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -90,9 +90,12 @@ | ||
if (newValue === undefined || newValue === NaN) { | ||
if (newValue === NaN) { | ||
return NaN.toString(); | ||
}; | ||
//Validation "precision" | ||
if (Math.abs(Math.round(precision * 1) / 1) !== precision) { | ||
throw new Error(`O argumento "precision" precisa ser um número inteiro positivo.`); | ||
//Validation "precision": | ||
// Case1: precision < 0 => does not use precision | ||
// Case2: presicion not int => does not use precision | ||
if (precision < 0 || Math.abs(Math.round(precision * 1) / 1) !== precision) { | ||
//Once stringToNumber returns number format, we need to change to pt-br | ||
return NumberUtils.changeFormat(newValue.toString()); | ||
}; | ||
@@ -103,4 +106,6 @@ | ||
let prettyPrecisionInternal: number = 0; | ||
if (prettyPrecision === NaN) { | ||
prettyPrecisionInternal = 0; | ||
// Case1: prettyPrecision < 0 => prettyPrecision does not change de format that means: prettyPrecisionInternal = precision; | ||
// Case2: prettyPrecision not int => prettyPrecision does not change de format that means: prettyPrecisionInternal = precision; | ||
if (prettyPrecision === NaN || Math.abs(Math.round(prettyPrecision * 1) / 1) !== prettyPrecision) { | ||
prettyPrecisionInternal = precision; | ||
} else { | ||
@@ -110,7 +115,2 @@ prettyPrecisionInternal = prettyPrecision; | ||
if (Math.abs(Math.round(prettyPrecisionInternal * 1) / 1) !== prettyPrecisionInternal) { | ||
throw new Error(`O argumento "prettyPrecision" precisa ser um número inteiro positivo.`); | ||
}; | ||
let newValueStr: string; | ||
@@ -120,9 +120,7 @@ newValueStr = (Math.round(newValue * Math.pow(10, precision)) / Math.pow(10, precision)).toLocaleString('pt-br', { minimumFractionDigits: precision }); | ||
//prettyPrecision | ||
if (prettyPrecision !== NaN) { | ||
const varSettingPP: number = precision - prettyPrecisionInternal; | ||
if (varSettingPP > 0) { | ||
for (let i = 0; i < varSettingPP; i++) { | ||
if (newValueStr.substring(newValueStr.length - 1) == "0" && precision > 0) { | ||
newValueStr = newValueStr.substring(0, newValueStr.length - 1); | ||
} | ||
const varSettingPP: number = precision - prettyPrecisionInternal; | ||
if (varSettingPP > 0) { | ||
for (let i = 0; i < varSettingPP; i++) { | ||
if (newValueStr.substring(newValueStr.length - 1) == "0" && precision > 0) { | ||
newValueStr = newValueStr.substring(0, newValueStr.length - 1); | ||
} | ||
@@ -129,0 +127,0 @@ } |
@@ -14,2 +14,3 @@ import { NumberUtils } from '../../src/utils/NumberUtils'; | ||
{ valueInput: '11500', valueOutput: 11500 }, | ||
{ valueInput: '1.000', valueOutput: 1 }, | ||
{ valueInput: '11.500,003', valueOutput: 11500.003 }, | ||
@@ -66,11 +67,33 @@ { valueInput: '-100', valueOutput: -100 }, | ||
{ precision: 2, prettyprecision: 1, strvalue: '1,23', strvalueOutPut: '1,23' }, | ||
{ precision: 4, prettyprecision: 2, strvalue: '1.200.999', strvalueOutPut: 'NaN' }, | ||
{ precision: 2, prettyprecision: 1, strvalue: '1.000', strvalueOutPut: '1,0' }, | ||
{ precision: 2, prettyprecision: 1, strvalue: '1200999,99', strvalueOutPut: '1.200.999,99' }, | ||
{ precision: 4, prettyprecision: 2, strvalue: '1,232,333.345', strvalueOutPut: 'NaN' }, | ||
{ precision: 2, prettyprecision: 1, strvalue: '1,23', strvalueOutPut: '1,23' }, | ||
{ precision: 4, prettyprecision: 0, strvalue: '1200.00001', strvalueOutPut: '1.200' }, | ||
{ precision: 4, prettyprecision: 0, strvalue: 'String', strvalueOutPut: 'NaN' }, | ||
{ precision: 4, prettyprecision: 0, strvalue: '', strvalueOutPut: 'NaN' }, | ||
] | ||
{ precision: 4, prettyprecision: undefined, strvalue: '1.200.999,3333', strvalueOutPut: '1.200.999,3333' }, | ||
{ precision: 2, prettyprecision: undefined, strvalue: '1.000', strvalueOutPut: '1,00' }, | ||
{ precision: 2, prettyprecision: undefined, strvalue: '1200999,99', strvalueOutPut: '1.200.999,99' }, | ||
{ precision: 4, prettyprecision: undefined, strvalue: '1,232,333.345', strvalueOutPut: 'NaN' }, | ||
{ precision: 2, prettyprecision: undefined, strvalue: '1,23', strvalueOutPut: '1,23' }, | ||
{ precision: 4, prettyprecision: undefined, strvalue: '1.200.999', strvalueOutPut: 'NaN' }, | ||
{ precision: 4, prettyprecision: undefined, strvalue: 'NaN', strvalueOutPut: 'NaN' }, | ||
{ precision: 1, prettyprecision: -1, strvalue: '111,199', strvalueOutPut: '111,2' }, | ||
{ precision: 1, prettyprecision: -1, strvalue: '111,1000', strvalueOutPut: '111,1' }, | ||
{ precision: 1, prettyprecision: 1.3, strvalue: '111,100', strvalueOutPut: '111,1' }, | ||
{ precision: -1, prettyprecision: 1, strvalue: '111,1009', strvalueOutPut: '111,1009' }, | ||
{ precision: -1, prettyprecision: 1, strvalue: '111,1000', strvalueOutPut: '111,1' }, | ||
{ precision: 1.2, prettyprecision: 1, strvalue: '111,1009', strvalueOutPut: '111,1009' }, | ||
{ precision: 2, prettyprecision: -1, strvalue: '111,199', strvalueOutPut: '111,20' }, | ||
{ precision: 2, prettyprecision: -1, strvalue: '111,1000', strvalueOutPut: '111,10' }, | ||
{ precision: -2, prettyprecision: 1, strvalue: '111,1009', strvalueOutPut: '111,1009' }, | ||
{ precision: -2, prettyprecision: 1, strvalue: '111,1000', strvalueOutPut: '111,1' }, | ||
]; | ||
it('format', function () { | ||
@@ -85,12 +108,5 @@ let result: string = ''; | ||
it('Should return error an error in cause of a negative precision', function () { | ||
expect(() => { NumberUtils.format('111', -1, 1); }).toThrowError('O argumento "precision" precisa ser um número inteiro positivo.'); | ||
}); | ||
it('Should return error an error in cause of a negative prettyPrecision', function () { | ||
expect(() => { NumberUtils.format('111', 1, -1); }).toThrowError('O argumento "prettyPrecision" precisa ser um número inteiro positivo.'); | ||
}); | ||
// keepOnlyDecimalSeparator | ||
@@ -97,0 +113,0 @@ // ---------------------------------------------------------------------------------------- |
Sorry, the diff of this file is not supported yet
96414
1875