unique-sequence
Generate a sequence of short strings unique within their scope.
The generated strings are as short as possible.
Unlike uuid
they are not universally unique. However a generator does not
repeat a sequence, making strings unique within the scope of a generator
instance.
This utility is built using Node.js.
Introduction
This library was written to generate short property names for JavaScript
objects.
uuid
is one way of generating property names which would make an object look like:
{
'2bdff0e2-9c4f-4b12-8669-7dac3dbf3203': 1,
'7b5c615c-03b2-4bb0-b8ff-995cebeed679': true
}
In the above, property names 2bdff0e2-9c4f-4b12-8669-7dac3dbf3203
,
7b5c615c-03b2-4bb0-b8ff-995cebeed679
are relatively long.
Instead, if you want to generate a few and very short names such as a
,
b
then this library can help.
Quick example
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);
console.log(gen.next().value);
How it works
It is similar to a sequence of integers.
For instance, below is a sequence generated using 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
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, using a specified list of
characters (or strings).
Generators available
- generatorCustom
- generatorAlphaUpper
- generatorAlphaLower
- generatorAlphaNumUpper
- generatorAlphaNumLower
- generatorNum
Getting started
Install this library
npm install unique-sequence
Example program
const {
generatorCustom,
generatorNum,
generatorAlphaUpper,
generatorAlphaLower,
generatorAlphaNumUpper,
generatorAlphaNumLower,
} = require('unique-sequence');
const gen = generatorAlphaLower();
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
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.3
2022-01-15
version 1.0.0
2021-12-16
- Initial code to generate custom sequence, upper/lowercase, alphanum.
Note: Change log dates are yyyy-mm-dd.