@telemok/unique-var-name

0.0.1 • Public • Published

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:

  1. Create your NodeJs, Browser or Webview app.
  2. Run: npm import @telemok/unique-var-name
  3. Code: import { nextUniqueVarName } from '@telemok/unique-var-name';
  4. 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!

Url Lng Result
length
Collis Can be
var name
# Regex Comment
https://npmjs.com/package/@telemok/unique-var-name nextUniqueVarName (key) JS short auto yes n [a-zA-Z_]
[a-zA-Z0-9_]*
this
https://npmjs.com/package/@telemok/unique-var-name nextUniqueString (prefix) JS short auto no n [a-zA-Z0-9_]* this
.toString(radix) JS short manual no - [0-9a-z]*
https://npmjs.com/package/@gorebel/css-class-generator JS short manual no [-a-zA-Z]+
https://npmjs.com/package/scopeid JS short auto no n [0-9]
https://npmjs.com/package/element-id-generator JS short auto no 1 [0-9a-z]
https://npmjs.com/package/nanoid JS 21 chance no [A-Za-z0-9_-]{21} fast?
popular
https://npmjs.com/package/identifier JS fix chance no [a-zA-Z0-9_]{n}
https://npmjs.com/package/node-random-name JS no chance no human
names
https://npmjs.com/package/unique-names-generator JS no chance no goods
names
https://npmjs.com/package/project-name-generator JS no chance no project
names
https://npmjs.com/package/unique-string JS 32 chance no [a-z0-9]{32} 14M dl
https://npmjs.com/package/@paralleldrive/cuid2 JS 24 chance no [a-z0-9]{24}
https://npmjs.com/package/randomstring JS ? chance ? [a-z0-9]+
https://npmjs.com/package/random-ext JS ? chance ? [a-z0-9]+
https://npmjs.com/package/hat JS 32 chance no [a-z0-9]{32}
https://npmjs.com/package/randomized-string JS no chance no [a-z0-9A-Z]
https://npmjs.com/package/meaningful-string JS fix chance no [A-Za-z0-9]
https://npmjs.com/package/generate-unique-id JS fix chance no [custom]{n} 6K dl
https://npmjs.com/package/unique-slug JS fix no [0-9A-H]{4} 19M dl
https://npmjs.com/package/puid-js JS fix * [custom]{n}
https://npmjs.com/package/random-string-gen JS fix * [0-9a-zA-Z]{n}
https://npmjs.com/package/lite-id JS fix no [0-9a-zA-Z_]
https://npmjs.com/package/random-web-token JS fix no [0-9a-zA-Z]{n}
https://npmjs.com/package/uniqider JS 16 no [0-9a-zA-Z]{16}

Package Sidebar

Install

npm i @telemok/unique-var-name

Weekly Downloads

0

Version

0.0.1

License

Apache-2.0

Unpacked Size

21.5 kB

Total Files

11

Last publish

Collaborators

  • telemok