@operato/utils
Advanced tools
Comparing version 7.0.0 to 7.0.5
@@ -6,2 +6,11 @@ # Change Log | ||
### [7.0.5](https://github.com/hatiolab/operato/compare/v7.0.4...v7.0.5) (2024-07-04) | ||
### :bug: Bug Fix | ||
* all number-formatter => @operato/utils ([1bdf7de](https://github.com/hatiolab/operato/commit/1bdf7de06096ed7c4c3adc189765b17f7d553ec8)) | ||
## [7.0.0](https://github.com/hatiolab/operato/compare/v7.0.0-rc.13...v7.0.0) (2024-06-30) | ||
@@ -8,0 +17,0 @@ |
@@ -13,2 +13,2 @@ /** | ||
*/ | ||
export declare function format(mask: any, value: any): string; | ||
export declare function format(mask: string, value: number): string; |
@@ -19,9 +19,9 @@ /* | ||
if (!mask || isNaN(+value)) { | ||
return value; // return as it is. | ||
return value.toString(); // return as it is. | ||
} | ||
var isNegative, result, decimal, group, posLeadZero, posTrailZero, posSeparator, part, szSep, integer, | ||
let isNegative, result, decimal, group, posLeadZero, posTrailZero, posSeparator, part, szSep, integer, | ||
// find prefix/suffix | ||
len = mask.length, start = mask.search(/[0-9\-\+#]/), prefix = start > 0 ? mask.substring(0, start) : '', | ||
// reverse string: not an ideal method if there are surrogate pairs | ||
str = mask.split('').reverse().join(''), end = str.search(/[0-9\-\+#]/), offset = len - end, substr = mask.substring(offset, offset + 1), indx = offset + (substr === '.' || substr === ',' ? 1 : 0), suffix = end > 0 ? mask.substring(indx, len) : ''; | ||
str = mask.split('').reverse().join(''), end = str.search(/[0-9\-\+#]/), offset = len - end, substr = mask.substring(offset, offset + 1), indx = offset + (substr === '.' || substr === ',' ? 1 : 0), suffix = end > 0 ? mask.substring(indx, len) : '', splittedMask, splittedValue, stringValue; | ||
// mask with prefix & suffix removed | ||
@@ -31,3 +31,3 @@ mask = mask.substring(start, indx); | ||
value = mask.charAt(0) === '-' ? -value : +value; | ||
isNegative = value < 0 ? (value = -value) : 0; // process only abs(), and turn on flag. | ||
isNegative = value < 0 ? ((value = -value), true) : false; // process only abs(), and turn on flag. | ||
// search for separator for grp & decimal, anything not digit, not +/- sign, not #. | ||
@@ -38,18 +38,18 @@ result = mask.match(/[^\d\-\+#]/g); | ||
// split the decimal for the format string if any. | ||
mask = mask.split(decimal); | ||
splittedMask = mask.split(decimal); | ||
// Fix the decimal first, toFixed will auto fill trailing zero. | ||
value = value.toFixed(mask[1] && mask[1].length); | ||
value = +value + ''; // convert number to string to trim off *all* trailing decimal zero(es) | ||
value = parseFloat(value.toFixed((splittedMask[1] && splittedMask[1].length) || 0)); | ||
stringValue = +value + ''; // convert number to string to trim off *all* trailing decimal zero(es) | ||
// fill back any trailing zero according to format | ||
posTrailZero = mask[1] && mask[1].lastIndexOf('0'); // look for last zero in format | ||
part = value.split('.'); | ||
posTrailZero = (splittedMask[1] && splittedMask[1].lastIndexOf('0')) || 0; // look for last zero in format | ||
part = stringValue.split('.'); | ||
// integer will get !part[1] | ||
if (!part[1] || (part[1] && part[1].length <= posTrailZero)) { | ||
value = (+value).toFixed(posTrailZero + 1); | ||
stringValue = (+value).toFixed(posTrailZero + 1); | ||
} | ||
szSep = mask[0].split(group); // look for separator | ||
mask[0] = szSep.join(''); // join back without separator for counting the pos of any leading 0. | ||
posLeadZero = mask[0] && mask[0].indexOf('0'); | ||
szSep = splittedMask[0].split(group); // look for separator | ||
splittedMask[0] = szSep.join(''); // join back without separator for counting the pos of any leading 0. | ||
posLeadZero = (splittedMask[0] && splittedMask[0].indexOf('0')) || 0; | ||
if (posLeadZero > -1) { | ||
while (part[0].length < mask[0].length - posLeadZero) { | ||
while (part[0].length < splittedMask[0].length - posLeadZero) { | ||
part[0] = '0' + part[0]; | ||
@@ -61,9 +61,9 @@ } | ||
} | ||
value = value.split('.'); | ||
value[0] = part[0]; | ||
splittedValue = stringValue.split('.'); | ||
splittedValue[0] = part[0]; | ||
// process the first group separator from decimal (.) only, the rest ignore. | ||
// get the length of the last slice of split result. | ||
posSeparator = szSep[1] && szSep[szSep.length - 1].length; | ||
posSeparator = (szSep[1] && szSep[szSep.length - 1].length) || 0; | ||
if (posSeparator) { | ||
integer = value[0]; | ||
integer = splittedValue[0]; | ||
str = ''; | ||
@@ -75,3 +75,3 @@ offset = integer.length % posSeparator; | ||
// -posSeparator so that won't trail separator on full length | ||
/*jshint -W018 */ | ||
/* jshint -W018 */ | ||
if (!((indx - offset + 1) % posSeparator) && indx < len - posSeparator) { | ||
@@ -81,7 +81,7 @@ str += group; | ||
} | ||
value[0] = str; | ||
splittedValue[0] = str; | ||
} | ||
value[1] = mask[1] && value[1] ? decimal + value[1] : ''; | ||
splittedValue[1] = splittedMask[1] && splittedValue[1] ? decimal + splittedValue[1] : ''; | ||
// remove negative sign if result is zero | ||
result = value.join(''); | ||
result = splittedValue.join(''); | ||
if (result === '0' || result === '') { | ||
@@ -92,4 +92,4 @@ // remove negative sign if result is zero | ||
// 앞에 +가 붙는다면 양수일 경우에도 +를 표기해줌 | ||
var fixedPlusSign; | ||
if (mask[0].substring(0, 1) == '+') | ||
let fixedPlusSign; | ||
if (splittedMask[0].substring(0, 1) === '+') | ||
fixedPlusSign = isNegative ? '-' : '+'; | ||
@@ -96,0 +96,0 @@ else |
@@ -5,3 +5,3 @@ { | ||
"author": "heartyoh", | ||
"version": "7.0.0", | ||
"version": "7.0.5", | ||
"main": "dist/src/index.js", | ||
@@ -123,3 +123,3 @@ "module": "dist/src/index.js", | ||
}, | ||
"gitHead": "b1bdd00f672eb45ffc409ed971ac60007eda4090" | ||
"gitHead": "78f3be8a64bb2be76cba1d244f55c120ea5cea46" | ||
} |
@@ -18,17 +18,17 @@ /* | ||
*/ | ||
export function format(mask: any, value: any): string { | ||
export function format(mask: string, value: number): string { | ||
if (!mask || isNaN(+value)) { | ||
return value // return as it is. | ||
return value.toString() // return as it is. | ||
} | ||
var isNegative, | ||
result, | ||
decimal, | ||
group, | ||
posLeadZero, | ||
posTrailZero, | ||
posSeparator, | ||
part, | ||
szSep, | ||
integer, | ||
let isNegative: boolean, | ||
result: string | RegExpMatchArray | null, | ||
decimal: string, | ||
group: string, | ||
posLeadZero: number, | ||
posTrailZero: number, | ||
posSeparator: number, | ||
part: string[], | ||
szSep: string[], | ||
integer: string, | ||
// find prefix/suffix | ||
@@ -44,3 +44,6 @@ len = mask.length, | ||
indx = offset + (substr === '.' || substr === ',' ? 1 : 0), | ||
suffix = end > 0 ? mask.substring(indx, len) : '' | ||
suffix = end > 0 ? mask.substring(indx, len) : '', | ||
splittedMask: string[], | ||
splittedValue: string[], | ||
stringValue: string | ||
@@ -52,3 +55,3 @@ // mask with prefix & suffix removed | ||
value = mask.charAt(0) === '-' ? -value : +value | ||
isNegative = value < 0 ? (value = -value) : 0 // process only abs(), and turn on flag. | ||
isNegative = value < 0 ? ((value = -value), true) : false // process only abs(), and turn on flag. | ||
@@ -61,20 +64,20 @@ // search for separator for grp & decimal, anything not digit, not +/- sign, not #. | ||
// split the decimal for the format string if any. | ||
mask = mask.split(decimal) | ||
splittedMask = mask.split(decimal) | ||
// Fix the decimal first, toFixed will auto fill trailing zero. | ||
value = value.toFixed(mask[1] && mask[1].length) | ||
value = +value + '' // convert number to string to trim off *all* trailing decimal zero(es) | ||
value = parseFloat(value.toFixed((splittedMask[1] && splittedMask[1].length) || 0)) | ||
stringValue = +value + '' // convert number to string to trim off *all* trailing decimal zero(es) | ||
// fill back any trailing zero according to format | ||
posTrailZero = mask[1] && mask[1].lastIndexOf('0') // look for last zero in format | ||
part = value.split('.') | ||
posTrailZero = (splittedMask[1] && splittedMask[1].lastIndexOf('0')) || 0 // look for last zero in format | ||
part = stringValue.split('.') | ||
// integer will get !part[1] | ||
if (!part[1] || (part[1] && part[1].length <= posTrailZero)) { | ||
value = (+value).toFixed(posTrailZero + 1) | ||
stringValue = (+value).toFixed(posTrailZero + 1) | ||
} | ||
szSep = mask[0].split(group) // look for separator | ||
mask[0] = szSep.join('') // join back without separator for counting the pos of any leading 0. | ||
szSep = splittedMask[0].split(group) // look for separator | ||
splittedMask[0] = szSep.join('') // join back without separator for counting the pos of any leading 0. | ||
posLeadZero = mask[0] && mask[0].indexOf('0') | ||
posLeadZero = (splittedMask[0] && splittedMask[0].indexOf('0')) || 0 | ||
if (posLeadZero > -1) { | ||
while (part[0].length < mask[0].length - posLeadZero) { | ||
while (part[0].length < splittedMask[0].length - posLeadZero) { | ||
part[0] = '0' + part[0] | ||
@@ -86,10 +89,10 @@ } | ||
value = value.split('.') | ||
value[0] = part[0] | ||
splittedValue = stringValue.split('.') | ||
splittedValue[0] = part[0] | ||
// process the first group separator from decimal (.) only, the rest ignore. | ||
// get the length of the last slice of split result. | ||
posSeparator = szSep[1] && szSep[szSep.length - 1].length | ||
posSeparator = (szSep[1] && szSep[szSep.length - 1].length) || 0 | ||
if (posSeparator) { | ||
integer = value[0] | ||
integer = splittedValue[0] | ||
str = '' | ||
@@ -101,3 +104,3 @@ offset = integer.length % posSeparator | ||
// -posSeparator so that won't trail separator on full length | ||
/*jshint -W018 */ | ||
/* jshint -W018 */ | ||
if (!((indx - offset + 1) % posSeparator) && indx < len - posSeparator) { | ||
@@ -107,8 +110,8 @@ str += group | ||
} | ||
value[0] = str | ||
splittedValue[0] = str | ||
} | ||
value[1] = mask[1] && value[1] ? decimal + value[1] : '' | ||
splittedValue[1] = splittedMask[1] && splittedValue[1] ? decimal + splittedValue[1] : '' | ||
// remove negative sign if result is zero | ||
result = value.join('') | ||
result = splittedValue.join('') | ||
if (result === '0' || result === '') { | ||
@@ -120,5 +123,5 @@ // remove negative sign if result is zero | ||
// 앞에 +가 붙는다면 양수일 경우에도 +를 표기해줌 | ||
var fixedPlusSign | ||
let fixedPlusSign: string | ||
if (mask[0].substring(0, 1) == '+') fixedPlusSign = isNegative ? '-' : '+' | ||
if (splittedMask[0].substring(0, 1) === '+') fixedPlusSign = isNegative ? '-' : '+' | ||
else fixedPlusSign = isNegative ? '-' : '' | ||
@@ -125,0 +128,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
306111
3448