npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ganbarodigital/ts-lib-uuid-parser

v0.4.0

Published

Converts human-readable UUIDs to bytes

Downloads

2

Readme

UUID Parser for Typescript

Node.js CI

Introduction

This TypeScript library will take any RFC 4122 UUID, and parse it into a stream of bytes.

The library also offers a Uuid type, and a useful validation function.

Quick Start

# run this from your Terminal
npm install @ganbarodigital/ts-lib-uuid-parser
// add this import to your Typescript code
import { Uuid } from "@ganbarodigital/ts-lib-uuid-parser/lib/v1"

VS Code users: once you've added a single import anywhere in your project, you'll then be able to auto-import anything else that this library exports.

V1 API

Uuid()

import { Branded } from "@ganbarodigital/ts-lib-value-objects/lib/v2";

/**
 * A type-safe representation of a UUID / GUID
 */
export type Uuid = Branded<string, "uuid">;

Uuid is a branded type. It represents a validated RFC 4122 UUID.

For example:

import { uuidFromFormatted } from "@ganbarodigital/ts-lib-uuid-parser/lib/v1";

// creates a new Uuid
const myUuid = uuidFromFormatted("9c47cb7c-9793-4944-9189-61a938d0e9bd");

Uuid Automatic String Conversion

At runtime, Uuid is just a string. You can use it anywhere you'd normally use a string.

import { Uuid } from "@ganbarodigital/ts-lib-uuid-parser/lib/v1";

// creates a new Uuid
const myUuid = uuidFromFormatted("9c47cb7c-9793-4944-9189-61a938d0e9bd");

// outputs the string "9c47cb7c-9793-4944-9189-61a938d0e9bd"
console.log(myUuid);

isUuidData()

function isUuidData(input: string): boolean

isUuidData() is a data guard. It returns true if the input string contains a well-formatted UUID.

For example:

import { isUuidData } from "@ganbarodigital/ts-lib-uuid-parser/lib/v1";

if (!isUuidData("12345-67890")) {
    throw new Error("invalid UUID");
}

mustBeUuidData()

import { OnError } from "@ganbarodigital/ts-lib-error-reporting/lib/v1";

export function mustBeUuidData(input: string, onError?: OnError): void

mustBeUuidData() is a data guarantee. It throws an InvalidUuidError if the input isn't an acceptable UUID.

uuidFromBytes()

import { OnError } from "@ganbarodigital/ts-lib-error-reporting/lib/v1";

export function uuidFromBytes(input: Buffer, onError?: OnError): Uuid

uuidFromBytes() is a data transform. It builds a human-readable UUID from a Buffer, reading from offset 0. It returns the resulting Uuid value.

If the resulting string isn't a valid UUID, the onError handler is called.

uuidToBytes()

export function uuidToBytes(uuid: Uuid, target?: Buffer): Buffer

uuidToBytes() is a data transform. It converts a human-readable UUID into bytes, and writes those bytes into the target Buffer, starting at offset 0.

If you do not provide a target Buffer to write to, uuidToBytes() will allocate one for you.

The return value is the Buffer that has been written to.

uuidFromFormatted()

import { OnError } from "@ganbarodigital/ts-lib-error-reporting/lib/v1";

/**
 * this is our main factory for building Uuids
 */
export function uuidFromFormatted(input: string, onError?: OnError): Uuid;

uuidFromFormatted() is a smart constructor. It validates that input contains a well-formed UUID, and returns a Uuid type.

If the input validation fails, uuidFromFormatted() calls the onError handler. The onError handler must throw an Error of some kind.

uuidFromUnformatted()

import { OnError } from "@ganbarodigital/ts-lib-error-reporting/lib/v1";

/**
 * converts an array of bytes into a type-safe UUID
 */
export function uuidFromUnformatted(input: string, onError?: OnError): Uuid;

uuidFromUnformatted() is a data transform. It converts the contents of input into a well-formatted UUID, and then calls uuidFromFormatted().

NPM Scripts

npm run clean

Use npm run clean to delete all of the compiled code.

npm run build

Use npm run build to compile the Typescript into plain Javascript. The compiled code is placed into the lib/ folder.

npm run build does not compile the unit test code.

npm run test

Use npm run test to compile and run the unit tests. The compiled code is placed into the lib/ folder.

npm run cover

Use npm run cover to compile the unit tests, run them, and see code coverage metrics.

Metrics are written to the terminal, and are also published as HTML into the coverage/ folder.