vue-currency-input
Advanced tools
Comparing version 1.7.0 to 1.7.1
/** | ||
* Vue Currency Input 1.7.0 | ||
* Vue Currency Input 1.7.1 | ||
* (c) 2019 Matthias Stiller | ||
@@ -18,2 +18,25 @@ * @license MIT | ||
function createCurrencyFormat (ref) { | ||
var locale = ref.locale; | ||
var currency = ref.currency; | ||
var min = ref.min; | ||
var str = new Intl.NumberFormat(locale, { style: 'currency', currency: currency }).format(1234); | ||
var decimalLimit = (str.match(/0/g) || []).length; | ||
var decimalSymbol = decimalLimit > 0 ? str.substr(str.indexOf('4') + 1, 1) : null; | ||
var allowDecimal = decimalLimit !== 0; | ||
var prefix = str.substring(0, str.indexOf('1')); | ||
var suffix = str.substring(str.lastIndexOf(decimalLimit > 0 ? '0' : '4') + 1); | ||
var thousandsSeparatorSymbol = str.substr(str.indexOf('1') + 1, 1); | ||
var allowNegative = min == null || min < 0; | ||
return { | ||
prefix: prefix, | ||
suffix: suffix, | ||
thousandsSeparatorSymbol: thousandsSeparatorSymbol, | ||
decimalSymbol: decimalSymbol, | ||
decimalLimit: decimalLimit, | ||
allowDecimal: allowDecimal, | ||
allowNegative: allowNegative | ||
} | ||
} | ||
var onlyDigits = function (str) { return str.replace(/\D+/g, ''); }; | ||
@@ -136,21 +159,2 @@ var endsWith = function (str, search) { | ||
function currencyFormatConfig (locale, currency) { | ||
var numberFormat = new Intl.NumberFormat(locale, { style: 'currency', currency: currency }); | ||
var formattedNumber = numberFormat.format(1234); | ||
var decimalLimit = (formattedNumber.match(/0/g) || []).length; | ||
var decimalSymbol = decimalLimit > 0 ? formattedNumber.substr(formattedNumber.indexOf('4') + 1, 1) : null; | ||
var allowDecimal = decimalSymbol !== null; | ||
var prefix = formattedNumber.substring(0, formattedNumber.indexOf('1')); | ||
var suffix = formattedNumber.substring(formattedNumber.lastIndexOf(decimalLimit > 0 ? '0' : '4') + 1); | ||
var thousandsSeparatorSymbol = formattedNumber.substr(formattedNumber.indexOf('1') + 1, 1); | ||
return { | ||
prefix: prefix, | ||
suffix: suffix, | ||
thousandsSeparatorSymbol: thousandsSeparatorSymbol, | ||
decimalSymbol: decimalSymbol, | ||
decimalLimit: decimalLimit, | ||
allowDecimal: allowDecimal | ||
} | ||
} | ||
function dispatchEvent (el, eventName, data) { | ||
@@ -186,3 +190,3 @@ var event = document.createEvent('CustomEvent'); | ||
var options = ref.options; | ||
var currencyFormatConfig = ref.currencyFormatConfig; | ||
var currencyFormat = ref.currencyFormat; | ||
inputElement.$ci.focus = true; | ||
@@ -192,3 +196,3 @@ if (options.distractionFree) { | ||
var caretPosition = getCaretPosition(inputElement); | ||
format(inputElement, parse(inputElement.value, currencyFormatConfig)); | ||
format(inputElement, parse(inputElement.value, currencyFormat)); | ||
inputElement.setSelectionRange(caretPosition, caretPosition); | ||
@@ -227,3 +231,3 @@ }, 0); | ||
{options: options, | ||
currencyFormatConfig: currencyFormatConfig(options.locale, options.currency), | ||
currencyFormat: createCurrencyFormat(options), | ||
textMaskInputElement: createTextMaskInputElement({ inputElement: inputElement, mask: [] })}); | ||
@@ -233,4 +237,4 @@ return inputElement | ||
var applyFixedFractionFormat = function (el, value) { | ||
if ( value === void 0 ) value = parse(el.value, el.$ci.currencyFormatConfig); | ||
if (value != null && !el.$ci.currencyFormatConfig.allowDecimal) { | ||
if ( value === void 0 ) value = parse(el.value, el.$ci.currencyFormat); | ||
if (value != null && !el.$ci.currencyFormat.allowDecimal) { | ||
value = Math.round(value); | ||
@@ -245,3 +249,3 @@ } | ||
var options = ref.options; | ||
var currencyFormatConfig = ref.currencyFormatConfig; | ||
var currencyFormat = ref.currencyFormat; | ||
var textMaskInputElement = ref.textMaskInputElement; | ||
@@ -259,3 +263,3 @@ var focus = ref.focus; | ||
} | ||
value = new Intl.NumberFormat(options.locale, { minimumFractionDigits: hideNegligibleFractionDigits ? 0 : currencyFormatConfig.decimalLimit }).format(value); | ||
value = new Intl.NumberFormat(options.locale, { minimumFractionDigits: hideNegligibleFractionDigits ? 0 : currencyFormat.decimalLimit }).format(value); | ||
if (options.distractionFree) { | ||
@@ -270,6 +274,6 @@ value += ' '; | ||
if (options.validateOnInput) { | ||
if (options.min != null && parse(conformedValue, currencyFormatConfig) < options.min) { | ||
if (options.min != null && parse(conformedValue, currencyFormat) < options.min) { | ||
return previousConformedValue | ||
} | ||
if (options.max != null && parse(conformedValue, currencyFormatConfig) > options.max) { | ||
if (options.max != null && parse(conformedValue, currencyFormat) > options.max) { | ||
return previousConformedValue | ||
@@ -280,9 +284,8 @@ } | ||
}, | ||
mask: createCurrencyMask(Object.assign({}, currencyFormatConfig, | ||
{prefix: hideCurrencySymbol ? '' : currencyFormatConfig.prefix, | ||
suffix: hideCurrencySymbol ? '' : currencyFormatConfig.suffix, | ||
thousandsSeparatorSymbol: hideThousandsSeparatorSymbol ? '' : currencyFormatConfig.thousandsSeparatorSymbol, | ||
allowNegative: (options.min === null && options.max === null) || options.min < 0 || options.max < 0})) | ||
mask: createCurrencyMask(Object.assign({}, currencyFormat, | ||
{prefix: hideCurrencySymbol ? '' : currencyFormat.prefix, | ||
suffix: hideCurrencySymbol ? '' : currencyFormat.suffix, | ||
thousandsSeparatorSymbol: hideThousandsSeparatorSymbol ? '' : currencyFormat.thousandsSeparatorSymbol})) | ||
}); | ||
var numberValue = parse(el.value, currencyFormatConfig); | ||
var numberValue = parse(el.value, currencyFormat); | ||
el.$ci.numberValue = numberValue; | ||
@@ -292,3 +295,3 @@ dispatchEvent(el, 'format-complete', { numberValue: numberValue }); | ||
var getCaretPosition = function (el) { | ||
var ref = el.$ci.currencyFormatConfig; | ||
var ref = el.$ci.currencyFormat; | ||
var prefix = ref.prefix; | ||
@@ -411,3 +414,3 @@ var thousandsSeparatorSymbol = ref.thousandsSeparatorSymbol; | ||
if ( currency === void 0 ) currency = options.currency; | ||
return parse(str, currencyFormatConfig(locale, currency)); | ||
return parse(str, createCurrencyFormat(Object.assign({}, options, {locale: locale, currency: currency}))) | ||
}; | ||
@@ -414,0 +417,0 @@ } |
/** | ||
* Vue Currency Input 1.7.0 | ||
* Vue Currency Input 1.7.1 | ||
* (c) 2019 Matthias Stiller | ||
@@ -42,2 +42,25 @@ * @license MIT | ||
function createCurrencyFormat (ref) { | ||
var locale = ref.locale; | ||
var currency = ref.currency; | ||
var min = ref.min; | ||
var str = new Intl.NumberFormat(locale, { style: 'currency', currency: currency }).format(1234); | ||
var decimalLimit = (str.match(/0/g) || []).length; | ||
var decimalSymbol = decimalLimit > 0 ? str.substr(str.indexOf('4') + 1, 1) : null; | ||
var allowDecimal = decimalLimit !== 0; | ||
var prefix = str.substring(0, str.indexOf('1')); | ||
var suffix = str.substring(str.lastIndexOf(decimalLimit > 0 ? '0' : '4') + 1); | ||
var thousandsSeparatorSymbol = str.substr(str.indexOf('1') + 1, 1); | ||
var allowNegative = min == null || min < 0; | ||
return { | ||
prefix: prefix, | ||
suffix: suffix, | ||
thousandsSeparatorSymbol: thousandsSeparatorSymbol, | ||
decimalSymbol: decimalSymbol, | ||
decimalLimit: decimalLimit, | ||
allowDecimal: allowDecimal, | ||
allowNegative: allowNegative | ||
} | ||
} | ||
var onlyDigits = function (str) { return str.replace(/\D+/g, ''); }; | ||
@@ -160,21 +183,2 @@ var endsWith = function (str, search) { | ||
function currencyFormatConfig (locale, currency) { | ||
var numberFormat = new Intl.NumberFormat(locale, { style: 'currency', currency: currency }); | ||
var formattedNumber = numberFormat.format(1234); | ||
var decimalLimit = (formattedNumber.match(/0/g) || []).length; | ||
var decimalSymbol = decimalLimit > 0 ? formattedNumber.substr(formattedNumber.indexOf('4') + 1, 1) : null; | ||
var allowDecimal = decimalSymbol !== null; | ||
var prefix = formattedNumber.substring(0, formattedNumber.indexOf('1')); | ||
var suffix = formattedNumber.substring(formattedNumber.lastIndexOf(decimalLimit > 0 ? '0' : '4') + 1); | ||
var thousandsSeparatorSymbol = formattedNumber.substr(formattedNumber.indexOf('1') + 1, 1); | ||
return { | ||
prefix: prefix, | ||
suffix: suffix, | ||
thousandsSeparatorSymbol: thousandsSeparatorSymbol, | ||
decimalSymbol: decimalSymbol, | ||
decimalLimit: decimalLimit, | ||
allowDecimal: allowDecimal | ||
} | ||
} | ||
function dispatchEvent (el, eventName, data) { | ||
@@ -210,3 +214,3 @@ var event = document.createEvent('CustomEvent'); | ||
var options = ref.options; | ||
var currencyFormatConfig = ref.currencyFormatConfig; | ||
var currencyFormat = ref.currencyFormat; | ||
inputElement.$ci.focus = true; | ||
@@ -216,3 +220,3 @@ if (options.distractionFree) { | ||
var caretPosition = getCaretPosition(inputElement); | ||
format(inputElement, parse(inputElement.value, currencyFormatConfig)); | ||
format(inputElement, parse(inputElement.value, currencyFormat)); | ||
inputElement.setSelectionRange(caretPosition, caretPosition); | ||
@@ -251,3 +255,3 @@ }, 0); | ||
{options: options, | ||
currencyFormatConfig: currencyFormatConfig(options.locale, options.currency), | ||
currencyFormat: createCurrencyFormat(options), | ||
textMaskInputElement: textMaskCore_1({ inputElement: inputElement, mask: [] })}); | ||
@@ -257,4 +261,4 @@ return inputElement | ||
var applyFixedFractionFormat = function (el, value) { | ||
if ( value === void 0 ) value = parse(el.value, el.$ci.currencyFormatConfig); | ||
if (value != null && !el.$ci.currencyFormatConfig.allowDecimal) { | ||
if ( value === void 0 ) value = parse(el.value, el.$ci.currencyFormat); | ||
if (value != null && !el.$ci.currencyFormat.allowDecimal) { | ||
value = Math.round(value); | ||
@@ -269,3 +273,3 @@ } | ||
var options = ref.options; | ||
var currencyFormatConfig = ref.currencyFormatConfig; | ||
var currencyFormat = ref.currencyFormat; | ||
var textMaskInputElement = ref.textMaskInputElement; | ||
@@ -283,3 +287,3 @@ var focus = ref.focus; | ||
} | ||
value = new Intl.NumberFormat(options.locale, { minimumFractionDigits: hideNegligibleFractionDigits ? 0 : currencyFormatConfig.decimalLimit }).format(value); | ||
value = new Intl.NumberFormat(options.locale, { minimumFractionDigits: hideNegligibleFractionDigits ? 0 : currencyFormat.decimalLimit }).format(value); | ||
if (options.distractionFree) { | ||
@@ -294,6 +298,6 @@ value += ' '; | ||
if (options.validateOnInput) { | ||
if (options.min != null && parse(conformedValue, currencyFormatConfig) < options.min) { | ||
if (options.min != null && parse(conformedValue, currencyFormat) < options.min) { | ||
return previousConformedValue | ||
} | ||
if (options.max != null && parse(conformedValue, currencyFormatConfig) > options.max) { | ||
if (options.max != null && parse(conformedValue, currencyFormat) > options.max) { | ||
return previousConformedValue | ||
@@ -304,9 +308,8 @@ } | ||
}, | ||
mask: createCurrencyMask(Object.assign({}, currencyFormatConfig, | ||
{prefix: hideCurrencySymbol ? '' : currencyFormatConfig.prefix, | ||
suffix: hideCurrencySymbol ? '' : currencyFormatConfig.suffix, | ||
thousandsSeparatorSymbol: hideThousandsSeparatorSymbol ? '' : currencyFormatConfig.thousandsSeparatorSymbol, | ||
allowNegative: (options.min === null && options.max === null) || options.min < 0 || options.max < 0})) | ||
mask: createCurrencyMask(Object.assign({}, currencyFormat, | ||
{prefix: hideCurrencySymbol ? '' : currencyFormat.prefix, | ||
suffix: hideCurrencySymbol ? '' : currencyFormat.suffix, | ||
thousandsSeparatorSymbol: hideThousandsSeparatorSymbol ? '' : currencyFormat.thousandsSeparatorSymbol})) | ||
}); | ||
var numberValue = parse(el.value, currencyFormatConfig); | ||
var numberValue = parse(el.value, currencyFormat); | ||
el.$ci.numberValue = numberValue; | ||
@@ -316,3 +319,3 @@ dispatchEvent(el, 'format-complete', { numberValue: numberValue }); | ||
var getCaretPosition = function (el) { | ||
var ref = el.$ci.currencyFormatConfig; | ||
var ref = el.$ci.currencyFormat; | ||
var prefix = ref.prefix; | ||
@@ -435,3 +438,3 @@ var thousandsSeparatorSymbol = ref.thousandsSeparatorSymbol; | ||
if ( currency === void 0 ) currency = options.currency; | ||
return parse(str, currencyFormatConfig(locale, currency)); | ||
return parse(str, createCurrencyFormat(Object.assign({}, options, {locale: locale, currency: currency}))) | ||
}; | ||
@@ -438,0 +441,0 @@ } |
{ | ||
"name": "vue-currency-input", | ||
"description": "Easy input of currency formatted numbers for Vue.js.", | ||
"version": "1.7.0", | ||
"version": "1.7.1", | ||
"license": "MIT", | ||
@@ -39,3 +39,3 @@ "unpkg": "dist/vue-currency-input.umd.js", | ||
"peerDependencies": { | ||
"vue": "2.5" | ||
"vue": "^2.5" | ||
}, | ||
@@ -42,0 +42,0 @@ "devDependencies": { |
import { createTextMaskInputElement } from 'text-mask-core' | ||
import Vue from 'vue' | ||
import defaultOptions from './defaultOptions' | ||
import createCurrencyFormat from './utils/createCurrencyFormat' | ||
import createCurrencyMask from './utils/createCurrencyMask' | ||
import currencyFormatConfig from './utils/currencyFormatConfig' | ||
import dispatchEvent from './utils/dispatchEvent' | ||
@@ -30,3 +30,3 @@ import elementMatches from './utils/elementMatches' | ||
inputElement.addEventListener('focus', () => { | ||
const { options, currencyFormatConfig } = inputElement.$ci | ||
const { options, currencyFormat } = inputElement.$ci | ||
inputElement.$ci.focus = true | ||
@@ -36,3 +36,3 @@ if (options.distractionFree) { | ||
const caretPosition = getCaretPosition(inputElement) | ||
format(inputElement, parse(inputElement.value, currencyFormatConfig)) | ||
format(inputElement, parse(inputElement.value, currencyFormat)) | ||
inputElement.setSelectionRange(caretPosition, caretPosition) | ||
@@ -72,3 +72,3 @@ }, 0) | ||
options, | ||
currencyFormatConfig: currencyFormatConfig(options.locale, options.currency), | ||
currencyFormat: createCurrencyFormat(options), | ||
textMaskInputElement: createTextMaskInputElement({ inputElement, mask: [] }) | ||
@@ -79,4 +79,4 @@ } | ||
const applyFixedFractionFormat = (el, value = parse(el.value, el.$ci.currencyFormatConfig)) => { | ||
if (value != null && !el.$ci.currencyFormatConfig.allowDecimal) { | ||
const applyFixedFractionFormat = (el, value = parse(el.value, el.$ci.currencyFormat)) => { | ||
if (value != null && !el.$ci.currencyFormat.allowDecimal) { | ||
value = Math.round(value) | ||
@@ -88,3 +88,3 @@ } | ||
const format = (el, value = el.value, { options, currencyFormatConfig, textMaskInputElement, focus } = el.$ci) => { | ||
const format = (el, value = el.value, { options, currencyFormat, textMaskInputElement, focus } = el.$ci) => { | ||
const hideNegligibleFractionDigits = focus && (options.distractionFree === true || options.distractionFree.hideNegligibleFractionDigits) | ||
@@ -100,3 +100,3 @@ const hideCurrencySymbol = focus && (options.distractionFree === true || options.distractionFree.hideCurrencySymbol) | ||
} | ||
value = new Intl.NumberFormat(options.locale, { minimumFractionDigits: hideNegligibleFractionDigits ? 0 : currencyFormatConfig.decimalLimit }).format(value) | ||
value = new Intl.NumberFormat(options.locale, { minimumFractionDigits: hideNegligibleFractionDigits ? 0 : currencyFormat.decimalLimit }).format(value) | ||
if (options.distractionFree) { | ||
@@ -111,6 +111,6 @@ // force invalidation of text mask's previousConformedValue | ||
if (options.validateOnInput) { | ||
if (options.min != null && parse(conformedValue, currencyFormatConfig) < options.min) { | ||
if (options.min != null && parse(conformedValue, currencyFormat) < options.min) { | ||
return previousConformedValue | ||
} | ||
if (options.max != null && parse(conformedValue, currencyFormatConfig) > options.max) { | ||
if (options.max != null && parse(conformedValue, currencyFormat) > options.max) { | ||
return previousConformedValue | ||
@@ -122,10 +122,9 @@ } | ||
mask: createCurrencyMask({ | ||
...currencyFormatConfig, | ||
prefix: hideCurrencySymbol ? '' : currencyFormatConfig.prefix, | ||
suffix: hideCurrencySymbol ? '' : currencyFormatConfig.suffix, | ||
thousandsSeparatorSymbol: hideThousandsSeparatorSymbol ? '' : currencyFormatConfig.thousandsSeparatorSymbol, | ||
allowNegative: (options.min === null && options.max === null) || options.min < 0 || options.max < 0 | ||
...currencyFormat, | ||
prefix: hideCurrencySymbol ? '' : currencyFormat.prefix, | ||
suffix: hideCurrencySymbol ? '' : currencyFormat.suffix, | ||
thousandsSeparatorSymbol: hideThousandsSeparatorSymbol ? '' : currencyFormat.thousandsSeparatorSymbol | ||
}) | ||
}) | ||
const numberValue = parse(el.value, currencyFormatConfig) | ||
const numberValue = parse(el.value, currencyFormat) | ||
el.$ci.numberValue = numberValue | ||
@@ -136,3 +135,3 @@ dispatchEvent(el, 'format-complete', { numberValue }) | ||
const getCaretPosition = (el) => { | ||
const { prefix, thousandsSeparatorSymbol } = el.$ci.currencyFormatConfig | ||
const { prefix, thousandsSeparatorSymbol } = el.$ci.currencyFormat | ||
const { distractionFree } = el.$ci.options | ||
@@ -139,0 +138,0 @@ const hideCurrencySymbol = distractionFree === true || distractionFree.hideCurrencySymbol |
import component from './component' | ||
import defaultOptions from './defaultOptions' | ||
import directive from './directive' | ||
import currencyFormatConfig from './utils/currencyFormatConfig' | ||
import createCurrencyFormat from './utils/createCurrencyFormat' | ||
import { parse } from './utils/formatHelper' | ||
@@ -17,3 +17,5 @@ | ||
Vue.directive(directiveName, directive) | ||
Vue.prototype.$parseCurrency = (str, locale = options.locale, currency = options.currency) => parse(str, currencyFormatConfig(locale, currency)) | ||
Vue.prototype.$parseCurrency = (str, locale = options.locale, currency = options.currency) => { | ||
return parse(str, createCurrencyFormat({ ...options, locale, currency })) | ||
} | ||
} | ||
@@ -20,0 +22,0 @@ } |
1227
56659