vue-currency-input
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -442,14 +442,2 @@ module.exports = | ||
/***/ "3846": | ||
/***/ (function(module, exports, __webpack_require__) { | ||
// 21.2.5.3 get RegExp.prototype.flags() | ||
if (__webpack_require__("9e1e") && /./g.flags != 'g') __webpack_require__("86cc").f(RegExp.prototype, 'flags', { | ||
configurable: true, | ||
get: __webpack_require__("0bfb") | ||
}); | ||
/***/ }), | ||
/***/ "3b2b": | ||
@@ -730,35 +718,2 @@ /***/ (function(module, exports, __webpack_require__) { | ||
/***/ "6b54": | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
__webpack_require__("3846"); | ||
var anObject = __webpack_require__("cb7c"); | ||
var $flags = __webpack_require__("0bfb"); | ||
var DESCRIPTORS = __webpack_require__("9e1e"); | ||
var TO_STRING = 'toString'; | ||
var $toString = /./[TO_STRING]; | ||
var define = function (fn) { | ||
__webpack_require__("2aba")(RegExp.prototype, TO_STRING, fn, true); | ||
}; | ||
// 21.2.5.14 RegExp.prototype.toString() | ||
if (__webpack_require__("79e5")(function () { return $toString.call({ source: 'a', flags: 'b' }) != '/a/b'; })) { | ||
define(function toString() { | ||
var R = anObject(this); | ||
return '/'.concat(R.source, '/', | ||
'flags' in R ? R.flags : !DESCRIPTORS && R instanceof RegExp ? $flags.call(R) : undefined); | ||
}); | ||
// FF44- RegExp#toString has a wrong name | ||
} else if ($toString.name != TO_STRING) { | ||
define(function toString() { | ||
return $toString.call(this); | ||
}); | ||
} | ||
/***/ }), | ||
/***/ "7726": | ||
@@ -1278,2 +1233,18 @@ /***/ (function(module, exports) { | ||
/***/ "ee1d": | ||
/***/ (function(module, exports, __webpack_require__) { | ||
// 20.1.2.4 Number.isNaN(number) | ||
var $export = __webpack_require__("5ca1"); | ||
$export($export.S, 'Number', { | ||
isNaN: function isNaN(number) { | ||
// eslint-disable-next-line no-self-compare | ||
return number != number; | ||
} | ||
}); | ||
/***/ }), | ||
/***/ "f559": | ||
@@ -1333,3 +1304,3 @@ /***/ (function(module, exports, __webpack_require__) { | ||
// CONCATENATED MODULE: ./node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"node_modules//.cache//vue-loader","cacheIdentifier":"ca0e2238-vue-loader-template"}!./node_modules/vue-loader/lib/loaders/templateLoader.js??vue-loader-options!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/CurrencyInput.vue?vue&type=template&id=0f171a3d& | ||
// CONCATENATED MODULE: ./node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"node_modules//.cache//vue-loader","cacheIdentifier":"ca0e2238-vue-loader-template"}!./node_modules/vue-loader/lib/loaders/templateLoader.js??vue-loader-options!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/CurrencyInput.vue?vue&type=template&id=297e1838& | ||
var render = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('input',_vm._g({domProps:{"value":_vm.formattedValue}},_vm.listeners))} | ||
@@ -1339,7 +1310,4 @@ var staticRenderFns = [] | ||
// CONCATENATED MODULE: ./src/components/CurrencyInput.vue?vue&type=template&id=0f171a3d& | ||
// CONCATENATED MODULE: ./src/components/CurrencyInput.vue?vue&type=template&id=297e1838& | ||
// EXTERNAL MODULE: ./node_modules/core-js/modules/es6.regexp.split.js | ||
var es6_regexp_split = __webpack_require__("28a5"); | ||
// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/defineProperty.js | ||
@@ -1386,2 +1354,5 @@ function _defineProperty(obj, key, value) { | ||
// EXTERNAL MODULE: ./node_modules/core-js/modules/es6.regexp.replace.js | ||
var es6_regexp_replace = __webpack_require__("a481"); | ||
// EXTERNAL MODULE: ./node_modules/core-js/modules/es6.regexp.constructor.js | ||
@@ -1393,8 +1364,63 @@ var es6_regexp_constructor = __webpack_require__("3b2b"); | ||
// EXTERNAL MODULE: ./node_modules/core-js/modules/es6.regexp.replace.js | ||
var es6_regexp_replace = __webpack_require__("a481"); | ||
// EXTERNAL MODULE: ./node_modules/core-js/modules/es6.string.starts-with.js | ||
var es6_string_starts_with = __webpack_require__("f559"); | ||
// EXTERNAL MODULE: ./node_modules/core-js/modules/es6.regexp.to-string.js | ||
var es6_regexp_to_string = __webpack_require__("6b54"); | ||
// EXTERNAL MODULE: ./node_modules/core-js/modules/es6.regexp.split.js | ||
var es6_regexp_split = __webpack_require__("28a5"); | ||
// EXTERNAL MODULE: ./node_modules/core-js/modules/es6.number.is-nan.js | ||
var es6_number_is_nan = __webpack_require__("ee1d"); | ||
// EXTERNAL MODULE: ./node_modules/core-js/modules/es6.string.ends-with.js | ||
var es6_string_ends_with = __webpack_require__("aef6"); | ||
// CONCATENATED MODULE: ./src/utils/formatHelper.js | ||
var onlyDigits = function onlyDigits(str) { | ||
return str.replace(/\D+/g, ''); | ||
}; | ||
var removePrefix = function removePrefix(str, prefix) { | ||
if (prefix && str.startsWith(prefix)) { | ||
return str.substr(prefix.length); | ||
} | ||
return str; | ||
}; | ||
var removeSuffix = function removeSuffix(str, suffix) { | ||
if (suffix && str.endsWith(suffix)) { | ||
return str.slice(0, suffix.length * -1); | ||
} | ||
return str; | ||
}; | ||
var parse = function parse(str) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
decimalSymbol = _ref.decimalSymbol, | ||
_ref$allowNegative = _ref.allowNegative, | ||
allowNegative = _ref$allowNegative === void 0 ? true : _ref$allowNegative; | ||
if (str && str.trim().length > 0) { | ||
var negative = str.startsWith('-') && allowNegative; | ||
var numberParts = str.split(decimalSymbol); | ||
var number = onlyDigits(numberParts[0]); | ||
if (negative) { | ||
number = '-' + number; | ||
} | ||
if (numberParts.length === 2) { | ||
number += '.' + onlyDigits(numberParts[1]); | ||
} | ||
number = Number(number); | ||
return Number.isNaN(number) ? null : number; | ||
} | ||
return null; | ||
}; | ||
// CONCATENATED MODULE: ./src/utils/createNumberMask.js | ||
@@ -1406,3 +1432,3 @@ | ||
var nonDigitsRegExp = /\D+/g; | ||
var digitRegExp = /\d/; | ||
@@ -1417,3 +1443,3 @@ var caretTrap = '[]'; | ||
var createNumberMask = function createNumberMask() { | ||
var createNumberMask_createNumberMask = function createNumberMask() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
@@ -1431,3 +1457,3 @@ _ref$prefix = _ref.prefix, | ||
_ref$allowNegative = _ref.allowNegative, | ||
allowNegative = _ref$allowNegative === void 0 ? false : _ref$allowNegative, | ||
allowNegative = _ref$allowNegative === void 0 ? true : _ref$allowNegative, | ||
_ref$decimalSymbol = _ref.decimalSymbol, | ||
@@ -1445,26 +1471,23 @@ decimalSymbol = _ref$decimalSymbol === void 0 ? '.' : _ref$decimalSymbol, | ||
var rawValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
var rawValueLength = rawValue.length; | ||
var indexOfLastDecimal = rawValue.lastIndexOf(decimalSymbol); | ||
var hasDecimal = indexOfLastDecimal !== -1; | ||
var isNegative = rawValue[0] === '-' && allowNegative; | ||
var integer, fraction, mask; | ||
if (isNegative) { | ||
rawValue = rawValue.toString().substr(1); | ||
if (rawValue === '' || rawValue[0] === prefix[0] && rawValue.length === 1) { | ||
return prefix.split('').concat([digitRegExp]).concat(suffix.split('')); | ||
} else if (rawValue === decimalSymbol && allowDecimal) { | ||
return prefix.split('').concat(['0', decimalSymbol, digitRegExp]).concat(suffix.split('')); | ||
} | ||
if (rawValue.slice(suffixLength * -1) === suffix) { | ||
rawValue = rawValue.slice(0, suffixLength * -1); | ||
} | ||
var indexOfDecimalSymbol = rawValue.indexOf(decimalSymbol); | ||
var hasDecimal = indexOfDecimalSymbol !== -1; | ||
var negative = rawValue.startsWith('-') && allowNegative; | ||
var integer, fraction; | ||
rawValue = removePrefix(rawValue, '-'); | ||
rawValue = removePrefix(rawValue, prefix); | ||
rawValue = removeSuffix(rawValue, suffix); | ||
if (hasDecimal && allowDecimal) { | ||
integer = rawValue.slice(rawValue.slice(0, prefixLength) === prefix ? prefixLength : 0, indexOfLastDecimal); | ||
fraction = rawValue.slice(indexOfLastDecimal + 1, rawValueLength); | ||
fraction = convertToMask(fraction.replace(nonDigitsRegExp, '')); | ||
var numberParts = rawValue.split(decimalSymbol); | ||
integer = numberParts[0]; | ||
fraction = convertToMask(onlyDigits(numberParts[1])); | ||
} else { | ||
if (rawValue.slice(0, prefixLength) === prefix) { | ||
integer = rawValue.slice(prefixLength); | ||
} else { | ||
integer = rawValue; | ||
} | ||
integer = rawValue; | ||
} | ||
@@ -1478,9 +1501,9 @@ | ||
integer = integer.replace(nonDigitsRegExp, ''); | ||
integer = onlyDigits(integer); | ||
integer = integer.replace(/^0+(0$|[^0])/, '$1'); | ||
integer = includeThousandsSeparator ? integer.replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparatorSymbol) : integer; | ||
mask = convertToMask(integer); | ||
var mask = convertToMask(integer); | ||
if (hasDecimal && allowDecimal) { | ||
if (rawValue[indexOfLastDecimal - 1] !== decimalSymbol) { | ||
if (rawValue[indexOfDecimalSymbol - 1] !== decimalSymbol) { | ||
mask.push(caretTrap); | ||
@@ -1504,7 +1527,11 @@ } | ||
if (isNegative) { | ||
if (negative) { | ||
if (mask.length === prefixLength) { | ||
mask.push(digitRegExp); | ||
} | ||
mask = [/-/].concat(mask); | ||
} | ||
if (suffix.length > 0) { | ||
if (suffixLength > 0) { | ||
mask = mask.concat(suffix.split('')); | ||
@@ -1517,34 +1544,6 @@ } | ||
/* harmony default export */ var utils_createNumberMask = (createNumberMask); | ||
// EXTERNAL MODULE: ./node_modules/core-js/modules/es6.string.ends-with.js | ||
var es6_string_ends_with = __webpack_require__("aef6"); | ||
// EXTERNAL MODULE: ./node_modules/core-js/modules/es6.string.starts-with.js | ||
var es6_string_starts_with = __webpack_require__("f559"); | ||
// CONCATENATED MODULE: ./src/utils/formatHelper.js | ||
var onlyDigits = function onlyDigits(str) { | ||
return str.replace(/\D+/g, ''); | ||
}; | ||
var removePrefix = function removePrefix(str, prefix) { | ||
if (prefix.length && str.startsWith(prefix)) { | ||
return str.substr(prefix.length); | ||
} | ||
return str; | ||
}; | ||
var removeSuffix = function removeSuffix(str, suffix) { | ||
if (suffix.length && str.endsWith(suffix)) { | ||
return str.slice(0, suffix.length * -1); | ||
} | ||
return str; | ||
}; | ||
/* harmony default export */ var utils_createNumberMask = (createNumberMask_createNumberMask); | ||
// CONCATENATED MODULE: ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/thread-loader/dist/cjs.js!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/CurrencyInput.vue?vue&type=script&lang=js& | ||
// | ||
@@ -1577,2 +1576,6 @@ // | ||
default: true | ||
}, | ||
allowNegative: { | ||
type: Boolean, | ||
default: true | ||
} | ||
@@ -1595,4 +1598,4 @@ }, | ||
}, | ||
focus: function focus() { | ||
return _this.handleFocus(); | ||
focus: function focus(e) { | ||
return _this.handleFocus(e); | ||
}, | ||
@@ -1612,2 +1615,3 @@ blur: function blur() { | ||
thousandsSeparatorSymbol: formattedNumber.substr(formattedNumber.indexOf('1') + 1, 1), | ||
allowNegative: this.allowNegative, | ||
allowDecimal: allowDecimal | ||
@@ -1619,2 +1623,3 @@ }; | ||
inputElement: this.$el, | ||
guide: false, | ||
mask: utils_createNumberMask(this.config) | ||
@@ -1641,2 +1646,5 @@ }); | ||
}, | ||
allowNegative: function allowNegative() { | ||
this.updateValue(this.formattedValue); | ||
}, | ||
locale: 'applyFixedFractionFormat', | ||
@@ -1651,3 +1659,2 @@ currency: 'applyFixedFractionFormat' | ||
this.updateValue(e.target.value); | ||
this.$emit('input', this.numberValue); | ||
}, | ||
@@ -1658,38 +1665,21 @@ handleBlur: function handleBlur() { | ||
}, | ||
handleFocus: function handleFocus() { | ||
handleFocus: function handleFocus(e) { | ||
var _this2 = this; | ||
this.focus = true; | ||
this.$nextTick(function () { | ||
var caretPosition = e.target.selectionStart - _this2.config.prefix.length; | ||
_this2.focus = true; | ||
if (this.numberValue !== null) { | ||
if (this.distractionFree) { | ||
this.format(new Intl.NumberFormat(this.locale).format(this.numberValue)); | ||
if (_this2.numberValue !== null && _this2.distractionFree) { | ||
_this2.format(new Intl.NumberFormat(_this2.locale).format(_this2.numberValue)); | ||
_this2.setCaretPosition(Math.max(0, caretPosition)); | ||
} | ||
this.$nextTick(function () { | ||
return _this2.setCaretPosition(_this2.formattedValue.length - _this2.config.suffix.length); | ||
}); | ||
} | ||
}); | ||
}, | ||
updateValue: function updateValue(value) { | ||
this.format(value); | ||
this.numberValue = this.parse(this.formattedValue); | ||
this.numberValue = parse(this.formattedValue, this.config); | ||
this.$emit('input', this.numberValue); | ||
}, | ||
parse: function parse(str) { | ||
if (str.length === 0) { | ||
return null; | ||
} | ||
str = removePrefix(str, this.config.prefix); | ||
str = removeSuffix(str, this.config.suffix); | ||
if (this.config.allowDecimal && str.indexOf(this.config.decimalSymbol) !== -1) { | ||
var numberParts = str.split(this.config.decimalSymbol); | ||
var intDigits = onlyDigits(numberParts[0]); | ||
var fractionDigits = numberParts[1]; | ||
return Number("".concat(intDigits, ".").concat(fractionDigits)); | ||
} else { | ||
return Number(onlyDigits(str)); | ||
} | ||
}, | ||
format: function format(value) { | ||
@@ -1700,3 +1690,5 @@ this.textMaskInputElement.update(value); | ||
applyFixedFractionFormat: function applyFixedFractionFormat() { | ||
if (this.numberValue !== null) { | ||
if (this.numberValue === null) { | ||
this.formattedValue = ''; | ||
} else { | ||
this.format(this.fixedFractionNumberFormat.format(this.numberValue.toFixed(2))); | ||
@@ -1703,0 +1695,0 @@ } |
{ | ||
"name": "vue-currency-input", | ||
"description": "A component for Vue.js proving an easy input of currency formatted numbers.", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
"main": "dist/vue-currency-input.common.js", | ||
"files": ["dist"], | ||
"files": [ | ||
"dist" | ||
], | ||
"author": "Matthias Stiller", | ||
@@ -13,4 +15,11 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/dm4t2/vue-currency-input", | ||
"keywords": ["vue", "input mask", "text mask", "money", "currency format", "i18n"], | ||
"homepage": "https://dm4t2.github.io/vue-currency-input/", | ||
"keywords": [ | ||
"vue", | ||
"input mask", | ||
"text mask", | ||
"money input", | ||
"currency format", | ||
"i18n" | ||
], | ||
"scripts": { | ||
@@ -20,3 +29,5 @@ "serve": "vue-cli-service serve", | ||
"lint": "vue-cli-service lint", | ||
"test:unit": "vue-cli-service test:unit" | ||
"test:unit": "vue-cli-service test:unit", | ||
"docs:dev": "vuepress dev docs", | ||
"docs:build": "vuepress build docs" | ||
}, | ||
@@ -23,0 +34,0 @@ "dependencies": { |
@@ -0,42 +1,8 @@ | ||
[](https://travis-ci.com/dm4t2/vue-currency-input) | ||
[](https://badge.fury.io/js/vue-currency-input) | ||
 | ||
# Vue Currency Input | ||
A component for Vue.js proving an easy input of currency formatted numbers. | ||
Features: | ||
* Format as you type | ||
* I18n support (locale dependent formatting) | ||
* Distraction free input (automatically hides the currency prefix/suffix and unnecessary fraction part on focus) | ||
## Usage | ||
### Installation | ||
npm install vue-currency-input | ||
# OR | ||
yarn add vue-currency-input | ||
### Import the component | ||
<template> | ||
<CurrencyInput v-model="value" :currency="currency"/> | ||
</template> | ||
<script> | ||
import CurrencyInput from 'vue-currency-input' | ||
export default { | ||
components: { CurrencyInput }, | ||
data: () => ({ | ||
value: 100, | ||
currency: 'USD' | ||
}) | ||
} | ||
</script> | ||
## Props | ||
Name | Type | Description | ||
--- | --- | --- | ||
`value` | Number | The value of the input. In conjunction with `v-model` it updates the binded value with the raw number value on input changes. | ||
`currency` | String | A [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) currency code. This prop is required. | ||
`locale` | String | A [BCP 47](https://tools.ietf.org/html/bcp47) language tag (for example `en` or `de-DE`). Default is the runtime's default locale. | ||
`distraction-free` | Boolean | Whether to hide the currency prefix/suffix and unnecessary fraction part on focus. Default is `true`. | ||
**[Project Home Page with Live Demo](https://dm4t2.github.io/vue-currency-input/)** |
67123
1491
9