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

jsonxpr

v0.0.4

Published

A TypeScript library for evaluating expressions against JSON objects.

Downloads

214

Readme

JSONxpr

A TypeScript library for evaluating expressions against JSON objects.

Features

  • 🔍 Declarative operator syntax
  • 📝 Variable substitution with dot notation
  • 🔢 Rich set of operators for various data types
  • 🎯 Type-safe implementation
  • 🔗 Nested object and array access
  • 🛡️ String literal escaping

Installation

pnpm install jsonxpr

Basic Usage

import { jsonxpr } from "jsonxpr";

// Simple mathematical operation
jsonxpr({ $add: [1, 2, 3] });
// => 6

// Simple string operation
jsonxpr({ $toUpper: "hello" });
// => "HELLO"

// Simple array operation
jsonxpr({ $size: ["a", "b", "c"] });
// => 3

Working with Variables

// Using variables
const expr = { $toLower: "$name" };
const vars = { name: "JOHN" };
jsonxpr(expr, vars)
// => "john"

// Nested variable access
const expr = { $concat: ["$user.firstName", " ", "$user.lastName"] };
const vars = {
  user: {
    firstName: "John",
    lastName: "Doe"
  }
};
jsonxpr(expr, vars)
// => "John Doe"

Advanced Examples

Filter Array with nested condition

const expr = {
  $filter: {
    input: "$items",
    as: "item",
    cond: { $gt: ["$$item.stock", 0] },
  },
};

const vars = {
  items: [
    { id: 1, stock: 5 },
    { id: 2, stock: 0 },
    { id: 3, stock: 3 },
  ],
};

jsonxpr(expr, vars);
// => [{ id: 1, stock: 5 }, { id: 3, stock: 3 }]

Complex Conditions

const expr = {
  $switch: {
    branches: [
      {
        case: { $gt: ["$value", 100] },
        then: "high",
      },
      {
        case: { $gt: ["$value", 50] },
        then: "medium",
      },
    ],
    default: "low",
  },
};

const vars = { value: 75 };
jsonxpr(expr, vars);
// => "medium"

Available Operators

See the complete list of available operators.

Escaping Special Characters

Use the # prefix to escape strings that start with $:

const expr = {
  "#$special": "This is treated as a literal $special key",
};

Custom Operators

You can create a custom instance of jsonxpr with your own operators using createJsonxpr:

import type { Operation, Value, Variables } from "jsonxpr";
import { createJsonxpr } from "jsonxpr";

// Create instance with custom date operators
const customJsonxpr = createJsonxpr({
  $parseDate: ((value: Value, vars: Variables): number => {
    const dateStr = jsonxpr(value, vars) as string;
    return new Date(dateStr).getTime();
  }) as Operation<Value, number>,
});

// Usage
const result = customJsonxpr(
  {
    timestamp: { $parseDate: "$date" },
  },
  {
    date: "2024-01-15T09:30:00Z",
  },
);

// Result:
// {
//   timestamp: 1705311000000,
// }

Overriding Built-in Operators

You can also override built-in operators with custom implementations:

import { z } from "zod";

// Override built-in email validation with Zod
const zodJsonxpr = createJsonxpr({
  $isValidEmail: ((value: Value, vars: Variables): boolean => {
    const email = jsonxpr(value, vars) as string;
    const result = z.string().email().safeParse(email);
    return result.success;
  }) as Operation<Value, boolean>,
});

// Usage
const result = zodJsonxpr({
  isValid: { $isValidEmail: "[email protected]" },
});
// { isValid: true }

Custom operators can:

  • Use other operators in their implementation
  • Access variables with jsonxpr(value, vars)
  • Override built-in operators
  • Return any serializable value
  • Integrate with third-party libraries