-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from jvalerodev/main
feat: convert ULID to UUID and vice versa
- Loading branch information
Showing
8 changed files
with
203 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
export const MAX_ULID = "7ZZZZZZZZZZZZZZZZZZZZZZZZZ"; | ||
export const MIN_ULID = "00000000000000000000000000"; | ||
|
||
export const ULID_REGEX = /^[0-7][0-9a-hjkmnp-tv-zA-HJKMNP-TV-Z]{25}$/; | ||
export const UUID_REGEX = /^[0-9a-fA-F]{8}-(?:[0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$/; | ||
export const B32_CHARACTERS = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Code from https://github.com/devbanana/crockford-base32/blob/develop/src/index.ts | ||
import { B32_CHARACTERS } from "./constants.js"; | ||
|
||
export function crockfordEncode(input: Uint8Array): string { | ||
const output: number[] = []; | ||
let bitsRead = 0; | ||
let buffer = 0; | ||
|
||
const reversedInput = new Uint8Array(input.slice().reverse()); | ||
|
||
for (const byte of reversedInput) { | ||
buffer |= byte << bitsRead; | ||
bitsRead += 8; | ||
|
||
while (bitsRead >= 5) { | ||
output.unshift(buffer & 0x1f); | ||
buffer >>>= 5; | ||
bitsRead -= 5; | ||
} | ||
} | ||
|
||
if (bitsRead > 0) { | ||
output.unshift(buffer & 0x1f); | ||
} | ||
|
||
return output.map(byte => B32_CHARACTERS.charAt(byte)).join(""); | ||
} | ||
|
||
export function crockfordDecode(input: string): Uint8Array { | ||
const sanitizedInput = input.toUpperCase().split("").reverse().join(""); | ||
|
||
const output: number[] = []; | ||
let bitsRead = 0; | ||
let buffer = 0; | ||
|
||
for (const character of sanitizedInput) { | ||
const byte = B32_CHARACTERS.indexOf(character); | ||
|
||
if (byte === -1) { | ||
throw new Error(`Invalid base 32 character found in string: ${character}`); | ||
} | ||
|
||
buffer |= byte << bitsRead; | ||
bitsRead += 5; | ||
|
||
while (bitsRead >= 8) { | ||
output.unshift(buffer & 0xff); | ||
buffer >>>= 8; | ||
bitsRead -= 8; | ||
} | ||
} | ||
|
||
if (bitsRead >= 5 || buffer > 0) { | ||
output.unshift(buffer & 0xff); | ||
} | ||
|
||
return new Uint8Array(output); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters