unique-var-name (beta version)
JS unique short var names generator without collisions
Features
- Shortest names.
- First symbol not digit. URL and name-friendly: No special characters.
- Not random names.
- ([a-zA-Z_][0-9a-zA-Z_]*) name format.
- Counter: "a", "b", ..., "z", "A", ..., "Z", "_", "a0", "a1", ..., "a_", "b0", ... "__", "a00", ...
- Can be used as variable names, class names, id, and other.
- Like a num.toString(radix), but radix increased from 36 to 63 and first symbol not digit.
- No dependencies. No network connection.
- Different counter for each key/prefix.
- Fast: 210-240M calls / second (Ryzen 5500U).
Examples:
Available in folder /examples/
1. Show how to generate var names
import {nextUniqueVarName} from "@telemok/unique-var-name.js"
let m = new Set();
for (let i = 0; i < 7370; i++) {
let s = nextUniqueVarName();
console.log(i, s);
if (m.has(s))
throw(new Error(s))
m.add(s);
}
/* output:
0 a
1 b
2 c
...
49 X
50 Y
51 Z
52 _
53 a0
54 a1
55 a2
...
3388 _X
3389 _Y
3390 _Z
3391 __
3392 a00
3393 a01
3394 a02
...
7357 a_X
7358 a_Y
7359 a_Z
7360 a__
7361 b00
7362 b01
7363 b02
...
*/
2. Show how to generate strings with prefix
import {nextUniqueString} from "@telemok/unique-var-name.js"
for (let i = 0; i < 7370; i++) {
let s = nextUniqueString("prefix_");
let sA1 = nextUniqueString("_");
let sA2 = nextUniqueString("_");
console.log(i, s, sA1, sA2);
}
/* output:
0 prefix_0 _0 _1
1 prefix_1 _2 _3
2 prefix_2 _4 _5
...
29 prefix_t _W _X
30 prefix_u _Y _Z
31 prefix_v __ _10
32 prefix_w _11 _12
33 prefix_x _13 _14
//new size begin on symbol "1", skip "0"
*/
Source code:
https://npmjs.com/@telemok/unique-var-name
Installation:
- Create your NodeJs, Browser or Webview app.
- Run: npm import @telemok/unique-var-name
- Code: import { nextUniqueVarName } from '@telemok/unique-var-name';
- Code: var nextName = nextUniqueVarName();
Functions:
/* Different counter for each key.
240M calls / second (Ryzen 5500U).
return unique shortest string with first symbol not digit
*/
nextUniqueVarName(key);
/* Different counter for each prefix.
210M calls / second (Ryzen 5500U).
return prefix + unique shortest string
*/
nextUniqueString(prefix);
/*Return one random char from string*/
getRandomCharOfString(str);
Tests:
Test1.Generate 30000000 unique names
ok
Another libs another authors:
Warning, table has many mistakes!