Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate unique numeric short ID in nodejs?

How to generate unique numeric short ID in nodejs?

I need to give each of my transaction one of these numeric short id

There can be millions of transactions

like image 409
Carson Yau Avatar asked Nov 30 '22 14:11

Carson Yau


1 Answers

I have written an npm package named custom-id to solve that problem. You can tailor your custom-id as you like. If you want it numeric, you can do this. It can generate alphanumeric value too. You are concerned about the readability. That's why the plugin is written. It is built for both human and machine. We use familiar letters (from given name & email) in our randomly generated ID. People can easily recognize those familiar letters as they use it every time, every day, every moment. You can randomly generate IDs with uuid or nanoid, but these are not generated for human. These are for the machine. But this library is for both of them.

You can download this package from here - custom-id

npm i custom-id

You can generate an ID instantly by giving an empty object as the argument.

var customId = require("custom-id");

customId({}); // Voila... A random 8 character string will be generated automatically

The custom ID will be generated in this format -

✌✌** 2 Number + 2 Letter + 2 Number + 2 Letter = 8 characters ** ✌✌

All those number and letter will be generated randomly. We use cryptography to generate ids (if available).

enter image description here

You can read about the detailed documentation from here - NPM

But in your case, you just want a number, which is human readable, intuitive & unique. The code will be like -

var customId = require("custom-id")
customId({
  name: "123456",
  email: "78910"
});
// Random Result - "20111070" or "11435189" or "64656618"

You should look into that number. This is just a number but easy on the eye and easy to remember. This random id generator will not create a number like - 24359625! Thanks to its unique but very simple algorithm. That's why I suggest you use it.

like image 158
Md Fazlul Karim Avatar answered Dec 04 '22 04:12

Md Fazlul Karim