From c7e5a4d5a3ff8ec67ad10c9169e785e915b9811f Mon Sep 17 00:00:00 2001 From: Martin Bayly Date: Fri, 11 Mar 2016 11:04:15 -0800 Subject: [PATCH] added new method hasValueChanged to check to see if a new value would change the output value of the mask --- lib/index.js | 7 +++++++ test/index.js | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/index.js b/lib/index.js index 2427124..330cd5c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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('') +} + InputMask.prototype.getRawValue = function getRawValue() { var rawValue = [] for (var i = 0; i < this.value.length; i++) { diff --git a/test/index.js b/test/index.js index 1fee1df..8dd14ca 100644 --- a/test/index.js +++ b/test/index.js @@ -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') +})