0.0.2 • Published 3 years ago
simple-id-provider v0.0.2
SIMPLE ID PROVIDER
a simple ID Provider using javascript
- given an intended id provides a unique string which is a meaningful id
- no random string is given in case of no id param
- type safe with typescript 🦺
- currently supports only ESM
- fast⚡
installation
npm i simple-id-provider
usage
import IDProvider from 'simple-id-provider';
const idProvider = new IDProvider();
let id = idProvider.getId("test");
console.log(id) // test
let newid = idProvider.getId("test");
console.log(id) // test-1;
console.log(newid) // test-2api
- constructor
options(optional) can be passed intoIDProvider()asIDProvider(options)which is a javascript object containing these key value pairs:caseSensitiveboolean (defaults tofalse) - if true then case sensitiveness is considered else whole id is converted to lowercase lettersallowDuplicatesboolean (defaults totrue) - if false then it'll throw error if same id is given in same context. for eg. the above code will throw error as same stringtestis being passed twice.specialCharsAllowedstring (defaults to hyphen:-character) - By default the id provider will only allow alphanueric values and ignore any special char used (except hyphen:-that is set as default). for eg:idProvider.getId("hello&world")will returnhello-worldas id and&char will be ignored as ifwhitespacechar. If you want to let other special chars be in the returned id pass string of all those chars inspecialCharsAllowedoption.
getId(id)accepts intented id as string and returns a unique id in the execution context.please note that
idparam is mandatory and it doesn't return a random id if nothing is provided.