Gathering detailed insights and metrics for uid-generator
Gathering detailed insights and metrics for uid-generator
Gathering detailed insights and metrics for uid-generator
Gathering detailed insights and metrics for uid-generator
npm install uid-generator
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
20 Stars
41 Commits
1 Forks
2 Watching
1 Branches
1 Contributors
Updated on 03 Jan 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-11.8%
666
Compared to previous day
Last week
-19%
4,558
Compared to previous week
Last month
3.1%
21,106
Compared to previous month
Last year
-15.6%
200,067
Compared to previous year
Generates cryptographically strong, pseudo-random UIDs with custom size and base-encoding. Generated UIDs are strings that are guaranteed to always be the same length depending on the specified bit-size.
Great for generating things like compact API keys and UIDs that are safe to use in URLs and cookies.
Tip: The main benefit of this module is the ability to easily generate human-safe UIDs (using base58) and large UIDs that are more compact (using something like base94). If you’re just looking to generate URL-safe base64 IDs, the best package for that is uid-safe
.
1npm install uid-generator 2# or 3yarn add uid-generator
1const UIDGenerator = require('uid-generator'); 2const uidgen = new UIDGenerator(); // Default is a 128-bit UID encoded in base58 3 4// Async with `await` 5await uidgen.generate(); // -> 'B1q2hUEKmeVp9zWepx9cnp' 6 7// Async with promise 8uidgen.generate() 9 .then(uid => console.log(uid)); // -> 'PXmRJVrtzFAHsxjs7voD5R' 10 11// Async with callback 12uidgen.generate((err, uid) => { 13 if (err) throw err; 14 console.log(uid); // -> '4QhmRwHwwrgFqXULXNtx4d' 15}); 16 17// Sync 18uidgen.generateSync(); // -> '8Vw3bgbMMzeYfrQHQ8p3Jr'
Creates a new UIDGenerator
instance that generates bitSize
-bit or uidLength
-sized UIDs encoded using the characters in baseEncoding
.
Param | Type | Default | Description |
---|---|---|---|
[bitSize] | number | 128 | The size of the UID to generate in bits. Must be a multiple of 8 . |
[baseEncoding] | string | UIDGenerator.BASE58 | One of the UIDGenerator.BASE## constants or a custom string of characters to use to encode the UID. |
Note: If a custom baseEncoding
that has URL-unsafe characters is used, it is up to you to URL-encode the resulting UID.
Example
1new UIDGenerator(); 2new UIDGenerator(256); 3new UIDGenerator(UIDGenerator.BASE16); 4new UIDGenerator(512, UIDGenerator.BASE62); 5new UIDGenerator('01'); // Custom encoding (base2)
?Promise<string>
Asynchronously generates a UID.
Param | Type | Description |
---|---|---|
[cb] | ?function(error, uid) | An optional callback that will be called with the results of generating the UID. If not specified, the function will return a promise. |
Returns: ?Promise<string>
- A promise that will resolve with the UID or reject with an error. Returns nothing if the cb
parameter is specified.
async
/await
Example
1const uidgen = new UIDGenerator(); 2// This must be inside an async function 3const uid = await uidgen.generate();
Promise Example
1const uidgen = new UIDGenerator(); 2 3uidgen.generate() 4 .then(uid => { 5 // Use uid here 6 });
Callback Example
1const uidgen = new UIDGenerator(); 2 3uidgen.generate((err, uid) => { 4 if (err) throw err; 5 // Use uid here 6});
string
Synchronously generates a UID.
Returns: string
- The generated UID.
Example
1const uidgen = new UIDGenerator(); 2const uid = uidgen.generateSync();
number
The size of the UID that will be generated in bits (the bitSize
value passed to the UIDGenerator
constructor).
If the uidLength
parameter is passed to the constructor instead of bitSize
, bitSize
is calculated as follows:
1bitSize = Math.ceil(length * Math.log2(base));
Example
1new UIDGenerator().bitSize // -> 128 2new UIDGenerator(256).bitSize // -> 256
number
The length of the UID string that will be generated. The generated UID will always be this length.
This will be the same as the uidLength
parameter passed to the UIDGenerator
constructor.
If the uidLength
parameter is not passed to the constructor, it will be calculated using the bitSize
parameter as follows:
1uidLength = Math.ceil(bitSize / Math.log2(base))
Example
1new UIDGenerator().uidLength // -> 22 2new UIDGenerator(256, UIDGenerator.BASE62).uidLength // -> 43
string
The set of characters used to encode the UID string (the baseEncoding
value passed to the UIDGenerator
constructor).
Example
1new UIDGenerator().baseEncoding // -> '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' 2new UIDGenerator(UIDGenerator.BASE16).baseEncoding // -> '0123456789abcdef' 3new UIDGenerator('01').baseEncoding // -> '01'
number
The base of the UID that will be generated (which is the number of characters in the baseEncoding
).
Example
1new UIDGenerator().base // -> 58 2new UIDGenerator(UIDGenerator.BASE16).base // -> 16 3new UIDGenerator('01').base // -> 2
string
0123456789abcdef
string
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
string
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
(all alphanumeric characters except for 0
, O
, I
, and l
— characters easily mistaken for each other)
The default base.
Tip: Use this base to create UIDs that are easy to type in manually.
string
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
string
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-._~
(all ASCII characters that do not need to be encoded in a URI as specified by RFC 3986)
string
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!'()*-._~
(all ASCII characters that are not encoded by encodeURIComponent()
)
string
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
(all readable ASCII characters)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More