-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeqstring.js
More file actions
84 lines (69 loc) · 2.21 KB
/
Copy pathSeqstring.js
File metadata and controls
84 lines (69 loc) · 2.21 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
class Seqstring {
/**
* Construct the sequential string generator.
*
* @param {Number} minLength Minimum length of string
* @param {Number} maxLength Maximum length of string
* @param {Object} characters Array of characters to use to generate strings
* @return {Object} Return ES6 generator
*/
constructor(minLength, maxLength, characters) {
this.minLength = minLength || 1;
this.maxLength = maxLength || 1e2;
this.characters = characters || 'abcdefghijklmnopqrstuvwxyz0123456789'.split('');
return Seqstring.generator(this.minLength, this.maxLength, this.characters);
}
/**
* Generate a string with given characters.
*
* @param {Number} num
* @param {Object} characters Array of characters to use to generate strings
* @return {String} Return generated string
*/
static generateString(index, characters) {
var string = '';
var modulo = 0;
while (index > -1) {
modulo = index % characters.length;
string = characters[modulo] + string;
index = ((index - modulo) / characters.length) - 1;
}
return string;
}
/**
* Create generator function.
*
* @param {Number} minLength Minimum length of string
* @param {Number} maxLength Maximum length of string
* @param {Object} characters Array of characters to use to generate strings
*/
static * generator(minLength, maxLength, characters) {
var min = Seqstring.getIndexOffset(characters.length, minLength);
var max = Seqstring.getIndexOffset(characters.length, maxLength);
var index = min - 1;
while (index > -1) {
if(max === index) {
break;
}
var string = Seqstring.generateString(index, characters);
yield string;
index++;
}
}
/**
* Used to calculate index for minimum/maximum string length.
*
* @param {Number} characterCount
* @param {Number} offset
* @return {Number}
*/
static getIndexOffset(characterCount, offset) {
var indexOffset = 0;
for (var i = 0; i < offset; i++) {
indexOffset += Math.pow(characterCount, i);
}
return indexOffset;
}
}
module.exports = Seqstring;