vue-currency-input
Advanced tools
Comparing version 1.8.3 to 1.8.4
/** | ||
* Vue Currency Input 1.8.3 | ||
* Vue Currency Input 1.8.4 | ||
* (c) 2019 Matthias Stiller | ||
@@ -38,6 +38,10 @@ * @license MIT | ||
}; | ||
var removeCurrencySymbol = function (str, ref) { | ||
var prefix = ref.prefix; | ||
var suffix = ref.suffix; | ||
return removePrefix(removeSuffix(str, suffix), prefix) | ||
var stripCurrencySymbolAndMinusSign = function (str, currencyFormat) { | ||
var prefix = currencyFormat.prefix; | ||
var suffix = currencyFormat.suffix; | ||
var value = str.replace(prefix, '').replace(suffix, ''); | ||
return { | ||
value: removePrefix(value, '-'), | ||
negative: startsWith(value, '-') | ||
} | ||
}; | ||
@@ -57,6 +61,6 @@ var isNumber = function (str) { return str.match(/^-?\d+(\.\d+)?$/); }; | ||
} | ||
var negative = startsWith(str, '-'); | ||
str = removePrefix(str, '-'); | ||
str = removeCurrencySymbol(str, { prefix: prefix, suffix: suffix }); | ||
var numberParts = str.split(decimalSymbol); | ||
var ref$1 = stripCurrencySymbolAndMinusSign(str, { prefix: prefix, suffix: suffix }); | ||
var value = ref$1.value; | ||
var negative = ref$1.negative; | ||
var numberParts = value.split(decimalSymbol); | ||
if (numberParts.length > 2) { | ||
@@ -98,5 +102,3 @@ return null | ||
var caretPositionFromLeft = inputtedValue.length - caretPosition; | ||
if (newValue === inputtedValue) { | ||
return caretPosition | ||
} else if (Math.abs(newValue.length - inputtedValue.length) > 1 && caretPosition <= decimalSymbolPosition) { | ||
if (Math.abs(newValue.length - inputtedValue.length) > 1 && caretPosition <= decimalSymbolPosition) { | ||
return newValue.indexOf(decimalSymbol) + 1 | ||
@@ -116,46 +118,37 @@ } else if (newValue.substr(caretPosition, 1) === groupingSymbol && count(newValue, groupingSymbol) === count(inputtedValue, groupingSymbol) + 1) { | ||
}; | ||
var getCaretPositionOnFocus = function (el) { | ||
var position = el.selectionStart; | ||
var ref = el.$ci.currencyFormat; | ||
var getCaretPositionAfterApplyingDistractionFreeFormat = function (ref, ref$1, value, caretPosition) { | ||
var prefix = ref.prefix; | ||
var groupingSymbol = ref.groupingSymbol; | ||
var ref$1 = el.$ci.options; | ||
var hideCurrencySymbol = ref$1.hideCurrencySymbol; | ||
var hideGroupingSymbol = ref$1.hideGroupingSymbol; | ||
var result = caretPosition; | ||
if (hideCurrencySymbol) { | ||
position -= prefix.length; | ||
result -= prefix.length; | ||
} | ||
if (hideGroupingSymbol) { | ||
position -= count(el.value.substring(0, el.selectionStart), groupingSymbol); | ||
result -= count(value.substring(0, caretPosition), groupingSymbol); | ||
} | ||
return Math.max(0, position) | ||
return Math.max(0, result) | ||
}; | ||
var isValidInteger = function (integer, ref) { | ||
var prefix = ref.prefix; | ||
var isValidInteger = function (integer, groupingSymbol) { return integer.match(new RegExp(("^-?(0|[1-9]\\d{0,2}(\\" + groupingSymbol + "?\\d{3})*)$"))); }; | ||
var isFractionIncomplete = function (value, ref) { | ||
var decimalSymbol = ref.decimalSymbol; | ||
var groupingSymbol = ref.groupingSymbol; | ||
return integer.replace(prefix, '').match(new RegExp(("^-?(0|[1-9]\\d{0,2}(\\" + groupingSymbol + "?\\d{3})*)$"))); | ||
}; | ||
var isFractionIncomplete = function (value, currencyFormat) { | ||
var prefix = currencyFormat.prefix; | ||
var decimalSymbol = currencyFormat.decimalSymbol; | ||
var groupingSymbol = currencyFormat.groupingSymbol; | ||
var numberParts = value.split(decimalSymbol); | ||
return endsWith(value, decimalSymbol) && numberParts.length === 2 && isValidInteger(numberParts[0], { prefix: prefix, groupingSymbol: groupingSymbol }) | ||
return endsWith(value, decimalSymbol) && numberParts.length === 2 && isValidInteger(numberParts[0], groupingSymbol) | ||
}; | ||
var checkIncompleteValue = function (value, previousConformedValue, currencyFormat) { | ||
var checkIncompleteValue = function (value, negative, previousConformedValue, currencyFormat) { | ||
var prefix = currencyFormat.prefix; | ||
var negativePrefix = currencyFormat.negativePrefix; | ||
var suffix = currencyFormat.suffix; | ||
var decimalSymbol = currencyFormat.decimalSymbol; | ||
var decimalLength = currencyFormat.decimalLength; | ||
var negative = startsWith(value, '-'); | ||
value = removePrefix(value, '-'); | ||
value = removeCurrencySymbol(value, currencyFormat); | ||
if (value === '' && negative && previousConformedValue !== ("-" + prefix)) { | ||
return ("-" + prefix + suffix) | ||
if (value === '' && negative && previousConformedValue !== negativePrefix) { | ||
return ("" + negativePrefix + suffix) | ||
} else if (decimalLength > 0) { | ||
if (isFractionIncomplete(value, currencyFormat)) { | ||
return ("" + (negative ? '-' : '') + prefix + value + suffix) | ||
return ("" + (negative ? negativePrefix : prefix) + value + suffix) | ||
} else if (startsWith(value, decimalSymbol)) { | ||
return ("" + (negative ? '-' : '') + prefix + "0" + decimalSymbol + ((onlyDigits(value.substr(1)).substr(0, decimalLength))) + suffix) | ||
return ((negative ? negativePrefix : prefix) + "0" + decimalSymbol + ((onlyDigits(value.substr(1)).substr(0, decimalLength))) + suffix) | ||
} | ||
@@ -181,17 +174,20 @@ } | ||
}; | ||
function conformToMask (value, currencyFormat, previousConformedValue) { | ||
function conformToMask (str, currencyFormat, previousConformedValue) { | ||
if ( previousConformedValue === void 0 ) previousConformedValue = ''; | ||
if (typeof value === 'string') { | ||
value = value.trim(); | ||
var numberValue = checkNumberValue(value, currencyFormat); | ||
if (typeof str === 'string') { | ||
str = str.trim(); | ||
var numberValue = checkNumberValue(str, currencyFormat); | ||
if (numberValue != null) { | ||
return numberValue | ||
} | ||
var incompleteValue = checkIncompleteValue(value, previousConformedValue, currencyFormat); | ||
var ref = stripCurrencySymbolAndMinusSign(str, currencyFormat); | ||
var value = ref.value; | ||
var negative = ref.negative; | ||
var incompleteValue = checkIncompleteValue(value, negative, previousConformedValue, currencyFormat); | ||
if (incompleteValue != null) { | ||
return { conformedValue: incompleteValue } | ||
} | ||
var ref = value.split(currencyFormat.decimalSymbol); | ||
var integer = ref[0]; | ||
var fraction = ref.slice(1); | ||
var ref$1 = value.split(currencyFormat.decimalSymbol); | ||
var integer = ref$1[0]; | ||
var fraction = ref$1.slice(1); | ||
var integerDigits = onlyDigits(integer).replace(/^0+(0$|[^0])/, '$1'); | ||
@@ -203,3 +199,3 @@ var fractionDigits = onlyDigits(fraction.join('')).substr(0, currencyFormat.decimalLength); | ||
var number = integerDigits; | ||
if (startsWith(value, '-')) { | ||
if (negative) { | ||
number = "-" + number; | ||
@@ -215,3 +211,3 @@ } | ||
} | ||
} else if (number === '-' && previousConformedValue !== ("-" + (currencyFormat.prefix))) { | ||
} else if (number === '-' && previousConformedValue !== currencyFormat.negativePrefix) { | ||
return { conformedValue: previousConformedValue } | ||
@@ -228,6 +224,8 @@ } else { | ||
var currency = ref.currency; | ||
var str = new Intl.NumberFormat(locale, { style: 'currency', currency: currency }).format(123456); | ||
var numberFormat = new Intl.NumberFormat(locale, { style: 'currency', currency: currency }); | ||
var str = numberFormat.format(123456); | ||
var decimalLength = (str.match(/0/g) || []).length; | ||
var decimalSymbol = decimalLength > 0 ? str.substr(str.indexOf('6') + 1, 1) : null; | ||
var prefix = str.substring(0, str.indexOf('1')); | ||
var negativePrefix = numberFormat.format(-1).substring(0, str.indexOf('1') + 1); | ||
var suffix = str.substring(str.lastIndexOf(decimalLength > 0 ? '0' : '6') + 1); | ||
@@ -237,2 +235,3 @@ var groupingSymbol = str.substr(str.indexOf('3') + 1, 1); | ||
prefix: prefix, | ||
negativePrefix: negativePrefix, | ||
suffix: suffix, | ||
@@ -279,8 +278,21 @@ groupingSymbol: groupingSymbol, | ||
inputElement.$ci.focus = true; | ||
if (inputElement.$ci.options.distractionFree) { | ||
var ref = inputElement.$ci; | ||
var currencyFormat = ref.currencyFormat; | ||
var options = ref.options; | ||
var distractionFree = options.distractionFree; | ||
var hideCurrencySymbol = options.hideCurrencySymbol; | ||
var hideGroupingSymbol = options.hideGroupingSymbol; | ||
var hideNegligibleDecimalDigits = options.hideNegligibleDecimalDigits; | ||
if (distractionFree === true || hideCurrencySymbol || hideGroupingSymbol || hideNegligibleDecimalDigits) { | ||
setTimeout(function () { | ||
var position = getCaretPositionOnFocus(inputElement); | ||
var value = inputElement.value; | ||
var selectionStart = inputElement.selectionStart; | ||
var selectionEnd = inputElement.selectionEnd; | ||
applyDistractionFreeFormat(inputElement); | ||
setCaretPosition(inputElement, position); | ||
}, 0); | ||
if (Math.abs(selectionStart - selectionEnd) > 0) { | ||
inputElement.setSelectionRange(0, inputElement.value.length); | ||
} else { | ||
setCaretPosition(inputElement, getCaretPositionAfterApplyingDistractionFreeFormat(currencyFormat, options, value, selectionStart)); | ||
} | ||
}); | ||
} | ||
@@ -380,2 +392,3 @@ }); | ||
{prefix: hideCurrencySymbol ? '' : currencyFormat.prefix, | ||
negativePrefix: hideCurrencySymbol ? '-' : currencyFormat.negativePrefix, | ||
suffix: hideCurrencySymbol ? '' : currencyFormat.suffix}), previousConformedValue); | ||
@@ -382,0 +395,0 @@ var conformedValue = ref$1.conformedValue; |
/** | ||
* Vue Currency Input 1.8.3 | ||
* Vue Currency Input 1.8.4 | ||
* (c) 2019 Matthias Stiller | ||
@@ -44,6 +44,10 @@ * @license MIT | ||
}; | ||
var removeCurrencySymbol = function (str, ref) { | ||
var prefix = ref.prefix; | ||
var suffix = ref.suffix; | ||
return removePrefix(removeSuffix(str, suffix), prefix) | ||
var stripCurrencySymbolAndMinusSign = function (str, currencyFormat) { | ||
var prefix = currencyFormat.prefix; | ||
var suffix = currencyFormat.suffix; | ||
var value = str.replace(prefix, '').replace(suffix, ''); | ||
return { | ||
value: removePrefix(value, '-'), | ||
negative: startsWith(value, '-') | ||
} | ||
}; | ||
@@ -63,6 +67,6 @@ var isNumber = function (str) { return str.match(/^-?\d+(\.\d+)?$/); }; | ||
} | ||
var negative = startsWith(str, '-'); | ||
str = removePrefix(str, '-'); | ||
str = removeCurrencySymbol(str, { prefix: prefix, suffix: suffix }); | ||
var numberParts = str.split(decimalSymbol); | ||
var ref$1 = stripCurrencySymbolAndMinusSign(str, { prefix: prefix, suffix: suffix }); | ||
var value = ref$1.value; | ||
var negative = ref$1.negative; | ||
var numberParts = value.split(decimalSymbol); | ||
if (numberParts.length > 2) { | ||
@@ -104,5 +108,3 @@ return null | ||
var caretPositionFromLeft = inputtedValue.length - caretPosition; | ||
if (newValue === inputtedValue) { | ||
return caretPosition | ||
} else if (Math.abs(newValue.length - inputtedValue.length) > 1 && caretPosition <= decimalSymbolPosition) { | ||
if (Math.abs(newValue.length - inputtedValue.length) > 1 && caretPosition <= decimalSymbolPosition) { | ||
return newValue.indexOf(decimalSymbol) + 1 | ||
@@ -122,46 +124,37 @@ } else if (newValue.substr(caretPosition, 1) === groupingSymbol && count(newValue, groupingSymbol) === count(inputtedValue, groupingSymbol) + 1) { | ||
}; | ||
var getCaretPositionOnFocus = function (el) { | ||
var position = el.selectionStart; | ||
var ref = el.$ci.currencyFormat; | ||
var getCaretPositionAfterApplyingDistractionFreeFormat = function (ref, ref$1, value, caretPosition) { | ||
var prefix = ref.prefix; | ||
var groupingSymbol = ref.groupingSymbol; | ||
var ref$1 = el.$ci.options; | ||
var hideCurrencySymbol = ref$1.hideCurrencySymbol; | ||
var hideGroupingSymbol = ref$1.hideGroupingSymbol; | ||
var result = caretPosition; | ||
if (hideCurrencySymbol) { | ||
position -= prefix.length; | ||
result -= prefix.length; | ||
} | ||
if (hideGroupingSymbol) { | ||
position -= count(el.value.substring(0, el.selectionStart), groupingSymbol); | ||
result -= count(value.substring(0, caretPosition), groupingSymbol); | ||
} | ||
return Math.max(0, position) | ||
return Math.max(0, result) | ||
}; | ||
var isValidInteger = function (integer, ref) { | ||
var prefix = ref.prefix; | ||
var isValidInteger = function (integer, groupingSymbol) { return integer.match(new RegExp(("^-?(0|[1-9]\\d{0,2}(\\" + groupingSymbol + "?\\d{3})*)$"))); }; | ||
var isFractionIncomplete = function (value, ref) { | ||
var decimalSymbol = ref.decimalSymbol; | ||
var groupingSymbol = ref.groupingSymbol; | ||
return integer.replace(prefix, '').match(new RegExp(("^-?(0|[1-9]\\d{0,2}(\\" + groupingSymbol + "?\\d{3})*)$"))); | ||
}; | ||
var isFractionIncomplete = function (value, currencyFormat) { | ||
var prefix = currencyFormat.prefix; | ||
var decimalSymbol = currencyFormat.decimalSymbol; | ||
var groupingSymbol = currencyFormat.groupingSymbol; | ||
var numberParts = value.split(decimalSymbol); | ||
return endsWith(value, decimalSymbol) && numberParts.length === 2 && isValidInteger(numberParts[0], { prefix: prefix, groupingSymbol: groupingSymbol }) | ||
return endsWith(value, decimalSymbol) && numberParts.length === 2 && isValidInteger(numberParts[0], groupingSymbol) | ||
}; | ||
var checkIncompleteValue = function (value, previousConformedValue, currencyFormat) { | ||
var checkIncompleteValue = function (value, negative, previousConformedValue, currencyFormat) { | ||
var prefix = currencyFormat.prefix; | ||
var negativePrefix = currencyFormat.negativePrefix; | ||
var suffix = currencyFormat.suffix; | ||
var decimalSymbol = currencyFormat.decimalSymbol; | ||
var decimalLength = currencyFormat.decimalLength; | ||
var negative = startsWith(value, '-'); | ||
value = removePrefix(value, '-'); | ||
value = removeCurrencySymbol(value, currencyFormat); | ||
if (value === '' && negative && previousConformedValue !== ("-" + prefix)) { | ||
return ("-" + prefix + suffix) | ||
if (value === '' && negative && previousConformedValue !== negativePrefix) { | ||
return ("" + negativePrefix + suffix) | ||
} else if (decimalLength > 0) { | ||
if (isFractionIncomplete(value, currencyFormat)) { | ||
return ("" + (negative ? '-' : '') + prefix + value + suffix) | ||
return ("" + (negative ? negativePrefix : prefix) + value + suffix) | ||
} else if (startsWith(value, decimalSymbol)) { | ||
return ("" + (negative ? '-' : '') + prefix + "0" + decimalSymbol + ((onlyDigits(value.substr(1)).substr(0, decimalLength))) + suffix) | ||
return ((negative ? negativePrefix : prefix) + "0" + decimalSymbol + ((onlyDigits(value.substr(1)).substr(0, decimalLength))) + suffix) | ||
} | ||
@@ -187,17 +180,20 @@ } | ||
}; | ||
function conformToMask (value, currencyFormat, previousConformedValue) { | ||
function conformToMask (str, currencyFormat, previousConformedValue) { | ||
if ( previousConformedValue === void 0 ) previousConformedValue = ''; | ||
if (typeof value === 'string') { | ||
value = value.trim(); | ||
var numberValue = checkNumberValue(value, currencyFormat); | ||
if (typeof str === 'string') { | ||
str = str.trim(); | ||
var numberValue = checkNumberValue(str, currencyFormat); | ||
if (numberValue != null) { | ||
return numberValue | ||
} | ||
var incompleteValue = checkIncompleteValue(value, previousConformedValue, currencyFormat); | ||
var ref = stripCurrencySymbolAndMinusSign(str, currencyFormat); | ||
var value = ref.value; | ||
var negative = ref.negative; | ||
var incompleteValue = checkIncompleteValue(value, negative, previousConformedValue, currencyFormat); | ||
if (incompleteValue != null) { | ||
return { conformedValue: incompleteValue } | ||
} | ||
var ref = value.split(currencyFormat.decimalSymbol); | ||
var integer = ref[0]; | ||
var fraction = ref.slice(1); | ||
var ref$1 = value.split(currencyFormat.decimalSymbol); | ||
var integer = ref$1[0]; | ||
var fraction = ref$1.slice(1); | ||
var integerDigits = onlyDigits(integer).replace(/^0+(0$|[^0])/, '$1'); | ||
@@ -209,3 +205,3 @@ var fractionDigits = onlyDigits(fraction.join('')).substr(0, currencyFormat.decimalLength); | ||
var number = integerDigits; | ||
if (startsWith(value, '-')) { | ||
if (negative) { | ||
number = "-" + number; | ||
@@ -221,3 +217,3 @@ } | ||
} | ||
} else if (number === '-' && previousConformedValue !== ("-" + (currencyFormat.prefix))) { | ||
} else if (number === '-' && previousConformedValue !== currencyFormat.negativePrefix) { | ||
return { conformedValue: previousConformedValue } | ||
@@ -234,6 +230,8 @@ } else { | ||
var currency = ref.currency; | ||
var str = new Intl.NumberFormat(locale, { style: 'currency', currency: currency }).format(123456); | ||
var numberFormat = new Intl.NumberFormat(locale, { style: 'currency', currency: currency }); | ||
var str = numberFormat.format(123456); | ||
var decimalLength = (str.match(/0/g) || []).length; | ||
var decimalSymbol = decimalLength > 0 ? str.substr(str.indexOf('6') + 1, 1) : null; | ||
var prefix = str.substring(0, str.indexOf('1')); | ||
var negativePrefix = numberFormat.format(-1).substring(0, str.indexOf('1') + 1); | ||
var suffix = str.substring(str.lastIndexOf(decimalLength > 0 ? '0' : '6') + 1); | ||
@@ -243,2 +241,3 @@ var groupingSymbol = str.substr(str.indexOf('3') + 1, 1); | ||
prefix: prefix, | ||
negativePrefix: negativePrefix, | ||
suffix: suffix, | ||
@@ -285,8 +284,21 @@ groupingSymbol: groupingSymbol, | ||
inputElement.$ci.focus = true; | ||
if (inputElement.$ci.options.distractionFree) { | ||
var ref = inputElement.$ci; | ||
var currencyFormat = ref.currencyFormat; | ||
var options = ref.options; | ||
var distractionFree = options.distractionFree; | ||
var hideCurrencySymbol = options.hideCurrencySymbol; | ||
var hideGroupingSymbol = options.hideGroupingSymbol; | ||
var hideNegligibleDecimalDigits = options.hideNegligibleDecimalDigits; | ||
if (distractionFree === true || hideCurrencySymbol || hideGroupingSymbol || hideNegligibleDecimalDigits) { | ||
setTimeout(function () { | ||
var position = getCaretPositionOnFocus(inputElement); | ||
var value = inputElement.value; | ||
var selectionStart = inputElement.selectionStart; | ||
var selectionEnd = inputElement.selectionEnd; | ||
applyDistractionFreeFormat(inputElement); | ||
setCaretPosition(inputElement, position); | ||
}, 0); | ||
if (Math.abs(selectionStart - selectionEnd) > 0) { | ||
inputElement.setSelectionRange(0, inputElement.value.length); | ||
} else { | ||
setCaretPosition(inputElement, getCaretPositionAfterApplyingDistractionFreeFormat(currencyFormat, options, value, selectionStart)); | ||
} | ||
}); | ||
} | ||
@@ -386,2 +398,3 @@ }); | ||
{prefix: hideCurrencySymbol ? '' : currencyFormat.prefix, | ||
negativePrefix: hideCurrencySymbol ? '-' : currencyFormat.negativePrefix, | ||
suffix: hideCurrencySymbol ? '' : currencyFormat.suffix}), previousConformedValue); | ||
@@ -388,0 +401,0 @@ var conformedValue = ref$1.conformedValue; |
{ | ||
"name": "vue-currency-input", | ||
"description": "Easy input of currency formatted numbers for Vue.js.", | ||
"version": "1.8.3", | ||
"version": "1.8.4", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "unpkg": "dist/vue-currency-input.umd.js", |
import Vue from 'vue' | ||
import defaultOptions from './defaultOptions' | ||
import { getCaretPositionAfterFormat, getCaretPositionOnFocus, setCaretPosition } from './utils/caretPosition' | ||
import { getCaretPositionAfterApplyingDistractionFreeFormat, getCaretPositionAfterFormat, setCaretPosition } from './utils/caretPosition' | ||
import conformToMask from './utils/conformToMask' | ||
@@ -36,8 +36,14 @@ import createCurrencyFormat from './utils/createCurrencyFormat' | ||
inputElement.$ci.focus = true | ||
if (inputElement.$ci.options.distractionFree) { | ||
const { currencyFormat, options } = inputElement.$ci | ||
const { distractionFree, hideCurrencySymbol, hideGroupingSymbol, hideNegligibleDecimalDigits } = options | ||
if (distractionFree === true || hideCurrencySymbol || hideGroupingSymbol || hideNegligibleDecimalDigits) { | ||
setTimeout(() => { | ||
const position = getCaretPositionOnFocus(inputElement) | ||
const { value, selectionStart, selectionEnd } = inputElement | ||
applyDistractionFreeFormat(inputElement) | ||
setCaretPosition(inputElement, position) | ||
}, 0) | ||
if (Math.abs(selectionStart - selectionEnd) > 0) { | ||
inputElement.setSelectionRange(0, inputElement.value.length) | ||
} else { | ||
setCaretPosition(inputElement, getCaretPositionAfterApplyingDistractionFreeFormat(currencyFormat, options, value, selectionStart)) | ||
} | ||
}) | ||
} | ||
@@ -127,2 +133,3 @@ }) | ||
prefix: hideCurrencySymbol ? '' : currencyFormat.prefix, | ||
negativePrefix: hideCurrencySymbol ? '-' : currencyFormat.negativePrefix, | ||
suffix: hideCurrencySymbol ? '' : currencyFormat.suffix | ||
@@ -129,0 +136,0 @@ }, previousConformedValue) |
@@ -11,5 +11,3 @@ import { count, onlyDigits, removeSuffix } from './formatHelper' | ||
if (newValue === inputtedValue) { | ||
return caretPosition | ||
} else if (Math.abs(newValue.length - inputtedValue.length) > 1 && caretPosition <= decimalSymbolPosition) { | ||
if (Math.abs(newValue.length - inputtedValue.length) > 1 && caretPosition <= decimalSymbolPosition) { | ||
return newValue.indexOf(decimalSymbol) + 1 | ||
@@ -30,13 +28,11 @@ } else if (newValue.substr(caretPosition, 1) === groupingSymbol && count(newValue, groupingSymbol) === count(inputtedValue, groupingSymbol) + 1) { | ||
export const getCaretPositionOnFocus = (el) => { | ||
let position = el.selectionStart | ||
const { prefix, groupingSymbol } = el.$ci.currencyFormat | ||
const { hideCurrencySymbol, hideGroupingSymbol } = el.$ci.options | ||
export const getCaretPositionAfterApplyingDistractionFreeFormat = ({ prefix, groupingSymbol }, { hideCurrencySymbol, hideGroupingSymbol }, value, caretPosition) => { | ||
let result = caretPosition | ||
if (hideCurrencySymbol) { | ||
position -= prefix.length | ||
result -= prefix.length | ||
} | ||
if (hideGroupingSymbol) { | ||
position -= count(el.value.substring(0, el.selectionStart), groupingSymbol) | ||
result -= count(value.substring(0, caretPosition), groupingSymbol) | ||
} | ||
return Math.max(0, position) | ||
return Math.max(0, result) | ||
} |
@@ -1,23 +0,19 @@ | ||
import { endsWith, isNumber, onlyDigits, removeCurrencySymbol, removePrefix, startsWith } from './formatHelper' | ||
import { endsWith, isNumber, onlyDigits, startsWith, stripCurrencySymbolAndMinusSign } from './formatHelper' | ||
const isValidInteger = (integer, { prefix, groupingSymbol }) => integer.replace(prefix, '').match(new RegExp(`^-?(0|[1-9]\\d{0,2}(\\${groupingSymbol}?\\d{3})*)$`)) | ||
const isValidInteger = (integer, groupingSymbol) => integer.match(new RegExp(`^-?(0|[1-9]\\d{0,2}(\\${groupingSymbol}?\\d{3})*)$`)) | ||
const isFractionIncomplete = (value, currencyFormat) => { | ||
const { prefix, decimalSymbol, groupingSymbol } = currencyFormat | ||
const isFractionIncomplete = (value, { decimalSymbol, groupingSymbol }) => { | ||
const numberParts = value.split(decimalSymbol) | ||
return endsWith(value, decimalSymbol) && numberParts.length === 2 && isValidInteger(numberParts[0], { prefix, groupingSymbol }) | ||
return endsWith(value, decimalSymbol) && numberParts.length === 2 && isValidInteger(numberParts[0], groupingSymbol) | ||
} | ||
const checkIncompleteValue = (value, previousConformedValue, currencyFormat) => { | ||
const { prefix, suffix, decimalSymbol, decimalLength } = currencyFormat | ||
const negative = startsWith(value, '-') | ||
value = removePrefix(value, '-') | ||
value = removeCurrencySymbol(value, currencyFormat) | ||
if (value === '' && negative && previousConformedValue !== `-${prefix}`) { | ||
return `-${prefix}${suffix}` | ||
const checkIncompleteValue = (value, negative, previousConformedValue, currencyFormat) => { | ||
const { prefix, negativePrefix, suffix, decimalSymbol, decimalLength } = currencyFormat | ||
if (value === '' && negative && previousConformedValue !== negativePrefix) { | ||
return `${negativePrefix}${suffix}` | ||
} else if (decimalLength > 0) { | ||
if (isFractionIncomplete(value, currencyFormat)) { | ||
return `${negative ? '-' : ''}${prefix}${value}${suffix}` | ||
return `${negative ? negativePrefix : prefix}${value}${suffix}` | ||
} else if (startsWith(value, decimalSymbol)) { | ||
return `${negative ? '-' : ''}${prefix}0${decimalSymbol}${(onlyDigits(value.substr(1)).substr(0, decimalLength))}${suffix}` | ||
return `${negative ? negativePrefix : prefix}0${decimalSymbol}${(onlyDigits(value.substr(1)).substr(0, decimalLength))}${suffix}` | ||
} | ||
@@ -44,7 +40,7 @@ } | ||
export default (value, currencyFormat, previousConformedValue = '') => { | ||
if (typeof value === 'string') { | ||
value = value.trim() | ||
export default (str, currencyFormat, previousConformedValue = '') => { | ||
if (typeof str === 'string') { | ||
str = str.trim() | ||
const numberValue = checkNumberValue(value, currencyFormat) | ||
const numberValue = checkNumberValue(str, currencyFormat) | ||
if (numberValue != null) { | ||
@@ -54,3 +50,4 @@ return numberValue | ||
const incompleteValue = checkIncompleteValue(value, previousConformedValue, currencyFormat) | ||
const { value, negative } = stripCurrencySymbolAndMinusSign(str, currencyFormat) | ||
const incompleteValue = checkIncompleteValue(value, negative, previousConformedValue, currencyFormat) | ||
if (incompleteValue != null) { | ||
@@ -69,3 +66,3 @@ return { conformedValue: incompleteValue } | ||
let number = integerDigits | ||
if (startsWith(value, '-')) { | ||
if (negative) { | ||
number = `-${number}` | ||
@@ -81,3 +78,3 @@ } | ||
} | ||
} else if (number === '-' && previousConformedValue !== `-${currencyFormat.prefix}`) { | ||
} else if (number === '-' && previousConformedValue !== currencyFormat.negativePrefix) { | ||
return { conformedValue: previousConformedValue } | ||
@@ -84,0 +81,0 @@ } else { |
export default ({ locale, currency }) => { | ||
const str = new Intl.NumberFormat(locale, { style: 'currency', currency }).format(123456) | ||
const numberFormat = new Intl.NumberFormat(locale, { style: 'currency', currency }) | ||
const str = numberFormat.format(123456) | ||
const decimalLength = (str.match(/0/g) || []).length | ||
const decimalSymbol = decimalLength > 0 ? str.substr(str.indexOf('6') + 1, 1) : null | ||
const prefix = str.substring(0, str.indexOf('1')) | ||
const negativePrefix = numberFormat.format(-1).substring(0, str.indexOf('1') + 1) | ||
const suffix = str.substring(str.lastIndexOf(decimalLength > 0 ? '0' : '6') + 1) | ||
@@ -11,2 +13,3 @@ const groupingSymbol = str.substr(str.indexOf('3') + 1, 1) | ||
prefix, | ||
negativePrefix, | ||
suffix, | ||
@@ -13,0 +16,0 @@ groupingSymbol, |
@@ -27,4 +27,9 @@ export const onlyDigits = (str) => str.replace(/\D+/g, '') | ||
export const removeCurrencySymbol = (str, { prefix, suffix }) => { | ||
return removePrefix(removeSuffix(str, suffix), prefix) | ||
export const stripCurrencySymbolAndMinusSign = (str, currencyFormat) => { | ||
let { prefix, suffix } = currencyFormat | ||
const value = str.replace(prefix, '').replace(suffix, '') | ||
return { | ||
value: removePrefix(value, '-'), | ||
negative: startsWith(value, '-') | ||
} | ||
} | ||
@@ -41,6 +46,4 @@ | ||
} | ||
const negative = startsWith(str, '-') | ||
str = removePrefix(str, '-') | ||
str = removeCurrencySymbol(str, { prefix, suffix }) | ||
const numberParts = str.split(decimalSymbol) | ||
let { value, negative } = stripCurrencySymbolAndMinusSign(str, { prefix, suffix }) | ||
const numberParts = value.split(decimalSymbol) | ||
if (numberParts.length > 2) { | ||
@@ -47,0 +50,0 @@ return null |
61689
1489