Create random, unique or index based ID
# NPM
npm i usid
# Yarn
yarn add usid
import {
uid,
createIndex,
DEFAULT_CHARSET,
rand
} from "https://deno.land/x/usid/mod.ts";
const {
uid,
createIndex,
DEFAULT_CHARSET,
rand
} = require('usid');
// imagine that x is a database connector
let x = 0;
const options = {
get: () => x,
set: (value) => x = value,
charset: DEFAULT_CHARSET, // optional,
min: 3, // optional, default is 0
}
const getID = createIndex(options);
for (let i = 0; i < 3; i++) console.log(getID());
// Output:
// aab
// aac
// aad
Generate random ID with fixed length
const len = 5; // optional, default is 3
console.log(rand(len, DEFAULT_CHARSET)); // default charset is optional
// Output: random 5 characters based on DEFAULT_CHARSET
Generate unique ID with fixed length
const output_length = 24; // optional, default is 24
const base_decimal_length = 18; // optional, default is 18
console.log(uid(output_length, base_decimal_length, DEFAULT_CHARSET)); // default charset is optional
// Output: outputs 24 characters based on DEFAULT_CHARSET
Apache-2.0@eru123