vue-currency-input
Advanced tools
Comparing version 1.11.4 to 1.12.0
/** | ||
* Vue Currency Input 1.11.4 | ||
* Vue Currency Input 1.12.0 | ||
* (c) 2019 Matthias Stiller | ||
@@ -29,6 +29,11 @@ * @license MIT | ||
var currency = ref.currency; | ||
var decimalLength = ref.decimalLength; | ||
var minimumFractionDigits = 2; | ||
if (decimalLength !== undefined) { | ||
minimumFractionDigits = decimalLength; | ||
} | ||
if (currency == null) { | ||
return createCurrencyFormat(new Intl.NumberFormat(locale, { minimumFractionDigits: 2 })) | ||
return createCurrencyFormat(new Intl.NumberFormat(locale, { minimumFractionDigits: minimumFractionDigits })) | ||
} else if (typeof currency === 'object') { | ||
return Object.assign({}, createCurrencyFormat(new Intl.NumberFormat(locale, { minimumFractionDigits: 2 })), | ||
return Object.assign({}, createCurrencyFormat(new Intl.NumberFormat(locale, { minimumFractionDigits: minimumFractionDigits })), | ||
{prefix: currency.prefix || '', | ||
@@ -38,3 +43,7 @@ negativePrefix: ("-" + (currency.prefix || '')), | ||
} else { | ||
return createCurrencyFormat(new Intl.NumberFormat(locale, { style: 'currency', currency: currency })) | ||
var currencyFormat = createCurrencyFormat(new Intl.NumberFormat(locale, { style: 'currency', currency: currency })); | ||
if (currencyFormat.decimalLength > 0 && decimalLength !== undefined) { | ||
currencyFormat.decimalLength = decimalLength; | ||
} | ||
return currencyFormat | ||
} | ||
@@ -75,22 +84,19 @@ } | ||
function parse (str, ref) { | ||
if ( ref === void 0 ) ref = {}; | ||
var prefix = ref.prefix; | ||
var suffix = ref.suffix; | ||
var groupingSymbol = ref.groupingSymbol; | ||
var decimalSymbol = ref.decimalSymbol; | ||
if (typeof str === 'number') { | ||
return str | ||
} else if (str && typeof str === 'string') { | ||
var getNumber = function (number, valueAsInteger, decimalLength) { | ||
return number != null && valueAsInteger ? Number(number.toFixed(decimalLength).split('.').join('')) : number | ||
}; | ||
function parse (str, currencyFormat, valueAsInteger) { | ||
if ( valueAsInteger === void 0 ) valueAsInteger = false; | ||
if (typeof str === 'string') { | ||
if (isNumber(str)) { | ||
return Number(str) | ||
return getNumber(Number(str), valueAsInteger, currencyFormat.decimalLength) | ||
} | ||
var ref$1 = stripCurrencySymbolAndMinusSign(str, { prefix: prefix, suffix: suffix }); | ||
var value = ref$1.value; | ||
var negative = ref$1.negative; | ||
var numberParts = value.split(decimalSymbol); | ||
var ref = stripCurrencySymbolAndMinusSign(str, currencyFormat); | ||
var value = ref.value; | ||
var negative = ref.negative; | ||
var numberParts = value.split(currencyFormat.decimalSymbol); | ||
if (numberParts.length > 2) { | ||
return null | ||
} | ||
var integer = numberParts[0].replace(new RegExp(("\\" + groupingSymbol), 'g'), ''); | ||
var integer = numberParts[0].replace(new RegExp(("\\" + (currencyFormat.groupingSymbol)), 'g'), ''); | ||
if (integer.length && !integer.match(/^\d+$/g)) { | ||
@@ -111,3 +117,3 @@ return null | ||
} | ||
return Number(number) | ||
return getNumber(Number(number), valueAsInteger, currencyFormat.decimalLength) | ||
} | ||
@@ -118,7 +124,8 @@ } | ||
var parseCurrency = function (formattedValue, locale, currency) { return parse(formattedValue, createCurrencyFormat$1({ locale: locale, currency: currency })); }; | ||
var parseCurrency = function (formattedValue, options) { return parse(formattedValue, createCurrencyFormat$1(options), options.valueAsInteger); }; | ||
var defaultOptions = { | ||
var DEFAULT_OPTIONS = { | ||
locale: undefined, | ||
currency: 'EUR', | ||
valueAsInteger: false, | ||
distractionFree: true, | ||
@@ -132,10 +139,8 @@ decimalLength: undefined, | ||
var setCaretPosition = function (el, position) { return el.setSelectionRange(position, position); }; | ||
var getCaretPositionAfterFormat = function (el, inputtedValue, caretPosition) { | ||
var ref = el.$ci.currencyFormat; | ||
var prefix = ref.prefix; | ||
var suffix = ref.suffix; | ||
var decimalSymbol = ref.decimalSymbol; | ||
var decimalLength = ref.decimalLength; | ||
var groupingSymbol = ref.groupingSymbol; | ||
var newValue = el.value; | ||
var getCaretPositionAfterFormat = function (newValue, inputtedValue, caretPosition, currencyFormat, options) { | ||
var prefix = currencyFormat.prefix; | ||
var suffix = currencyFormat.suffix; | ||
var decimalSymbol = currencyFormat.decimalSymbol; | ||
var decimalLength = currencyFormat.decimalLength; | ||
var groupingSymbol = currencyFormat.groupingSymbol; | ||
var decimalSymbolPosition = inputtedValue.indexOf(decimalSymbol) + 1; | ||
@@ -148,3 +153,3 @@ var caretPositionFromLeft = inputtedValue.length - caretPosition; | ||
} else { | ||
if (!el.$ci.options.autoDecimalMode && decimalSymbolPosition !== 0 && caretPosition > decimalSymbolPosition) { | ||
if (!options.autoDecimalMode && decimalSymbolPosition !== 0 && caretPosition > decimalSymbolPosition) { | ||
if (onlyDigits(removeSuffix(inputtedValue.substr(decimalSymbolPosition), suffix)).length - 1 === decimalLength) { | ||
@@ -154,3 +159,3 @@ caretPositionFromLeft -= 1; | ||
} | ||
return el.$ci.options.hideCurrencySymbol | ||
return options.distractionFree.hideCurrencySymbol | ||
? newValue.length - caretPositionFromLeft | ||
@@ -160,13 +165,9 @@ : Math.max(newValue.length - Math.max(caretPositionFromLeft, suffix.length), prefix.length === 0 ? 0 : prefix.length + 1) | ||
}; | ||
var getCaretPositionAfterApplyingDistractionFreeFormat = function (ref, ref$1, value, caretPosition) { | ||
var prefix = ref.prefix; | ||
var groupingSymbol = ref.groupingSymbol; | ||
var hideCurrencySymbol = ref$1.hideCurrencySymbol; | ||
var hideGroupingSymbol = ref$1.hideGroupingSymbol; | ||
var getDistractionFreeCaretPosition = function (formatConfig, options, value, caretPosition) { | ||
var result = caretPosition; | ||
if (hideCurrencySymbol) { | ||
result -= prefix.length; | ||
if (options.distractionFree.hideCurrencySymbol) { | ||
result -= formatConfig.prefix.length; | ||
} | ||
if (hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), groupingSymbol); | ||
if (options.distractionFree.hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), formatConfig.groupingSymbol); | ||
} | ||
@@ -268,25 +269,21 @@ return Math.max(0, result) | ||
var max = options.max; | ||
var decimalLength = options.decimalLength; | ||
var distractionFree = options.distractionFree; | ||
var autoDecimalMode = options.autoDecimalMode; | ||
options.hideCurrencySymbol = options.currency == null || distractionFree === true || distractionFree.hideCurrencySymbol; | ||
options.hideNegligibleDecimalDigits = !autoDecimalMode && (distractionFree === true || distractionFree.hideNegligibleDecimalDigits); | ||
options.hideGroupingSymbol = distractionFree === true || distractionFree.hideGroupingSymbol; | ||
if (typeof distractionFree === 'boolean') { | ||
options.distractionFree = { | ||
hideCurrencySymbol: distractionFree, | ||
hideNegligibleDecimalDigits: distractionFree, | ||
hideGroupingSymbol: distractionFree | ||
}; | ||
} | ||
if (autoDecimalMode) { | ||
options.distractionFree.hideNegligibleDecimalDigits = false; | ||
} | ||
if (min != null && max != null && min > max) { | ||
throw new Error('Invalid value range') | ||
} | ||
if (decimalLength < 0 || decimalLength > 20) { | ||
throw new Error('Decimal length must be between 0 and 20') | ||
} | ||
var currencyFormat = createCurrencyFormat$1(options); | ||
if (currencyFormat.decimalLength > 0 && decimalLength !== undefined) { | ||
currencyFormat.decimalLength = decimalLength; | ||
} | ||
inputElement.$ci = Object.assign({}, inputElement.$ci || {}, | ||
{options: options, | ||
currencyFormat: currencyFormat, | ||
decimalFormat: Object.assign({}, currencyFormat, | ||
{prefix: '', | ||
negativePrefix: '-', | ||
suffix: ''})}); | ||
currencyFormat: currencyFormat}); | ||
return inputElement | ||
@@ -312,13 +309,17 @@ }; | ||
}; | ||
var updateInputValue = function (el, value, distractionFree) { | ||
if ( distractionFree === void 0 ) distractionFree = false; | ||
var hideCurrencySymbolOnFocus = function (el) { return el.$ci.focus && el.$ci.options.distractionFree.hideCurrencySymbol; }; | ||
var hideGroupingSymbolOnFocus = function (el) { return el.$ci.focus && el.$ci.options.distractionFree.hideGroupingSymbol; }; | ||
var hideNegligibleDecimalDigitsOnFocus = function (el) { return el.$ci.focus && el.$ci.options.distractionFree.hideNegligibleDecimalDigits; }; | ||
var updateInputValue = function (el, value) { | ||
if (value != null) { | ||
var ref = el.$ci; | ||
var options = ref.options; | ||
var decimalFormat = ref.decimalFormat; | ||
var currencyFormat = ref.currencyFormat; | ||
var focus = ref.focus; | ||
var previousConformedValue = ref.previousConformedValue; | ||
var hideCurrencySymbol = focus && options.hideCurrencySymbol; | ||
var formatConfig = hideCurrencySymbol ? decimalFormat : currencyFormat; | ||
var formatConfig = Object.assign({}, currencyFormat); | ||
if (hideCurrencySymbolOnFocus(el)) { | ||
formatConfig.prefix = ''; | ||
formatConfig.negativePrefix = '-'; | ||
formatConfig.suffix = ''; | ||
} | ||
var ref$1 = conformToMask(value, formatConfig, options, previousConformedValue); | ||
@@ -329,6 +330,4 @@ var conformedValue = ref$1.conformedValue; | ||
var formattedValue = new Intl.NumberFormat(options.locale, { | ||
useGrouping: !(focus && options.hideGroupingSymbol), | ||
minimumFractionDigits: distractionFree | ||
? (options.hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : currencyFormat.decimalLength) | ||
: fractionDigits.length | ||
useGrouping: !hideGroupingSymbolOnFocus(el), | ||
minimumFractionDigits: hideNegligibleDecimalDigitsOnFocus(el) ? fractionDigits.replace(/0+$/, '').length : Math.min(formatConfig.decimalLength, fractionDigits.length) | ||
}).format(Math.abs(conformedValue)); | ||
@@ -340,3 +339,3 @@ var isNegativeZero = conformedValue === 0 && (1 / conformedValue < 0); | ||
el.value = conformedValue; | ||
el.$ci.numberValue = parse(el.value, formatConfig); | ||
el.$ci.numberValue = parse(el.value, formatConfig, options.valueAsInteger); | ||
} | ||
@@ -350,4 +349,11 @@ } else { | ||
updateInputValue(el, value); | ||
dispatchEvent(el, 'format-complete', { numberValue: el.$ci.numberValue }); | ||
var numberValue = getNumber(el.$ci.numberValue, el.$ci.options.valueAsInteger, el.$ci.currencyFormat.decimalLength); | ||
dispatchEvent(el, 'format-complete', { numberValue: numberValue }); | ||
}; | ||
var toFloat = function (number, options, currencyFormat) { | ||
if (options.valueAsInteger) { | ||
return number / Math.pow(10, currencyFormat.decimalLength) | ||
} | ||
return number | ||
}; | ||
var addEventListener = function (el) { | ||
@@ -357,5 +363,8 @@ el.addEventListener('input', function () { | ||
var selectionStart = el.selectionStart; | ||
var el_$ci = el.$ci; | ||
var currencyFormat = el_$ci.currencyFormat; | ||
var options = el_$ci.options; | ||
format(el, value); | ||
if (el.$ci.focus) { | ||
setCaretPosition(el, getCaretPositionAfterFormat(el, value, selectionStart)); | ||
setCaretPosition(el, getCaretPositionAfterFormat(el.value, value, selectionStart, currencyFormat, options)); | ||
} | ||
@@ -365,4 +374,8 @@ }, { capture: true }); | ||
var detail = ref.detail; | ||
if (!el.$ci.focus) { | ||
applyFixedFractionFormat(el, detail.value); | ||
var ref$1 = el.$ci; | ||
var focus = ref$1.focus; | ||
var currencyFormat = ref$1.currencyFormat; | ||
var options = ref$1.options; | ||
if (!focus) { | ||
applyFixedFractionFormat(el, toFloat(detail.value, options, currencyFormat)); | ||
} | ||
@@ -372,10 +385,3 @@ }); | ||
el.$ci.focus = true; | ||
var ref = el.$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) { | ||
if (hideCurrencySymbolOnFocus(el) || hideGroupingSymbolOnFocus(el) || hideNegligibleDecimalDigitsOnFocus(el)) { | ||
setTimeout(function () { | ||
@@ -385,7 +391,7 @@ var value = el.value; | ||
var selectionEnd = el.selectionEnd; | ||
updateInputValue(el, el.value, true); | ||
updateInputValue(el, el.value); | ||
if (Math.abs(selectionStart - selectionEnd) > 0) { | ||
el.setSelectionRange(0, el.value.length); | ||
} else { | ||
setCaretPosition(el, getCaretPositionAfterApplyingDistractionFreeFormat(currencyFormat, options, value, selectionStart)); | ||
setCaretPosition(el, getDistractionFreeCaretPosition(el.$ci.currencyFormat, el.$ci.options, value, selectionStart)); | ||
} | ||
@@ -404,6 +410,10 @@ }); | ||
var context = ref$1.context; | ||
var inputElement = init(el, options, context.$CI_DEFAULT_OPTIONS || defaultOptions); | ||
var inputElement = init(el, options, context.$CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS); | ||
Vue.nextTick(function () { | ||
if (inputElement.value) { | ||
applyFixedFractionFormat(inputElement, parse(inputElement.value, inputElement.$ci.currencyFormat)); | ||
var value = inputElement.value; | ||
var inputElement_$ci = inputElement.$ci; | ||
var currencyFormat = inputElement_$ci.currencyFormat; | ||
var options = inputElement_$ci.options; | ||
if (value) { | ||
applyFixedFractionFormat(inputElement, toFloat(parse(value, currencyFormat), options, currencyFormat)); | ||
} | ||
@@ -417,4 +427,4 @@ }); | ||
var context = ref$1.context; | ||
if (!!value && Object.keys(defaultOptions).some(function (key) { return oldValue[key] !== value[key]; })) { | ||
var inputElement = init(el, value, context.$CI_DEFAULT_OPTIONS || defaultOptions); | ||
if (!!value && Object.keys(DEFAULT_OPTIONS).some(function (key) { return oldValue[key] !== value[key]; })) { | ||
var inputElement = init(el, value, context.$CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS); | ||
applyFixedFractionFormat(inputElement, inputElement.$ci.numberValue); | ||
@@ -468,2 +478,6 @@ } | ||
}, | ||
valueAsInteger: { | ||
type: Boolean, | ||
default: undefined | ||
}, | ||
min: { | ||
@@ -486,4 +500,4 @@ type: Number, | ||
var this$1 = this; | ||
var options = Object.assign({}, this.$CI_DEFAULT_OPTIONS || defaultOptions); | ||
Object.keys(defaultOptions).forEach(function (key) { | ||
var options = Object.assign({}, this.$CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS); | ||
Object.keys(DEFAULT_OPTIONS).forEach(function (key) { | ||
if (this$1[key] !== undefined) { | ||
@@ -524,10 +538,9 @@ options[key] = this$1[key]; | ||
var globalOptions = ref.globalOptions; if ( globalOptions === void 0 ) globalOptions = {}; | ||
var options = Object.assign({}, defaultOptions, globalOptions); | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = options; | ||
var defaultOptions = Object.assign({}, DEFAULT_OPTIONS, globalOptions); | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = defaultOptions; | ||
Vue.component(componentName, component); | ||
Vue.directive(directiveName, directive); | ||
Vue.prototype.$parseCurrency = function (str, locale, currency) { | ||
if ( locale === void 0 ) locale = options.locale; | ||
if ( currency === void 0 ) currency = options.currency; | ||
return parseCurrency(str, locale, currency); | ||
Vue.prototype.$parseCurrency = function (str, options) { | ||
if ( options === void 0 ) options = defaultOptions; | ||
return parseCurrency(str, options); | ||
}; | ||
@@ -534,0 +547,0 @@ } |
/** | ||
* Vue Currency Input 1.11.4 | ||
* Vue Currency Input 1.12.0 | ||
* (c) 2019 Matthias Stiller | ||
@@ -10,3 +10,3 @@ * @license MIT | ||
(global = global || self, factory(global.VueCurrencyInput = {}, global.Vue)); | ||
}(this, function (exports, Vue) { 'use strict'; | ||
}(this, (function (exports, Vue) { 'use strict'; | ||
@@ -36,6 +36,11 @@ Vue = Vue && Vue.hasOwnProperty('default') ? Vue['default'] : Vue; | ||
var currency = ref.currency; | ||
var decimalLength = ref.decimalLength; | ||
var minimumFractionDigits = 2; | ||
if (decimalLength !== undefined) { | ||
minimumFractionDigits = decimalLength; | ||
} | ||
if (currency == null) { | ||
return createCurrencyFormat(new Intl.NumberFormat(locale, { minimumFractionDigits: 2 })) | ||
return createCurrencyFormat(new Intl.NumberFormat(locale, { minimumFractionDigits: minimumFractionDigits })) | ||
} else if (typeof currency === 'object') { | ||
return Object.assign({}, createCurrencyFormat(new Intl.NumberFormat(locale, { minimumFractionDigits: 2 })), | ||
return Object.assign({}, createCurrencyFormat(new Intl.NumberFormat(locale, { minimumFractionDigits: minimumFractionDigits })), | ||
{prefix: currency.prefix || '', | ||
@@ -45,3 +50,7 @@ negativePrefix: ("-" + (currency.prefix || '')), | ||
} else { | ||
return createCurrencyFormat(new Intl.NumberFormat(locale, { style: 'currency', currency: currency })) | ||
var currencyFormat = createCurrencyFormat(new Intl.NumberFormat(locale, { style: 'currency', currency: currency })); | ||
if (currencyFormat.decimalLength > 0 && decimalLength !== undefined) { | ||
currencyFormat.decimalLength = decimalLength; | ||
} | ||
return currencyFormat | ||
} | ||
@@ -82,22 +91,19 @@ } | ||
function parse (str, ref) { | ||
if ( ref === void 0 ) ref = {}; | ||
var prefix = ref.prefix; | ||
var suffix = ref.suffix; | ||
var groupingSymbol = ref.groupingSymbol; | ||
var decimalSymbol = ref.decimalSymbol; | ||
if (typeof str === 'number') { | ||
return str | ||
} else if (str && typeof str === 'string') { | ||
var getNumber = function (number, valueAsInteger, decimalLength) { | ||
return number != null && valueAsInteger ? Number(number.toFixed(decimalLength).split('.').join('')) : number | ||
}; | ||
function parse (str, currencyFormat, valueAsInteger) { | ||
if ( valueAsInteger === void 0 ) valueAsInteger = false; | ||
if (typeof str === 'string') { | ||
if (isNumber(str)) { | ||
return Number(str) | ||
return getNumber(Number(str), valueAsInteger, currencyFormat.decimalLength) | ||
} | ||
var ref$1 = stripCurrencySymbolAndMinusSign(str, { prefix: prefix, suffix: suffix }); | ||
var value = ref$1.value; | ||
var negative = ref$1.negative; | ||
var numberParts = value.split(decimalSymbol); | ||
var ref = stripCurrencySymbolAndMinusSign(str, currencyFormat); | ||
var value = ref.value; | ||
var negative = ref.negative; | ||
var numberParts = value.split(currencyFormat.decimalSymbol); | ||
if (numberParts.length > 2) { | ||
return null | ||
} | ||
var integer = numberParts[0].replace(new RegExp(("\\" + groupingSymbol), 'g'), ''); | ||
var integer = numberParts[0].replace(new RegExp(("\\" + (currencyFormat.groupingSymbol)), 'g'), ''); | ||
if (integer.length && !integer.match(/^\d+$/g)) { | ||
@@ -118,3 +124,3 @@ return null | ||
} | ||
return Number(number) | ||
return getNumber(Number(number), valueAsInteger, currencyFormat.decimalLength) | ||
} | ||
@@ -125,7 +131,8 @@ } | ||
var parseCurrency = function (formattedValue, locale, currency) { return parse(formattedValue, createCurrencyFormat$1({ locale: locale, currency: currency })); }; | ||
var parseCurrency = function (formattedValue, options) { return parse(formattedValue, createCurrencyFormat$1(options), options.valueAsInteger); }; | ||
var defaultOptions = { | ||
var DEFAULT_OPTIONS = { | ||
locale: undefined, | ||
currency: 'EUR', | ||
valueAsInteger: false, | ||
distractionFree: true, | ||
@@ -139,10 +146,8 @@ decimalLength: undefined, | ||
var setCaretPosition = function (el, position) { return el.setSelectionRange(position, position); }; | ||
var getCaretPositionAfterFormat = function (el, inputtedValue, caretPosition) { | ||
var ref = el.$ci.currencyFormat; | ||
var prefix = ref.prefix; | ||
var suffix = ref.suffix; | ||
var decimalSymbol = ref.decimalSymbol; | ||
var decimalLength = ref.decimalLength; | ||
var groupingSymbol = ref.groupingSymbol; | ||
var newValue = el.value; | ||
var getCaretPositionAfterFormat = function (newValue, inputtedValue, caretPosition, currencyFormat, options) { | ||
var prefix = currencyFormat.prefix; | ||
var suffix = currencyFormat.suffix; | ||
var decimalSymbol = currencyFormat.decimalSymbol; | ||
var decimalLength = currencyFormat.decimalLength; | ||
var groupingSymbol = currencyFormat.groupingSymbol; | ||
var decimalSymbolPosition = inputtedValue.indexOf(decimalSymbol) + 1; | ||
@@ -155,3 +160,3 @@ var caretPositionFromLeft = inputtedValue.length - caretPosition; | ||
} else { | ||
if (!el.$ci.options.autoDecimalMode && decimalSymbolPosition !== 0 && caretPosition > decimalSymbolPosition) { | ||
if (!options.autoDecimalMode && decimalSymbolPosition !== 0 && caretPosition > decimalSymbolPosition) { | ||
if (onlyDigits(removeSuffix(inputtedValue.substr(decimalSymbolPosition), suffix)).length - 1 === decimalLength) { | ||
@@ -161,3 +166,3 @@ caretPositionFromLeft -= 1; | ||
} | ||
return el.$ci.options.hideCurrencySymbol | ||
return options.distractionFree.hideCurrencySymbol | ||
? newValue.length - caretPositionFromLeft | ||
@@ -167,13 +172,9 @@ : Math.max(newValue.length - Math.max(caretPositionFromLeft, suffix.length), prefix.length === 0 ? 0 : prefix.length + 1) | ||
}; | ||
var getCaretPositionAfterApplyingDistractionFreeFormat = function (ref, ref$1, value, caretPosition) { | ||
var prefix = ref.prefix; | ||
var groupingSymbol = ref.groupingSymbol; | ||
var hideCurrencySymbol = ref$1.hideCurrencySymbol; | ||
var hideGroupingSymbol = ref$1.hideGroupingSymbol; | ||
var getDistractionFreeCaretPosition = function (formatConfig, options, value, caretPosition) { | ||
var result = caretPosition; | ||
if (hideCurrencySymbol) { | ||
result -= prefix.length; | ||
if (options.distractionFree.hideCurrencySymbol) { | ||
result -= formatConfig.prefix.length; | ||
} | ||
if (hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), groupingSymbol); | ||
if (options.distractionFree.hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), formatConfig.groupingSymbol); | ||
} | ||
@@ -275,25 +276,21 @@ return Math.max(0, result) | ||
var max = options.max; | ||
var decimalLength = options.decimalLength; | ||
var distractionFree = options.distractionFree; | ||
var autoDecimalMode = options.autoDecimalMode; | ||
options.hideCurrencySymbol = options.currency == null || distractionFree === true || distractionFree.hideCurrencySymbol; | ||
options.hideNegligibleDecimalDigits = !autoDecimalMode && (distractionFree === true || distractionFree.hideNegligibleDecimalDigits); | ||
options.hideGroupingSymbol = distractionFree === true || distractionFree.hideGroupingSymbol; | ||
if (typeof distractionFree === 'boolean') { | ||
options.distractionFree = { | ||
hideCurrencySymbol: distractionFree, | ||
hideNegligibleDecimalDigits: distractionFree, | ||
hideGroupingSymbol: distractionFree | ||
}; | ||
} | ||
if (autoDecimalMode) { | ||
options.distractionFree.hideNegligibleDecimalDigits = false; | ||
} | ||
if (min != null && max != null && min > max) { | ||
throw new Error('Invalid value range') | ||
} | ||
if (decimalLength < 0 || decimalLength > 20) { | ||
throw new Error('Decimal length must be between 0 and 20') | ||
} | ||
var currencyFormat = createCurrencyFormat$1(options); | ||
if (currencyFormat.decimalLength > 0 && decimalLength !== undefined) { | ||
currencyFormat.decimalLength = decimalLength; | ||
} | ||
inputElement.$ci = Object.assign({}, inputElement.$ci || {}, | ||
{options: options, | ||
currencyFormat: currencyFormat, | ||
decimalFormat: Object.assign({}, currencyFormat, | ||
{prefix: '', | ||
negativePrefix: '-', | ||
suffix: ''})}); | ||
currencyFormat: currencyFormat}); | ||
return inputElement | ||
@@ -319,13 +316,17 @@ }; | ||
}; | ||
var updateInputValue = function (el, value, distractionFree) { | ||
if ( distractionFree === void 0 ) distractionFree = false; | ||
var hideCurrencySymbolOnFocus = function (el) { return el.$ci.focus && el.$ci.options.distractionFree.hideCurrencySymbol; }; | ||
var hideGroupingSymbolOnFocus = function (el) { return el.$ci.focus && el.$ci.options.distractionFree.hideGroupingSymbol; }; | ||
var hideNegligibleDecimalDigitsOnFocus = function (el) { return el.$ci.focus && el.$ci.options.distractionFree.hideNegligibleDecimalDigits; }; | ||
var updateInputValue = function (el, value) { | ||
if (value != null) { | ||
var ref = el.$ci; | ||
var options = ref.options; | ||
var decimalFormat = ref.decimalFormat; | ||
var currencyFormat = ref.currencyFormat; | ||
var focus = ref.focus; | ||
var previousConformedValue = ref.previousConformedValue; | ||
var hideCurrencySymbol = focus && options.hideCurrencySymbol; | ||
var formatConfig = hideCurrencySymbol ? decimalFormat : currencyFormat; | ||
var formatConfig = Object.assign({}, currencyFormat); | ||
if (hideCurrencySymbolOnFocus(el)) { | ||
formatConfig.prefix = ''; | ||
formatConfig.negativePrefix = '-'; | ||
formatConfig.suffix = ''; | ||
} | ||
var ref$1 = conformToMask(value, formatConfig, options, previousConformedValue); | ||
@@ -336,6 +337,4 @@ var conformedValue = ref$1.conformedValue; | ||
var formattedValue = new Intl.NumberFormat(options.locale, { | ||
useGrouping: !(focus && options.hideGroupingSymbol), | ||
minimumFractionDigits: distractionFree | ||
? (options.hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : currencyFormat.decimalLength) | ||
: fractionDigits.length | ||
useGrouping: !hideGroupingSymbolOnFocus(el), | ||
minimumFractionDigits: hideNegligibleDecimalDigitsOnFocus(el) ? fractionDigits.replace(/0+$/, '').length : Math.min(formatConfig.decimalLength, fractionDigits.length) | ||
}).format(Math.abs(conformedValue)); | ||
@@ -347,3 +346,3 @@ var isNegativeZero = conformedValue === 0 && (1 / conformedValue < 0); | ||
el.value = conformedValue; | ||
el.$ci.numberValue = parse(el.value, formatConfig); | ||
el.$ci.numberValue = parse(el.value, formatConfig, options.valueAsInteger); | ||
} | ||
@@ -357,4 +356,11 @@ } else { | ||
updateInputValue(el, value); | ||
dispatchEvent(el, 'format-complete', { numberValue: el.$ci.numberValue }); | ||
var numberValue = getNumber(el.$ci.numberValue, el.$ci.options.valueAsInteger, el.$ci.currencyFormat.decimalLength); | ||
dispatchEvent(el, 'format-complete', { numberValue: numberValue }); | ||
}; | ||
var toFloat = function (number, options, currencyFormat) { | ||
if (options.valueAsInteger) { | ||
return number / Math.pow(10, currencyFormat.decimalLength) | ||
} | ||
return number | ||
}; | ||
var addEventListener = function (el) { | ||
@@ -364,5 +370,8 @@ el.addEventListener('input', function () { | ||
var selectionStart = el.selectionStart; | ||
var el_$ci = el.$ci; | ||
var currencyFormat = el_$ci.currencyFormat; | ||
var options = el_$ci.options; | ||
format(el, value); | ||
if (el.$ci.focus) { | ||
setCaretPosition(el, getCaretPositionAfterFormat(el, value, selectionStart)); | ||
setCaretPosition(el, getCaretPositionAfterFormat(el.value, value, selectionStart, currencyFormat, options)); | ||
} | ||
@@ -372,4 +381,8 @@ }, { capture: true }); | ||
var detail = ref.detail; | ||
if (!el.$ci.focus) { | ||
applyFixedFractionFormat(el, detail.value); | ||
var ref$1 = el.$ci; | ||
var focus = ref$1.focus; | ||
var currencyFormat = ref$1.currencyFormat; | ||
var options = ref$1.options; | ||
if (!focus) { | ||
applyFixedFractionFormat(el, toFloat(detail.value, options, currencyFormat)); | ||
} | ||
@@ -379,10 +392,3 @@ }); | ||
el.$ci.focus = true; | ||
var ref = el.$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) { | ||
if (hideCurrencySymbolOnFocus(el) || hideGroupingSymbolOnFocus(el) || hideNegligibleDecimalDigitsOnFocus(el)) { | ||
setTimeout(function () { | ||
@@ -392,7 +398,7 @@ var value = el.value; | ||
var selectionEnd = el.selectionEnd; | ||
updateInputValue(el, el.value, true); | ||
updateInputValue(el, el.value); | ||
if (Math.abs(selectionStart - selectionEnd) > 0) { | ||
el.setSelectionRange(0, el.value.length); | ||
} else { | ||
setCaretPosition(el, getCaretPositionAfterApplyingDistractionFreeFormat(currencyFormat, options, value, selectionStart)); | ||
setCaretPosition(el, getDistractionFreeCaretPosition(el.$ci.currencyFormat, el.$ci.options, value, selectionStart)); | ||
} | ||
@@ -411,6 +417,10 @@ }); | ||
var context = ref$1.context; | ||
var inputElement = init(el, options, context.$CI_DEFAULT_OPTIONS || defaultOptions); | ||
var inputElement = init(el, options, context.$CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS); | ||
Vue.nextTick(function () { | ||
if (inputElement.value) { | ||
applyFixedFractionFormat(inputElement, parse(inputElement.value, inputElement.$ci.currencyFormat)); | ||
var value = inputElement.value; | ||
var inputElement_$ci = inputElement.$ci; | ||
var currencyFormat = inputElement_$ci.currencyFormat; | ||
var options = inputElement_$ci.options; | ||
if (value) { | ||
applyFixedFractionFormat(inputElement, toFloat(parse(value, currencyFormat), options, currencyFormat)); | ||
} | ||
@@ -424,4 +434,4 @@ }); | ||
var context = ref$1.context; | ||
if (!!value && Object.keys(defaultOptions).some(function (key) { return oldValue[key] !== value[key]; })) { | ||
var inputElement = init(el, value, context.$CI_DEFAULT_OPTIONS || defaultOptions); | ||
if (!!value && Object.keys(DEFAULT_OPTIONS).some(function (key) { return oldValue[key] !== value[key]; })) { | ||
var inputElement = init(el, value, context.$CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS); | ||
applyFixedFractionFormat(inputElement, inputElement.$ci.numberValue); | ||
@@ -475,2 +485,6 @@ } | ||
}, | ||
valueAsInteger: { | ||
type: Boolean, | ||
default: undefined | ||
}, | ||
min: { | ||
@@ -493,4 +507,4 @@ type: Number, | ||
var this$1 = this; | ||
var options = Object.assign({}, this.$CI_DEFAULT_OPTIONS || defaultOptions); | ||
Object.keys(defaultOptions).forEach(function (key) { | ||
var options = Object.assign({}, this.$CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS); | ||
Object.keys(DEFAULT_OPTIONS).forEach(function (key) { | ||
if (this$1[key] !== undefined) { | ||
@@ -531,10 +545,9 @@ options[key] = this$1[key]; | ||
var globalOptions = ref.globalOptions; if ( globalOptions === void 0 ) globalOptions = {}; | ||
var options = Object.assign({}, defaultOptions, globalOptions); | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = options; | ||
var defaultOptions = Object.assign({}, DEFAULT_OPTIONS, globalOptions); | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = defaultOptions; | ||
Vue.component(componentName, component); | ||
Vue.directive(directiveName, directive); | ||
Vue.prototype.$parseCurrency = function (str, locale, currency) { | ||
if ( locale === void 0 ) locale = options.locale; | ||
if ( currency === void 0 ) currency = options.currency; | ||
return parseCurrency(str, locale, currency); | ||
Vue.prototype.$parseCurrency = function (str, options) { | ||
if ( options === void 0 ) options = defaultOptions; | ||
return parseCurrency(str, options); | ||
}; | ||
@@ -555,2 +568,2 @@ } | ||
})); | ||
}))); |
{ | ||
"name": "vue-currency-input", | ||
"description": "Easy input of currency formatted numbers for Vue.js.", | ||
"version": "1.11.4", | ||
"version": "1.12.0", | ||
"license": "MIT", | ||
@@ -10,2 +10,3 @@ "unpkg": "dist/vue-currency-input.umd.js", | ||
"main": "dist/vue-currency-input.umd.js", | ||
"types": "src/index.d.ts", | ||
"files": [ | ||
@@ -42,10 +43,12 @@ "dist/*.js", | ||
"devDependencies": { | ||
"@vue/cli-plugin-babel": "^3.11.0", | ||
"@vue/cli-plugin-eslint": "^3.11.0", | ||
"@vue/cli-plugin-unit-jest": "^3.11.0", | ||
"@vue/cli-service": "^3.11.0", | ||
"@vue/cli-plugin-babel": "^4.0.5", | ||
"@vue/cli-plugin-eslint": "^4.0.5", | ||
"@vue/cli-plugin-unit-jest": "^4.0.5", | ||
"@vue/cli-service": "^4.0.5", | ||
"@vue/eslint-config-standard": "^4.0.0", | ||
"@vue/test-utils": "^1.0.0-beta.29", | ||
"babel-core": "7.0.0-bridge.0", | ||
"babel-jest": "^24.9.0", | ||
"babel-eslint": "^10.0.3", | ||
"core-js": "^3.3.2", | ||
"eslint": "^5.16.0", | ||
"eslint-plugin-vue": "^5.0.0", | ||
"intl": "^1.2.5", | ||
@@ -76,3 +79,14 @@ "node-sass": "^4.12.0", | ||
"parser": "babel-eslint" | ||
} | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": [ | ||
"**/__tests__/*.{j,t}s?(x)", | ||
"**/tests/unit/**/*.spec.{j,t}s?(x)" | ||
], | ||
"env": { | ||
"jest": true | ||
} | ||
} | ||
] | ||
}, | ||
@@ -86,28 +100,7 @@ "postcss": { | ||
"> 1%", | ||
"last 2 versions", | ||
"not ie <= 8" | ||
"last 2 versions" | ||
], | ||
"jest": { | ||
"moduleFileExtensions": [ | ||
"js", | ||
"jsx", | ||
"json", | ||
"vue" | ||
], | ||
"transform": { | ||
"^.+\\.vue$": "vue-jest", | ||
".+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$": "jest-transform-stub", | ||
"^.+\\.jsx?$": "babel-jest" | ||
}, | ||
"moduleNameMapper": { | ||
"^@/(.*)$": "<rootDir>/src/$1" | ||
}, | ||
"snapshotSerializers": [ | ||
"jest-serializer-vue" | ||
], | ||
"testMatch": [ | ||
"**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)" | ||
], | ||
"testURL": "http://localhost/" | ||
"preset": "@vue/cli-plugin-unit-jest" | ||
} | ||
} |
@@ -6,7 +6,7 @@ import createCurrencyFormat from './utils/createCurrencyFormat' | ||
* Parses a number from a currency formatted string. | ||
* | ||
* @param {String} formattedValue The currency formatted string to be parsed, for example `$1,234.50`. | ||
* @param {String} locale A [BCP 47](https://tools.ietf.org/html/bcp47) language tag (for example `en` or `de-DE`). | ||
* @param {String | Object} currency A [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) currency code (for example `USD` or `EUR`), an object `{prefix, suffix}` or `null`. | ||
* @param {Object} options The configured options of the respective `v-currency` directive. | ||
* @returns {number | null} The parsed number or `null` if the formatted string does not match. | ||
*/ | ||
export const parseCurrency = (formattedValue, locale, currency) => parse(formattedValue, createCurrencyFormat({ locale, currency })) | ||
export const parseCurrency = (formattedValue, options) => parse(formattedValue, createCurrencyFormat(options), options.valueAsInteger) |
@@ -47,2 +47,6 @@ import defaultOptions from './defaultOptions' | ||
}, | ||
valueAsInteger: { | ||
type: Boolean, | ||
default: undefined | ||
}, | ||
min: { | ||
@@ -49,0 +53,0 @@ type: Number, |
export default { | ||
locale: undefined, | ||
currency: 'EUR', | ||
valueAsInteger: false, | ||
distractionFree: true, | ||
@@ -5,0 +6,0 @@ decimalLength: undefined, |
import Vue from 'vue' | ||
import defaultOptions from './defaultOptions' | ||
import { getCaretPositionAfterApplyingDistractionFreeFormat, getCaretPositionAfterFormat, setCaretPosition } from './utils/caretPosition' | ||
import { getCaretPositionAfterFormat, getDistractionFreeCaretPosition, setCaretPosition } from './utils/caretPosition' | ||
import conformToMask from './utils/conformToMask' | ||
import createCurrencyFormat from './utils/createCurrencyFormat' | ||
import dispatchEvent from './utils/dispatchEvent' | ||
import parse from './utils/parse' | ||
import parse, { getNumber } from './utils/parse' | ||
@@ -15,26 +15,21 @@ const init = (el, optionsFromBinding, defaultOptions) => { | ||
const options = { ...defaultOptions, ...optionsFromBinding } | ||
const { min, max, decimalLength, distractionFree, autoDecimalMode } = options | ||
options.hideCurrencySymbol = options.currency == null || distractionFree === true || distractionFree.hideCurrencySymbol | ||
options.hideNegligibleDecimalDigits = !autoDecimalMode && (distractionFree === true || distractionFree.hideNegligibleDecimalDigits) | ||
options.hideGroupingSymbol = distractionFree === true || distractionFree.hideGroupingSymbol | ||
const { min, max, distractionFree, autoDecimalMode } = options | ||
if (typeof distractionFree === 'boolean') { | ||
options.distractionFree = { | ||
hideCurrencySymbol: distractionFree, | ||
hideNegligibleDecimalDigits: distractionFree, | ||
hideGroupingSymbol: distractionFree | ||
} | ||
} | ||
if (autoDecimalMode) { | ||
options.distractionFree.hideNegligibleDecimalDigits = false | ||
} | ||
if (min != null && max != null && min > max) { | ||
throw new Error('Invalid value range') | ||
} | ||
if (decimalLength < 0 || decimalLength > 20) { | ||
throw new Error('Decimal length must be between 0 and 20') | ||
} | ||
const currencyFormat = createCurrencyFormat(options) | ||
if (currencyFormat.decimalLength > 0 && decimalLength !== undefined) { | ||
currencyFormat.decimalLength = decimalLength | ||
} | ||
inputElement.$ci = { | ||
...inputElement.$ci || {}, | ||
options, | ||
currencyFormat, | ||
decimalFormat: { | ||
...currencyFormat, | ||
prefix: '', | ||
negativePrefix: '-', | ||
suffix: '' | ||
} | ||
currencyFormat | ||
} | ||
@@ -58,14 +53,22 @@ return inputElement | ||
const updateInputValue = (el, value, distractionFree = false) => { | ||
const hideCurrencySymbolOnFocus = el => el.$ci.focus && el.$ci.options.distractionFree.hideCurrencySymbol | ||
const hideGroupingSymbolOnFocus = el => el.$ci.focus && el.$ci.options.distractionFree.hideGroupingSymbol | ||
const hideNegligibleDecimalDigitsOnFocus = el => el.$ci.focus && el.$ci.options.distractionFree.hideNegligibleDecimalDigits | ||
const updateInputValue = (el, value) => { | ||
if (value != null) { | ||
const { options, decimalFormat, currencyFormat, focus, previousConformedValue } = el.$ci | ||
const hideCurrencySymbol = focus && options.hideCurrencySymbol | ||
const formatConfig = hideCurrencySymbol ? decimalFormat : currencyFormat | ||
const { options, currencyFormat, previousConformedValue } = el.$ci | ||
const formatConfig = { ...currencyFormat } | ||
if (hideCurrencySymbolOnFocus(el)) { | ||
formatConfig.prefix = '' | ||
formatConfig.negativePrefix = '-' | ||
formatConfig.suffix = '' | ||
} | ||
const { conformedValue, fractionDigits } = conformToMask(value, formatConfig, options, previousConformedValue) | ||
if (typeof conformedValue === 'number') { | ||
const formattedValue = new Intl.NumberFormat(options.locale, { | ||
useGrouping: !(focus && options.hideGroupingSymbol), | ||
minimumFractionDigits: distractionFree | ||
? (options.hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : currencyFormat.decimalLength) | ||
: fractionDigits.length | ||
useGrouping: !hideGroupingSymbolOnFocus(el), | ||
minimumFractionDigits: hideNegligibleDecimalDigitsOnFocus(el) ? fractionDigits.replace(/0+$/, '').length : Math.min(formatConfig.decimalLength, fractionDigits.length) | ||
}).format(Math.abs(conformedValue)) | ||
@@ -77,3 +80,3 @@ const isNegativeZero = conformedValue === 0 && (1 / conformedValue < 0) | ||
el.value = conformedValue | ||
el.$ci.numberValue = parse(el.value, formatConfig) | ||
el.$ci.numberValue = parse(el.value, formatConfig, options.valueAsInteger) | ||
} | ||
@@ -88,11 +91,19 @@ } else { | ||
updateInputValue(el, value) | ||
dispatchEvent(el, 'format-complete', { numberValue: el.$ci.numberValue }) | ||
const numberValue = getNumber(el.$ci.numberValue, el.$ci.options.valueAsInteger, el.$ci.currencyFormat.decimalLength) | ||
dispatchEvent(el, 'format-complete', { numberValue }) | ||
} | ||
const toFloat = (number, options, currencyFormat) => { | ||
if (options.valueAsInteger) { | ||
return number / Math.pow(10, currencyFormat.decimalLength) | ||
} | ||
return number | ||
} | ||
const addEventListener = (el) => { | ||
el.addEventListener('input', () => { | ||
const { value, selectionStart } = el | ||
const { value, selectionStart, $ci: { currencyFormat, options } } = el | ||
format(el, value) | ||
if (el.$ci.focus) { | ||
setCaretPosition(el, getCaretPositionAfterFormat(el, value, selectionStart)) | ||
setCaretPosition(el, getCaretPositionAfterFormat(el.value, value, selectionStart, currencyFormat, options)) | ||
} | ||
@@ -102,4 +113,5 @@ }, { capture: true }) | ||
el.addEventListener('format', ({ detail }) => { | ||
if (!el.$ci.focus) { | ||
applyFixedFractionFormat(el, detail.value) | ||
const { focus, currencyFormat, options } = el.$ci | ||
if (!focus) { | ||
applyFixedFractionFormat(el, toFloat(detail.value, options, currencyFormat)) | ||
} | ||
@@ -110,12 +122,10 @@ }) | ||
el.$ci.focus = true | ||
const { currencyFormat, options } = el.$ci | ||
const { distractionFree, hideCurrencySymbol, hideGroupingSymbol, hideNegligibleDecimalDigits } = options | ||
if (distractionFree === true || hideCurrencySymbol || hideGroupingSymbol || hideNegligibleDecimalDigits) { | ||
if (hideCurrencySymbolOnFocus(el) || hideGroupingSymbolOnFocus(el) || hideNegligibleDecimalDigitsOnFocus(el)) { | ||
setTimeout(() => { | ||
const { value, selectionStart, selectionEnd } = el | ||
updateInputValue(el, el.value, true) | ||
updateInputValue(el, el.value) | ||
if (Math.abs(selectionStart - selectionEnd) > 0) { | ||
el.setSelectionRange(0, el.value.length) | ||
} else { | ||
setCaretPosition(el, getCaretPositionAfterApplyingDistractionFreeFormat(currencyFormat, options, value, selectionStart)) | ||
setCaretPosition(el, getDistractionFreeCaretPosition(el.$ci.currencyFormat, el.$ci.options, value, selectionStart)) | ||
} | ||
@@ -136,4 +146,5 @@ }) | ||
Vue.nextTick(() => { | ||
if (inputElement.value) { | ||
applyFixedFractionFormat(inputElement, parse(inputElement.value, inputElement.$ci.currencyFormat)) | ||
const { value, $ci: { currencyFormat, options } } = inputElement | ||
if (value) { | ||
applyFixedFractionFormat(inputElement, toFloat(parse(value, currencyFormat), options, currencyFormat)) | ||
} | ||
@@ -140,0 +151,0 @@ }) |
import { parseCurrency } from './api' | ||
import component from './component' | ||
import defaultOptions from './defaultOptions' | ||
import DEFAULT_OPTIONS from './defaultOptions' | ||
import directive from './directive' | ||
@@ -12,8 +12,8 @@ | ||
} = {}) { | ||
const options = { ...defaultOptions, ...globalOptions } | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = options | ||
const defaultOptions = { ...DEFAULT_OPTIONS, ...globalOptions } | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = defaultOptions | ||
Vue.component(componentName, component) | ||
Vue.directive(directiveName, directive) | ||
Vue.prototype.$parseCurrency = (str, locale = options.locale, currency = options.currency) => parseCurrency(str, locale, currency) | ||
Vue.prototype.$parseCurrency = (str, options = defaultOptions) => parseCurrency(str, options) | ||
} | ||
} |
@@ -5,5 +5,4 @@ import { count, onlyDigits, removeSuffix } from './formatHelper' | ||
export const getCaretPositionAfterFormat = (el, inputtedValue, caretPosition) => { | ||
const { prefix, suffix, decimalSymbol, decimalLength, groupingSymbol } = el.$ci.currencyFormat | ||
const newValue = el.value | ||
export const getCaretPositionAfterFormat = (newValue, inputtedValue, caretPosition, currencyFormat, options) => { | ||
const { prefix, suffix, decimalSymbol, decimalLength, groupingSymbol } = currencyFormat | ||
const decimalSymbolPosition = inputtedValue.indexOf(decimalSymbol) + 1 | ||
@@ -17,3 +16,3 @@ let caretPositionFromLeft = inputtedValue.length - caretPosition | ||
} else { | ||
if (!el.$ci.options.autoDecimalMode && decimalSymbolPosition !== 0 && caretPosition > decimalSymbolPosition) { | ||
if (!options.autoDecimalMode && decimalSymbolPosition !== 0 && caretPosition > decimalSymbolPosition) { | ||
if (onlyDigits(removeSuffix(inputtedValue.substr(decimalSymbolPosition), suffix)).length - 1 === decimalLength) { | ||
@@ -23,3 +22,3 @@ caretPositionFromLeft -= 1 | ||
} | ||
return el.$ci.options.hideCurrencySymbol | ||
return options.distractionFree.hideCurrencySymbol | ||
? newValue.length - caretPositionFromLeft | ||
@@ -30,11 +29,11 @@ : Math.max(newValue.length - Math.max(caretPositionFromLeft, suffix.length), prefix.length === 0 ? 0 : prefix.length + 1) | ||
export const getCaretPositionAfterApplyingDistractionFreeFormat = ({ prefix, groupingSymbol }, { hideCurrencySymbol, hideGroupingSymbol }, value, caretPosition) => { | ||
export const getDistractionFreeCaretPosition = (formatConfig, options, value, caretPosition) => { | ||
let result = caretPosition | ||
if (hideCurrencySymbol) { | ||
result -= prefix.length | ||
if (options.distractionFree.hideCurrencySymbol) { | ||
result -= formatConfig.prefix.length | ||
} | ||
if (hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), groupingSymbol) | ||
if (options.distractionFree.hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), formatConfig.groupingSymbol) | ||
} | ||
return Math.max(0, result) | ||
} |
@@ -21,8 +21,12 @@ const createCurrencyFormat = (numberFormat) => { | ||
export default ({ locale, currency }) => { | ||
export default ({ locale, currency, decimalLength }) => { | ||
let minimumFractionDigits = 2 | ||
if (decimalLength !== undefined) { | ||
minimumFractionDigits = decimalLength | ||
} | ||
if (currency == null) { | ||
return createCurrencyFormat(new Intl.NumberFormat(locale, { minimumFractionDigits: 2 })) | ||
return createCurrencyFormat(new Intl.NumberFormat(locale, { minimumFractionDigits })) | ||
} else if (typeof currency === 'object') { | ||
return { | ||
...createCurrencyFormat(new Intl.NumberFormat(locale, { minimumFractionDigits: 2 })), | ||
...createCurrencyFormat(new Intl.NumberFormat(locale, { minimumFractionDigits })), | ||
prefix: currency.prefix || '', | ||
@@ -33,4 +37,8 @@ negativePrefix: `-${currency.prefix || ''}`, | ||
} else { | ||
return createCurrencyFormat(new Intl.NumberFormat(locale, { style: 'currency', currency })) | ||
const currencyFormat = createCurrencyFormat(new Intl.NumberFormat(locale, { style: 'currency', currency })) | ||
if (currencyFormat.decimalLength > 0 && decimalLength !== undefined) { | ||
currencyFormat.decimalLength = decimalLength | ||
} | ||
return currencyFormat | ||
} | ||
} |
import { isNumber, stripCurrencySymbolAndMinusSign } from './formatHelper' | ||
export default (str, { prefix, suffix, groupingSymbol, decimalSymbol } = {}) => { | ||
if (typeof str === 'number') { | ||
return str | ||
} else if (str && typeof str === 'string') { | ||
export const getNumber = (number, valueAsInteger, decimalLength) => { | ||
return number != null && valueAsInteger ? Number(number.toFixed(decimalLength).split('.').join('')) : number | ||
} | ||
export default (str, currencyFormat, valueAsInteger = false) => { | ||
if (typeof str === 'string') { | ||
if (isNumber(str)) { | ||
return Number(str) | ||
return getNumber(Number(str), valueAsInteger, currencyFormat.decimalLength) | ||
} | ||
let { value, negative } = stripCurrencySymbolAndMinusSign(str, { prefix, suffix }) | ||
const numberParts = value.split(decimalSymbol) | ||
let { value, negative } = stripCurrencySymbolAndMinusSign(str, currencyFormat) | ||
const numberParts = value.split(currencyFormat.decimalSymbol) | ||
if (numberParts.length > 2) { | ||
return null | ||
} | ||
const integer = numberParts[0].replace(new RegExp(`\\${groupingSymbol}`, 'g'), '') | ||
const integer = numberParts[0].replace(new RegExp(`\\${currencyFormat.groupingSymbol}`, 'g'), '') | ||
if (integer.length && !integer.match(/^\d+$/g)) { | ||
@@ -31,3 +33,3 @@ return null | ||
} | ||
return Number(number) | ||
return getNumber(Number(number), valueAsInteger, currencyFormat.decimalLength) | ||
} | ||
@@ -34,0 +36,0 @@ } |
66880
21
1588
22