Gathering detailed insights and metrics for typeid-js
Gathering detailed insights and metrics for typeid-js
TypeScript implementation of TypeIDs: type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs
npm install typeid-js
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Total Downloads
5,720,459
Last Day
28,655
Last Week
134,768
Last Month
601,270
Last Year
5,403,781
312 Stars
27 Commits
6 Forks
8 Watching
1 Branches
9 Contributors
Minified
Minified + Gzipped
Latest Version
1.1.0
Package Id
typeid-js@1.1.0
Unpacked Size
91.08 kB
Size
15.31 kB
File Count
8
NPM Version
8.19.3
Node Version
19.2.0
Publised On
20 Sept 2024
Cumulative downloads
Total Downloads
Last day
-25.1%
28,655
Compared to previous day
Last week
-21.1%
134,768
Compared to previous week
Last month
18.6%
601,270
Compared to previous month
Last year
1,606.4%
5,403,781
Compared to previous year
1
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 jetify team. It provides an npm package that can be used by any JavaScript or TypeScript project.
Using npm:
1npm install typeid-js
Using yarn:
1yarn add typeid-js
Using pnpm:
1pnpm add typeid-js
Note: this package requires Typescript > 5.0.0
To create a random TypeID of a given type, use the typeid()
function:
1import { typeid } from 'typeid-js'; 2const tid = typeid('prefix');
The prefix is optional, so if you need to create an id without a type prefix, you can do that too:
1import { typeid } from 'typeid-js'; 2const tid = typeid();
The return type of typeid("prefix")
is TypeID<"prefix">
, which lets you use
TypeScript's type checking to ensure you are passing the correct type prefix to
functions that expect it.
For example, you can create a function that only accepts TypeIDs of type user
:
1import { typeid, TypeID } from 'typeid-js'; 2 3function doSomethingWithUserID(id: TypeID<'user'>) { 4 // ... 5}
In addition to the typeid()
function, the TypeID
class has additional methods
to encode/decode from other formats.
For example, to parse an existing typeid from a string:
1import { TypeID } from 'typeid-js'; 2 3// The prefix is optional, but it enforces the prefix and returns a 4// TypeID<"prefix"> instead of TypeID<string> 5const tid = TypeID.fromString('prefix_00041061050r3gg28a1c60t3gf', 'prefix');
To encode an existing UUID as a TypeID:
1import { TypeID } from 'typeid-js'; 2 3// In this case TypeID<"prefix"> is inferred from the first argument 4const tid = TypeID.fromUUID('prefix', '00000000-0000-0000-0000-000000000000');
The full list of methods includes:
getType()
: Returns the type of the type prefixgetSuffix()
: Returns uuid suffix in its base32 representationtoString()
: Encodes the object as a string, using the canonical formattoUUID()
: Decodes the TypeID into a UUID string in hex format. The type prefix is ignoredtoUUIDBytes()
: Decodes the TypeID into a UUID byte array. The type prefix is ignoredfromString(str, prefix?)
: Parses a TypeID from a string, optionally checking the prefixfromUUID(prefix, uuid)
: Creates a TypeID from a prefix and a UUID in hex formatfromUUIDBytes(prefix, bytes)
: Creates a TypeID from a prefix and a UUID in byte array formatNo vulnerabilities found.
No security vulnerabilities found.