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

@jdiamond/jsonexpr

v2.0.0

Published

JSON with expressions

Downloads

2

Readme

JSON with Expressions

Simple support for dynamic expressions in JSON.

Usage

Install:

npm install @jdiamond/jsonexpr

Import:

import { evaluateExpression } from "@jdiamond/jsonexpr";

All input must be trusted!

This library does not parse JSON strings. Use JSON.parse() or JSON5 or Hjson or whatever.

The evaluateExpression function looks like this:

function evaluateExpression(
  expr: unknown,
  ctx?: unknown,
  params?: string[],
  args?: unknown[]
): unknown;

ctx can be accessed via the this keyword. If you don't need to do that and only want to acess arguments via their parameter names, you can pass in null or undefined here.

params is the array of names the expression uses to access arguments. You can use curly braces to destructure objects just like in normal JavaScript.

args are the actual values the expression will read from.

If you are using TypeScript, you probably want to use something like Zod to parse the output for a type-safe result:

import { evaluateExpression } from "@jdiamond/jsonexpr";
import { getStringFromSomewhere, SomeZodSchema } from "./your-job";

const string = getStringFromSomewhere();
const parsed = JSON.parse(string);
const evaled = evaluateExpression(parsed);
const result = SomeZodSchema.parse(result);

Syntax

Dynamic expressions are strings that start with { and end with }.

{
  "static": "this is a static string",
  "dynamic": "{'this is a ' + 'dynamic'.toUpperCase() + ' string'}"
}

This syntax is inspired by JSX except the curly braces have to be inside strings to keep the JSON valid.

Extra whitespace at the start or end of the strings is not supported. Embedding expressions in the middle of a string is also not supported.

Examples

Simple:

assert.deepEqual(
  evaluateExpression({
    static: "this is a static string",
    dynamic: "{'this is a ' + 'dynamic'.toUpperCase() + ' string'}",
  }),
  {
    static: "this is a static string",
    dynamic: "this is a DYNAMIC string",
  }
);

Not supported:

assert.deepEqual(
  evaluateExpression({
    "no-whitespace": " {this is a static string} ",
    "no-embedding": "this is {also a static} string",
  }),
  {
    "no-whitespace": " {this is a static string} ",
    "no-embedding": "this is {also a static} string",
  }
);

Template literal:

assert.deepEqual(
  evaluateExpression({
    "template-literal": "{`this is a ${'dynamic'.toUpperCase()} string`}",
  }),
  {
    "template-literal": "this is a DYNAMIC string",
  }
);

Nested:

assert.deepEqual(
  evaluateExpression({
    array: ["one", "{1 + 1}", "three"],
    object: {
      foo: "bar",
      baz: "{'quux'.toUpperCase()}",
    },
  }),
  {
    array: ["one", 2, "three"],
    object: {
      foo: "bar",
      baz: "QUUX",
    },
  }
);

Complex:

assert.deepEqual(
  evaluateExpression({
    array: "{[1,2,3].map((num) => num + 3)}",
    object: "{Object.fromEntries([['foo', 'bar'], ['baz', 'quux']])}",
  }),
  {
    array: [4, 5, 6],
    object: { foo: "bar", baz: "quux" },
  }
);

Context:

assert.deepEqual(
  evaluateExpression(
    {
      result: "{this.a + this.b}",
    },
    {
      a: 1,
      b: 2,
    }
  ),
  {
    result: 3,
  }
);

Parameters/arguments:

assert.deepEqual(
  evaluateExpression(
    {
      result: "{input.a + input.b}",
    },
    null,
    ["input"],
    [
      {
        a: 1,
        b: 2,
      },
    ]
  ),
  {
    result: 3,
  }
);

Destructued parameters/arguments:

assert.deepEqual(
  evaluateExpression(
    {
      result: "{a + b}",
    },
    null,
    ["{a, b}"],
    [
      {
        a: 1,
        b: 2,
      },
    ]
  ),
  {
    result: 3,
  }
);