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

added new method hasValueChanged to check to see if a new value would… #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,13 @@ InputMask.prototype.getValue = function getValue() {
return this.value.join('')
}

InputMask.prototype.hasValueChanged = function hasValueChanged(nextValue) {
if (!nextValue) {
nextValue = ''
}
return this.value.join('') !== this.pattern.formatValue(nextValue.split('')).join('')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this doing a join? (Apologies if the question is a bit naive. I have access to this repo, but haven’t read any of the implementation)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few months since I looked at this, but I think it's because InputMask stores its internal representation of the field data as an array. Don't recall exactly why, but all related to how it processes the mask characters I believe.

}

InputMask.prototype.getRawValue = function getRawValue() {
var rawValue = []
for (var i = 0; i < this.value.length; i++) {
Expand Down
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,16 @@ test('History', function(t) {
t.deepEqual([mask.getValue(), mask.selection], ['abc123', {start: 6, end: 6}])
t.false(mask.redo(), 'invalid redo - nothing more to redo')
})

test('hasValueChanged', function(t) {
t.plan(2)

function checkValueChanged(value, pattern, nextValue) {
var mask = new InputMask({pattern: pattern})
mask.paste(value)
return mask.hasValueChanged(nextValue)
}

t.true(checkValueChanged('100', '111%', '99'), 'value should have changed')
t.false(checkValueChanged('100', '111%', '100'), 'value should not have changed')
})