ulid-uuid-converter
Version:
A tiny zero dependency library for ULID to UUID conversion and vice versa
50 lines (32 loc) • 1.66 kB
Markdown
> A tiny zero dependency library for ULID to UUID conversion and vice versa.
```sh
npm i ulid-uuid-converter
```
```typescript
import { UUIDtoULID, ULIDtoUUID } from "ulid-uuid-converter";
const ulid = UUIDtoULID("0186675b-9439-536e-1c4c-561c280fa87b");
// outputs: 01GSKNQ51SADQ1RK2P3GM0ZA3V
const uuid = ULIDtoUUID("01GSKNQ51SADQ1RK2P3GM0ZA3V");
// outputs: 0186675b-9439-536e-1c4c-561c280fa87b
```
You can pass an optional second argument with `nullOnInvalidInput` to both functions. With this option the function behavior changes: instead of throwing, it will return `null` if a bad input is passed as argument.
| Option | Type | Default | Description |
| ------------------ | --------- | ------- | ---------------------------------------------- |
| nullOnInvalidInput | `boolean` | `false` | Return `null` instead of throwing on bad input |
```typescript
// these will throw!
const ulid = UUIDtoULID("bad string");
const uuid = ULIDtoUUID("another bad string");
// these will return null instead of throwing
const ulid = UUIDtoULID("bad string", { nullOnInvalidInput: true });
const uuid = ULIDtoUUID("another bad string", { nullOnInvalidInput: true });
```
Big thanks to [devbanana](https://github.com/devbanana) for the [Crockford's Base32](https://github.com/devbanana/crockford-base32) encoding and decoding functions.
This project is licensed under the [MIT License](https://github.com/TheEdoRan/ulid-uuid-converter/blob/main/LICENSE).