Skip to content

Commit

Permalink
feat(createNumberMask): adds multiplier option
Browse files Browse the repository at this point in the history
fixes #46
  • Loading branch information
renato-bohler committed Jul 18, 2018
1 parent 0bd0058 commit 8e4ea2b
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/createNumberMask.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default options => {
prefix = '',
suffix = '',
decimalPlaces = 0,
multiplier = 1,
stringValue = false,
allowEmpty = false,
allowNegative = false,
Expand All @@ -23,6 +24,7 @@ export default options => {

const format = storeValue => {
let number = storeValue;

if (number === undefined || number === '') {
if (allowEmpty) {
return '';
Expand All @@ -44,6 +46,9 @@ export default options => {
sign = `${sign} `;
}

// Apply the multiplier
number *= 1 / multiplier;

// Reformat the number
number = numberToLocaleString(number, locale, decimalPlaces);

Expand All @@ -58,14 +63,14 @@ export default options => {
const suffixRegex = new RegExp(`${escapedSuffix}$`);

// Checks if we need to negate the value
let multiplier = 1;
let negateMultiplier = 1;
if (allowNegative) {
const minusRegexp = /-/g;
const power =
countOcurrences(updatedValue, minusRegexp) -
countOcurrences(prefix, minusRegexp) -
countOcurrences(suffix, minusRegexp);
multiplier = (-1) ** power % 2;
negateMultiplier = (-1) ** power % 2;
}

// Extracting the digits out of updatedValue
Expand Down Expand Up @@ -100,7 +105,11 @@ export default options => {
}

// Get the number out of digits
let number = Number(digits) / 10 ** decimalPlaces * multiplier;
let number = Number(digits) / 10 ** decimalPlaces * negateMultiplier;

// Apply the multiplier
number = Number((number * multiplier).toPrecision(10));

if (stringValue) {
number = number.toString();
}
Expand Down

0 comments on commit 8e4ea2b

Please sign in to comment.