Skip to content

Commit 3a5eb59

Browse files
committed
fix: propertly check input to public interface
We were depending on es6 default params to set expected values. However default params only apply to undefined values, and not for null. Thus an erro was been thrown when null is been passed in as the value or config object.
1 parent 45a1e89 commit 3a5eb59

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

src/core.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import masker from './masker'
22
export const CONFIG_KEY = '__input-facade__'
33

4-
export function FacadeValue(val = '') {
5-
this.masked = this.unmasked = val
4+
export function FacadeValue(val) {
5+
this.masked = this.unmasked = val || ''
66
}
77

88
/**
@@ -22,12 +22,12 @@ export function FacadeInputEvent() {
2222
*
2323
* @param {object} config The mask config object
2424
*/
25-
export function normalizeConfig(config = {}) {
25+
export function normalizeConfig(config) {
2626
if (Array.isArray(config) || typeof config === 'string') {
2727
config = { mask: config }
2828
}
2929

30-
return config
30+
return config || {}
3131
}
3232

3333
/**

src/masker.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ let tokenDefinitions = defaultTokens
99
* @param {object} tokens the new token object
1010
*/
1111
export function setTokens(tokens) {
12+
if (!tokens) return
1213
tokenDefinitions = tokens
1314
}
1415

1516
/**
1617
* Given an array of masks, determines which one is the appropriate one based on the value
1718
*
1819
* @param {String} inputValue the inputValue value to mask
19-
* @param {{masks: [String]}} config
20+
* @param {object} config
2021
* @param {Array} config.masks the list of masks to choose from
2122
* @returns {FacadeValue} facade value object
2223
*/
23-
export function dynamic(inputValue, config = {}) {
24+
export function dynamic(inputValue, config) {
2425
const masks = config.masks.slice().sort((a, b) => a.length - b.length)
2526
const withConfig = (overrides) => Object.assign({}, config, overrides)
2627

@@ -53,19 +54,17 @@ export function dynamic(inputValue, config = {}) {
5354
*
5455
* @param {string} value the value to mask
5556
* @param {{mask: String, tokens: Object, prepend: Boolean}} config
57+
* @param {object} config
5658
* @param {string} config.mask the masking string
5759
* @param {object} config.tokens the tokens to add/override to the global
5860
* @param {boolean} config.prepend whether or not to add masking characters to the input before the user types.
5961
*/
60-
export function formatter(value = '', config = {}) {
62+
export function formatter(value, config) {
6163
let { mask = '', tokens, prepend = false } = config
6264

6365
// append/override global tokens instead of complete override
6466
tokens = tokens ? Object.assign({}, tokenDefinitions, tokens) : tokenDefinitions
6567

66-
// ensure we have a string
67-
value = value.toString()
68-
6968
let output = new FacadeValue()
7069
let escaped = false
7170

@@ -124,6 +123,8 @@ export function formatter(value = '', config = {}) {
124123
* @returns {FacadeValue} facade value object
125124
*/
126125
export default function masker(value, config) {
126+
// ensure we have proper input
127+
value = (value || '').toString()
127128
config = normalizeConfig(config)
128129

129130
// disable on empty mask

tests/formatter.test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { formatter } from '../src/masker'
22

3-
test('default vals', () => {
4-
expect(formatter('123')).toMatchObject({ masked: '', unmasked: '' })
5-
})
6-
73
test('12 -> #.#', () => {
84
expect(formatter('12', { mask: '#.#' })).toMatchObject({ masked: '1.2', unmasked: '12' })
95
})

tests/masker.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@ const tokens = {
44
F: { pattern: /[a-f0-9]/i }
55
}
66

7-
test('no mask given', () => {
7+
test('no errors with invalid input to public interface', () => {
8+
expect(masker()).toMatchObject({ masked: '', unmasked: '' })
9+
expect(masker(null)).toMatchObject({ masked: '', unmasked: '' })
10+
expect(masker(null, null)).toMatchObject({ masked: '', unmasked: '' })
11+
expect(masker(null, { mask: null })).toMatchObject({ masked: '', unmasked: '' })
12+
})
13+
14+
test('no mask given, value should be returned as is', () => {
815
expect(masker('123')).toMatchObject({ masked: '123', unmasked: '123' })
16+
expect(masker('123', null)).toMatchObject({ masked: '123', unmasked: '123' })
17+
expect(masker('123', { mask : null })).toMatchObject({ masked: '123', unmasked: '123' })
918
})
1019

1120
test('single mask given', () => {

0 commit comments

Comments
 (0)