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-expand

v1.4.0

Published

Expand self referencing json documents

Downloads

170

Readme

json-expand

Expands your self referencing json

Installation

Standalone Script

$ git clone https://github.com/joshwillik/json-expand.git
$ chmod +x json-expand/json-expand.js
$ mv json-expand/json-expand.js ~/my-scripts/json-expand
# Assuming ~/my-scripts is in $PATH

NPM module

$ npm install -g json-expand

CLI Usage

// config.json
{
  "apiDomain": "api.{{baseDomain}}",
  "baseDomain": "{{userAccount}}.{{hostingProvider}}",
  "userAccount": "joshwillik",
  "hostingProvider": "superhost.foobar",
  "authDomain": "{{google.accounts.base}}{{google.accounts.url}}",
  "google": {
    "accounts": {
      "url": "/login",
      "base": "accounts.google.com"
    }
  }
}
$ json-expand < config.json
{
  "apiDomain": "api.joshwillik.superhost.foobar",
  "baseDomain": "joshwillik.superhost.foobar",
  "userAccount": "joshwillik",
  "hostingProvider": "superhost.foobar",
  "authDomain": "accounts.google.com/login",
  "google": {
    "accounts": {
      "url": "/login",
      "base": "accounts.google.com"
    }
  }
}

Node.js usage

var expand = require( 'json-expand' )
var obj = {
    foo: "bar",
    message: "foo = {{foo}}"
}
expand( obj )
> { foo: 'bar', message: 'foo = bar' }

Rules

Any expression wrapped in {{ }} will be expanded to the correct value or an empty string if none exists. The expression to expand may come before the thing it will expand to.

{
    "name": "John, son of {{parent}}",
    "parent": "Bill"
}
// becomes
{
    "name": "John, son of Bill",
    "parent": "Bill"
}

An expression may reference sub-properties of the document it resides in. All expressions are evaluated starting at the top scope.

{
    "description": "John drives with {{vehicle.wheels}} wheels",
    "vehicle": {
        "wheels":4
    }
}
// becomes
{
    "description": "John drives with 4 wheels",
    "vehicle": {
        "wheels":4
    }
}

Using || operator will make json-expand attempt to expand each option, in order, until one works. Once one value is valid, the rest of the options are ignored.

{
    "description": "I enjoy {{ favoriteFood || foodIKindaLike }}",
    "foodIKindaLike": "spaghetti"
}
// becomes
{
    "description": "I enjoy spaghetti",
    "foodIKindaLike": "spaghetti"
}
// but
{
    "description": "I enjoy {{ favoriteFood || foodIKindaLike }}",
    "foodIKindaLike": "spaghetti",
    "favoriteFood": "meatballs"
}
// becomes
{
    "description": "I enjoy meatballs",
    "foodIKindaLike": "spaghetti",
    "favoriteFood": "meatballs"
}

Environment variables may be substituted by adding a $ before an expression. Using || works just fine with these.

{
    "remoteHost": "{{ $REMOTE_HOST || defaultHost }}",
    "defaultHost": "production.project.com"
}
// if REMOTE_HOST has been set to 'dev.project.com
{
    "remoteHost": "dev.project.com",
    "defaultHost": "production.project.com"
}
// otherwise
{
    "remoteHost": "production.project.com",
    "defaultHost": "production.project.com"
}

Expressions can be concatinated together if desired. The + has higher precedence than ||, so {{ foo || bar + ' test'}} will result in either (foo) or (bar + ' test'), never (foo + ' test').

Using parenthesis to override the order of operations will not work in this version.

{
    "baseDomain": "my-project.com",
    "apiDomain": "{{ doesNotExist || 'api.' + baseDomain }}"
}
// becomes
{
    "baseDomain": "my-project.com",
    "apiDomain": "api.my-project.com"
}