Skip to content

Commit 754b811

Browse files
authored
chore: Makes the option argument optional and updates docs (#14)
* chore: Makes the option argument optional and updates docs * chore: further improve docs
1 parent 6481f56 commit 754b811

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed

README.md

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,42 @@
77
JavaScript module to generate all possible variations of strings over an
88
alphabet 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
2864
import 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
3167
for (
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)
3874
for (
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
4985
for (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!)
5490
const values = isv({
5591
alphabet: "abc1",
5692
});
5793

94+
// pull values from the iterator one by one
5895
console.log(values.next()); // { value: 'a', done: false }
5996
console.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

65141
The way the generation algorithm work is using an n-ary tree where n is the size

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"require": "./dist/index.js"
1111
}
1212
},
13-
"files": ["dist/", "src/", "types/"],
13+
"files": ["dist/"],
1414
"scripts": {
1515
"build": "tsc",
1616
"lint:fix": "biome check --write --organize-imports-enabled=true .",

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export type GenOptions = {
5050
maxIterations?: number
5151
}
5252

53-
export function* indexedStringVariation(options: GenOptions) {
53+
export function* indexedStringVariation(options: GenOptions = {}) {
5454
const alphabet = options.alphabet
5555
? cleanAlphabet(options.alphabet)
5656
: defaultAlphabet

0 commit comments

Comments
 (0)