Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

nicolaspearson/node.ts.uuid

Repository files navigation

Node Typescript UUID

License Current Version npm Build Status

A simple uuid generator. The generator uses the MAC address / IPv6 address + the process id + the current time to ensure a unique id is generated. The length of the uuid can be specified, this will either increase or decrease the number of characters in the uuid. If the uuid needs to be padded to create a longer uuid, a bitwise operation is used to generate the unique characters. A prefix may also specified.

Notice (Deprecated)

The source code for this library has been moved to: Kalos.

Test Coverage

Coverage lines Coverage functions Coverage branches Coverage statements

Installation

npm install node-ts-uuid --save

Usage

import { Uuid, UuidOptions } from 'node-ts-uuid';

const options: UuidOptions = {
	length: 50,
	prefix: 'test-'
};

export function getUuid() {
	const uuid: string = Uuid.generate(options);
	console.log(uuid);
	return uuid;
}

Will generate a uuid similar to: test-6xke0ccwg0k00emrjrz81gu8a63657918908addf9942c

Methods

generate(options?: UuidOptions)

Generates a UUID using the IPv6 / Mac Address, the process id, and the current time. Optionally add a prefix, and limit / pad the uuid to be a specific length.

getPid()

Returns the running process id or a randomly generated 5 digit number.

getAddress()

Returns the MAC Address or IPv6 Address, if neither are available a randomly generated 8 digit number is returned.

getNow()

Returns the current epoch time or the previously returned epoch time incremented by 1.

postProcessUuid(uuid: string, length?: number)

Pads / limits the length of the provided uuid if the length is shorter than desired a bitwise operation provides the randomly generated characters.

Options

UuidOptions {
	length?: number;
	prefix?: string;
}
  • Length: Pad / Limit the length of the uuid.
  • Prefix: Add a prefix to the uuid.