Skip to content

Commit

Permalink
Enforce stricter integer parsing with filterInt
Browse files Browse the repository at this point in the history
filterInt enforces that the value to be encoded is indeed an integer.

From parseInt documentation on MDN:

> If parseInt encounters a character that is not a numeral in the
specified radix, it ignores it and all succeeding characters and
returns the integer value parsed up to that point.

See more here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global
_Objects/parseInt
  • Loading branch information
ebramanti committed Aug 28, 2016
1 parent fc03be6 commit 0036859
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion hashids.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export default class Hashids {

let uniqueAlphabet = '', sepsLength, diff;

this.filterInt = (value) => {
if(/^(\-|\+)?([0-9]+|Infinity)$/.test(value)) {
return parseInt(value, 10);
}

return NaN;
}

/* alphabet vars */

this.escapeRegExp = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
Expand Down Expand Up @@ -104,7 +112,7 @@ export default class Hashids {
}

for (let i = 0; i !== numbers.length; i++) {
numbers[i] = parseInt(numbers[i], 10);
numbers[i] = this.filterInt(numbers[i]);
if (numbers[i] >= 0) {
continue;
} else {
Expand Down

0 comments on commit 0036859

Please sign in to comment.