Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty string placeholderChars #20

Merged
merged 1 commit into from
Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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