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 🙏

© 2026 – Pkg Stats / Ryan Hefner

rulebook-ts

v0.1.1

Published

A tiny, type-safe rule parsing system for TypeScript.

Downloads

22

Readme

📘 RuleBook

A tiny, type-safe rule parsing system for TypeScript.

Designed for game development.

Define string templates with typed placeholders, parse runtime values into strongly-typed parameters, and query them safely.

✨ Features

  • 🧩 Template-based string matching ("user:[id]")
  • 🔒 Fully type-safe parameter inference
  • ⚡ Fast runtime matching using compiled RegExp
  • 🧠 Typed query API (ruleset.get(...))
  • 🔗 Merge multiple rule sets easily

📦 Installation

npm install rulebook-ts

🚀 Quick Start

import { RuleBook } from "rulebook-ts";

const placeholders = {
  id: {
    pattern: /\d+/,
    parse: (s: string) => Number(s),
  },
  slug: {
    pattern: /[a-z-]+/,
    parse: (s: string) => s,
  },
};

const templates = ["user:[id]", "post:[slug]"] as const;

const book = new RuleBook(templates, placeholders);

const rule = book.create("user:42");
const ruleset = book.createSet(["user:42", "post:hello-world"]);

🧠 Type Inference

The magic is that parameters are inferred directly from your template:

const user = rule.get("user:[id]");
//     ⇖ [number]

const users = ruleset.get("user:[id]");
//     ⇖ [number][]

const posts = ruleset.get("post:[slug]");
//     ⇖ [string][]

Each entry corresponds to one matched rule.

🔍 API

new RuleBook(templates, placeholders)

Create a rule book.

Templates

An array of template strings:

"user:[id]";
"post:[slug]";

Use as const to preserve literal types.

Placeholders

A map of placeholder definitions:

const placeholders = {
  id: {
    pattern: /\d+/,
    parse: (s: string) => Number(s),
  },
};
  • pattern: RegExp used for matching
  • parse: transforms matched string into typed value

createSet(rules: string[]): Ruleset

Parse a list of strings into a typed ruleset.

const ruleset = book.createSet(["user:1", "user:2"]);

Throws if a rule does not match any template.

ruleset.get(template);

Get all parsed params for a template.

ruleset.get("user:[id]");
// => [[1], [2]]
ruleset.has(template);

Check if any rule matches a template:

ruleset.has("user:[id]"); // true

mergeSets(...sets)

Merge multiple rulesets into one (deduplicated by value):

const merged = book.mergeSets(setA, setB);

Falsy values are ignored, so this is safe:

book.mergeSets(setA, maybeSet && setB);

🧩 Multiple Parameters

Templates can contain multiple placeholders:

const templates = ["range:[min]-[max]"] as const;

const placeholders = {
  min: {
    pattern: /\d+/,
    parse: Number,
  },
  max: {
    pattern: /\d+/,
    parse: Number,
  },
};

const book = new RuleBook(templates, placeholders);

const ruleset = book.createSet(["range:10-20"]);

ruleset.get("range:[min]-[max]");
// => [[10, 20]]

❗ Errors

Unknown placeholder

If a template references a placeholder that is not defined:

"user:[unknown]";

➡️ Throws at construction time.

Unmatched rule

If a rule doesn't match any template:

book.createSet(["invalid"]);

➡️ Throws at construction time.

🧪 Tips

Preserve literal types

Always use as const:

const templates = ["user:[id]"] as const;

Otherwise, TypeScript will treat them as string[] and you’ll lose inference.

Keep parsing pure

Your parse functions should be deterministic and side-effect free.

📄 License

MIT