@philiprehberger/cuid-ts
v0.1.3
Published
Collision-resistant sortable unique IDs
Downloads
418
Readme
@philiprehberger/cuid-ts
Collision-resistant sortable unique IDs for TypeScript
Installation
npm install @philiprehberger/cuid-tsUsage
import { createId, createIdFactory, isCuid } from '@philiprehberger/cuid-ts';
// Generate a unique ID
const id = createId();
// => "clx8f0a3b00001k9z..."
// Generate with options
const customId = createId({ prefix: 'user', length: 32 });
// Create a pre-configured factory
const generateOrderId = createIdFactory({ prefix: 'ord', length: 28 });
const orderId = generateOrderId();
// Type guard
if (isCuid(someValue)) {
// someValue is typed as Cuid
}API
| Export | Description |
| --- | --- |
| createId(options?) | Generate a collision-resistant sortable unique ID. Options: prefix (default 'c'), length (default 24), fingerprint. |
| createIdFactory(options) | Return a zero-argument function that generates IDs with the given options baked in. |
| isCuid(value) | Type guard that returns true if value matches the CUID format (starts with a lowercase letter, lowercase alphanumeric). |
| Cuid | Branded string type representing a valid CUID. |
| CreateIdOptions | Options interface: prefix?: string, length?: number, fingerprint?: string. |
Development
npm install
npm run build
npm test
npm run typecheckSupport
If you find this project useful:
