get-short-id
Advanced tools
Comparing version
11
index.js
@@ -1,3 +0,2 @@ | ||
const anyBase = require('any-base'); | ||
const uuid = require('lite-uuid-v4'); | ||
const {nanoid} = require('nanoid'); | ||
@@ -7,9 +6,7 @@ function getId(prefix, chars) { | ||
chars = chars || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_'; | ||
const fromHex = anyBase(anyBase.HEX, chars); | ||
let _uuid = uuid(); | ||
// skip version number & "-" | ||
_uuid = _uuid.slice(0, 8) + _uuid.slice(9, 13) + _uuid.slice(15, 18) + _uuid.slice(15, 18) + _uuid.slice(24); | ||
return prefix + fromHex(_uuid); | ||
// generate 1k IDs/second and you will need 2 billion years to have 1% probability of 1 collision | ||
// https://zelark.github.io/nano-id-cc/ | ||
return prefix + nanoid(chars, 24); | ||
} | ||
module.exports = getId; |
{ | ||
"name": "get-short-id", | ||
"version": "1.0.3", | ||
"description": "Random ID generator function with options to provide prefix and character set. Uses UUID v4 as random string source.", | ||
"version": "1.0.4", | ||
"description": "Random ID generator function with options to provide prefix and character set. Uses nanoid(23) as random string source.", | ||
"main": "index.js", | ||
@@ -23,5 +23,4 @@ "scripts": {}, | ||
"dependencies": { | ||
"any-base": "^1.1.0", | ||
"lite-uuid-v4": "^1.0.0" | ||
"nanoid": "3.1.23" | ||
} | ||
} |
`npm i get-short-id` | ||
This package uses UUID v4 as base random string yet it removes all not usefull parts. Each uuid v4 string has "4" to identify uuid version, all uuid v4 strings also have "-" | ||
multiple times. Often this is not needed. What is very usefull instead is to have prefixes for IDs to identify what kind of object this ID represents. Like user IDs start with `u-` | ||
It is useful to have prefixes for IDs to identify what kind of object this ID represents. Like user IDs start with `u-` | ||
, invoices with `in-` and so on. | ||
@@ -10,3 +9,3 @@ | ||
// getId('prefix', 'character-set') | ||
// getId('prefix', 'custom-character-set') | ||
@@ -135,1 +134,12 @@ for(let i = 0; i < 10; i++) { | ||
``` | ||
## Fun Math game | ||
- Generate 1000 IDs per second | ||
- Continue doing so for 2 billion years | ||
Congrats! You have 1% probability to witness a collision | ||
(we take 23 characters from nanoid) | ||
https://zelark.github.io/nano-id-cc/ |
6436
13.67%1
-50%7
75%144
7.46%13
-18.75%+ Added
+ Added
- Removed
- Removed
- Removed
- Removed