Skip to content
This repository was archived by the owner on Sep 4, 2024. It is now read-only.

Commit 5f1490b

Browse files
committed
fix(filter): prevent crashes for null values, cover filter by tests
Fixes #510
1 parent 7fde886 commit 5f1490b

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/__tests__/index.test.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createLocalVue, mount } from '@vue/test-utils';
22
import createNumberMask from 'text-mask-addons/dist/createNumberMask';
3-
import VueMask, { VueMaskDirective, VueMaskPlugin } from '../index';
3+
import VueMask, { VueMaskDirective, VueMaskPlugin, VueMaskFilter } from '../index';
44
import { timeRangeMask } from '../utils/timeRangeMask';
55

66
describe('plugin/directive registration', () => {
@@ -18,10 +18,20 @@ describe('plugin/directive registration', () => {
1818
expect(VueMaskPlugin).toEqual(expect.any(Function));
1919
});
2020

21+
it('named export `VueMaskFilter` should be a function', () => {
22+
expect(VueMaskFilter).toEqual(expect.any(Function));
23+
});
24+
2125
it('named export `VueMaskDirective` should be an object', () => {
2226
expect(VueMaskDirective).toEqual(expect.any(Object));
2327
});
2428

29+
it('should register `VMask` filter', () => {
30+
expect(Vue.options.filters.VMask).toBeUndefined();
31+
Vue.use(VueMask);
32+
expect(Vue.options.filters.VMask).toEqual(expect.any(Function));
33+
});
34+
2535
it('should register `v-mask` directive', () => {
2636
expect(Vue.options.directives.mask).toBeUndefined();
2737
Vue.use(VueMask);
@@ -234,3 +244,43 @@ describe('directive usage', () => {
234244
expect(wrapper.vm.$el.value).toBe('19:32');
235245
});
236246
});
247+
248+
describe('filter usage', () => {
249+
let mountWithMask;
250+
251+
beforeEach(() => {
252+
const localVue = createLocalVue();
253+
localVue.use(VueMask);
254+
mountWithMask = (arg, options) => mount(arg, { ...options, localVue });
255+
});
256+
257+
it('should mask static string', () => {
258+
const wrapper = mountWithMask({
259+
template: '<span>{{ "9999999999" | VMask("(###) ###-####") }}</span>',
260+
});
261+
expect(wrapper.text()).toBe('(999) 999-9999');
262+
});
263+
264+
it('should mask static number', () => {
265+
const wrapper = mountWithMask({
266+
template: '<span>{{ 9999999999 | VMask("(###) ###-####") }}</span>',
267+
});
268+
expect(wrapper.text()).toBe('(999) 999-9999');
269+
});
270+
271+
it('should mask dynamic value', () => {
272+
const wrapper = mountWithMask({
273+
data: () => ({ val: '8888888888' }),
274+
template: '<span>{{ val | VMask("(###) ###-####") }}</span>',
275+
});
276+
expect(wrapper.text()).toBe('(888) 888-8888');
277+
});
278+
279+
it.each([null, undefined])('should pass through %p without modification', (val) => {
280+
const wrapper = mountWithMask({
281+
data: () => ({ val }),
282+
template: '<span>{{ val | VMask("(###) ###-####") }}</span>',
283+
});
284+
expect(wrapper.text()).toBe('');
285+
});
286+
});

src/filter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// eslint-disable-next-line import/no-extraneous-dependencies
22
import conformToMask from 'text-mask-core/src/conformToMask';
33
import { stringMaskToRegExpMask } from './maskToRegExpMask';
4+
import { isString } from './utils';
45

56
/**
67
* Vue filter definition
@@ -9,6 +10,7 @@ import { stringMaskToRegExpMask } from './maskToRegExpMask';
910
*/
1011
export default (value, stringMask) => {
1112
const mask = stringMaskToRegExpMask(stringMask);
12-
const { conformedValue } = conformToMask(value, mask, { guide: false });
13+
if (!isString(value) && !Number.isFinite(value)) return value;
14+
const { conformedValue } = conformToMask(`${value}`, mask, { guide: false });
1315
return conformedValue;
1416
};

0 commit comments

Comments
 (0)