unique-sequence
Generate a sequence of short strings unique within their scope.
This utility is built using Node.js.
const { generatorCustom } = require('unique-sequence');
const gen = generatorCustom(['a', 'b']);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
Generators available
- generatorCustom
- generatorAlphaUpper
- generatorAlphaLower
- generatorAlphaNumUpper
- generatorAlphaNumLower
- generatorNum
Introduction
This library generates unique short strings. The generator starts out by
creating strings of just one character.
A list of strings are used to generate a sequence of strings similar to the
sequence of natural numbers or integers.
For instance, below is a sequence of natural numbers generated using the list of
decimal digits 0
to 9
.
0, 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
Similarly, below is a sequence using the list of latin alphabets a
to j
a, b, c, d, e, f, g, h, i, j,
ba, bb, bc, bd, be, bf, bg, bh, bi, bj,
ca, cb, cc, cd, ce, cf, cg, ch, ci, cj,
da
In a similar manner, this library generates sequences, given a list of strings.
Getting started
Install this library
npm install unique-sequence
Example program
const {
generatorCustom,
generatorNum,
generatorAlphaUpper,
generatorAlphaLower,
generatorAlphaNumUpper,
generatorAlphaNumLower,
} = require('unique-sequence');
const list = ['a', 'b'];
const gen2 = generatorCustom(list);
console.log(gen2.next().value);
console.log(gen2.next().value);
console.log(gen2.next().value);
console.log(gen2.next().value);
console.log(gen2.next().value);
console.log(gen2.next().value);
console.log(gen2.next().value);
console.log(gen2.next().value);
console.log(gen2.next().value);
const gen3 = generatorCustom(['x', 'y', 'z']);
console.log(gen3.next().value);
console.log(gen3.next().value);
console.log(gen3.next().value);
console.log(gen3.next().value);
console.log(gen3.next().value);
console.log(gen3.next().value);
console.log(gen3.next().value);
console.log(gen3.next().value);
console.log(gen3.next().value);
console.log(gen3.next().value);
function times(gen, count = 64) {
const arr = [];
for (let i = 0; i < count; i++) {
arr.push(gen.next().value);
}
return arr;
}
console.log(times(generatorCustom(['x', 'y', 'z']), 10));
console.log(times(generatorAlphaUpper()));
console.log(times(generatorAlphaLower()));
console.log(times(generatorAlphaNumLower()));
console.log(times(generatorAlphaNumUpper()));
console.log(times(generatorNum()));
console.log(times(generatorCustom(['rat,', 'a', 'tat'])));
console.log(times(generatorCustom([
'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',
])));
try {
console.log(times(generatorCustom([
'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',
'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',
])));
} catch (e) {
console.error('error:', e.message);
}
Change log
version 1.0.0
2021-12-16
- Initial code to generate custom sequence, upper/lowercase, alphanum.
Note: Change log dates are yyyy-mm-dd.