Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including jsexpr with all npm packages installed. Try it out:

var jsexpr = require("jsexpr")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

jsexpr v0.7.2

String and JSON expression interpolator and evaluator. Interpolates or evaluates a string against a json object, or transforms an object into another based on a json template

jsexpr

JSON Expressions!

Build Status

String and JSON expression interpolator and evaluator. Interpolates or evaluates a string against a json object, or transforms an object into another based on a json template.

Installation

npm install jsexpr

String Evaluation

const expression = require("jsexpr");

var fn = expression.fn("(${host}=='mymachine' || ${host}=='yourmachine') && ${appName}=='su'");

// Result: false
var result = fn({host:"mymachine",appName:23});

String Interpolation

const expression = require("jsexpr");

var expr = expression.expr("/var/${date}/${client.address}/file.log");

// Result: /var/2017-01-01/localhost/file.log
var result = expr({date:"2017-01-01",client:{address:"localhost"}});

Object Interpolation

const expression = require("jsexpr");

var jsexpr = expression.expr({
    time : "${client}/${address.host}:${address.port}",
    data : {
        request : {
            headers : "${headers}"
        }
    }
});

var result = jsexpr({
    client : "HOST001",
    address : {
        host : "localhost",
        port : 8080
    },
    headers : "Content-Type: application/json"
});

Object Interpolation Result

{
    time: 'HOST001/localhost:8080',
    data: {
        request: {
            headers: 'Content-Type: application/json'
        }
    }
}

JSON Stringify

var input1 = {client : "HOST001", address : {host : "localhost", port : 8080}, headers : "Content-Type: application/json"};

let jxpr1 = expr.expr("${JSON}");
let jxpr2 = expr.expr("${JSON:address}");
let jxpr3 = expr.expr("${JSON:address:0}");
let jxpr4 = expr.expr("${JSON:0}");

// =>

{
  "client": "HOST001",
  "address": {
    "host": "localhost",
    "port": 8080
  },
  "headers": "Content-Type: application/json"
}

{
  "host": "localhost",
  "port": 8080
}

{"host":"localhost","port":8080}

{"client":"HOST001","address":{"host":"localhost","port":8080},"headers":"Content-Type: application/json"}

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free