vue-currency-input
Advanced tools
Comparing version 1.14.3 to 1.15.0
/** | ||
* Vue Currency Input 1.14.3 | ||
* Vue Currency Input 1.15.0 | ||
* (c) 2019 Matthias Stiller | ||
@@ -71,2 +71,12 @@ * @license MIT | ||
}; | ||
var insertCurrencySymbol = function (value, currencyFormat, negative, hideCurrencySymbol) { | ||
var prefix = currencyFormat.prefix; | ||
var negativePrefix = currencyFormat.negativePrefix; | ||
var suffix = currencyFormat.suffix; | ||
if (hideCurrencySymbol) { | ||
prefix = suffix = ''; | ||
negativePrefix = '-'; | ||
} | ||
return ("" + (negative ? negativePrefix : prefix) + value + suffix) | ||
}; | ||
var stripCurrencySymbolAndMinusSign = function (str, ref) { | ||
@@ -125,3 +135,7 @@ var prefix = ref.prefix; | ||
var parseCurrency = function (formattedValue, options) { return parse(formattedValue, createCurrencyFormat$1(options), options.valueAsInteger); }; | ||
function dispatchEvent (el, eventName, data) { | ||
var event = document.createEvent('CustomEvent'); | ||
event.initCustomEvent(eventName, true, true, data); | ||
el.dispatchEvent(event); | ||
} | ||
@@ -135,5 +149,6 @@ var DEFAULT_OPTIONS = { | ||
autoDecimalMode: false, | ||
min: null, | ||
max: null | ||
valueRange: undefined | ||
}; | ||
var parseCurrency = function (formattedValue, options) { return parse(formattedValue, createCurrencyFormat$1(Object.assign({}, DEFAULT_OPTIONS, options)), options.valueAsInteger); }; | ||
var setValue = function (el, value) { return dispatchEvent(el, 'format', { value: value }); }; | ||
@@ -164,9 +179,9 @@ var setCaretPosition = function (el, position) { return el.setSelectionRange(position, position); }; | ||
}; | ||
var getDistractionFreeCaretPosition = function (formatConfig, options, value, caretPosition) { | ||
var getDistractionFreeCaretPosition = function (currencyFormat, options, value, caretPosition) { | ||
var result = caretPosition; | ||
if (options.distractionFree.hideCurrencySymbol) { | ||
result -= formatConfig.prefix.length; | ||
result -= currencyFormat.prefix.length; | ||
} | ||
if (options.distractionFree.hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), formatConfig.groupingSymbol); | ||
result -= count(value.substring(0, caretPosition), currencyFormat.groupingSymbol); | ||
} | ||
@@ -183,15 +198,13 @@ return Math.max(0, result) | ||
}; | ||
var checkIncompleteValue = function (value, negative, previousConformedValue, formatConfig) { | ||
var prefix = formatConfig.prefix; | ||
var negativePrefix = formatConfig.negativePrefix; | ||
var suffix = formatConfig.suffix; | ||
var decimalSymbol = formatConfig.decimalSymbol; | ||
var maximumFractionDigits = formatConfig.maximumFractionDigits; | ||
if (value === '' && negative && previousConformedValue !== negativePrefix) { | ||
return ("" + negativePrefix + suffix) | ||
var checkIncompleteValue = function (value, negative, previousConformedValue, currencyFormat, hideCurrencySymbol) { | ||
var negativePrefix = currencyFormat.negativePrefix; | ||
var decimalSymbol = currencyFormat.decimalSymbol; | ||
var maximumFractionDigits = currencyFormat.maximumFractionDigits; | ||
if (value === '' && negative && previousConformedValue !== (hideCurrencySymbol ? '-' : negativePrefix)) { | ||
return insertCurrencySymbol('', currencyFormat, negative, hideCurrencySymbol) | ||
} else if (maximumFractionDigits > 0) { | ||
if (isFractionIncomplete(value, formatConfig)) { | ||
return ("" + (negative ? negativePrefix : prefix) + value + suffix) | ||
if (isFractionIncomplete(value, currencyFormat)) { | ||
return insertCurrencySymbol(value, currencyFormat, negative, hideCurrencySymbol) | ||
} else if (startsWith(value, decimalSymbol)) { | ||
return ((negative ? negativePrefix : prefix) + "0" + decimalSymbol + ((onlyDigits(value.substr(1)).substr(0, maximumFractionDigits))) + suffix) | ||
return insertCurrencySymbol(("0" + decimalSymbol + ((onlyDigits(value.substr(1)).substr(0, maximumFractionDigits)))), currencyFormat, negative, hideCurrencySymbol) | ||
} | ||
@@ -207,3 +220,3 @@ } | ||
var negative = startsWith(value, '-'); | ||
var conformedValue = value === '-' ? Number(-0) : Number(("" + (negative ? '-' : '') + (removeLeadingZeros(onlyDigits(value))))) / Math.pow(10, minimumFractionDigits); | ||
var conformedValue = value === '-' ? -0 : Number(("" + (negative ? '-' : '') + (removeLeadingZeros(onlyDigits(value))))) / Math.pow(10, minimumFractionDigits); | ||
return { | ||
@@ -215,35 +228,31 @@ conformedValue: conformedValue, | ||
}; | ||
var isFractionInvalid = function (fraction, numberOfFractionDigits) { return fraction.length > 0 && numberOfFractionDigits === 0; }; | ||
function conformToMask (str, formatConfig, options, previousConformedValue) { | ||
function conformToMask (str, currencyFormat, previousConformedValue, hideCurrencySymbol, autoDecimalMode) { | ||
if ( previousConformedValue === void 0 ) previousConformedValue = ''; | ||
if ( hideCurrencySymbol === void 0 ) hideCurrencySymbol = false; | ||
if ( autoDecimalMode === void 0 ) autoDecimalMode = false; | ||
if (typeof str === 'string') { | ||
if (formatConfig.minimumFractionDigits > 0 && options.autoDecimalMode) { | ||
return getAutoDecimalModeConformedValue(str, previousConformedValue, formatConfig) | ||
if (currencyFormat.minimumFractionDigits > 0 && autoDecimalMode) { | ||
return getAutoDecimalModeConformedValue(str, previousConformedValue, currencyFormat) | ||
} | ||
var ref = stripCurrencySymbolAndMinusSign(str, formatConfig); | ||
var ref = stripCurrencySymbolAndMinusSign(str, currencyFormat); | ||
var value = ref.value; | ||
var negative = ref.negative; | ||
var incompleteValue = checkIncompleteValue(value, negative, previousConformedValue, formatConfig); | ||
var incompleteValue = checkIncompleteValue(value, negative, previousConformedValue, currencyFormat, hideCurrencySymbol); | ||
if (incompleteValue != null) { | ||
return { conformedValue: incompleteValue } | ||
} | ||
var ref$1 = value.split(formatConfig.decimalSymbol); | ||
var ref$1 = value.split(currencyFormat.decimalSymbol); | ||
var integer = ref$1[0]; | ||
var fraction = ref$1.slice(1); | ||
var integerDigits = removeLeadingZeros(onlyDigits(integer)); | ||
var fractionDigits = onlyDigits(fraction.join('')).substr(0, formatConfig.maximumFractionDigits); | ||
if (isFractionInvalid(fraction, fractionDigits.length)) { | ||
var fractionDigits = onlyDigits(fraction.join('')).substr(0, currencyFormat.maximumFractionDigits); | ||
var invalidFraction = fraction.length > 0 && fractionDigits.length === 0; | ||
var invalidNegativeValue = integerDigits === '' && negative && (previousConformedValue === str.slice(0, -1) || previousConformedValue !== currencyFormat.negativePrefix); | ||
if (invalidFraction || invalidNegativeValue) { | ||
return { conformedValue: previousConformedValue } | ||
} | ||
var number = integerDigits; | ||
if (negative) { | ||
number = "-" + number; | ||
} | ||
if (isNumber(number)) { | ||
} else if (isNumber(integerDigits)) { | ||
return { | ||
conformedValue: Number((number + "." + fractionDigits)), | ||
conformedValue: Number(("" + (negative ? '-' : '') + integerDigits + "." + fractionDigits)), | ||
fractionDigits: fractionDigits | ||
} | ||
} else if (number === '-' && (previousConformedValue === str.slice(0, -1) || previousConformedValue !== formatConfig.negativePrefix)) { | ||
return { conformedValue: previousConformedValue } | ||
} else { | ||
@@ -256,8 +265,2 @@ return { conformedValue: '' } | ||
function dispatchEvent (el, eventName, data) { | ||
var event = document.createEvent('CustomEvent'); | ||
event.initCustomEvent(eventName, true, true, data); | ||
el.dispatchEvent(event); | ||
} | ||
var equal = function (a, b) { | ||
@@ -288,4 +291,2 @@ if (a === b) { | ||
var options = Object.assign({}, ($CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS), optionsFromBinding); | ||
var min = options.min; | ||
var max = options.max; | ||
var distractionFree = options.distractionFree; | ||
@@ -303,5 +304,2 @@ var autoDecimalMode = options.autoDecimalMode; | ||
} | ||
if (min != null && max != null && min > max) { | ||
throw new Error('Invalid value range') | ||
} | ||
var currencyFormat = createCurrencyFormat$1(options); | ||
@@ -314,21 +312,29 @@ inputElement.$ci = Object.assign({}, inputElement.$ci || {}, | ||
}; | ||
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 | ||
}; | ||
var applyFixedFractionFormat = function (el, value) { | ||
var oldValue = el.value; | ||
if (value != null) { | ||
var ref = el.$ci.options; | ||
var min = ref.min; | ||
var max = ref.max; | ||
var valueRange = ref.valueRange; | ||
var locale = ref.locale; | ||
if (min != null && value < min) { | ||
value = min; | ||
} | ||
if (max != null && value > max) { | ||
value = max; | ||
} | ||
var ref$1 = el.$ci.currencyFormat; | ||
var maximumFractionDigits = ref$1.maximumFractionDigits; | ||
var minimumFractionDigits = ref$1.minimumFractionDigits; | ||
value = validateValueRange(value, valueRange); | ||
value = new Intl.NumberFormat(locale, { minimumFractionDigits: minimumFractionDigits, maximumFractionDigits: maximumFractionDigits }).format(value); | ||
} | ||
format(el, value); | ||
if (!el.$ci.inputEvent) { | ||
if (!el.$ci.inputEvent && oldValue !== el.value) { | ||
dispatchEvent(el, 'input'); | ||
@@ -342,29 +348,24 @@ } | ||
var focus = ref.focus; | ||
var options = ref.options; | ||
var ref_options = ref.options; | ||
var autoDecimalMode = ref_options.autoDecimalMode; | ||
var distractionFree = ref_options.distractionFree; | ||
var locale = ref_options.locale; | ||
var currencyFormat = ref.currencyFormat; | ||
var previousConformedValue = ref.previousConformedValue; | ||
var ref$1 = options.distractionFree; | ||
var hideCurrencySymbol = ref$1.hideCurrencySymbol; | ||
var hideGroupingSymbol = ref$1.hideGroupingSymbol; | ||
var formatConfig = Object.assign({}, currencyFormat); | ||
if (focus && hideCurrencySymbol) { | ||
formatConfig.prefix = ''; | ||
formatConfig.negativePrefix = '-'; | ||
formatConfig.suffix = ''; | ||
} | ||
var ref$2 = conformToMask(value, formatConfig, options, previousConformedValue); | ||
var conformedValue = ref$2.conformedValue; | ||
var fractionDigits = ref$2.fractionDigits; | ||
var hideCurrencySymbol = focus && distractionFree.hideCurrencySymbol; | ||
var ref$1 = conformToMask(value, currencyFormat, previousConformedValue, hideCurrencySymbol, autoDecimalMode); | ||
var conformedValue = ref$1.conformedValue; | ||
var fractionDigits = ref$1.fractionDigits; | ||
if (typeof conformedValue === 'number') { | ||
var formattedValue = new Intl.NumberFormat(options.locale, { | ||
useGrouping: !(focus && hideGroupingSymbol), | ||
minimumFractionDigits: hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : Math.min(formatConfig.minimumFractionDigits, fractionDigits.length), | ||
maximumFractionDigits: formatConfig.maximumFractionDigits | ||
var formattedValue = new Intl.NumberFormat(locale, { | ||
useGrouping: !(focus && distractionFree.hideGroupingSymbol), | ||
minimumFractionDigits: hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : Math.min(currencyFormat.minimumFractionDigits, fractionDigits.length), | ||
maximumFractionDigits: currencyFormat.maximumFractionDigits | ||
}).format(Math.abs(conformedValue)); | ||
var isNegativeZero = conformedValue === 0 && (1 / conformedValue < 0); | ||
el.value = "" + (isNegativeZero || conformedValue < 0 ? formatConfig.negativePrefix : formatConfig.prefix) + formattedValue + (formatConfig.suffix); | ||
el.value = insertCurrencySymbol(formattedValue, currencyFormat, isNegativeZero || conformedValue < 0, hideCurrencySymbol); | ||
el.$ci.numberValue = conformedValue; | ||
} else { | ||
el.value = conformedValue; | ||
el.$ci.numberValue = parse(el.value, formatConfig, false); | ||
el.$ci.numberValue = parse(el.value, currencyFormat, false); | ||
} | ||
@@ -377,13 +378,14 @@ } else { | ||
var format = function (el, value) { | ||
var oldValue = el.$ci.numberValue; | ||
updateInputValue(el, value); | ||
var ref = el.$ci; | ||
var numberValue = ref.numberValue; | ||
var newValue = ref.numberValue; | ||
var currencyFormat = ref.currencyFormat; | ||
var options = ref.options; | ||
var inputEvent = ref.inputEvent; | ||
if (numberValue != null) { | ||
numberValue = toInteger(numberValue, options.valueAsInteger, currencyFormat.maximumFractionDigits); | ||
if (newValue != null) { | ||
newValue = toInteger(newValue, options.valueAsInteger, currencyFormat.maximumFractionDigits); | ||
} | ||
if (inputEvent) { | ||
dispatchEvent(el, inputEvent, { numberValue: numberValue }); | ||
dispatchEvent(el, inputEvent, { oldValue: oldValue, newValue: newValue }); | ||
} | ||
@@ -468,2 +470,3 @@ }; | ||
function objectWithoutProperties (obj, exclude) { var target = {}; for (var k in obj) if (Object.prototype.hasOwnProperty.call(obj, k) && exclude.indexOf(k) === -1) target[k] = obj[k]; return target; } | ||
var inputEvent = 'format-complete'; | ||
var component = { | ||
@@ -478,5 +481,3 @@ render: function render (h) { | ||
value: this.options, | ||
modifiers: { | ||
inputEvent: 'format-complete' | ||
} | ||
modifiers: { inputEvent: inputEvent } | ||
}], | ||
@@ -519,9 +520,5 @@ on: this.listeners() | ||
}, | ||
min: { | ||
type: Number, | ||
valueRange: { | ||
type: Object, | ||
default: undefined | ||
}, | ||
max: { | ||
type: Number, | ||
default: undefined | ||
} | ||
@@ -550,7 +547,8 @@ }, | ||
methods: { | ||
setValue: function setValue (value) { | ||
dispatchEvent(this.$el, 'format', { value: value }); | ||
setValue: function setValue$1 (value) { | ||
setValue(this.$el, value); | ||
}, | ||
listeners: function listeners () { | ||
var this$1 = this; | ||
var obj; | ||
var ref = this.$listeners; | ||
@@ -561,7 +559,11 @@ var input = ref.input; | ||
return Object.assign({}, listeners, | ||
{'format-complete': function (ref) { | ||
var detail = ref.detail; | ||
this$1.$emit('input', detail.numberValue); | ||
( obj = {}, obj[inputEvent] = function (e) { | ||
var ref = e.detail; | ||
var oldValue = ref.oldValue; | ||
var newValue = ref.newValue; | ||
if (oldValue !== newValue) { | ||
this$1.$emit('input', newValue); | ||
} | ||
this$1.formattedValue = this$1.$el.value; | ||
}}) | ||
}, obj )) | ||
} | ||
@@ -577,8 +579,7 @@ } | ||
var globalOptions = ref.globalOptions; if ( globalOptions === void 0 ) globalOptions = {}; | ||
var defaultOptions = Object.assign({}, DEFAULT_OPTIONS, globalOptions); | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = defaultOptions; | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = Object.assign({}, DEFAULT_OPTIONS, globalOptions); | ||
Vue.component(componentName, component); | ||
Vue.directive(directiveName, directive); | ||
Vue.prototype.$parseCurrency = function (str, options) { | ||
if ( options === void 0 ) options = defaultOptions; | ||
if ( options === void 0 ) options = globalOptions; | ||
return parseCurrency(str, options); | ||
@@ -589,3 +590,7 @@ }; | ||
if (typeof window !== 'undefined' && window.Vue) { | ||
window.Vue.use(plugin); | ||
} | ||
export default plugin; | ||
export { directive as CurrencyDirective, component as CurrencyInput, parseCurrency }; | ||
export { directive as CurrencyDirective, component as CurrencyInput, parseCurrency, setValue }; |
/** | ||
* Vue Currency Input 1.14.3 | ||
* Vue Currency Input 1.15.0 | ||
* (c) 2019 Matthias Stiller | ||
@@ -77,2 +77,12 @@ * @license MIT | ||
}; | ||
var insertCurrencySymbol = function (value, currencyFormat, negative, hideCurrencySymbol) { | ||
var prefix = currencyFormat.prefix; | ||
var negativePrefix = currencyFormat.negativePrefix; | ||
var suffix = currencyFormat.suffix; | ||
if (hideCurrencySymbol) { | ||
prefix = suffix = ''; | ||
negativePrefix = '-'; | ||
} | ||
return ("" + (negative ? negativePrefix : prefix) + value + suffix) | ||
}; | ||
var stripCurrencySymbolAndMinusSign = function (str, ref) { | ||
@@ -131,3 +141,7 @@ var prefix = ref.prefix; | ||
var parseCurrency = function (formattedValue, options) { return parse(formattedValue, createCurrencyFormat$1(options), options.valueAsInteger); }; | ||
function dispatchEvent (el, eventName, data) { | ||
var event = document.createEvent('CustomEvent'); | ||
event.initCustomEvent(eventName, true, true, data); | ||
el.dispatchEvent(event); | ||
} | ||
@@ -141,5 +155,6 @@ var DEFAULT_OPTIONS = { | ||
autoDecimalMode: false, | ||
min: null, | ||
max: null | ||
valueRange: undefined | ||
}; | ||
var parseCurrency = function (formattedValue, options) { return parse(formattedValue, createCurrencyFormat$1(Object.assign({}, DEFAULT_OPTIONS, options)), options.valueAsInteger); }; | ||
var setValue = function (el, value) { return dispatchEvent(el, 'format', { value: value }); }; | ||
@@ -170,9 +185,9 @@ var setCaretPosition = function (el, position) { return el.setSelectionRange(position, position); }; | ||
}; | ||
var getDistractionFreeCaretPosition = function (formatConfig, options, value, caretPosition) { | ||
var getDistractionFreeCaretPosition = function (currencyFormat, options, value, caretPosition) { | ||
var result = caretPosition; | ||
if (options.distractionFree.hideCurrencySymbol) { | ||
result -= formatConfig.prefix.length; | ||
result -= currencyFormat.prefix.length; | ||
} | ||
if (options.distractionFree.hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), formatConfig.groupingSymbol); | ||
result -= count(value.substring(0, caretPosition), currencyFormat.groupingSymbol); | ||
} | ||
@@ -189,15 +204,13 @@ return Math.max(0, result) | ||
}; | ||
var checkIncompleteValue = function (value, negative, previousConformedValue, formatConfig) { | ||
var prefix = formatConfig.prefix; | ||
var negativePrefix = formatConfig.negativePrefix; | ||
var suffix = formatConfig.suffix; | ||
var decimalSymbol = formatConfig.decimalSymbol; | ||
var maximumFractionDigits = formatConfig.maximumFractionDigits; | ||
if (value === '' && negative && previousConformedValue !== negativePrefix) { | ||
return ("" + negativePrefix + suffix) | ||
var checkIncompleteValue = function (value, negative, previousConformedValue, currencyFormat, hideCurrencySymbol) { | ||
var negativePrefix = currencyFormat.negativePrefix; | ||
var decimalSymbol = currencyFormat.decimalSymbol; | ||
var maximumFractionDigits = currencyFormat.maximumFractionDigits; | ||
if (value === '' && negative && previousConformedValue !== (hideCurrencySymbol ? '-' : negativePrefix)) { | ||
return insertCurrencySymbol('', currencyFormat, negative, hideCurrencySymbol) | ||
} else if (maximumFractionDigits > 0) { | ||
if (isFractionIncomplete(value, formatConfig)) { | ||
return ("" + (negative ? negativePrefix : prefix) + value + suffix) | ||
if (isFractionIncomplete(value, currencyFormat)) { | ||
return insertCurrencySymbol(value, currencyFormat, negative, hideCurrencySymbol) | ||
} else if (startsWith(value, decimalSymbol)) { | ||
return ((negative ? negativePrefix : prefix) + "0" + decimalSymbol + ((onlyDigits(value.substr(1)).substr(0, maximumFractionDigits))) + suffix) | ||
return insertCurrencySymbol(("0" + decimalSymbol + ((onlyDigits(value.substr(1)).substr(0, maximumFractionDigits)))), currencyFormat, negative, hideCurrencySymbol) | ||
} | ||
@@ -213,3 +226,3 @@ } | ||
var negative = startsWith(value, '-'); | ||
var conformedValue = value === '-' ? Number(-0) : Number(("" + (negative ? '-' : '') + (removeLeadingZeros(onlyDigits(value))))) / Math.pow(10, minimumFractionDigits); | ||
var conformedValue = value === '-' ? -0 : Number(("" + (negative ? '-' : '') + (removeLeadingZeros(onlyDigits(value))))) / Math.pow(10, minimumFractionDigits); | ||
return { | ||
@@ -221,35 +234,31 @@ conformedValue: conformedValue, | ||
}; | ||
var isFractionInvalid = function (fraction, numberOfFractionDigits) { return fraction.length > 0 && numberOfFractionDigits === 0; }; | ||
function conformToMask (str, formatConfig, options, previousConformedValue) { | ||
function conformToMask (str, currencyFormat, previousConformedValue, hideCurrencySymbol, autoDecimalMode) { | ||
if ( previousConformedValue === void 0 ) previousConformedValue = ''; | ||
if ( hideCurrencySymbol === void 0 ) hideCurrencySymbol = false; | ||
if ( autoDecimalMode === void 0 ) autoDecimalMode = false; | ||
if (typeof str === 'string') { | ||
if (formatConfig.minimumFractionDigits > 0 && options.autoDecimalMode) { | ||
return getAutoDecimalModeConformedValue(str, previousConformedValue, formatConfig) | ||
if (currencyFormat.minimumFractionDigits > 0 && autoDecimalMode) { | ||
return getAutoDecimalModeConformedValue(str, previousConformedValue, currencyFormat) | ||
} | ||
var ref = stripCurrencySymbolAndMinusSign(str, formatConfig); | ||
var ref = stripCurrencySymbolAndMinusSign(str, currencyFormat); | ||
var value = ref.value; | ||
var negative = ref.negative; | ||
var incompleteValue = checkIncompleteValue(value, negative, previousConformedValue, formatConfig); | ||
var incompleteValue = checkIncompleteValue(value, negative, previousConformedValue, currencyFormat, hideCurrencySymbol); | ||
if (incompleteValue != null) { | ||
return { conformedValue: incompleteValue } | ||
} | ||
var ref$1 = value.split(formatConfig.decimalSymbol); | ||
var ref$1 = value.split(currencyFormat.decimalSymbol); | ||
var integer = ref$1[0]; | ||
var fraction = ref$1.slice(1); | ||
var integerDigits = removeLeadingZeros(onlyDigits(integer)); | ||
var fractionDigits = onlyDigits(fraction.join('')).substr(0, formatConfig.maximumFractionDigits); | ||
if (isFractionInvalid(fraction, fractionDigits.length)) { | ||
var fractionDigits = onlyDigits(fraction.join('')).substr(0, currencyFormat.maximumFractionDigits); | ||
var invalidFraction = fraction.length > 0 && fractionDigits.length === 0; | ||
var invalidNegativeValue = integerDigits === '' && negative && (previousConformedValue === str.slice(0, -1) || previousConformedValue !== currencyFormat.negativePrefix); | ||
if (invalidFraction || invalidNegativeValue) { | ||
return { conformedValue: previousConformedValue } | ||
} | ||
var number = integerDigits; | ||
if (negative) { | ||
number = "-" + number; | ||
} | ||
if (isNumber(number)) { | ||
} else if (isNumber(integerDigits)) { | ||
return { | ||
conformedValue: Number((number + "." + fractionDigits)), | ||
conformedValue: Number(("" + (negative ? '-' : '') + integerDigits + "." + fractionDigits)), | ||
fractionDigits: fractionDigits | ||
} | ||
} else if (number === '-' && (previousConformedValue === str.slice(0, -1) || previousConformedValue !== formatConfig.negativePrefix)) { | ||
return { conformedValue: previousConformedValue } | ||
} else { | ||
@@ -262,8 +271,2 @@ return { conformedValue: '' } | ||
function dispatchEvent (el, eventName, data) { | ||
var event = document.createEvent('CustomEvent'); | ||
event.initCustomEvent(eventName, true, true, data); | ||
el.dispatchEvent(event); | ||
} | ||
var equal = function (a, b) { | ||
@@ -294,4 +297,2 @@ if (a === b) { | ||
var options = Object.assign({}, ($CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS), optionsFromBinding); | ||
var min = options.min; | ||
var max = options.max; | ||
var distractionFree = options.distractionFree; | ||
@@ -309,5 +310,2 @@ var autoDecimalMode = options.autoDecimalMode; | ||
} | ||
if (min != null && max != null && min > max) { | ||
throw new Error('Invalid value range') | ||
} | ||
var currencyFormat = createCurrencyFormat$1(options); | ||
@@ -320,21 +318,29 @@ inputElement.$ci = Object.assign({}, inputElement.$ci || {}, | ||
}; | ||
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 | ||
}; | ||
var applyFixedFractionFormat = function (el, value) { | ||
var oldValue = el.value; | ||
if (value != null) { | ||
var ref = el.$ci.options; | ||
var min = ref.min; | ||
var max = ref.max; | ||
var valueRange = ref.valueRange; | ||
var locale = ref.locale; | ||
if (min != null && value < min) { | ||
value = min; | ||
} | ||
if (max != null && value > max) { | ||
value = max; | ||
} | ||
var ref$1 = el.$ci.currencyFormat; | ||
var maximumFractionDigits = ref$1.maximumFractionDigits; | ||
var minimumFractionDigits = ref$1.minimumFractionDigits; | ||
value = validateValueRange(value, valueRange); | ||
value = new Intl.NumberFormat(locale, { minimumFractionDigits: minimumFractionDigits, maximumFractionDigits: maximumFractionDigits }).format(value); | ||
} | ||
format(el, value); | ||
if (!el.$ci.inputEvent) { | ||
if (!el.$ci.inputEvent && oldValue !== el.value) { | ||
dispatchEvent(el, 'input'); | ||
@@ -348,29 +354,24 @@ } | ||
var focus = ref.focus; | ||
var options = ref.options; | ||
var ref_options = ref.options; | ||
var autoDecimalMode = ref_options.autoDecimalMode; | ||
var distractionFree = ref_options.distractionFree; | ||
var locale = ref_options.locale; | ||
var currencyFormat = ref.currencyFormat; | ||
var previousConformedValue = ref.previousConformedValue; | ||
var ref$1 = options.distractionFree; | ||
var hideCurrencySymbol = ref$1.hideCurrencySymbol; | ||
var hideGroupingSymbol = ref$1.hideGroupingSymbol; | ||
var formatConfig = Object.assign({}, currencyFormat); | ||
if (focus && hideCurrencySymbol) { | ||
formatConfig.prefix = ''; | ||
formatConfig.negativePrefix = '-'; | ||
formatConfig.suffix = ''; | ||
} | ||
var ref$2 = conformToMask(value, formatConfig, options, previousConformedValue); | ||
var conformedValue = ref$2.conformedValue; | ||
var fractionDigits = ref$2.fractionDigits; | ||
var hideCurrencySymbol = focus && distractionFree.hideCurrencySymbol; | ||
var ref$1 = conformToMask(value, currencyFormat, previousConformedValue, hideCurrencySymbol, autoDecimalMode); | ||
var conformedValue = ref$1.conformedValue; | ||
var fractionDigits = ref$1.fractionDigits; | ||
if (typeof conformedValue === 'number') { | ||
var formattedValue = new Intl.NumberFormat(options.locale, { | ||
useGrouping: !(focus && hideGroupingSymbol), | ||
minimumFractionDigits: hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : Math.min(formatConfig.minimumFractionDigits, fractionDigits.length), | ||
maximumFractionDigits: formatConfig.maximumFractionDigits | ||
var formattedValue = new Intl.NumberFormat(locale, { | ||
useGrouping: !(focus && distractionFree.hideGroupingSymbol), | ||
minimumFractionDigits: hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : Math.min(currencyFormat.minimumFractionDigits, fractionDigits.length), | ||
maximumFractionDigits: currencyFormat.maximumFractionDigits | ||
}).format(Math.abs(conformedValue)); | ||
var isNegativeZero = conformedValue === 0 && (1 / conformedValue < 0); | ||
el.value = "" + (isNegativeZero || conformedValue < 0 ? formatConfig.negativePrefix : formatConfig.prefix) + formattedValue + (formatConfig.suffix); | ||
el.value = insertCurrencySymbol(formattedValue, currencyFormat, isNegativeZero || conformedValue < 0, hideCurrencySymbol); | ||
el.$ci.numberValue = conformedValue; | ||
} else { | ||
el.value = conformedValue; | ||
el.$ci.numberValue = parse(el.value, formatConfig, false); | ||
el.$ci.numberValue = parse(el.value, currencyFormat, false); | ||
} | ||
@@ -383,13 +384,14 @@ } else { | ||
var format = function (el, value) { | ||
var oldValue = el.$ci.numberValue; | ||
updateInputValue(el, value); | ||
var ref = el.$ci; | ||
var numberValue = ref.numberValue; | ||
var newValue = ref.numberValue; | ||
var currencyFormat = ref.currencyFormat; | ||
var options = ref.options; | ||
var inputEvent = ref.inputEvent; | ||
if (numberValue != null) { | ||
numberValue = toInteger(numberValue, options.valueAsInteger, currencyFormat.maximumFractionDigits); | ||
if (newValue != null) { | ||
newValue = toInteger(newValue, options.valueAsInteger, currencyFormat.maximumFractionDigits); | ||
} | ||
if (inputEvent) { | ||
dispatchEvent(el, inputEvent, { numberValue: numberValue }); | ||
dispatchEvent(el, inputEvent, { oldValue: oldValue, newValue: newValue }); | ||
} | ||
@@ -474,2 +476,3 @@ }; | ||
function objectWithoutProperties (obj, exclude) { var target = {}; for (var k in obj) if (Object.prototype.hasOwnProperty.call(obj, k) && exclude.indexOf(k) === -1) target[k] = obj[k]; return target; } | ||
var inputEvent = 'format-complete'; | ||
var component = { | ||
@@ -484,5 +487,3 @@ render: function render (h) { | ||
value: this.options, | ||
modifiers: { | ||
inputEvent: 'format-complete' | ||
} | ||
modifiers: { inputEvent: inputEvent } | ||
}], | ||
@@ -525,9 +526,5 @@ on: this.listeners() | ||
}, | ||
min: { | ||
type: Number, | ||
valueRange: { | ||
type: Object, | ||
default: undefined | ||
}, | ||
max: { | ||
type: Number, | ||
default: undefined | ||
} | ||
@@ -556,7 +553,8 @@ }, | ||
methods: { | ||
setValue: function setValue (value) { | ||
dispatchEvent(this.$el, 'format', { value: value }); | ||
setValue: function setValue$1 (value) { | ||
setValue(this.$el, value); | ||
}, | ||
listeners: function listeners () { | ||
var this$1 = this; | ||
var obj; | ||
var ref = this.$listeners; | ||
@@ -567,7 +565,11 @@ var input = ref.input; | ||
return Object.assign({}, listeners, | ||
{'format-complete': function (ref) { | ||
var detail = ref.detail; | ||
this$1.$emit('input', detail.numberValue); | ||
( obj = {}, obj[inputEvent] = function (e) { | ||
var ref = e.detail; | ||
var oldValue = ref.oldValue; | ||
var newValue = ref.newValue; | ||
if (oldValue !== newValue) { | ||
this$1.$emit('input', newValue); | ||
} | ||
this$1.formattedValue = this$1.$el.value; | ||
}}) | ||
}, obj )) | ||
} | ||
@@ -583,8 +585,7 @@ } | ||
var globalOptions = ref.globalOptions; if ( globalOptions === void 0 ) globalOptions = {}; | ||
var defaultOptions = Object.assign({}, DEFAULT_OPTIONS, globalOptions); | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = defaultOptions; | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = Object.assign({}, DEFAULT_OPTIONS, globalOptions); | ||
Vue.component(componentName, component); | ||
Vue.directive(directiveName, directive); | ||
Vue.prototype.$parseCurrency = function (str, options) { | ||
if ( options === void 0 ) options = defaultOptions; | ||
if ( options === void 0 ) options = globalOptions; | ||
return parseCurrency(str, options); | ||
@@ -603,2 +604,3 @@ }; | ||
exports.parseCurrency = parseCurrency; | ||
exports.setValue = setValue; | ||
@@ -605,0 +607,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
{ | ||
"name": "vue-currency-input", | ||
"description": "Easy input of currency formatted numbers for Vue.js.", | ||
"version": "1.14.3", | ||
"version": "1.15.0", | ||
"license": "MIT", | ||
@@ -22,8 +22,12 @@ "unpkg": "dist/vue-currency-input.umd.js", | ||
}, | ||
"homepage": "https://dm4t2.github.io/vue-currency-input/", | ||
"homepage": "https://dm4t2.github.io/vue-currency-input", | ||
"keywords": [ | ||
"vue", | ||
"text mask", | ||
"input mask", | ||
"currency format", | ||
"i18n" | ||
"currency input", | ||
"money input", | ||
"format", | ||
"i18n", | ||
"ISO 4217" | ||
], | ||
@@ -43,21 +47,21 @@ "scripts": { | ||
"devDependencies": { | ||
"@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/cli-plugin-babel": "^4.1.1", | ||
"@vue/cli-plugin-eslint": "^4.1.1", | ||
"@vue/cli-plugin-unit-jest": "^4.1.1", | ||
"@vue/cli-service": "^4.1.1", | ||
"@vue/eslint-config-standard": "^4.0.0", | ||
"@vue/test-utils": "^1.0.0-beta.29", | ||
"@vue/test-utils": "^1.0.0-beta.30", | ||
"babel-eslint": "^10.0.3", | ||
"core-js": "^3.3.2", | ||
"core-js": "^3.5.0", | ||
"eslint": "^5.16.0", | ||
"eslint-plugin-vue": "^5.0.0", | ||
"node-sass": "^4.12.0", | ||
"rollup": "^1.21.3", | ||
"eslint-plugin-vue": "^6.0.1", | ||
"node-sass": "^4.13.0", | ||
"rollup": "^1.27.13", | ||
"rollup-plugin-buble": "^0.19.8", | ||
"rollup-plugin-cleanup": "^3.1.1", | ||
"sass-loader": "^8.0.0", | ||
"vue": "^2.6.10", | ||
"vue-template-compiler": "^2.6.10", | ||
"vuepress": "^1.1.0" | ||
"vue": "^2.6.11", | ||
"vue-template-compiler": "^2.6.11", | ||
"vuepress": "^1.2.0" | ||
} | ||
} |
import createCurrencyFormat from './utils/createCurrencyFormat' | ||
import parse from './utils/parse' | ||
import dispatchEvent from './utils/dispatchEvent' | ||
export const DEFAULT_OPTIONS = { | ||
locale: undefined, | ||
currency: 'EUR', | ||
valueAsInteger: false, | ||
distractionFree: true, | ||
precision: undefined, | ||
autoDecimalMode: false, | ||
valueRange: undefined | ||
} | ||
/** | ||
@@ -11,2 +22,10 @@ * Parses a number from a currency formatted string. | ||
*/ | ||
export const parseCurrency = (formattedValue, options) => parse(formattedValue, createCurrencyFormat(options), options.valueAsInteger) | ||
export const parseCurrency = (formattedValue, options) => parse(formattedValue, createCurrencyFormat({ ...DEFAULT_OPTIONS, ...options }), options.valueAsInteger) | ||
/** | ||
* Sets a value of a input programmatically. | ||
* | ||
* @param {HTMLInputElement} el An input element using on the `v-currency` directive. | ||
* @param {Number} value The number to be set. | ||
*/ | ||
export const setValue = (el, value) => dispatchEvent(el, 'format', { value }) |
@@ -1,5 +0,6 @@ | ||
import defaultOptions from './defaultOptions' | ||
import currencyDirective from './directive' | ||
import dispatchEvent from './utils/dispatchEvent' | ||
import { DEFAULT_OPTIONS, setValue } from './api' | ||
const inputEvent = 'format-complete' | ||
export default { | ||
@@ -14,5 +15,3 @@ render (h) { | ||
value: this.options, | ||
modifiers: { | ||
inputEvent: 'format-complete' | ||
} | ||
modifiers: { inputEvent } | ||
}], | ||
@@ -55,9 +54,5 @@ on: this.listeners() | ||
}, | ||
min: { | ||
type: Number, | ||
valueRange: { | ||
type: Object, | ||
default: undefined | ||
}, | ||
max: { | ||
type: Number, | ||
default: undefined | ||
} | ||
@@ -72,4 +67,4 @@ }, | ||
options () { | ||
const options = { ...this.$CI_DEFAULT_OPTIONS || defaultOptions } | ||
Object.keys(defaultOptions).forEach((key) => { | ||
const options = { ...this.$CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS } | ||
Object.keys(DEFAULT_OPTIONS).forEach(key => { | ||
if (this[key] !== undefined) { | ||
@@ -87,3 +82,3 @@ options[key] = this[key] | ||
setValue (value) { | ||
dispatchEvent(this.$el, 'format', { value }) | ||
setValue(this.$el, value) | ||
}, | ||
@@ -94,4 +89,7 @@ listeners () { | ||
...listeners, | ||
'format-complete': ({ detail }) => { | ||
this.$emit('input', detail.numberValue) | ||
[inputEvent]: (e) => { | ||
const { oldValue, newValue } = e.detail | ||
if (oldValue !== newValue) { | ||
this.$emit('input', newValue) | ||
} | ||
this.formattedValue = this.$el.value | ||
@@ -98,0 +96,0 @@ } |
import Vue from 'vue' | ||
import defaultOptions from './defaultOptions' | ||
import { getCaretPositionAfterFormat, getDistractionFreeCaretPosition, setCaretPosition } from './utils/caretPosition' | ||
@@ -10,2 +9,4 @@ import conformToMask from './utils/conformToMask' | ||
import { toFloat, toInteger } from './utils/numberUtils' | ||
import { DEFAULT_OPTIONS } from './api' | ||
import { insertCurrencySymbol } from './utils/stringUtils' | ||
@@ -17,4 +18,4 @@ const init = (el, optionsFromBinding, { inputEvent }, { $CI_DEFAULT_OPTIONS }) => { | ||
} | ||
const options = { ...($CI_DEFAULT_OPTIONS || defaultOptions), ...optionsFromBinding } | ||
const { min, max, distractionFree, autoDecimalMode } = options | ||
const options = { ...($CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS), ...optionsFromBinding } | ||
const { distractionFree, autoDecimalMode } = options | ||
if (typeof distractionFree === 'boolean') { | ||
@@ -30,5 +31,2 @@ options.distractionFree = { | ||
} | ||
if (min != null && max != null && min > max) { | ||
throw new Error('Invalid value range') | ||
} | ||
const currencyFormat = createCurrencyFormat(options) | ||
@@ -44,16 +42,25 @@ inputElement.$ci = { | ||
const applyFixedFractionFormat = (el, value) => { | ||
if (value != null) { | ||
const { min, max, locale } = el.$ci.options | ||
if (min != null && value < min) { | ||
const validateValueRange = (value, valueRange) => { | ||
if (valueRange) { | ||
const { min, max } = valueRange | ||
if (min !== undefined && value < min) { | ||
value = min | ||
} | ||
if (max != null && value > max) { | ||
if (max !== undefined && value > max) { | ||
value = max | ||
} | ||
} | ||
return value | ||
} | ||
const applyFixedFractionFormat = (el, value) => { | ||
const oldValue = el.value | ||
if (value != null) { | ||
const { valueRange, locale } = el.$ci.options | ||
const { maximumFractionDigits, minimumFractionDigits } = el.$ci.currencyFormat | ||
value = validateValueRange(value, valueRange) | ||
value = new Intl.NumberFormat(locale, { minimumFractionDigits, maximumFractionDigits }).format(value) | ||
} | ||
format(el, value) | ||
if (!el.$ci.inputEvent) { | ||
if (!el.$ci.inputEvent && oldValue !== el.value) { | ||
dispatchEvent(el, 'input') | ||
@@ -65,23 +72,17 @@ } | ||
if (value != null) { | ||
const { focus, options, currencyFormat, previousConformedValue } = el.$ci | ||
const { hideCurrencySymbol, hideGroupingSymbol } = options.distractionFree | ||
const formatConfig = { ...currencyFormat } | ||
if (focus && hideCurrencySymbol) { | ||
formatConfig.prefix = '' | ||
formatConfig.negativePrefix = '-' | ||
formatConfig.suffix = '' | ||
} | ||
const { conformedValue, fractionDigits } = conformToMask(value, formatConfig, options, previousConformedValue) | ||
const { focus, options: { autoDecimalMode, distractionFree, locale }, currencyFormat, previousConformedValue } = el.$ci | ||
const hideCurrencySymbol = focus && distractionFree.hideCurrencySymbol | ||
const { conformedValue, fractionDigits } = conformToMask(value, currencyFormat, previousConformedValue, hideCurrencySymbol, autoDecimalMode) | ||
if (typeof conformedValue === 'number') { | ||
const formattedValue = new Intl.NumberFormat(options.locale, { | ||
useGrouping: !(focus && hideGroupingSymbol), | ||
minimumFractionDigits: hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : Math.min(formatConfig.minimumFractionDigits, fractionDigits.length), | ||
maximumFractionDigits: formatConfig.maximumFractionDigits | ||
const formattedValue = new Intl.NumberFormat(locale, { | ||
useGrouping: !(focus && distractionFree.hideGroupingSymbol), | ||
minimumFractionDigits: hideNegligibleDecimalDigits ? fractionDigits.replace(/0+$/, '').length : Math.min(currencyFormat.minimumFractionDigits, fractionDigits.length), | ||
maximumFractionDigits: currencyFormat.maximumFractionDigits | ||
}).format(Math.abs(conformedValue)) | ||
const isNegativeZero = conformedValue === 0 && (1 / conformedValue < 0) | ||
el.value = `${isNegativeZero || conformedValue < 0 ? formatConfig.negativePrefix : formatConfig.prefix}${formattedValue}${formatConfig.suffix}` | ||
el.value = insertCurrencySymbol(formattedValue, currencyFormat, isNegativeZero || conformedValue < 0, hideCurrencySymbol) | ||
el.$ci.numberValue = conformedValue | ||
} else { | ||
el.value = conformedValue | ||
el.$ci.numberValue = parse(el.value, formatConfig, false) | ||
el.$ci.numberValue = parse(el.value, currencyFormat, false) | ||
} | ||
@@ -95,9 +96,10 @@ } else { | ||
const format = (el, value) => { | ||
const oldValue = el.$ci.numberValue | ||
updateInputValue(el, value) | ||
let { numberValue, currencyFormat, options, inputEvent } = el.$ci | ||
if (numberValue != null) { | ||
numberValue = toInteger(numberValue, options.valueAsInteger, currencyFormat.maximumFractionDigits) | ||
let { numberValue: newValue, currencyFormat, options, inputEvent } = el.$ci | ||
if (newValue != null) { | ||
newValue = toInteger(newValue, options.valueAsInteger, currencyFormat.maximumFractionDigits) | ||
} | ||
if (inputEvent) { | ||
dispatchEvent(el, inputEvent, { numberValue }) | ||
dispatchEvent(el, inputEvent, { oldValue, newValue }) | ||
} | ||
@@ -104,0 +106,0 @@ } |
@@ -1,36 +0,43 @@ | ||
import Vue, { Component, DirectiveOptions } from 'vue' | ||
import { Component, DirectiveOptions, PluginFunction } from 'vue' | ||
interface CurrencyOptions { | ||
prefix: string, | ||
suffix: string | ||
export interface CurrencyOptions { | ||
prefix?: string, | ||
suffix?: string | ||
} | ||
interface DistractionFreeOptions { | ||
hideCurrencySymbol: boolean, | ||
hideGroupingSymbol: boolean, | ||
hideNegligibleDecimalDigits: boolean | ||
export interface DistractionFreeOptions { | ||
hideCurrencySymbol?: boolean, | ||
hideGroupingSymbol?: boolean, | ||
hideNegligibleDecimalDigits?: boolean | ||
} | ||
interface PrecisionOptions { | ||
min: number, | ||
max: number | ||
export interface NumberRange { | ||
min?: number, | ||
max?: number | ||
} | ||
interface CurrencyInputOptions { | ||
locale: string, | ||
currency: string | CurrencyOptions, | ||
valueAsInteger: boolean, | ||
distractionFree: boolean | DistractionFreeOptions, | ||
precision: number | PrecisionOptions, | ||
autoDecimalMode: boolean, | ||
min: number, | ||
max: number | ||
export interface CurrencyInputOptions { | ||
locale?: string, | ||
currency?: string | CurrencyOptions, | ||
valueAsInteger?: boolean, | ||
distractionFree?: boolean | DistractionFreeOptions, | ||
precision?: number | NumberRange, | ||
autoDecimalMode?: boolean, | ||
valueRange?: | NumberRange, | ||
} | ||
interface PluginOptions { | ||
globalOptions: CurrencyInputOptions, | ||
componentName: string, | ||
directiveName: string | ||
export interface PluginOptions { | ||
globalOptions?: CurrencyInputOptions, | ||
componentName?: string, | ||
directiveName?: string | ||
} | ||
export interface VueCurrencyInput { | ||
install: PluginFunction<PluginOptions> | ||
} | ||
declare const VueCurrencyInput: VueCurrencyInput | ||
export default VueCurrencyInput | ||
export const CurrencyDirective: DirectiveOptions | ||
@@ -42,3 +49,3 @@ | ||
export function install (vue: typeof Vue, options: PluginOptions): void | ||
export function setValue (el: HTMLInputElement, value: Number): void | ||
@@ -45,0 +52,0 @@ declare module 'vue/types/vue' { |
@@ -1,4 +0,3 @@ | ||
import { parseCurrency } from './api' | ||
import { DEFAULT_OPTIONS, parseCurrency } from './api' | ||
import component from './component' | ||
import DEFAULT_OPTIONS from './defaultOptions' | ||
import directive from './directive' | ||
@@ -12,8 +11,7 @@ | ||
} = {}) { | ||
const defaultOptions = { ...DEFAULT_OPTIONS, ...globalOptions } | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = defaultOptions | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = { ...DEFAULT_OPTIONS, ...globalOptions } | ||
Vue.component(componentName, component) | ||
Vue.directive(directiveName, directive) | ||
Vue.prototype.$parseCurrency = (str, options = defaultOptions) => parseCurrency(str, options) | ||
Vue.prototype.$parseCurrency = (str, options = globalOptions) => parseCurrency(str, options) | ||
} | ||
} |
@@ -26,11 +26,11 @@ import { count, onlyDigits } from './stringUtils' | ||
export const getDistractionFreeCaretPosition = (formatConfig, options, value, caretPosition) => { | ||
export const getDistractionFreeCaretPosition = (currencyFormat, options, value, caretPosition) => { | ||
let result = caretPosition | ||
if (options.distractionFree.hideCurrencySymbol) { | ||
result -= formatConfig.prefix.length | ||
result -= currencyFormat.prefix.length | ||
} | ||
if (options.distractionFree.hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), formatConfig.groupingSymbol) | ||
result -= count(value.substring(0, caretPosition), currencyFormat.groupingSymbol) | ||
} | ||
return Math.max(0, result) | ||
} |
@@ -1,2 +0,2 @@ | ||
import { endsWith, isNumber, onlyDigits, removeLeadingZeros, startsWith, stripCurrencySymbolAndMinusSign } from './stringUtils' | ||
import { endsWith, insertCurrencySymbol, isNumber, onlyDigits, removeLeadingZeros, startsWith, stripCurrencySymbolAndMinusSign } from './stringUtils' | ||
@@ -10,11 +10,11 @@ const isValidInteger = (integer, groupingSymbol) => integer.match(new RegExp(`^-?(0|[1-9]\\d{0,2}(\\${groupingSymbol}?\\d{3})*)$`)) | ||
const checkIncompleteValue = (value, negative, previousConformedValue, formatConfig) => { | ||
const { prefix, negativePrefix, suffix, decimalSymbol, maximumFractionDigits } = formatConfig | ||
if (value === '' && negative && previousConformedValue !== negativePrefix) { | ||
return `${negativePrefix}${suffix}` | ||
const checkIncompleteValue = (value, negative, previousConformedValue, currencyFormat, hideCurrencySymbol) => { | ||
let { negativePrefix, decimalSymbol, maximumFractionDigits } = currencyFormat | ||
if (value === '' && negative && previousConformedValue !== (hideCurrencySymbol ? '-' : negativePrefix)) { | ||
return insertCurrencySymbol('', currencyFormat, negative, hideCurrencySymbol) | ||
} else if (maximumFractionDigits > 0) { | ||
if (isFractionIncomplete(value, formatConfig)) { | ||
return `${negative ? negativePrefix : prefix}${value}${suffix}` | ||
if (isFractionIncomplete(value, currencyFormat)) { | ||
return insertCurrencySymbol(value, currencyFormat, negative, hideCurrencySymbol) | ||
} else if (startsWith(value, decimalSymbol)) { | ||
return `${negative ? negativePrefix : prefix}0${decimalSymbol}${(onlyDigits(value.substr(1)).substr(0, maximumFractionDigits))}${suffix}` | ||
return insertCurrencySymbol(`0${decimalSymbol}${(onlyDigits(value.substr(1)).substr(0, maximumFractionDigits))}`, currencyFormat, negative, hideCurrencySymbol) | ||
} | ||
@@ -30,3 +30,3 @@ } | ||
const negative = startsWith(value, '-') | ||
const conformedValue = value === '-' ? Number(-0) : Number(`${negative ? '-' : ''}${removeLeadingZeros(onlyDigits(value))}`) / Math.pow(10, minimumFractionDigits) | ||
const conformedValue = value === '-' ? -0 : Number(`${negative ? '-' : ''}${removeLeadingZeros(onlyDigits(value))}`) / Math.pow(10, minimumFractionDigits) | ||
return { | ||
@@ -39,12 +39,10 @@ conformedValue, | ||
const isFractionInvalid = (fraction, numberOfFractionDigits) => fraction.length > 0 && numberOfFractionDigits === 0 | ||
export default (str, formatConfig, options, previousConformedValue = '') => { | ||
export default (str, currencyFormat, previousConformedValue = '', hideCurrencySymbol = false, autoDecimalMode = false) => { | ||
if (typeof str === 'string') { | ||
if (formatConfig.minimumFractionDigits > 0 && options.autoDecimalMode) { | ||
return getAutoDecimalModeConformedValue(str, previousConformedValue, formatConfig) | ||
if (currencyFormat.minimumFractionDigits > 0 && autoDecimalMode) { | ||
return getAutoDecimalModeConformedValue(str, previousConformedValue, currencyFormat) | ||
} | ||
const { value, negative } = stripCurrencySymbolAndMinusSign(str, formatConfig) | ||
const incompleteValue = checkIncompleteValue(value, negative, previousConformedValue, formatConfig) | ||
const { value, negative } = stripCurrencySymbolAndMinusSign(str, currencyFormat) | ||
const incompleteValue = checkIncompleteValue(value, negative, previousConformedValue, currencyFormat, hideCurrencySymbol) | ||
if (incompleteValue != null) { | ||
@@ -54,21 +52,15 @@ return { conformedValue: incompleteValue } | ||
const [integer, ...fraction] = value.split(formatConfig.decimalSymbol) | ||
const [integer, ...fraction] = value.split(currencyFormat.decimalSymbol) | ||
const integerDigits = removeLeadingZeros(onlyDigits(integer)) | ||
const fractionDigits = onlyDigits(fraction.join('')).substr(0, formatConfig.maximumFractionDigits) | ||
const fractionDigits = onlyDigits(fraction.join('')).substr(0, currencyFormat.maximumFractionDigits) | ||
const invalidFraction = fraction.length > 0 && fractionDigits.length === 0 | ||
const invalidNegativeValue = integerDigits === '' && negative && (previousConformedValue === str.slice(0, -1) || previousConformedValue !== currencyFormat.negativePrefix) | ||
if (isFractionInvalid(fraction, fractionDigits.length)) { | ||
if (invalidFraction || invalidNegativeValue) { | ||
return { conformedValue: previousConformedValue } | ||
} | ||
let number = integerDigits | ||
if (negative) { | ||
number = `-${number}` | ||
} | ||
if (isNumber(number)) { | ||
} else if (isNumber(integerDigits)) { | ||
return { | ||
conformedValue: Number(`${number}.${fractionDigits}`), | ||
conformedValue: Number(`${negative ? '-' : ''}${integerDigits}.${fractionDigits}`), | ||
fractionDigits | ||
} | ||
} else if (number === '-' && (previousConformedValue === str.slice(0, -1) || previousConformedValue !== formatConfig.negativePrefix)) { | ||
return { conformedValue: previousConformedValue } | ||
} else { | ||
@@ -75,0 +67,0 @@ return { conformedValue: '' } |
@@ -15,2 +15,11 @@ export const removeLeadingZeros = (str) => str.replace(/^0+(0$|[^0])/, '$1') | ||
export const insertCurrencySymbol = (value, currencyFormat, negative, hideCurrencySymbol) => { | ||
let { prefix, negativePrefix, suffix } = currencyFormat | ||
if (hideCurrencySymbol) { | ||
prefix = suffix = '' | ||
negativePrefix = '-' | ||
} | ||
return `${negative ? negativePrefix : prefix}${value}${suffix}` | ||
} | ||
export const stripCurrencySymbolAndMinusSign = (str, { prefix, suffix }) => { | ||
@@ -17,0 +26,0 @@ const value = str.replace(prefix, '').replace(suffix, '') |
73018
1714
21