Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
json-e is a javascript object prarmetrization module written for embedding context in a javascript object. It supports following features
This module exposes following interface
// template is js object to be rendered
// context provides functions/properties to be used
var par = Parameterize(template1, context1); //constructor
par.setNewTemplate(template2); //replaces template1 with template2
par.setNewContext(context2); //replaces context1 with context2
par.getTemplate(); //returns current template
par.getContext(); //returns current context
par.render(); //renders template using context
It follows this "{{ expression }}" syntax. expression can be anything javascript eval can digest and provide a context object.
//substitution
//propert access
var template = {a: "{{ foo.bar }}"};
var context = {foo: {
bar: "zoo"
}};
var par = new Parameterize(template, context);
par.render();
console.log(par.getTemplate());
/*
output > {a: "zoo"};
*/
//substitution
//function evaluation
var template = {a: "{{ foo.bar() }}"};
var context = {foo: {
bar: function() {return 1;}
}};
var par = new Parameterize(template, context);
par.render();
console.log(par.getTemplate());
/*
output > {a: "1"};
*/
It follows this "${ expression }" or {$eval: expression} syntax. expression can be anything javascript eval can digest and provide a context object.
var template = {a: "${ foo.bar }"};
var context = {foo: {
bar: 123
}};
var par = new Parameterize(template, context);
par.render();
console.log(par.getTemplate());
/*
output > {a: 123};
*/
var template = {a: "${ foo.bar() }"};
var context = {foo: {
bar: function() {return 1;}
}};
var par = new Parameterize(template, context);
par.render();
console.log(par.getTemplate());
/*
output > {a: 1};
*/
var template = {a: : {$eval: "{{ foo.bar() }}"}};
var context = {foo: {
bar: function() {return 1;}
}};
var par = new Parameterize(template, context);
par.render();
console.log(par.getTemplate());
/*
output > {a: "1"};
*/
var template = {a: {$eval: "${ foo.bar() }"}};
var context = {foo: {
bar: function() {return 1;}
}};
var par = new Parameterize(template, context);
par.render();
console.log(par.getTemplate());
/*
output > {a: 1};
*/
We can access array elements using $array_name(index) syntax
var template = {image_version: "{{task.$images(0).$versions(0)}}", name: "{{task.$images(0).name}}"};
var context = {task: {
images: [{versions: ["12.10", ], name: "ubuntu"}]
}};
var par = new Parameterize(template, context);
par.render();
console.log(par.getTemplate());
/*
output > {image_version: "12.10", name: "ubuntu"}
*/
We can use $if contruct to choose between two different objects for evaluation
var template = {a: {
$if: "${ key1 < key2 }",
$then: "${ foo.func1() }",
$else: "${ foo.func2() }"
}};
var context = {foo: {
func1: function() {return 1;},
func2: function() {return 2;}
}, key1: 1, key2: 2};
var par = new Parameterize(template, context);
par.render();
console.log(par.getTemplate());
/*
output > {a: 1};
*/
$then and $else also supports substitution and recursive object evaluation. Following is an example for recursive object evaluation
var template = {a: {
$if: "${ key1 > key2 }",
$then: {b: "${ foo.func1() }"},
$else: {b: "${ foo.func2() }"}
}};
var context = {foo: {
func1: function() {return 1;},
func2: function() {return 2;}
}, key1: 1, key2: 2};
var par = new Parameterize(template, context);
par.render();
console.log(par.getTemplate());
/*
output > {a: {b: 1}};
*/
this construct can be used in the following ways
var template = {a: {
"$switch": "{{ 'case' + a }}",
case1: "foo",
case2: "bar"
}};
var context = {a: "1"};
var par = new Parameterize(template, context);
par.render();
console.log(par.getTemplate());
/*
output > {a: "foo"};
*/
var template = {a: {
"$switch": "{{ 'case' + a }}",
caseA: {b:1}
}};
var context = {a: "A"};
var par = new Parameterize(template, context);
par.render();
console.log(par.getTemplate());
/*
output > {a: {b: 1}};
*/
instead of js object we can use ${ expression } syntax as case value.
This module provides recursive evaluation of js objects and arrays for templates, along with deep property access from the context.
For more info checkout src/tests.js
FAQs
json parameterization module inspired from json-parameterization
The npm package json-e receives a total of 29,864 weekly downloads. As such, json-e popularity was classified as popular.
We found that json-e demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.