77JavaScript module to generate all possible variations of strings over an
88alphabet using an n-ary virtual tree.
99
10+ ## Quick start example:
11+
12+ ``` js
13+ // generate all strings of max length 3 using the alphabet "ab"
14+ import isv from " indexed-string-variation" ;
15+
16+ for (const str of isv ({ alphabet: " ab" , maxLen: 3 })) {
17+ console .log (str);
18+ }
19+ ```
20+
21+ Output:
22+
23+ ``` plain
24+ (empty string)
25+ a
26+ b
27+ aa
28+ ab
29+ ba
30+ bb
31+ aaa
32+ aab
33+ aba
34+ abb
35+ baa
36+ bab
37+ bba
38+ bbb
39+ ```
40+
41+ > [ !IMPORTANT] \
42+ > Note that the first result is always an empty string! If you want to start
43+ > from the first non-empty string, you can use the ` from ` option to specify the
44+ > starting index of ` 1n ` .
45+
1046## Requirements
1147
1248- Node.js >= 22
@@ -27,14 +63,14 @@ follows:
2763``` js
2864import isv from " indexed-string-variation" ;
2965
30- // Basic usage: generate all variations for a given alphabet
66+ // Basic usage: generate the first 23 variations for a given alphabet
3167for (
3268 const str of isv ({ alphabet: " abc1" , maxIterations: 23 })
3369) {
3470 console .log (str);
3571}
3672
37- // Generate variations from a specific index (using BigInt)
73+ // Generate 5 variations starting from a specific index (using BigInt)
3874for (
3975 const str of isv ({
4076 alphabet: " abc1" ,
@@ -45,21 +81,61 @@ for (
4581 console .log (str);
4682}
4783
48- // Generate variations up to a maximum string length
84+ // Generate variations up to a maximum string length of 2 chars
4985for (const str of isv ({ alphabet: " abc1" , maxLen: 2 })) {
5086 console .log (str);
5187}
5288
53- // endless variations (don't use a `for ... of` loop because it will never end!)
89+ // endless variations (careful if you use a `for ... of` loop because it will never end unless you have a break condition !)
5490const values = isv ({
5591 alphabet: " abc1" ,
5692});
5793
94+ // pull values from the iterator one by one
5895console .log (values .next ()); // { value: 'a', done: false }
5996console .log (values .next ()); // { value: 'b', done: false }
60- // ...
97+
98+ // use iterator helpers and the spread operator to pull multiple values
99+ // at once into an array
100+ console .log ([... isv ({ alphabet: " abc1" }).take (10 )]);
101+ // [
102+ // '', 'a', 'b',
103+ // 'c', '1', 'aa',
104+ // 'ab', 'ac', 'a1',
105+ // 'ba'
106+ // ]
61107```
62108
109+ > [ !TIP] \
110+ > Find more about iterator helpers the
111+ > [ Iterators MDN page] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator ) .
112+
113+ ### Options
114+
115+ The ` isv ` generator function accepts options that allow you to configure how the
116+ generation will behave:
117+
118+ - ` alphabet ` : a ` string ` containing the characters that will be used to generate
119+ the variations. The order of the characters in the string defines their
120+ lexicographic order (defaults to
121+ ` abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ` ).
122+ - ` from ` : a ` bigint ` representing the index from which to start generating the
123+ variations (defaults to ` 0n ` ).
124+ - ` to? ` : a ` bigint ` representing the index at which to stop generating the
125+ variations (optional, defaults to ` undefined ` , which indicates infinity).
126+ - maxLen: a ` number ` representing the maximum length of the generated strings
127+ (optional, defaults to ` undefined ` , which means no limit).
128+ - maxIterations: a ` number ` representing the maximum number of iterations to run
129+ (optional, defaults to ` undefined ` , which means no limit).
130+
131+ > [ !IMPORTANT] \
132+ > All the options are optional and by default the generator will be endless (it
133+ > will keep generating variations), so if you use it in a ` for ... of ` loop it
134+ > will never end unless you have an explicit mechanism to break the loop!
135+ > Alternatively, you can use iterator helpers such as
136+ > [ ` Iterator.prototype.take ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/take )
137+ > to limit the number of iterations.
138+
63139## How the algorithm works
64140
65141The way the generation algorithm work is using an n-ary tree where n is the size
0 commit comments