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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@type-hub/re-guard

v1.0.9

Published

Type-safe regex suite: build predicates, guards, and assertions from a single regex func

Readme

re-guard

Type-safe regex suite: build predicates, guards, and assertions from a single regex function.

Installation

npm install @type-hub/re-guard

Goal

import { collect } from "@type-hub/re-guard"
import { inputFuncs, inputTypes } from "./data"

const maybeTrue = Math.random() > 0.5

const randomValue =
  Math.random() > 0.5
    ? "@john_doe"
    : Math.random() > 0.5
    ? "#money"
    : Math.random() > 0.5
    ? true
    : Math.random() > 0.5
    ? "false"
    : "2"

// } = collect(inputFuncs).setTypes<inputTypes>().build()
const {
  asserts: a, // computed assertions
  guards, // computed guards
  inputs, // original input functions
} = collect<typeof inputFuncs, inputTypes>(inputFuncs)

// INFO: it must be like that due to TS limitations
export const hashTagAssertion: typeof a.hashTagAssert = a.hashTagAssert
export const mentionAssertion: typeof a.mentionAssert = a.mentionAssert
export const customAssertion: typeof a.customAssert = a.customAssert

Features

  • Type-safe: Full TypeScript support with accurate type inference
  • Guards & Assertions: Generate both type guards and assertions from your regex patterns
  • Single Source: Define patterns once, use them everywhere
  • Zod Integration: Seamless integration with Zod schemas
  • Zero Dependencies: Lightweight and efficient

Usage

Basic Example

import { collect } from "@type-hub/re-guard"

// Define your input functions (regex patterns)
const inputFuncs = {
  hashTag: (value: unknown) =>
    typeof value === "string" && /^#[a-zA-Z0-9]+$/.test(value),
  mention: (value: unknown) =>
    typeof value === "string" && /^@[a-zA-Z0-9_]+$/.test(value),
  custom: (value: unknown) =>
    typeof value === "boolean" || value === "true" || value === "false",
}

// Define your expected types
type InputTypes = {
  hashTag: string
  mention: string
  custom: boolean
}

// Create guards and assertions
const {
  asserts, // Type assertions
  guards, // Type guards
  inputs, // Original input functions
} = collect(inputFuncs).setTypes<InputTypes>().build()

// Using guards
if (guards.hashTagGuard(someValue)) {
  // someValue is typed as string here
  console.log(someValue)
}

// Using assertions
asserts.mentionAssert(someValue) // Throws if invalid
// someValue is typed as string after assertion

// Using with Zod (if you have Zod schemas)
const zodSchema = z.string().min(5)
const { guards: zodGuards } = collect({ myField: zodSchema }).build()

Type Guards vs Assertions

  • Type Guards (guards): Runtime checks that narrow types in TypeScript
  • Assertions (asserts): Runtime validations that throw errors for invalid values

Advanced Usage

// Mixing regular functions and Zod schemas
const mixedInputs = {
  email: z.string().email(),
  customRegex: (value: unknown) =>
    typeof value === "string" && /^[A-Z]+$/.test(value),
}

const { guards, asserts } = collect(mixedInputs).build()

// Type inference works automatically
const emailGuard = guards.emailGuard // Inferred return type: string
const regexGuard = guards.customRegexGuard // Inferred return type: string

API Reference

collect(inputLookup)

Main function to create type guards and assertions.

Parameters:

  • inputLookup: Record of input functions or Zod schemas

Returns:

Object containing:

  • guards: Type guard functions
  • asserts: Assertion functions
  • inputs: Original input functions

Error Handling

Assertions throw errors when validation fails. Make sure to handle these errors appropriately in your code:

try {
  asserts.emailAssert(value)
} catch (error) {
  console.error("Validation failed:", error.message)
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details