1.0.0 • Published 1 year ago

@cstan/local-uid v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

local-uid

A CommonJS module that generates and tests for a unique identifier.

The generator

This unique identifier can be used for database or other data identifiers. It conforms to our agency needs and legacy uid. The format is 12 hexadecimal characters, a hyphen, eight hexadecimal characters, a hyphen, 4 hexadecimal characters, a hyphen and four hexadecimal characters.

The first set of hexadecimal characters are derived from the current time and changes from millisecond to millisecond. The second set of characters are derived from an internal const string or from a string passed in as an argument. The striing passed in could be the hostname, company name or the name of the CEO's daughter (just kidding) but it can be any identifier that makes your generation of the UID uniquely yours. The last two sets of characters are randomly generated numbers converted to hexadecimal.

The validator

The validator merely ensures that the string submitted to it has the format generated above, that is 12 hex characters, a hyphen, 8 hex characters, a hyphen, 4 hex characters, a hyphen and 4 more hex characters.

Installation

npm install @cstan/local-uid

Use createLuid

const { createLuid } = require("@cstan/local-uid");

const newLuid = createLuid("Acme Corporation");
console.log(`New LUID: ${newLuid});
// New LUID: df8b686e18d4-13d503f2-1178-b8bf

Use validateLuid

const { validateLuid } = require("@cstan/local-uid");

fakeLuid = "df8b686e18d4-1178-b8bf-13d503f2";

if( validateLuid(fakeLuid)){
  console.log(`It is a good LUID.`);
} else {
  console.log(`It is an invalid LUID.`);
}
// It is an invalid LUID.

Note this would be an appropriate check before updating a data row or record.