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

simple-json-templater

v1.1.0

Published

Keeps, drops and replaces JSON value/key pairs in function of given variables.

Downloads

5

Readme

Simple JSON Templater

Keeps, drops and replaces JSON value/key pairs in function of given variables.

Installation

npm install simple-json-templater

Templating Language Specifications

The template is a JSON object that can contains meta keys which are the following:

  • $keepIf
  • $dropIf
  • $replaceByValue
  • $replaceByVariable

The meta keys describe how to build the final JSON. All meta keys are removed from the final JSON.

If you want to use a key that start with a dollar you can escape it by adding another dollar. Example:

{
  "abc": 123,
  "$$xyz": 456
}

gives:

{
  "abc": 123,
  "$xyz": 456
}

$replaceByValue

Can't be used concurrently with $replaceByVariable or $map.

The parent object will be replaced by the value. This is helpful in combinaison with $keepIf and $dropIf.

Example:

{
  "abc": [
    {
      "$replaceByValue": 123
    }
  ]
}

gives:

{
  "abc": [
    123
  ]
}

$replaceByVariable

Can't be used concurrently with $replaceByValue or $map.

The parent object will be replaced by the variable.

Example with var1 = "xyz":

{
  "abc": {
    "$replaceByVariable": "var1"
  }
}

gives:

{
  "abc": "xyz"
}

$map

Can't be used concurrently with $replaceByValue or $replaceByVariable.

Describe a variable wich its value will be maped by the $case meta keys. If no $case match the value, the $default meta key is used.

Example with var1 = "def":

{
  "xyz": {
    "$map": "var1",
    "$case:abc": 123,
    "$case:def": 456,
    "$default": 789
  }
}

gives:

{
  "xyz": 456
}

Example with var1 = "ghi":

{
  "xyz": {
    "$map": "var1",
    "$case:abc": 123,
    "$case:def": 456,
    "$default": 789
  }
}

gives:

{
  "xyz": 789
}

Example with var1 = "ghi" again but there is no $default key:

{
  "xyz": {
    "$map": "var1",
    "$case:abc": 123,
    "$case:def": 456
  },
  "uvw": [
    {
      "$map": "var1",
      "$case:abc": 123,
      "$case:def": 456
    }
  ]
}

gives:

{
  "uvw": [ null ]
}

$keepIf

Can't be used concurrently with $dropIf.

The parent object will be kept if and only if the expression is true.

Example with var1 = "xyz":

{
  "abc": {
    "$keepIf": "var1 is xyz",
    "$replaceByValue": 123
  },
  "def": {
    "$keepIf": "var1 isn't xyz",
    "$replaceByValue": 456
  }
}

gives:

{
  "abc": 123
}

$dropIf

Can't be used concurrently with $keepIf.

The parent object will be removed if and only if the expression is true.

Example with var1 = "xyz":

{
  "abc": {
    "$dropIf": "var1 is xyz",
    "$replaceByValue": 123
  },
  "def": {
    "$dropIf": "var1 isn't xyz",
    "$replaceByValue": 456
  }
}

gives:

{
  "def": 456
}

expression

An expression is a string that can have the following shape:

  • <variable> is <value>
  • <variable> isn't <value>

Variable and value can't have space.

Implementation

Import the parse function:

// like this
const { parse } = require('simple-json-templater');

// or like this
import { parse } from 'simple-json-templater';

Then use it:

/*

parse(
  template: { [key: string]: any },
  variables?: { [key: string]: string }
): any

*/

const result = parse({
  abc: {
    $replaceByVariable: 'var1'
  },
  xyz: [
    {
      $keepIf: 'var2 is keepFirst',
      $replaceByValue: 'first'
    },
    {
      $dropIf: 'var2 is keepFirst',
      $replaceByValue: 'second'
    }
  ]
}, {
  var1: '123',
  var2: 'keepSecond'
});

console.log(result);

/*

{
  abc: '123',
  xyz: [
    'second'
  ]
}

*/

Misc

contact: [email protected]
licence: GPL-3.0-or-later

GPL3