Skip to content

Commit

Permalink
Merge pull request #82 from cht8687/master
Browse files Browse the repository at this point in the history
[bug-fix] Fix when typing into the prefix itself
  • Loading branch information
nosir authored Aug 20, 2016
2 parents afad6f0 + e0c570f commit 58e3a10
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Cleave.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Cleave.prototype = {
value = Util.stripDelimiters(value, pps.delimiter, pps.delimiters);

// strip prefix
value = Util.getPrefixStrippedValue(value, pps.prefixLength);
value = Util.getPrefixStrippedValue(value, pps.prefix, pps.prefixLength);

// strip non-numeric characters
value = pps.numericOnly ? Util.strip(value, /[^\d]/g) : value;
Expand Down
14 changes: 13 additions & 1 deletion src/utils/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,22 @@ var Util = {
// for prefix: PRE
// (PRE123, 3) -> 123
// (PR123, 3) -> 23 this happens when user hits backspace in front of "PRE"
getPrefixStrippedValue: function (value, prefixLength) {
getPrefixStrippedValue: function (value, prefix, prefixLength) {
if (value.slice(0, prefixLength) !== prefix) {
var diffIndex = this.getFirstDiffIndex(prefix, value.slice(0, prefixLength));
value = prefix + value.slice(diffIndex, diffIndex+1) + value.slice(prefixLength+1);
}
return value.slice(prefixLength);
},

getFirstDiffIndex: function(prev, current) {
var index = 0;
while (prev.charAt(index) === current.charAt(index))
if (prev.charAt(index++) === '')
return -1;
return index;
},

getFormattedValue: function (value, blocks, blocksLength, delimiter, delimiters) {
var result = '',
multipleDelimiters = delimiters.length > 0,
Expand Down

0 comments on commit 58e3a10

Please sign in to comment.