-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (50 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
// This module will increment alphanumeric counters using 0-9 and a-Z
// For example next('a9') => 'aa'
// return null for invalid input
// blacklist is an array of strings you want to skip
exports.next = function next(input, blacklist) {
// lookup for borders of charcode increments
const borders = {
57: 97,
122: 65,
90: 48
};
// return a starting counter if input is falsey
if (!input) {
return '0';
}
// return null if input is otherwise invalid
if (typeof input !== 'string' || input.match(/[^a-zA-Z0-9]/)) {
return null;
}
// incriment and return
let nextStringArr = input.split('');
for (let i = input.length - 1; i >= 0; i--) {
let charCode = nextStringArr[i].charCodeAt(0);
let nextChar;
// is char on a border or not?
if (borders.hasOwnProperty(charCode)) {
nextChar = borders[charCode];
} else {
nextChar = charCode + 1;
}
nextStringArr[i] = String.fromCharCode(nextChar);
// do we need to increment the next character?
if (charCode === 90) {
// do we need to add a new character?
if (i === 0) {
nextStringArr.unshift('0');
i++;
} else {
continue;
}
}
let nextString = nextStringArr.join('');
// is this string blacklisted?
if (blacklist && blacklist.indexOf(nextString) > -1) {
return this.next(nextString, blacklist);
}
return nextString;
}
};