Skip to content

Commit

Permalink
Merge pull request #20 from NickGard/feat-empty-string-placeholderchar
Browse files Browse the repository at this point in the history
Allow empty string placeholderChars
  • Loading branch information
NickGard authored Sep 15, 2016
2 parents 941f858 + 20c3b22 commit 2539ad8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ function InputMask(options) {
throw new Error('InputMask: you must provide a pattern.')
}

if (options.placeholderChar.length !== 1) {
throw new Error('InputMask: placeholderChar should be a single character.')
if (typeof options.placeholderChar !== 'string' || options.placeholderChar.length > 1) {
throw new Error('InputMask: placeholderChar should be a single character or an empty string.')
}

this.placeholderChar = options.placeholderChar
Expand Down
14 changes: 9 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,23 @@ test('Leading static characters', function(t) {
})

test('Providing a custom placeholder character', function(t) {
t.plan(4)
t.plan(5)

var mask = new InputMask({pattern: '---- 1111', placeholderChar: ' '})
mask.selection = {start: 0, end: 15}
t.true(mask.input('3'), 'Valid input accepted with custom placeholderChar')
t.equal(mask.getValue(), '---- 3 ',
'Value applied to the first editable char with custom placeholderChar')
t.throws(function() { new InputMask({pattern: '--11', placeholderChar: '__'}) },
/InputMask: placeholderChar should be a single character/,
/InputMask: placeholderChar should be a single character or an empty string./,
'placholderChar length > 1 is invalid')
t.throws(function() { new InputMask({pattern: '--11', placeholderChar: ''}) },
/InputMask: placeholderChar should be a single character/,
'placholderChar length < 1 is invalid')

// With an empty string as the placeholderChar
mask = new InputMask({pattern: '---- 1111', placeholderChar: ''})
mask.selection = {start: 0, end: 15}
t.true(mask.input('3'), 'Valid input accepted with empty string placeholderChar')
t.equal(mask.getValue(), '---- 3',
'Value applied to the first editable char with empty string placeholderChar')
t.end()
})

Expand Down

0 comments on commit 2539ad8

Please sign in to comment.