Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typeid-js

Package Overview
Dependencies
Maintainers
3
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typeid-js

Official implementation of the TypeID specification in TypeScript. TypeIDs are type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
101K
decreased by-17.22%
Maintainers
3
Weekly downloads
 
Created
Source

Official TypeID-JS Package

JavaScript implementation of TypeIDs using TypeScript.

TypeIDs are a modern, type-safe, globally unique identifier based on the upcoming UUIDv7 standard. They provide a ton of nice properties that make them a great choice as the primary identifiers for your data in a database, APIs, and distributed systems. Read more about TypeIDs in their spec.

This is the official JavaScript / TypeScript implementation of TypeID by the jetpack.io team. It provides an npm package that can be used by any JavaScript or TypeScript project.

Installation

Using npm:

npm install typeid-js

Using yarn:

yarn add typeid-js

Using pnpm:

pnpm add typeid-js

Usage

To create a random TypeID of a given type, use the typeid() function:

import { typeid } from 'typeid-js';
const tid = typeid("prefix");

The prefix is optional, so if you need to create an id with a type prefix, you can do that too:

import { typeid } from 'typeid-js';
const tid = typeid();

In addition to the typeid() function, there's also a TypeID class that can be used to encode/decode from other formats.

For example, to parse an existing typeid from a string:

import { TypeID } from 'typeid-js';

const tid = TypeID.fromString("prefix_00041061050r3gg28a1c60t3gf);

To encode an existing UUID as a TypeID:

import { TypeID } from 'typeid-js';

const tid = TypeID.fromUUID("prefix", "00000000-0000-0000-0000-000000000000");

The full list of methods includes:

  • getType(): Returns the type of the type prefix
  • getSuffix(): Returns uuid suffix in its base32 representation
  • toString(): Encodes the object as a string, using the canonical format
  • toUUID(): Decodes the TypeID into a UUID string in hex format. The type prefix is ignored
  • toUUIDBytes(): Decodes the TypeID into a UUID byte array. The type prefix is ignored
  • fromString(str): Parses a TypeID from a string
  • fromUUID(prefix, uuid): Creates a TypeID from a prefix and a UUID in hex format
  • fromUUIDBytes(prefix, bytes): Creates a TypeID from a prefix and a UUID in byte array format

Keywords

FAQs

Package last updated on 06 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc