Skip to main content

@rtorcato/js-common / uuid

uuid

Functions

getUUID()

getUUID(): string

Defined in: uuid/index.ts:18

Generates a UUID v4 string.

Returns

string

A new UUID v4 string.


getUUIDv7()

getUUIDv7(): string

Defined in: uuid/index.ts:32

Generates a UUID v7 string (time-ordered). UUID v7 embeds a Unix timestamp, making them sortable and ideal for database primary keys.

Returns

string

A new UUID v7 string.

Example

getUUIDv7() // '018e5e2c-7c0a-7000-8000-0e02b2c3d479'

getShortUUID()

getShortUUID(): string

Defined in: uuid/index.ts:40

Generates a short UUID string using short-uuid.

Returns

string

A new short UUID string.


toShortUUID()

toShortUUID(uuid): string

Defined in: uuid/index.ts:54

Converts a standard UUID to a short UUID.

Parameters

uuid

string

The standard UUID string.

Returns

string

The short UUID string.

Example

toShortUUID('f47ac10b-58cc-4372-a567-0e02b2c3d479') // 'mhvXdrZT4jP5T8vBxuvm75'

fromShortUUID()

fromShortUUID(short): string

Defined in: uuid/index.ts:68

Converts a short UUID back to a standard UUID.

Parameters

short

string

The short UUID string.

Returns

string

The standard UUID string.

Example

fromShortUUID('mhvXdrZT4jP5T8vBxuvm75') // 'f47ac10b-58cc-4372-a567-0e02b2c3d479'

isUUID()

isUUID(id): boolean

Defined in: uuid/index.ts:77

Checks if a string is a valid UUID (any version).

Parameters

id

string

The string to validate.

Returns

boolean

True if valid UUID, false otherwise.


isUUIDv4()

isUUIDv4(id): boolean

Defined in: uuid/index.ts:84

Checks if a string is a valid UUID v4.

Parameters

id

string

The string to validate.

Returns

boolean

True if valid UUID v4, false otherwise.


isNilUUID()

isNilUUID(id): boolean

Defined in: uuid/index.ts:97

Checks if a UUID is the nil UUID (all zeros).

Parameters

id

string

The UUID string to check.

Returns

boolean

True if nil UUID, false otherwise.

Example

isNilUUID('00000000-0000-0000-0000-000000000000') // true
isNilUUID('f47ac10b-58cc-4372-a567-0e02b2c3d479') // false

getUUIDVersion()

getUUIDVersion(id): number

Defined in: uuid/index.ts:112

Gets the version number of a UUID.

Parameters

id

string

The UUID string.

Returns

number

The UUID version (1, 4, 7, etc.), or 0 for nil UUID.

Throws

If the UUID is invalid.

Example

getUUIDVersion('f47ac10b-58cc-4372-a567-0e02b2c3d479') // 4
getUUIDVersion('018e5e2c-7c0a-7000-8000-0e02b2c3d479') // 7
getUUIDVersion('00000000-0000-0000-0000-000000000000') // 0

getNilUUID()

getNilUUID(): string

Defined in: uuid/index.ts:123

Returns the nil UUID (all zeros).

Returns

string

The nil UUID string.


uuidToBytes()

uuidToBytes(id): Uint8Array

Defined in: uuid/index.ts:130

Converts a UUID string to a byte array (Uint8Array).

Parameters

id

string

The UUID string.

Returns

Uint8Array

The byte array representation of the UUID.


bytesToUUID()

bytesToUUID(bytes): string

Defined in: uuid/index.ts:137

Converts a byte array (Uint8Array) to a UUID string.

Parameters

bytes

Uint8Array

The byte array.

Returns

string

The UUID string.