vue-currency-input
Advanced tools
Comparing version 1.19.0 to 1.19.1
/** | ||
* Vue Currency Input 1.19.0 | ||
* Vue Currency Input 1.19.1 | ||
* (c) 2018-2020 Matthias Stiller | ||
@@ -33,2 +33,4 @@ * @license MIT | ||
var ps = numberFormat.format(123456); | ||
this.locale = locale; | ||
this.currency = currency; | ||
this.digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(function (i) { return i.toLocaleString(locale); }); | ||
@@ -73,2 +75,15 @@ this.decimalSymbol = count(ps, this.digits[0]) ? ps.substr(ps.indexOf(this.digits[6]) + 1, 1) : undefined; | ||
}; | ||
NumberFormat.prototype.format = function format (number, options) { | ||
if ( options === void 0 ) options = { | ||
minimumFractionDigits: this.minimumFractionDigits, | ||
maximumFractionDigits: this.maximumFractionDigits | ||
}; | ||
if (typeof this.currency === 'string') { | ||
return number.toLocaleString(this.locale, Object.assign({}, {style: 'currency', | ||
currency: this.currency}, | ||
options)) | ||
} else { | ||
return this.insertCurrencySymbol(Math.abs(number).toLocaleString(this.locale, options), number < 0 || (number === 0 && (1 / number < 0))) | ||
} | ||
}; | ||
NumberFormat.prototype.integerPattern = function integerPattern () { | ||
@@ -240,2 +255,3 @@ return ("(0|[1-9]\\d{0,2}(" + (escapeRegExp(this.groupingSymbol)) + "?\\d{3})*)") | ||
var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1; | ||
var init = function (el, optionsFromBinding, ref) { | ||
@@ -250,2 +266,3 @@ var $CI_DEFAULT_OPTIONS = ref.$CI_DEFAULT_OPTIONS; | ||
var autoDecimalMode = options.autoDecimalMode; | ||
var valueRange = options.valueRange; | ||
if (typeof distractionFree === 'boolean') { | ||
@@ -258,2 +275,13 @@ options.distractionFree = { | ||
} | ||
if (valueRange) { | ||
options.valueRange = { | ||
min: valueRange.min !== undefined ? Math.max(valueRange.min, -MAX_SAFE_INTEGER) : -MAX_SAFE_INTEGER, | ||
max: valueRange.max !== undefined ? Math.min(valueRange.max, MAX_SAFE_INTEGER) : MAX_SAFE_INTEGER | ||
}; | ||
} else { | ||
options.valueRange = { | ||
min: -MAX_SAFE_INTEGER, | ||
max: MAX_SAFE_INTEGER | ||
}; | ||
} | ||
if (autoDecimalMode) { | ||
@@ -269,18 +297,7 @@ options.distractionFree.hideNegligibleDecimalDigits = false; | ||
numberMask: options.autoDecimalMode ? new AutoDecimalModeNumberMask(currencyFormat) : new DefaultNumberMask(currencyFormat), | ||
currencyFormat: currencyFormat, | ||
decimalFormat: new NumberFormat(Object.assign({}, options, {currency: null}))}); | ||
currencyFormat: currencyFormat}); | ||
return inputElement | ||
}; | ||
var validateValueRange = function (value, valueRange) { | ||
if (valueRange) { | ||
var min = valueRange.min; | ||
var max = valueRange.max; | ||
if (min !== undefined && value < min) { | ||
value = min; | ||
} | ||
if (max !== undefined && value > max) { | ||
value = max; | ||
} | ||
} | ||
return value | ||
return Math.min(Math.max(value, valueRange.min), valueRange.max) | ||
}; | ||
@@ -297,9 +314,6 @@ var triggerEvent = function (el, eventName) { | ||
if ( forcedChange === void 0 ) forcedChange = false; | ||
var ref = el.$ci.options; | ||
var valueRange = ref.valueRange; | ||
var locale = ref.locale; | ||
var ref$1 = el.$ci.currencyFormat; | ||
var maximumFractionDigits = ref$1.maximumFractionDigits; | ||
var minimumFractionDigits = ref$1.minimumFractionDigits; | ||
format(el, value != null ? validateValueRange(value, valueRange).toLocaleString(locale, { minimumFractionDigits: minimumFractionDigits, maximumFractionDigits: maximumFractionDigits }) : null); | ||
var ref = el.$ci; | ||
var currencyFormat = ref.currencyFormat; | ||
var options = ref.options; | ||
format(el, value != null ? currencyFormat.format(validateValueRange(value, options.valueRange)) : null); | ||
if (value !== el.$ci.numberValue || forcedChange) { | ||
@@ -316,7 +330,5 @@ triggerEvent(el, 'change'); | ||
var currencyFormat = ref.currencyFormat; | ||
var decimalFormat = ref.decimalFormat; | ||
var previousConformedValue = ref.previousConformedValue; | ||
var allowNegative = options.allowNegative; | ||
var distractionFree = options.distractionFree; | ||
var locale = options.locale; | ||
var conformedValue = numberMask.conformToMask(value, previousConformedValue); | ||
@@ -335,8 +347,9 @@ var formattedValue; | ||
: Math.min(minimumFractionDigits, fractionDigits.length); | ||
formattedValue = Math.abs(numberValue).toLocaleString(locale, { | ||
useGrouping: !(focus && distractionFree.hideGroupingSymbol), | ||
minimumFractionDigits: minimumFractionDigits, | ||
maximumFractionDigits: maximumFractionDigits | ||
}); | ||
formattedValue = currencyFormat.insertCurrencySymbol(formattedValue, currencyFormat.isNegative(value)); | ||
formattedValue = numberValue > MAX_SAFE_INTEGER | ||
? previousConformedValue | ||
: currencyFormat.format(numberValue, { | ||
useGrouping: !(focus && distractionFree.hideGroupingSymbol), | ||
minimumFractionDigits: minimumFractionDigits, | ||
maximumFractionDigits: maximumFractionDigits | ||
}); | ||
} else { | ||
@@ -350,5 +363,5 @@ formattedValue = conformedValue; | ||
formattedValue = formattedValue | ||
.replace(currencyFormat.negativePrefix, decimalFormat.negativePrefix) | ||
.replace(currencyFormat.prefix, decimalFormat.prefix) | ||
.replace(currencyFormat.suffix, decimalFormat.suffix); | ||
.replace(currencyFormat.negativePrefix, currencyFormat.minusSymbol) | ||
.replace(currencyFormat.prefix, '') | ||
.replace(currencyFormat.suffix, ''); | ||
} | ||
@@ -392,3 +405,2 @@ el.value = formattedValue; | ||
el.addEventListener('focus', function () { | ||
el.$ci.oldValue = el.$ci.numberValue; | ||
el.$ci.focus = true; | ||
@@ -415,4 +427,9 @@ var ref = el.$ci.options.distractionFree; | ||
el.$ci.focus = false; | ||
applyFixedFractionFormat(el, el.$ci.numberValue, el.$ci.oldValue !== el.$ci.numberValue); | ||
applyFixedFractionFormat(el, el.$ci.numberValue); | ||
}); | ||
el.addEventListener('change', function (e) { | ||
if (!e.detail) { | ||
triggerEvent(el, 'change'); | ||
} | ||
}); | ||
}; | ||
@@ -419,0 +436,0 @@ var directive = { |
/** | ||
* Vue Currency Input 1.19.0 | ||
* Vue Currency Input 1.19.1 | ||
* (c) 2018-2020 Matthias Stiller | ||
@@ -39,2 +39,4 @@ * @license MIT | ||
var ps = numberFormat.format(123456); | ||
this.locale = locale; | ||
this.currency = currency; | ||
this.digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(function (i) { return i.toLocaleString(locale); }); | ||
@@ -79,2 +81,15 @@ this.decimalSymbol = count(ps, this.digits[0]) ? ps.substr(ps.indexOf(this.digits[6]) + 1, 1) : undefined; | ||
}; | ||
NumberFormat.prototype.format = function format (number, options) { | ||
if ( options === void 0 ) options = { | ||
minimumFractionDigits: this.minimumFractionDigits, | ||
maximumFractionDigits: this.maximumFractionDigits | ||
}; | ||
if (typeof this.currency === 'string') { | ||
return number.toLocaleString(this.locale, Object.assign({}, {style: 'currency', | ||
currency: this.currency}, | ||
options)) | ||
} else { | ||
return this.insertCurrencySymbol(Math.abs(number).toLocaleString(this.locale, options), number < 0 || (number === 0 && (1 / number < 0))) | ||
} | ||
}; | ||
NumberFormat.prototype.integerPattern = function integerPattern () { | ||
@@ -246,2 +261,3 @@ return ("(0|[1-9]\\d{0,2}(" + (escapeRegExp(this.groupingSymbol)) + "?\\d{3})*)") | ||
var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1; | ||
var init = function (el, optionsFromBinding, ref) { | ||
@@ -256,2 +272,3 @@ var $CI_DEFAULT_OPTIONS = ref.$CI_DEFAULT_OPTIONS; | ||
var autoDecimalMode = options.autoDecimalMode; | ||
var valueRange = options.valueRange; | ||
if (typeof distractionFree === 'boolean') { | ||
@@ -264,2 +281,13 @@ options.distractionFree = { | ||
} | ||
if (valueRange) { | ||
options.valueRange = { | ||
min: valueRange.min !== undefined ? Math.max(valueRange.min, -MAX_SAFE_INTEGER) : -MAX_SAFE_INTEGER, | ||
max: valueRange.max !== undefined ? Math.min(valueRange.max, MAX_SAFE_INTEGER) : MAX_SAFE_INTEGER | ||
}; | ||
} else { | ||
options.valueRange = { | ||
min: -MAX_SAFE_INTEGER, | ||
max: MAX_SAFE_INTEGER | ||
}; | ||
} | ||
if (autoDecimalMode) { | ||
@@ -275,18 +303,7 @@ options.distractionFree.hideNegligibleDecimalDigits = false; | ||
numberMask: options.autoDecimalMode ? new AutoDecimalModeNumberMask(currencyFormat) : new DefaultNumberMask(currencyFormat), | ||
currencyFormat: currencyFormat, | ||
decimalFormat: new NumberFormat(Object.assign({}, options, {currency: null}))}); | ||
currencyFormat: currencyFormat}); | ||
return inputElement | ||
}; | ||
var validateValueRange = function (value, valueRange) { | ||
if (valueRange) { | ||
var min = valueRange.min; | ||
var max = valueRange.max; | ||
if (min !== undefined && value < min) { | ||
value = min; | ||
} | ||
if (max !== undefined && value > max) { | ||
value = max; | ||
} | ||
} | ||
return value | ||
return Math.min(Math.max(value, valueRange.min), valueRange.max) | ||
}; | ||
@@ -303,9 +320,6 @@ var triggerEvent = function (el, eventName) { | ||
if ( forcedChange === void 0 ) forcedChange = false; | ||
var ref = el.$ci.options; | ||
var valueRange = ref.valueRange; | ||
var locale = ref.locale; | ||
var ref$1 = el.$ci.currencyFormat; | ||
var maximumFractionDigits = ref$1.maximumFractionDigits; | ||
var minimumFractionDigits = ref$1.minimumFractionDigits; | ||
format(el, value != null ? validateValueRange(value, valueRange).toLocaleString(locale, { minimumFractionDigits: minimumFractionDigits, maximumFractionDigits: maximumFractionDigits }) : null); | ||
var ref = el.$ci; | ||
var currencyFormat = ref.currencyFormat; | ||
var options = ref.options; | ||
format(el, value != null ? currencyFormat.format(validateValueRange(value, options.valueRange)) : null); | ||
if (value !== el.$ci.numberValue || forcedChange) { | ||
@@ -322,7 +336,5 @@ triggerEvent(el, 'change'); | ||
var currencyFormat = ref.currencyFormat; | ||
var decimalFormat = ref.decimalFormat; | ||
var previousConformedValue = ref.previousConformedValue; | ||
var allowNegative = options.allowNegative; | ||
var distractionFree = options.distractionFree; | ||
var locale = options.locale; | ||
var conformedValue = numberMask.conformToMask(value, previousConformedValue); | ||
@@ -341,8 +353,9 @@ var formattedValue; | ||
: Math.min(minimumFractionDigits, fractionDigits.length); | ||
formattedValue = Math.abs(numberValue).toLocaleString(locale, { | ||
useGrouping: !(focus && distractionFree.hideGroupingSymbol), | ||
minimumFractionDigits: minimumFractionDigits, | ||
maximumFractionDigits: maximumFractionDigits | ||
}); | ||
formattedValue = currencyFormat.insertCurrencySymbol(formattedValue, currencyFormat.isNegative(value)); | ||
formattedValue = numberValue > MAX_SAFE_INTEGER | ||
? previousConformedValue | ||
: currencyFormat.format(numberValue, { | ||
useGrouping: !(focus && distractionFree.hideGroupingSymbol), | ||
minimumFractionDigits: minimumFractionDigits, | ||
maximumFractionDigits: maximumFractionDigits | ||
}); | ||
} else { | ||
@@ -356,5 +369,5 @@ formattedValue = conformedValue; | ||
formattedValue = formattedValue | ||
.replace(currencyFormat.negativePrefix, decimalFormat.negativePrefix) | ||
.replace(currencyFormat.prefix, decimalFormat.prefix) | ||
.replace(currencyFormat.suffix, decimalFormat.suffix); | ||
.replace(currencyFormat.negativePrefix, currencyFormat.minusSymbol) | ||
.replace(currencyFormat.prefix, '') | ||
.replace(currencyFormat.suffix, ''); | ||
} | ||
@@ -398,3 +411,2 @@ el.value = formattedValue; | ||
el.addEventListener('focus', function () { | ||
el.$ci.oldValue = el.$ci.numberValue; | ||
el.$ci.focus = true; | ||
@@ -421,4 +433,9 @@ var ref = el.$ci.options.distractionFree; | ||
el.$ci.focus = false; | ||
applyFixedFractionFormat(el, el.$ci.numberValue, el.$ci.oldValue !== el.$ci.numberValue); | ||
applyFixedFractionFormat(el, el.$ci.numberValue); | ||
}); | ||
el.addEventListener('change', function (e) { | ||
if (!e.detail) { | ||
triggerEvent(el, 'change'); | ||
} | ||
}); | ||
}; | ||
@@ -425,0 +442,0 @@ var directive = { |
{ | ||
"name": "vue-currency-input", | ||
"description": "Easy input of currency formatted numbers for Vue.js.", | ||
"version": "1.19.0", | ||
"version": "1.19.1", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "unpkg": "dist/vue-currency-input.umd.js", |
@@ -9,2 +9,4 @@ import { DEFAULT_OPTIONS } from './api' | ||
const MAX_SAFE_INTEGER = Math.pow(2, 53) - 1 | ||
const init = (el, optionsFromBinding, { $CI_DEFAULT_OPTIONS }) => { | ||
@@ -16,3 +18,3 @@ const inputElement = el.tagName.toLowerCase() === 'input' ? el : el.querySelector('input') | ||
const options = { ...($CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS), ...optionsFromBinding } | ||
const { distractionFree, autoDecimalMode } = options | ||
const { distractionFree, autoDecimalMode, valueRange } = options | ||
if (typeof distractionFree === 'boolean') { | ||
@@ -25,2 +27,14 @@ options.distractionFree = { | ||
} | ||
if (valueRange) { | ||
options.valueRange = { | ||
min: valueRange.min !== undefined ? Math.max(valueRange.min, -MAX_SAFE_INTEGER) : -MAX_SAFE_INTEGER, | ||
max: valueRange.max !== undefined ? Math.min(valueRange.max, MAX_SAFE_INTEGER) : MAX_SAFE_INTEGER | ||
} | ||
} else { | ||
options.valueRange = { | ||
min: -MAX_SAFE_INTEGER, | ||
max: MAX_SAFE_INTEGER | ||
} | ||
} | ||
if (autoDecimalMode) { | ||
@@ -37,4 +51,3 @@ options.distractionFree.hideNegligibleDecimalDigits = false | ||
numberMask: options.autoDecimalMode ? new AutoDecimalModeNumberMask(currencyFormat) : new DefaultNumberMask(currencyFormat), | ||
currencyFormat, | ||
decimalFormat: new NumberFormat({ ...options, currency: null }) | ||
currencyFormat | ||
} | ||
@@ -45,12 +58,3 @@ return inputElement | ||
const validateValueRange = (value, valueRange) => { | ||
if (valueRange) { | ||
const { min, max } = valueRange | ||
if (min !== undefined && value < min) { | ||
value = min | ||
} | ||
if (max !== undefined && value > max) { | ||
value = max | ||
} | ||
} | ||
return value | ||
return Math.min(Math.max(value, valueRange.min), valueRange.max) | ||
} | ||
@@ -65,5 +69,4 @@ | ||
const applyFixedFractionFormat = (el, value, forcedChange = false) => { | ||
const { valueRange, locale } = el.$ci.options | ||
const { maximumFractionDigits, minimumFractionDigits } = el.$ci.currencyFormat | ||
format(el, value != null ? validateValueRange(value, valueRange).toLocaleString(locale, { minimumFractionDigits, maximumFractionDigits }) : null) | ||
const { currencyFormat, options } = el.$ci | ||
format(el, value != null ? currencyFormat.format(validateValueRange(value, options.valueRange)) : null) | ||
if (value !== el.$ci.numberValue || forcedChange) { | ||
@@ -76,4 +79,4 @@ triggerEvent(el, 'change') | ||
if (value != null) { | ||
const { focus, options, numberMask, currencyFormat, decimalFormat, previousConformedValue } = el.$ci | ||
const { allowNegative, distractionFree, locale } = options | ||
const { focus, options, numberMask, currencyFormat, previousConformedValue } = el.$ci | ||
const { allowNegative, distractionFree } = options | ||
const conformedValue = numberMask.conformToMask(value, previousConformedValue) | ||
@@ -90,8 +93,9 @@ let formattedValue | ||
: Math.min(minimumFractionDigits, fractionDigits.length) | ||
formattedValue = Math.abs(numberValue).toLocaleString(locale, { | ||
useGrouping: !(focus && distractionFree.hideGroupingSymbol), | ||
minimumFractionDigits, | ||
maximumFractionDigits | ||
}) | ||
formattedValue = currencyFormat.insertCurrencySymbol(formattedValue, currencyFormat.isNegative(value)) | ||
formattedValue = numberValue > MAX_SAFE_INTEGER | ||
? previousConformedValue | ||
: currencyFormat.format(numberValue, { | ||
useGrouping: !(focus && distractionFree.hideGroupingSymbol), | ||
minimumFractionDigits, | ||
maximumFractionDigits | ||
}) | ||
} else { | ||
@@ -105,5 +109,5 @@ formattedValue = conformedValue | ||
formattedValue = formattedValue | ||
.replace(currencyFormat.negativePrefix, decimalFormat.negativePrefix) | ||
.replace(currencyFormat.prefix, decimalFormat.prefix) | ||
.replace(currencyFormat.suffix, decimalFormat.suffix) | ||
.replace(currencyFormat.negativePrefix, currencyFormat.minusSymbol) | ||
.replace(currencyFormat.prefix, '') | ||
.replace(currencyFormat.suffix, '') | ||
} | ||
@@ -144,3 +148,2 @@ | ||
el.addEventListener('focus', () => { | ||
el.$ci.oldValue = el.$ci.numberValue | ||
el.$ci.focus = true | ||
@@ -163,4 +166,10 @@ const { hideCurrencySymbol, hideGroupingSymbol, hideNegligibleDecimalDigits } = el.$ci.options.distractionFree | ||
el.$ci.focus = false | ||
applyFixedFractionFormat(el, el.$ci.numberValue, el.$ci.oldValue !== el.$ci.numberValue) | ||
applyFixedFractionFormat(el, el.$ci.numberValue) | ||
}) | ||
el.addEventListener('change', (e) => { | ||
if (!e.detail) { | ||
triggerEvent(el, 'change') | ||
} | ||
}) | ||
} | ||
@@ -167,0 +176,0 @@ |
@@ -7,4 +7,6 @@ import { count, escapeRegExp, startsWith, substringBefore } from './utils/stringUtils' | ||
const numberFormat = new Intl.NumberFormat(locale, typeof currency === 'string' ? { currency, style: 'currency' } : { minimumFractionDigits: 1 }) | ||
const ps = numberFormat.format(123456) | ||
const ps = numberFormat.format(123456) | ||
this.locale = locale | ||
this.currency = currency | ||
this.digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(i => i.toLocaleString(locale)) | ||
@@ -53,2 +55,17 @@ this.decimalSymbol = count(ps, this.digits[0]) ? ps.substr(ps.indexOf(this.digits[6]) + 1, 1) : undefined | ||
format (number, options = { | ||
minimumFractionDigits: this.minimumFractionDigits, | ||
maximumFractionDigits: this.maximumFractionDigits | ||
}) { | ||
if (typeof this.currency === 'string') { | ||
return number.toLocaleString(this.locale, { | ||
style: 'currency', | ||
currency: this.currency, | ||
...options | ||
}) | ||
} else { | ||
return this.insertCurrencySymbol(Math.abs(number).toLocaleString(this.locale, options), number < 0 || (number === 0 && (1 / number < 0))) | ||
} | ||
} | ||
integerPattern () { | ||
@@ -55,0 +72,0 @@ return `(0|[1-9]\\d{0,2}(${escapeRegExp(this.groupingSymbol)}?\\d{3})*)` |
74664
1692