Skip to content

Commit

Permalink
[Issue-28] Fix prefix last letter gets pushed out issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nosir committed Aug 20, 2016
1 parent 58e3a10 commit 51c5920
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"form",
"input"
],
"version": "0.6.11",
"version": "0.6.12",
"author": {
"name": "Max Huang",
"email": "[email protected]",
Expand Down
20 changes: 18 additions & 2 deletions dist/cleave-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,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 Expand Up @@ -338,10 +338,26 @@ 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
4 changes: 2 additions & 2 deletions dist/cleave-angular.min.js

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions dist/cleave-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ return /******/ (function(modules) { // webpackBootstrap
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 Expand Up @@ -792,10 +792,24 @@ return /******/ (function(modules) { // webpackBootstrap
// for prefix: PRE
// (PRE123, 3) -> 123
// (PR123, 3) -> 23 this happens when user hits backspace in front of "PRE"
getPrefixStrippedValue: function getPrefixStrippedValue(value, prefixLength) {
getPrefixStrippedValue: function getPrefixStrippedValue(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 getFirstDiffIndex(prev, current) {
var index = 0;

while (prev.charAt(index) === current.charAt(index)) {
if (prev.charAt(index++) === '') return -1;
}return index;
},

getFormattedValue: function getFormattedValue(value, blocks, blocksLength, delimiter, delimiters) {
var result = '',
multipleDelimiters = delimiters.length > 0,
Expand Down
4 changes: 2 additions & 2 deletions dist/cleave-react.min.js

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions dist/cleave.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,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 Expand Up @@ -338,10 +338,26 @@ 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
4 changes: 2 additions & 2 deletions dist/cleave.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"form",
"input"
],
"version": "0.6.11",
"version": "0.6.12",
"author": {
"name": "Max Huang",
"url": "http://github.com/nosir",
Expand Down
2 changes: 1 addition & 1 deletion src/Cleave.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ var Cleave = React.createClass({
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
10 changes: 7 additions & 3 deletions src/utils/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,20 @@ var Util = {
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);

value = prefix + value.slice(diffIndex, diffIndex + 1) + value.slice(prefixLength + 1);
}

return value.slice(prefixLength);
},

getFirstDiffIndex: function(prev, current) {
getFirstDiffIndex: function (prev, current) {
var index = 0;
while (prev.charAt(index) === current.charAt(index))

while (prev.charAt(index) === current.charAt(index))
if (prev.charAt(index++) === '')
return -1;

return index;
},

Expand Down

0 comments on commit 51c5920

Please sign in to comment.