Skip to content

Commit 20c3b22

Browse files
author
Nick Gard
committed
Allow empty string placeholderChars
1 parent 4e2ebf9 commit 20c3b22

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ function InputMask(options) {
194194
throw new Error('InputMask: you must provide a pattern.')
195195
}
196196

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

201201
this.placeholderChar = options.placeholderChar

test/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,23 @@ test('Leading static characters', function(t) {
213213
})
214214

215215
test('Providing a custom placeholder character', function(t) {
216-
t.plan(4)
216+
t.plan(5)
217217

218218
var mask = new InputMask({pattern: '---- 1111', placeholderChar: ' '})
219219
mask.selection = {start: 0, end: 15}
220220
t.true(mask.input('3'), 'Valid input accepted with custom placeholderChar')
221221
t.equal(mask.getValue(), '---- 3 ',
222222
'Value applied to the first editable char with custom placeholderChar')
223223
t.throws(function() { new InputMask({pattern: '--11', placeholderChar: '__'}) },
224-
/InputMask: placeholderChar should be a single character/,
224+
/InputMask: placeholderChar should be a single character or an empty string./,
225225
'placholderChar length > 1 is invalid')
226-
t.throws(function() { new InputMask({pattern: '--11', placeholderChar: ''}) },
227-
/InputMask: placeholderChar should be a single character/,
228-
'placholderChar length < 1 is invalid')
226+
227+
// With an empty string as the placeholderChar
228+
mask = new InputMask({pattern: '---- 1111', placeholderChar: ''})
229+
mask.selection = {start: 0, end: 15}
230+
t.true(mask.input('3'), 'Valid input accepted with empty string placeholderChar')
231+
t.equal(mask.getValue(), '---- 3',
232+
'Value applied to the first editable char with empty string placeholderChar')
229233
t.end()
230234
})
231235

0 commit comments

Comments
 (0)