Skip to content

Commit 9985ceb

Browse files
committed
use const/let
1 parent 900ae9c commit 9985ceb

1 file changed

Lines changed: 42 additions & 42 deletions

File tree

lib/String_random.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ function String_random (pattern, options) {
33
const random = options && options.random || Math.random;
44

55
function processGrouping (pattern) {
6-
var tree = [];
7-
var stack = [ tree ];
8-
var n = 1;
6+
const tree = [];
7+
const stack = [ tree ];
8+
let n = 1;
99
while (pattern.length) {
10-
var chr = pattern.shift();
10+
const chr = pattern.shift();
1111
if (chr === '\\') {
12-
var next = pattern.shift();
12+
const next = pattern.shift();
1313
if (next === '(' || next === ')') {
1414
stack[0].push(next);
1515
} else {
1616
stack[0].push(chr, next);
1717
}
1818
} else
1919
if (chr === '(') {
20-
var inner = [];
20+
const inner = [];
2121
stack[0].push(inner);
2222
stack.unshift(inner);
2323

24-
var next = pattern.shift(); // no warnings
24+
let next = pattern.shift(); // no warnings
2525
if (next === '?') {
2626
next = pattern.shift();
2727
if (next === ':') {
@@ -50,12 +50,12 @@ function String_random (pattern, options) {
5050
}
5151

5252
function processSelect (tree) {
53-
var candinates = [ [] ];
53+
const candinates = [ [] ];
5454

5555
while (tree.length) {
56-
var chr = tree.shift();
56+
let chr = tree.shift();
5757
if (chr === '\\') {
58-
var next = tree.shift();
58+
const next = tree.shift();
5959
if (next === '|') {
6060
candinates[0].push(next);
6161
} else {
@@ -68,7 +68,7 @@ function String_random (pattern, options) {
6868
chr = tree.shift();
6969
candinates[0].push(chr);
7070
if (chr === '\\') {
71-
var next = tree.shift(); // no warnings
71+
const next = tree.shift(); // no warnings
7272
candinates[0].push(next);
7373
} else
7474
if (chr === ']') {
@@ -83,9 +83,9 @@ function String_random (pattern, options) {
8383
}
8484
}
8585

86-
for (var i = 0, it; (it = candinates[i]); i++) {
86+
for (let i = 0, it; (it = candinates[i]); i++) {
8787
tree.push(it);
88-
for (var j = 0, len = it.length; j < len; j++) {
88+
for (let j = 0, len = it.length; j < len; j++) {
8989
if (it[j] instanceof Array) {
9090
processSelect(it[j]);
9191
}
@@ -96,14 +96,14 @@ function String_random (pattern, options) {
9696
return [ tree ];
9797
}
9898

99-
var UPPERS = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
100-
var LOWERS = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
101-
var DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
102-
var SPACES = [" ", "\n", "\t"];
103-
var OTHERS = ["!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "`", "{", "|", "}", "~"];
104-
var ALL = [].concat(UPPERS, LOWERS, DIGITS, " ", OTHERS, ["_"]);
99+
const UPPERS = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
100+
const LOWERS = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
101+
const DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
102+
const SPACES = [" ", "\n", "\t"];
103+
const OTHERS = ["!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "`", "{", "|", "}", "~"];
104+
const ALL = [].concat(UPPERS, LOWERS, DIGITS, " ", OTHERS, ["_"]);
105105

106-
var CLASSES = {
106+
const CLASSES = {
107107
'd' : DIGITS,
108108
'D' : [].concat(UPPERS, LOWERS, SPACES, OTHERS, ['_']),
109109
'w' : [].concat(UPPERS, LOWERS, DIGITS, ['_']),
@@ -118,14 +118,14 @@ function String_random (pattern, options) {
118118
'0' : [ '\0' ]
119119
};
120120

121-
var REFERENCE = {};
121+
const REFERENCE = {};
122122

123123
function processOthers (tree) {
124-
var ret = '', candinates = [];
124+
let ret = '', candinates = [];
125125
tree = tree.slice(0);
126126

127127
function choice () {
128-
var ret = candinates[ Math.floor(candinates.length * random()) ];
128+
let ret = candinates[ Math.floor(candinates.length * random()) ];
129129
if (ret instanceof Array) {
130130
ret = processOthers(ret);
131131
}
@@ -134,26 +134,26 @@ function String_random (pattern, options) {
134134
}
135135

136136
while (tree.length) {
137-
var chr = tree.shift();
137+
let chr = tree.shift();
138138
switch (chr) {
139139
case '^':
140140
case '$':
141141
// do nothing
142142
break;
143143
case '*':
144-
for (var i = 0, len = random() * 10; i < len; i++) {
144+
for (let i = 0, len = random() * 10; i < len; i++) {
145145
ret += choice();
146146
}
147147
candinates = [];
148148
break;
149149
case '+':
150-
for (var i = 0, len = random() * 10 + 1; i < len; i++) {
150+
for (let i = 0, len = random() * 10 + 1; i < len; i++) {
151151
ret += choice();
152152
}
153153
candinates = [];
154154
break;
155155
case '{':
156-
var brace = '';
156+
let brace = '';
157157
while (tree.length) {
158158
chr = tree.shift();
159159
if (chr === '}') {
@@ -165,10 +165,10 @@ function String_random (pattern, options) {
165165

166166
if (chr !== '}') throw "missmatch brace: " + chr;
167167

168-
var dd = brace.split(/,/);
169-
var min = +dd[0];
170-
var max = (dd.length === 1) ? min : (+dd[1] || 10);
171-
for (var i = 0, len = Math.floor(random() * (max - min + 1)) + min; i < len; i++) {
168+
const dd = brace.split(/,/);
169+
const min = +dd[0];
170+
const max = (dd.length === 1) ? min : (+dd[1] || 10);
171+
for (let i = 0, len = Math.floor(random() * (max - min + 1)) + min; i < len; i++) {
172172
ret += choice();
173173
}
174174
candinates = [];
@@ -183,7 +183,7 @@ function String_random (pattern, options) {
183183

184184
case '\\':
185185
ret += choice();
186-
var escaped = tree.shift();
186+
const escaped = tree.shift();
187187

188188
if (escaped.match(/^[1-9]$/)) {
189189
candinates = [ REFERENCE[escaped] || '' ];
@@ -200,11 +200,11 @@ function String_random (pattern, options) {
200200
case '[':
201201
ret += choice();
202202

203-
var sets = [], negative = false;
203+
let sets = [], negative = false;
204204
while (tree.length) {
205205
chr = tree.shift();
206206
if (chr === '\\') {
207-
var next = tree.shift();
207+
const next = tree.shift();
208208
if (CLASSES[next]) {
209209
sets = sets.concat(CLASSES[next]);
210210
} else {
@@ -215,25 +215,25 @@ function String_random (pattern, options) {
215215
break;
216216
} else
217217
if (chr === '^') {
218-
var before = sets[ sets.length - 1];
218+
const before = sets[ sets.length - 1];
219219
if (!before) {
220220
negative = true;
221221
} else {
222222
sets.push(chr);
223223
}
224224
} else
225225
if (chr === '-') {
226-
var next = tree.shift(); // no warnings
226+
const next = tree.shift(); // no warnings
227227
if (next === ']') {
228228
sets.push(chr);
229229
chr = next;
230230
break;
231231
}
232-
var before = sets[ sets.length - 1]; // no warnings
232+
const before = sets[ sets.length - 1]; // no warnings
233233
if (!before) {
234234
sets.push(chr);
235235
} else {
236-
for (var i = before.charCodeAt(0) + 1, len = next.charCodeAt(0); i < len; i++) {
236+
for (let i = before.charCodeAt(0) + 1, len = next.charCodeAt(0); i < len; i++) {
237237
sets.push(String.fromCharCode(i));
238238
}
239239
}
@@ -244,13 +244,13 @@ function String_random (pattern, options) {
244244
if (chr !== ']') throw "missmatch bracket: " + chr;
245245

246246
if (negative) {
247-
var neg = {};
248-
for (var i = 0, len = sets.length; i < len; i++) {
247+
const neg = {};
248+
for (let i = 0, len = sets.length; i < len; i++) {
249249
neg[sets[i]] = true;
250250
}
251251

252252
candinates = [];
253-
for (var i = 0, len = ALL.length; i < len; i++) {
253+
for (let i = 0, len = ALL.length; i < len; i++) {
254254
if (!neg[ALL[i]]) candinates.push(ALL[i]);
255255
}
256256
} else {
@@ -269,7 +269,7 @@ function String_random (pattern, options) {
269269
return ret + choice();
270270
}
271271

272-
var tree;
272+
let tree;
273273
tree = processGrouping(pattern.split(''));
274274
tree = processSelect(tree);
275275
return processOthers(tree);

0 commit comments

Comments
 (0)