create-id
v1.0.2
Published
Create a unique id or string with prefix and suffix options
Downloads
148
Maintainers
Readme
Create Id
Install
npm install create-id
or
yarn add create-id
Usage
import createId from "create-id";
const createId = require("create-id");
createId can be called with no arguments to make a 20 character id
createId() => "x8HZPoiMRUhQzygXegAK"
Arguments
createId(prefix, suffix, length, chars);
prefix places a string at the front of the id suffix places a string at the end of the id Neither are counted against length. Must be a string, suggested to be something to help find type or location of data.
createId("myPrefix-", "_mySuffix") => "myPrefix-qsZGSjYRAmiJRt1s7v1h_mySuffix"
Encouraged to use a character like - or _ at the end of your prefix and beginning of your suffix
You can split id id.split("-")
to get your prefix or suffix
length is how long the random generated portion is 20 is the default and must be a number
createId(null, null, 6) => "ojZESh"
createId(null, null, 16) => "3hgV848o9KBXrpqf"
chars is the characters used for the random generated portion. Must be a string, not an array Default characters are upper and lower case alphabet and numbers
- "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
createId(null, null, null, "01") => "11110011000100100011"
createId(null, null, null, "1234567890ABCDEF") => "01B5E21371E9DB262996"
If returned id is an empty string the default method occurs
createId(null, null, 0, "!@#$") => "q8jOrZIXaKjJU5wnCvUX"
Examples
createId("myPrefix-", "_mySuffix", 10, "1234567890") => "myPrefix-1217738484_mySuffix"
createId("game-", "_2019", 32, "1234567890ABCDEF") =>"game-71BE363570E799222CABE7C5D94BC444_2019"
createId("game-") + createId("_", "#import", 6) => "game-z6QFnS3wr21dt8pe66J6_XIySVz#import"
Create random hexadecimal colors
createId("#", null, 6, "1234567890abcdef") => "#6482d1"
createId("#", null, 6, "1234567890abcdef") => "#f14a87"