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

json-predicate-transformer

v2.0.2

Published

A simple utility for modifying JSON objects using a predicate-transformer paradigm

Downloads

650

Readme

json-predicate-transformer

A simple utility for modifying JSON objects using a predicate-transformer paradigm

Usage

  import { predicateTransform } from "json-predicate-transformer"

  const preTransformedObject = {
    foo: {
      bar: 123,
      xyz: 200,
      dontTouchMe: 0,
    },
    baz: "hello world",
  };

  const result = predicateTransform(preTransformedObject, {
    keyHandlers: [
      {
          predicate: (path) => path === "foo.xyz",
          transformer: (path, value) => `key-${path}-${value}`
      }
    ],
    valueHandlers: [
      {
        predicate: (_, value) => value > 100,
        transformer: (_, value) => value * 2,
      },
      {
        predicate: (_, value) => typeof value === "string",
        transformer: () => "[string]",
      },
    ],
  });

  console.log(result)
  // {
  //     foo: {
  //         bar: 246,
  //         "key-xyz-200": 200,
  //         dontTouchMe: 0,
  //     },
  //     baz: "[string]"
  // }

API

This library is built in Typescript and includes type declarations so those are the authoritative and easiest to use source for the API.

predicateTransform(blob: object | any[], options: TransformerOptions): object | any[]

The main transformer function.

Important Types

interface TransformerOptions {
  keyHandlers?: []HandlerConfig,
  valueHandlers?: []HandlerConfig
}

interface HandlerConfig {
  predicate: (path: string, value: any) => boolean,
  transformer: (path: string, value: any) => any
}

FAQ

What is a HandlerConfig?

A HandlerConfig is the most fundamental building block for using json-predicate-transformer. A HandlerConfig is an object with two properties: a predicate function and transformer function.

A predicate has the function signature (path: string, value: any) => boolean.

A transformer has the function signature (path: string, value: any) => any.

You can specify any number of HandlerConfigs for keyHandlers and valueHandlers and they will be applied (in the provided order) on every JSON key or value, respectively, e.g:

if (predicate(...)) {
  newValue = transformer(...)
}

See the example usage at the top for an idea of what this means.

What is the difference between keyHandlers vs. valueHandlers?

Key handlers allow you to predicate-transform the keys of a JSON object while value handlers allow you to predicate-transform the values.

Key handlers and value handlers will both receive the same parameters to their predicate and transformer functions.

Assume the following input to predicateTransform:

{
  foo: {
    bar: 123
  }
}

For both key and value handlers, the path parameter will be the dotpath to the key being processed, e.g. "foo.bar", while the value parameter will be the value at that path, e.g. 123.

The key difference between them is simply which value the transformer's output is applied to. Assume the following super-simple HandlerConfig

{ predicate: () => true, transformer: () => "ZZZ" }

If this was provided in keyHandlers with the above object as input, it would result in

{
  ZZZ: {
    ZZZ: 123
  }
}

whereas if it was provided in valueHandlers, it would result in

{
  foo: {
    bar: "ZZZ"
  }
}

What's a "dotpath"?

A dotpath is simply notation you can use to refer to objects nested within a JSON object.

{
  foo: {
    bar: [
      { abc: 1, def: 2 },
      { xyz: 3, uvw: 4 }
    ],
    baz: 100
  }
}

For example, You can access the 1 using foo.bar.abc, the 100 using foo.baz.