Skip to content

Commit

Permalink
Throw error if mask is not an array (text-mask#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
lozjackson authored Apr 27, 2018
1 parent bc45546 commit 0ab0256
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions core/src/conformToMask.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import {convertMaskToPlaceholder} from './utilities'
import {convertMaskToPlaceholder, isArray} from './utilities'
import {placeholderChar as defaultPlaceholderChar} from './constants'

const emptyArray = []
const emptyString = ''

export default function conformToMask(rawValue = emptyString, mask = emptyString, config = {}) {
export default function conformToMask(rawValue = emptyString, mask = emptyArray, config = {}) {
if (!isArray(mask)) {
throw new Error(
'Text-mask:conformToMask; The mask property must be an array.'
)
}

// These configurations tell us how to conform the mask
const {
guide = true,
Expand Down
10 changes: 10 additions & 0 deletions core/src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import {placeholderChar as defaultPlaceholderChar} from './constants'
const emptyArray = []

export function convertMaskToPlaceholder(mask = emptyArray, placeholderChar = defaultPlaceholderChar) {
if (!isArray(mask)) {
throw new Error(
'Text-mask:convertMaskToPlaceholder; The mask property must be an array.'
)
}

if (mask.indexOf(placeholderChar) !== -1) {
throw new Error(
'Placeholder character must not be used as part of the mask. Please specify a character ' +
Expand All @@ -17,6 +23,10 @@ export function convertMaskToPlaceholder(mask = emptyArray, placeholderChar = de
}).join('')
}

export function isArray(value) {
return (Array.isArray && Array.isArray(value)) || value instanceof Array
}

export function isString(value) {
return typeof value === 'string' || value instanceof String
}
Expand Down

0 comments on commit 0ab0256

Please sign in to comment.