Skip to content

Commit

Permalink
Allow a null or undefined value to be given or set - treated as blank
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
insin committed Sep 11, 2015
1 parent 707761e commit 932437e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@ InputMask.prototype.setSelection = function setSelection(selection) {
}

InputMask.prototype.setValue = function setValue(value) {
if (value == null) {
value = ''
}
this.value = this.pattern.formatValue(value.split(''))
}

Expand Down
8 changes: 7 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('formatValueToPattern', function(t) {
})

test('Constructor options', function(t) {
t.plan(19)
t.plan(21)

t.throws(function() { new InputMask() },
/InputMask: you must provide a pattern./,
Expand Down Expand Up @@ -107,6 +107,12 @@ test('Constructor options', function(t) {
}
})
t.equal(mask.getValue(), 'abc1+', 'Custom formatting charactes are used')

// Value can be null or undefined
mask = new InputMask({pattern: '111 111', value: null})
t.equal(mask.getValue(), '___ ___', 'null value treated as blank')
mask = new InputMask({pattern: '111 111', value: undefined})
t.equal(mask.getValue(), '___ ___', 'undefined value treated as blank')
})

test('Formatting characters', function(t) {
Expand Down

0 comments on commit 932437e

Please sign in to comment.