https://github.com/sverweij/uuid-compact
Represent UUIDs with compact base64url encoding
https://github.com/sverweij/uuid-compact
compact uuid
Last synced: 12 months ago
JSON representation
Represent UUIDs with compact base64url encoding
- Host: GitHub
- URL: https://github.com/sverweij/uuid-compact
- Owner: sverweij
- License: mit
- Created: 2024-08-18T18:53:17.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-22T20:08:19.000Z (over 1 year ago)
- Last Synced: 2025-03-06T17:25:02.905Z (over 1 year ago)
- Topics: compact, uuid
- Language: JavaScript
- Homepage:
- Size: 60.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README-sample.ts
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
import {
toCompactUUID,
toCanonicalUUID,
isValidUUID,
isValidCompactUUID,
} from "uuid-compact";
// this package does not provide a way to generate UUID's, but you can use the
// use e.g. the node:crypto module to generate UUIDv4's (which is what most
// of us use anyway), or the 'uuid' package from npm to generate any of the other
// variants from RFC4122.
import { randomUUID } from "node:crypto";
const lUUID = randomUUID();
console.log("Original UUID :", lUUID);
// 1a664e13-ac6a-4365-937d-9e6e8193322e
const lCompactUUID = toCompactUUID(lUUID);
console.log("Compacted UUID:", lCompactUUID);
// GmZOE6xqQ2WTfZ5ugZMyLg
const lUUIDAgain = toCanonicalUUID(lCompactUUID);
console.log("Compacted UUID back to Canonical UUID again:", lUUIDAgain);
// 1a664e13-ac6a-4365-937d-9e6e8193322e
// If you're not sure where either the source of the UUID or the short form can be
// trusted to always have valid UUID's, or shortened ones, you can use the `isValidUUID`
// and `isValidBase64URL` functions to check if the input is valid like so:
let compactedMaybe: string = "";
if (isValidUUID(lUUID)) {
compactedMaybe = toCompactUUID(lUUID);
console.log("Valid UUID; compacted:", compactedMaybe);
} else {
throw new Error("Not a valid compact UUID");
}
let backToUUIDMaybe: string = "";
if (isValidCompactUUID(lCompactUUID)) {
backToUUIDMaybe = toCanonicalUUID(lCompactUUID);
console.log("Valid compact UUID; back to canonical:", backToUUIDMaybe);
} else {
throw new Error("Not a valid compact UUID (base64url string)");
}