id-generators
v1.0.6
Published
id-generators is small JavaScript library to generate ID with awesome Unique ID libraries.
Downloads
21
Maintainers
Readme
id-generators
id-generators is small JavaScript library to generate ID with awesome Unique ID libraries.
Installation
npm install id-generators
Usages
const generators = require("id-generators");
const generator = generators.get("nanoid");
const generate = generator();
console.log("ID: " + generate());
// ID: gCa0wL_8ElTje5cwOci1d
console.log("ID: " + generate());
// ID: 5C_YAjgQl4iM3zK-TValY
console.log("ID: " + generate());
// ID: gQOOwCoQyfCuGK7fgINLd
If you want to customize ID, you can pass an option as an argument to the generator
function.
const generators = require("id-generators");
const generator = generators.get("nanoid-simple");
const generate = generator({ size: 25 });
console.log("ID: " + generate());
// ID: oa9wj0kfm50gv2qse8l5xup0u
console.log("ID: " + generate());
// ID: xn5ff5odiylg4jehhhp9vtlxv
console.log("ID: " + generate());
// ID: dff7ay5x38k1pkc2pxjv7elky
| generator type | ID length | character set | options/default | description |
| ------------------------------------------- | --------- | ------------------------ | --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| cuid (default) | 25 | a-z0-9
, start with c
| | use cuid()
generated string. e.g. ck2bi7fxf00013ryng5jr1rer
|
| cuid-slug | 7-10 | a-z0-9
| | use cuid.slug()
generated string. e.g. xh23npi
|
| nanoid /nanoid-good | 21 | A-Za-z0-9_-
| size/21 | use nanoid()
or nanoid-good
generated string. e.g. EwUTt2eoka-oEV5kf-o0O
|
| nanoid-simple /nanoid-simple-good | 24 | a-z0-9
| size/24 | use nanoid/generate
or nanoid-good/generate
generated string. e.g. pfldm3gg8h9psydphotqe71d
|
| nanoid-lowercase /nanoid-lowercase-good | 26 | a-z
| size/26 | use nanoid/generate
or nanoid-good/generate
generated string. e.g. jsjxoibprplrdoitjmppotjrnm
|
If use
nanoid-good
ornanoid-xxx-good
, you should installnanoid-good
manually.npm install nanoid-good
Define Custom Generators
This sample shows how to register a generator function. The generator function should return a function that returns a ID.
const { register } = require("id-generators");
register("my_custom_id", function (option) {
option = option || {};
let size = option.size || 8;
let prefix = option.prefix || "items-";
return function (title) {
return (
prefix + title.toLowerCase().replace(/[^\w]/g, "").substring(0, size)
);
};
});
const generators = require("id-generators");
const generator = generators.get("my_custom_id");
const generate = generator({ size: 6 });
console.log("ID: " + generate("Hello World!"));
// ID: items-hellow
console.log("ID: " + generate("Foo Bar!"));
// ID: items-foobar
Related
- Awesome Unique ID - A curated list of awesome Unique ID libraries and resources.
- cuid - Collision-resistant ids optimized for horizontal scaling and binary search lookup performance.
- nanoid - A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
- nanoid-good - Guarantees you will not get any obscene words or other profanity in your ids generated by Nano ID.
License
Copyright (c) 2019 dailyrandomphoto. Licensed under the MIT license.