New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

vue-currency-input

Package Overview
Dependencies
Maintainers
1
Versions
91
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-currency-input - npm Package Compare versions

Comparing version 3.0.0 to 3.0.1

181

dist/index.cjs.js
/**
* Vue Currency Input 2.4.0
* Vue Currency Input 3.0.1
* (c) 2018-2022 Matthias Stiller

@@ -12,15 +12,2 @@ * @license MIT

const escapeRegExp = (str) => {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
};
const removeLeadingZeros = (str) => {
return str.replace(/^0+(0$|[^0])/, '$1');
};
const count = (str, search) => {
return (str.match(new RegExp(escapeRegExp(search), 'g')) || []).length;
};
const substringBefore = (str, search) => {
return str.substring(0, str.indexOf(search));
};
exports.CurrencyDisplay = void 0;

@@ -42,2 +29,15 @@ (function (CurrencyDisplay) {

const escapeRegExp = (str) => {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
};
const removeLeadingZeros = (str) => {
return str.replace(/^0+(0$|[^0])/, '$1');
};
const count = (str, search) => {
return (str.match(new RegExp(escapeRegExp(search), 'g')) || []).length;
};
const substringBefore = (str, search) => {
return str.substring(0, str.indexOf(search));
};
const DECIMAL_SEPARATORS = [',', '.', '٫'];

@@ -256,8 +256,8 @@ const INTEGER_PATTERN = '(0|[1-9]\\d*)';

class CurrencyInput {
constructor(el, options) {
this.el = el;
this.numberValue = null;
constructor(args) {
this.el = args.el;
this.onInput = args.onInput;
this.onChange = args.onChange;
this.addEventListener();
this.init(options);
this.setValue(this.currencyFormat.parse(this.el.value));
this.init(args.options);
}

@@ -278,5 +278,2 @@ setOptions(options) {

}
dispatchEvent(eventName) {
this.el.dispatchEvent(new CustomEvent(eventName, { detail: this.getValue() }));
}
init(options) {

@@ -341,3 +338,3 @@ this.options = {

if (number !== this.numberValue || forcedChange) {
this.dispatchEvent('change');
this.onChange(this.getValue());
}

@@ -397,49 +394,47 @@ }

this.formattedValue = this.el.value;
this.dispatchEvent('input');
this.onInput(this.getValue());
}
addEventListener() {
this.el.addEventListener('input', (e) => {
if (!e.detail) {
const { value, selectionStart } = this.el;
const inputEvent = e;
if (selectionStart && inputEvent.data && DECIMAL_SEPARATORS.includes(inputEvent.data)) {
this.decimalSymbolInsertedAt = selectionStart - 1;
}
this.format(value);
if (this.focus && selectionStart != null) {
const getCaretPositionAfterFormat = () => {
const { prefix, suffix, decimalSymbol, maximumFractionDigits, groupingSymbol } = this.currencyFormat;
let caretPositionFromLeft = value.length - selectionStart;
const newValueLength = this.formattedValue.length;
if (this.currencyFormat.minusSign === undefined && (value.startsWith('(') || value.startsWith('-')) && !value.endsWith(')')) {
return newValueLength - this.currencyFormat.negativeSuffix.length > 1 ? this.formattedValue.substring(selectionStart).length : 1;
const { value, selectionStart } = this.el;
const inputEvent = e;
if (selectionStart && inputEvent.data && DECIMAL_SEPARATORS.includes(inputEvent.data)) {
this.decimalSymbolInsertedAt = selectionStart - 1;
}
this.format(value);
if (this.focus && selectionStart != null) {
const getCaretPositionAfterFormat = () => {
const { prefix, suffix, decimalSymbol, maximumFractionDigits, groupingSymbol } = this.currencyFormat;
let caretPositionFromLeft = value.length - selectionStart;
const newValueLength = this.formattedValue.length;
if (this.currencyFormat.minusSign === undefined && (value.startsWith('(') || value.startsWith('-')) && !value.endsWith(')')) {
return newValueLength - this.currencyFormat.negativeSuffix.length > 1 ? this.formattedValue.substring(selectionStart).length : 1;
}
if (this.formattedValue.substr(selectionStart, 1) === groupingSymbol &&
count(this.formattedValue, groupingSymbol) === count(value, groupingSymbol) + 1) {
return newValueLength - caretPositionFromLeft - 1;
}
if (newValueLength < caretPositionFromLeft) {
return selectionStart;
}
if (decimalSymbol !== undefined && value.indexOf(decimalSymbol) !== -1) {
const decimalSymbolPosition = value.indexOf(decimalSymbol) + 1;
if (Math.abs(newValueLength - value.length) > 1 && selectionStart <= decimalSymbolPosition) {
return this.formattedValue.indexOf(decimalSymbol) + 1;
}
if (this.formattedValue.substr(selectionStart, 1) === groupingSymbol &&
count(this.formattedValue, groupingSymbol) === count(value, groupingSymbol) + 1) {
return newValueLength - caretPositionFromLeft - 1;
}
if (newValueLength < caretPositionFromLeft) {
return selectionStart;
}
if (decimalSymbol !== undefined && value.indexOf(decimalSymbol) !== -1) {
const decimalSymbolPosition = value.indexOf(decimalSymbol) + 1;
if (Math.abs(newValueLength - value.length) > 1 && selectionStart <= decimalSymbolPosition) {
return this.formattedValue.indexOf(decimalSymbol) + 1;
}
else {
if (!this.options.autoDecimalDigits && selectionStart > decimalSymbolPosition) {
if (this.currencyFormat.onlyDigits(value.substr(decimalSymbolPosition)).length - 1 === maximumFractionDigits) {
caretPositionFromLeft -= 1;
}
else {
if (!this.options.autoDecimalDigits && selectionStart > decimalSymbolPosition) {
if (this.currencyFormat.onlyDigits(value.substr(decimalSymbolPosition)).length - 1 === maximumFractionDigits) {
caretPositionFromLeft -= 1;
}
}
}
return this.options.hideCurrencySymbolOnFocus || this.options.currencyDisplay === exports.CurrencyDisplay.hidden
? newValueLength - caretPositionFromLeft
: Math.max(newValueLength - Math.max(caretPositionFromLeft, suffix.length), prefix.length);
};
this.setCaretPosition(getCaretPositionAfterFormat());
}
}
return this.options.hideCurrencySymbolOnFocus || this.options.currencyDisplay === exports.CurrencyDisplay.hidden
? newValueLength - caretPositionFromLeft
: Math.max(newValueLength - Math.max(caretPositionFromLeft, suffix.length), prefix.length);
};
this.setCaretPosition(getCaretPositionAfterFormat());
}
}, { capture: true });
});
this.el.addEventListener('focus', () => {

@@ -463,7 +458,5 @@ this.focus = true;

});
this.el.addEventListener('change', (e) => {
if (!e.detail) {
this.dispatchEvent('change');
}
}, { capture: true });
this.el.addEventListener('change', () => {
this.onChange(this.getValue());
});
}

@@ -519,6 +512,5 @@ getCaretPositionOnFocus(value, selectionStart) {

const findInput = (el) => ((el === null || el === void 0 ? void 0 : el.matches('input')) ? el : el === null || el === void 0 ? void 0 : el.querySelector('input'));
var useCurrencyInput = (options, autoEmit) => {
function useCurrencyInput(options, autoEmit) {
var _a, _b, _c, _d;
let numberInput;
let input;
let currencyInput;
const inputRef = vue.ref(null);

@@ -535,25 +527,22 @@ const formattedValue = vue.ref(null);

const changeEvent = lazyModel ? 'update:modelValue' : 'change';
const onInput = (e) => {
if (e.detail) {
if (!lazyModel && autoEmit !== false && modelValue.value !== e.detail.number) {
emit === null || emit === void 0 ? void 0 : emit(inputEvent, e.detail.number);
}
numberValue.value = e.detail.number;
formattedValue.value = e.detail.formatted;
}
};
const onChange = (e) => {
if (e.detail) {
emit === null || emit === void 0 ? void 0 : emit(changeEvent, e.detail.number);
}
};
vue.watch(inputRef, (value) => {
var _a;
if (value) {
input = findInput((_a = value === null || value === void 0 ? void 0 : value.$el) !== null && _a !== void 0 ? _a : value);
if (input) {
input.addEventListener('input', onInput);
input.addEventListener('change', onChange);
numberInput = new CurrencyInput(input, options);
numberInput.setValue(modelValue.value);
const el = findInput((_a = value === null || value === void 0 ? void 0 : value.$el) !== null && _a !== void 0 ? _a : value);
if (el) {
currencyInput = new CurrencyInput({
el,
options,
onInput: (value) => {
if (!lazyModel && autoEmit !== false && modelValue.value !== value.number) {
emit === null || emit === void 0 ? void 0 : emit(inputEvent, value.number);
}
numberValue.value = value.number;
formattedValue.value = value.formatted;
},
onChange: (value) => {
emit === null || emit === void 0 ? void 0 : emit(changeEvent, value.number);
}
});
currencyInput.setValue(modelValue.value);
}

@@ -565,9 +554,5 @@ else {

else {
numberInput = null;
currencyInput = null;
}
});
vue.onUnmounted(() => {
input === null || input === void 0 ? void 0 : input.removeEventListener('input', onInput);
input === null || input === void 0 ? void 0 : input.removeEventListener('change', onChange);
});
return {

@@ -577,7 +562,7 @@ inputRef,

formattedValue,
setValue: (value) => numberInput === null || numberInput === void 0 ? void 0 : numberInput.setValue(value),
setOptions: (options) => numberInput === null || numberInput === void 0 ? void 0 : numberInput.setOptions(options)
setValue: (value) => currencyInput === null || currencyInput === void 0 ? void 0 : currencyInput.setValue(value),
setOptions: (options) => currencyInput === null || currencyInput === void 0 ? void 0 : currencyInput.setOptions(options)
};
};
}
exports.useCurrencyInput = useCurrencyInput;

@@ -48,4 +48,4 @@ import { Ref } from 'vue';

declare const _default: (options: CurrencyInputOptions, autoEmit?: boolean) => UseCurrencyInput;
declare function useCurrencyInput(options: CurrencyInputOptions, autoEmit?: boolean): UseCurrencyInput;
export { CurrencyDisplay, CurrencyFormatOptions, CurrencyInputOptions, CurrencyInputValue, NumberRange, UseCurrencyInput, ValueScaling, _default as useCurrencyInput };
export { CurrencyDisplay, CurrencyFormatOptions, CurrencyInputOptions, CurrencyInputValue, NumberRange, UseCurrencyInput, ValueScaling, useCurrencyInput };
/**
* Vue Currency Input 2.4.0
* Vue Currency Input 3.0.1
* (c) 2018-2022 Matthias Stiller
* @license MIT
*/
import { ref, getCurrentInstance, version, computed, watch, onUnmounted } from 'vue';
import { ref, getCurrentInstance, version, computed, watch } from 'vue';
const escapeRegExp = (str) => {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
};
const removeLeadingZeros = (str) => {
return str.replace(/^0+(0$|[^0])/, '$1');
};
const count = (str, search) => {
return (str.match(new RegExp(escapeRegExp(search), 'g')) || []).length;
};
const substringBefore = (str, search) => {
return str.substring(0, str.indexOf(search));
};
var CurrencyDisplay;

@@ -37,2 +24,15 @@ (function (CurrencyDisplay) {

const escapeRegExp = (str) => {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
};
const removeLeadingZeros = (str) => {
return str.replace(/^0+(0$|[^0])/, '$1');
};
const count = (str, search) => {
return (str.match(new RegExp(escapeRegExp(search), 'g')) || []).length;
};
const substringBefore = (str, search) => {
return str.substring(0, str.indexOf(search));
};
const DECIMAL_SEPARATORS = [',', '.', '٫'];

@@ -251,8 +251,8 @@ const INTEGER_PATTERN = '(0|[1-9]\\d*)';

class CurrencyInput {
constructor(el, options) {
this.el = el;
this.numberValue = null;
constructor(args) {
this.el = args.el;
this.onInput = args.onInput;
this.onChange = args.onChange;
this.addEventListener();
this.init(options);
this.setValue(this.currencyFormat.parse(this.el.value));
this.init(args.options);
}

@@ -273,5 +273,2 @@ setOptions(options) {

}
dispatchEvent(eventName) {
this.el.dispatchEvent(new CustomEvent(eventName, { detail: this.getValue() }));
}
init(options) {

@@ -336,3 +333,3 @@ this.options = {

if (number !== this.numberValue || forcedChange) {
this.dispatchEvent('change');
this.onChange(this.getValue());
}

@@ -392,49 +389,47 @@ }

this.formattedValue = this.el.value;
this.dispatchEvent('input');
this.onInput(this.getValue());
}
addEventListener() {
this.el.addEventListener('input', (e) => {
if (!e.detail) {
const { value, selectionStart } = this.el;
const inputEvent = e;
if (selectionStart && inputEvent.data && DECIMAL_SEPARATORS.includes(inputEvent.data)) {
this.decimalSymbolInsertedAt = selectionStart - 1;
}
this.format(value);
if (this.focus && selectionStart != null) {
const getCaretPositionAfterFormat = () => {
const { prefix, suffix, decimalSymbol, maximumFractionDigits, groupingSymbol } = this.currencyFormat;
let caretPositionFromLeft = value.length - selectionStart;
const newValueLength = this.formattedValue.length;
if (this.currencyFormat.minusSign === undefined && (value.startsWith('(') || value.startsWith('-')) && !value.endsWith(')')) {
return newValueLength - this.currencyFormat.negativeSuffix.length > 1 ? this.formattedValue.substring(selectionStart).length : 1;
const { value, selectionStart } = this.el;
const inputEvent = e;
if (selectionStart && inputEvent.data && DECIMAL_SEPARATORS.includes(inputEvent.data)) {
this.decimalSymbolInsertedAt = selectionStart - 1;
}
this.format(value);
if (this.focus && selectionStart != null) {
const getCaretPositionAfterFormat = () => {
const { prefix, suffix, decimalSymbol, maximumFractionDigits, groupingSymbol } = this.currencyFormat;
let caretPositionFromLeft = value.length - selectionStart;
const newValueLength = this.formattedValue.length;
if (this.currencyFormat.minusSign === undefined && (value.startsWith('(') || value.startsWith('-')) && !value.endsWith(')')) {
return newValueLength - this.currencyFormat.negativeSuffix.length > 1 ? this.formattedValue.substring(selectionStart).length : 1;
}
if (this.formattedValue.substr(selectionStart, 1) === groupingSymbol &&
count(this.formattedValue, groupingSymbol) === count(value, groupingSymbol) + 1) {
return newValueLength - caretPositionFromLeft - 1;
}
if (newValueLength < caretPositionFromLeft) {
return selectionStart;
}
if (decimalSymbol !== undefined && value.indexOf(decimalSymbol) !== -1) {
const decimalSymbolPosition = value.indexOf(decimalSymbol) + 1;
if (Math.abs(newValueLength - value.length) > 1 && selectionStart <= decimalSymbolPosition) {
return this.formattedValue.indexOf(decimalSymbol) + 1;
}
if (this.formattedValue.substr(selectionStart, 1) === groupingSymbol &&
count(this.formattedValue, groupingSymbol) === count(value, groupingSymbol) + 1) {
return newValueLength - caretPositionFromLeft - 1;
}
if (newValueLength < caretPositionFromLeft) {
return selectionStart;
}
if (decimalSymbol !== undefined && value.indexOf(decimalSymbol) !== -1) {
const decimalSymbolPosition = value.indexOf(decimalSymbol) + 1;
if (Math.abs(newValueLength - value.length) > 1 && selectionStart <= decimalSymbolPosition) {
return this.formattedValue.indexOf(decimalSymbol) + 1;
}
else {
if (!this.options.autoDecimalDigits && selectionStart > decimalSymbolPosition) {
if (this.currencyFormat.onlyDigits(value.substr(decimalSymbolPosition)).length - 1 === maximumFractionDigits) {
caretPositionFromLeft -= 1;
}
else {
if (!this.options.autoDecimalDigits && selectionStart > decimalSymbolPosition) {
if (this.currencyFormat.onlyDigits(value.substr(decimalSymbolPosition)).length - 1 === maximumFractionDigits) {
caretPositionFromLeft -= 1;
}
}
}
return this.options.hideCurrencySymbolOnFocus || this.options.currencyDisplay === CurrencyDisplay.hidden
? newValueLength - caretPositionFromLeft
: Math.max(newValueLength - Math.max(caretPositionFromLeft, suffix.length), prefix.length);
};
this.setCaretPosition(getCaretPositionAfterFormat());
}
}
return this.options.hideCurrencySymbolOnFocus || this.options.currencyDisplay === CurrencyDisplay.hidden
? newValueLength - caretPositionFromLeft
: Math.max(newValueLength - Math.max(caretPositionFromLeft, suffix.length), prefix.length);
};
this.setCaretPosition(getCaretPositionAfterFormat());
}
}, { capture: true });
});
this.el.addEventListener('focus', () => {

@@ -458,7 +453,5 @@ this.focus = true;

});
this.el.addEventListener('change', (e) => {
if (!e.detail) {
this.dispatchEvent('change');
}
}, { capture: true });
this.el.addEventListener('change', () => {
this.onChange(this.getValue());
});
}

@@ -514,6 +507,5 @@ getCaretPositionOnFocus(value, selectionStart) {

const findInput = (el) => ((el === null || el === void 0 ? void 0 : el.matches('input')) ? el : el === null || el === void 0 ? void 0 : el.querySelector('input'));
var useCurrencyInput = (options, autoEmit) => {
function useCurrencyInput(options, autoEmit) {
var _a, _b, _c, _d;
let numberInput;
let input;
let currencyInput;
const inputRef = ref(null);

@@ -530,25 +522,22 @@ const formattedValue = ref(null);

const changeEvent = lazyModel ? 'update:modelValue' : 'change';
const onInput = (e) => {
if (e.detail) {
if (!lazyModel && autoEmit !== false && modelValue.value !== e.detail.number) {
emit === null || emit === void 0 ? void 0 : emit(inputEvent, e.detail.number);
}
numberValue.value = e.detail.number;
formattedValue.value = e.detail.formatted;
}
};
const onChange = (e) => {
if (e.detail) {
emit === null || emit === void 0 ? void 0 : emit(changeEvent, e.detail.number);
}
};
watch(inputRef, (value) => {
var _a;
if (value) {
input = findInput((_a = value === null || value === void 0 ? void 0 : value.$el) !== null && _a !== void 0 ? _a : value);
if (input) {
input.addEventListener('input', onInput);
input.addEventListener('change', onChange);
numberInput = new CurrencyInput(input, options);
numberInput.setValue(modelValue.value);
const el = findInput((_a = value === null || value === void 0 ? void 0 : value.$el) !== null && _a !== void 0 ? _a : value);
if (el) {
currencyInput = new CurrencyInput({
el,
options,
onInput: (value) => {
if (!lazyModel && autoEmit !== false && modelValue.value !== value.number) {
emit === null || emit === void 0 ? void 0 : emit(inputEvent, value.number);
}
numberValue.value = value.number;
formattedValue.value = value.formatted;
},
onChange: (value) => {
emit === null || emit === void 0 ? void 0 : emit(changeEvent, value.number);
}
});
currencyInput.setValue(modelValue.value);
}

@@ -560,9 +549,5 @@ else {

else {
numberInput = null;
currencyInput = null;
}
});
onUnmounted(() => {
input === null || input === void 0 ? void 0 : input.removeEventListener('input', onInput);
input === null || input === void 0 ? void 0 : input.removeEventListener('change', onChange);
});
return {

@@ -572,7 +557,7 @@ inputRef,

formattedValue,
setValue: (value) => numberInput === null || numberInput === void 0 ? void 0 : numberInput.setValue(value),
setOptions: (options) => numberInput === null || numberInput === void 0 ? void 0 : numberInput.setOptions(options)
setValue: (value) => currencyInput === null || currencyInput === void 0 ? void 0 : currencyInput.setValue(value),
setOptions: (options) => currencyInput === null || currencyInput === void 0 ? void 0 : currencyInput.setOptions(options)
};
};
}
export { CurrencyDisplay, ValueScaling, useCurrencyInput };
{
"name": "vue-currency-input",
"description": "Easy input of currency formatted numbers for Vue.js.",
"version": "3.0.0",
"version": "3.0.1",
"license": "MIT",

@@ -39,7 +39,7 @@ "module": "./dist/index.esm.js",

"docs": "vitepress build docs",
"test": "jest",
"test": "vitest",
"coverage": "vitest run --coverage",
"lint": "eslint --no-fix --max-warnings 0 {**/*,*}.{js,ts,vue}",
"prebuild": "rimraf dist",
"build": "tsc --emitDeclarationOnly --declaration --outDir dist/types && rollup -c rollup.config.js",
"release": "semantic-release"
"build": "tsc --emitDeclarationOnly --declaration --outDir dist/types && rollup -c rollup.config.js"
},

@@ -50,34 +50,31 @@ "peerDependencies": {

"devDependencies": {
"@rushstack/eslint-patch": "^1.1.3",
"@testing-library/dom": "^8.11.1",
"@rushstack/eslint-patch": "^1.2.0",
"@testing-library/dom": "^8.18.1",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^26.0.24",
"@typescript-eslint/eslint-plugin": "^5.26.0",
"@typescript-eslint/parser": "^5.26.0",
"@typescript-eslint/eslint-plugin": "^5.38.1",
"@typescript-eslint/parser": "^5.38.1",
"@vitest/coverage-c8": "^0.23.4",
"@vue/eslint-config-prettier": "^7.0.0",
"@vue/eslint-config-typescript": "^10.0.0",
"@vue/test-utils": "^2.0.0-rc.21",
"eslint": "^8.16.0",
"@vue/eslint-config-typescript": "^11.0.2",
"@vue/test-utils": "^2.0.2",
"eslint": "^8.24.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-vue": "^9.0.1",
"jest": "^26.6.3",
"jest-environment-jsdom-fifteen": "^1.0.2",
"lint-staged": "^12.1.4",
"prettier": "^2.6.2",
"eslint-plugin-vue": "^9.5.1",
"jsdom": "^20.0.1",
"lint-staged": "^13.0.3",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"rollup": "^2.62.0",
"rollup": "^2.79.1",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-dts": "^4.1.0",
"rollup-plugin-filesize": "^9.1.1",
"rollup-plugin-typescript2": "^0.31.1",
"semantic-release": "^19.0.2",
"simple-git-hooks": "^2.7.0",
"ts-jest": "^26.5.6",
"typescript": "^4.5.4",
"unplugin-vue-components": "^0.17.20",
"vite-plugin-windicss": "^1.7.1",
"vitepress": "^0.22.3",
"vue": "^3.2.26",
"vue-jest": "^5.0.0-alpha.10",
"windicss": "^3.4.4"
"rollup-plugin-dts": "^4.2.2",
"rollup-plugin-filesize": "^9.1.2",
"rollup-plugin-typescript2": "^0.34.0",
"simple-git-hooks": "^2.8.0",
"typescript": "^4.8.4",
"unplugin-vue-components": "^0.22.7",
"vite-plugin-windicss": "^1.8.8",
"vitepress": "^0.22.4",
"vitest": "^0.23.4",
"vue": "^3.2.40",
"windicss": "^3.5.6"
},

@@ -84,0 +81,0 @@ "simple-git-hooks": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc