vue-currency-input
Advanced tools
Comparing version 1.8.0 to 1.8.1
/** | ||
* Vue Currency Input 1.8.0 | ||
* Vue Currency Input 1.8.1 | ||
* (c) 2019 Matthias Stiller | ||
@@ -62,2 +62,7 @@ * @license MIT | ||
}; | ||
var removeCurrencySymbol = function (str, ref) { | ||
var prefix = ref.prefix; | ||
var suffix = ref.suffix; | ||
return removePrefix(removeSuffix(str, suffix), prefix) | ||
}; | ||
var parse = function (str, ref) { | ||
@@ -77,4 +82,3 @@ if ( ref === void 0 ) ref = {}; | ||
str = removePrefix(str, '-'); | ||
str = removePrefix(str, prefix); | ||
str = removeSuffix(str, suffix); | ||
str = removeCurrencySymbol(str, { prefix: prefix, suffix: suffix }); | ||
var numberParts = str.split(decimalSymbol); | ||
@@ -128,4 +132,3 @@ if (numberParts.length > 2) { | ||
value = removePrefix(value, '-'); | ||
value = removePrefix(value, prefix); | ||
value = removeSuffix(value, suffix); | ||
value = removeCurrencySymbol(value, currencyFormat); | ||
if (value === '' && negative && previousConformedValue !== ("-" + prefix)) { | ||
@@ -269,3 +272,3 @@ return ("-" + prefix + suffix) | ||
var position = getCaretPositionOnFocus(inputElement); | ||
format(inputElement); | ||
applyDistractionFreeFormat(inputElement); | ||
setCaretPosition(inputElement, position); | ||
@@ -342,2 +345,19 @@ }, 0); | ||
}; | ||
var applyDistractionFreeFormat = function (el) { | ||
var ref = el.$ci; | ||
var options = ref.options; | ||
var currencyFormat = ref.currencyFormat; | ||
var ref$1 = conformToMask(el.value, currencyFormat); | ||
var conformedValue = ref$1.conformedValue; | ||
var fractionDigits = ref$1.fractionDigits; | ||
if (typeof conformedValue === 'number') { | ||
el.value = new Intl.NumberFormat(options.locale, { | ||
style: options.hideCurrencySymbol ? 'decimal' : 'currency', | ||
useGrouping: !options.hideThousandsSeparatorSymbol, | ||
currency: options.currency, | ||
minimumFractionDigits: options.hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : fractionDigits.length | ||
}).format(conformedValue); | ||
} | ||
el.$ci.previousConformedValue = el.value; | ||
}; | ||
var format = function (el, value) { | ||
@@ -358,4 +378,6 @@ if ( value === void 0 ) value = el.value; | ||
if (typeof conformedValue === 'number') { | ||
if (options.validateOnInput && ((options.min != null && conformedValue < options.min) || (options.max != null && conformedValue > options.max))) { | ||
el.value = previousConformedValue; | ||
var min = options.min; | ||
var max = options.max; | ||
if (options.validateOnInput && ((min != null && conformedValue < min) || (max != null && conformedValue > max))) { | ||
el.value = previousConformedValue || null; | ||
} else { | ||
@@ -366,3 +388,3 @@ el.value = new Intl.NumberFormat(options.locale, { | ||
currency: options.currency, | ||
minimumFractionDigits: focus && options.hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : fractionDigits.length | ||
minimumFractionDigits: fractionDigits.length | ||
}).format(conformedValue); | ||
@@ -369,0 +391,0 @@ } |
/** | ||
* Vue Currency Input 1.8.0 | ||
* Vue Currency Input 1.8.1 | ||
* (c) 2019 Matthias Stiller | ||
@@ -68,2 +68,7 @@ * @license MIT | ||
}; | ||
var removeCurrencySymbol = function (str, ref) { | ||
var prefix = ref.prefix; | ||
var suffix = ref.suffix; | ||
return removePrefix(removeSuffix(str, suffix), prefix) | ||
}; | ||
var parse = function (str, ref) { | ||
@@ -83,4 +88,3 @@ if ( ref === void 0 ) ref = {}; | ||
str = removePrefix(str, '-'); | ||
str = removePrefix(str, prefix); | ||
str = removeSuffix(str, suffix); | ||
str = removeCurrencySymbol(str, { prefix: prefix, suffix: suffix }); | ||
var numberParts = str.split(decimalSymbol); | ||
@@ -134,4 +138,3 @@ if (numberParts.length > 2) { | ||
value = removePrefix(value, '-'); | ||
value = removePrefix(value, prefix); | ||
value = removeSuffix(value, suffix); | ||
value = removeCurrencySymbol(value, currencyFormat); | ||
if (value === '' && negative && previousConformedValue !== ("-" + prefix)) { | ||
@@ -275,3 +278,3 @@ return ("-" + prefix + suffix) | ||
var position = getCaretPositionOnFocus(inputElement); | ||
format(inputElement); | ||
applyDistractionFreeFormat(inputElement); | ||
setCaretPosition(inputElement, position); | ||
@@ -348,2 +351,19 @@ }, 0); | ||
}; | ||
var applyDistractionFreeFormat = function (el) { | ||
var ref = el.$ci; | ||
var options = ref.options; | ||
var currencyFormat = ref.currencyFormat; | ||
var ref$1 = conformToMask(el.value, currencyFormat); | ||
var conformedValue = ref$1.conformedValue; | ||
var fractionDigits = ref$1.fractionDigits; | ||
if (typeof conformedValue === 'number') { | ||
el.value = new Intl.NumberFormat(options.locale, { | ||
style: options.hideCurrencySymbol ? 'decimal' : 'currency', | ||
useGrouping: !options.hideThousandsSeparatorSymbol, | ||
currency: options.currency, | ||
minimumFractionDigits: options.hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : fractionDigits.length | ||
}).format(conformedValue); | ||
} | ||
el.$ci.previousConformedValue = el.value; | ||
}; | ||
var format = function (el, value) { | ||
@@ -364,4 +384,6 @@ if ( value === void 0 ) value = el.value; | ||
if (typeof conformedValue === 'number') { | ||
if (options.validateOnInput && ((options.min != null && conformedValue < options.min) || (options.max != null && conformedValue > options.max))) { | ||
el.value = previousConformedValue; | ||
var min = options.min; | ||
var max = options.max; | ||
if (options.validateOnInput && ((min != null && conformedValue < min) || (max != null && conformedValue > max))) { | ||
el.value = previousConformedValue || null; | ||
} else { | ||
@@ -372,3 +394,3 @@ el.value = new Intl.NumberFormat(options.locale, { | ||
currency: options.currency, | ||
minimumFractionDigits: focus && options.hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : fractionDigits.length | ||
minimumFractionDigits: fractionDigits.length | ||
}).format(conformedValue); | ||
@@ -375,0 +397,0 @@ } |
{ | ||
"name": "vue-currency-input", | ||
"description": "Easy input of currency formatted numbers for Vue.js.", | ||
"version": "1.8.0", | ||
"version": "1.8.1", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "unpkg": "dist/vue-currency-input.umd.js", |
@@ -39,3 +39,3 @@ import Vue from 'vue' | ||
const position = getCaretPositionOnFocus(inputElement) | ||
format(inputElement) | ||
applyDistractionFreeFormat(inputElement) | ||
setCaretPosition(inputElement, position) | ||
@@ -110,2 +110,16 @@ }, 0) | ||
const applyDistractionFreeFormat = (el) => { | ||
const { options, currencyFormat } = el.$ci | ||
const { conformedValue, fractionDigits } = conformToMask(el.value, currencyFormat) | ||
if (typeof conformedValue === 'number') { | ||
el.value = new Intl.NumberFormat(options.locale, { | ||
style: options.hideCurrencySymbol ? 'decimal' : 'currency', | ||
useGrouping: !options.hideThousandsSeparatorSymbol, | ||
currency: options.currency, | ||
minimumFractionDigits: options.hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : fractionDigits.length | ||
}).format(conformedValue) | ||
} | ||
el.$ci.previousConformedValue = el.value | ||
} | ||
const format = (el, value = el.value) => { | ||
@@ -121,4 +135,5 @@ const { options, currencyFormat, focus, previousConformedValue } = el.$ci | ||
if (typeof conformedValue === 'number') { | ||
if (options.validateOnInput && ((options.min != null && conformedValue < options.min) || (options.max != null && conformedValue > options.max))) { | ||
el.value = previousConformedValue | ||
const { min, max } = options | ||
if (options.validateOnInput && ((min != null && conformedValue < min) || (max != null && conformedValue > max))) { | ||
el.value = previousConformedValue || null | ||
} else { | ||
@@ -129,3 +144,3 @@ el.value = new Intl.NumberFormat(options.locale, { | ||
currency: options.currency, | ||
minimumFractionDigits: focus && options.hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : fractionDigits.length | ||
minimumFractionDigits: fractionDigits.length | ||
}).format(conformedValue) | ||
@@ -132,0 +147,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { endsWith, onlyDigits, removePrefix, removeSuffix, startsWith } from './formatHelper' | ||
import { endsWith, onlyDigits, removeCurrencySymbol, removePrefix, startsWith } from './formatHelper' | ||
@@ -18,4 +18,3 @@ const isValidInteger = (integer, { prefix, thousandsSeparatorSymbol }) => integer.replace(prefix, '').match(new RegExp(`^-?(0|[1-9]\\d{0,2}(\\${thousandsSeparatorSymbol}?\\d{3})*)$`)) | ||
value = removePrefix(value, '-') | ||
value = removePrefix(value, prefix) | ||
value = removeSuffix(value, suffix) | ||
value = removeCurrencySymbol(value, currencyFormat) | ||
if (value === '' && negative && previousConformedValue !== `-${prefix}`) { | ||
@@ -22,0 +21,0 @@ return `-${prefix}${suffix}` |
@@ -27,2 +27,6 @@ export const onlyDigits = (str) => str.replace(/\D+/g, '') | ||
export const removeCurrencySymbol = (str, { prefix, suffix }) => { | ||
return removePrefix(removeSuffix(str, suffix), prefix) | ||
} | ||
export const parse = (str, { prefix, suffix, thousandsSeparatorSymbol, decimalSymbol } = {}) => { | ||
@@ -37,4 +41,3 @@ if (typeof str === 'number') { | ||
str = removePrefix(str, '-') | ||
str = removePrefix(str, prefix) | ||
str = removeSuffix(str, suffix) | ||
str = removeCurrencySymbol(str, { prefix, suffix }) | ||
const numberParts = str.split(decimalSymbol) | ||
@@ -41,0 +44,0 @@ if (numberParts.length > 2) { |
60162
1456