Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsexpr

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsexpr - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

.travis.yml

13

dist/browser/jsexpr.js

@@ -49,12 +49,11 @@ "use strict";

function _val(obj, key) {
var arr = key.split(".");
arr.forEach(function (key) {
if (obj == null || obj == undefined) return;else obj = obj[key];
});
return obj || undefined;
try {
return eval("this." + key);
} catch (err) {
return undefined;
}
}
function val(obj, key) {
var v = _val(obj, key);
var v = _val.call(obj, obj, key);
return v === undefined ? "" : v;

@@ -61,0 +60,0 @@ }

/*! jsexpr 2018-06-11 */
"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r};!function u(i,f,c){function a(t,r){if(!f[t]){if(!i[t]){var n="function"==typeof require&&require;if(!r&&n)return n(t,!0);if(p)return p(t,!0);var o=new Error("Cannot find module '"+t+"'");throw o.code="MODULE_NOT_FOUND",o}var e=f[t]={exports:{}};i[t][0].call(e.exports,function(r){return a(i[t][1][r]||r)},e,e.exports,u,i,f,c)}return f[t].exports}for(var p="function"==typeof require&&require,r=0;r<c.length;r++)a(c[r]);return a}({1:[function(t,n,o){(function(){this.jsexpr;var r=t("./index.js");void 0!==o&&(void 0!==n&&n.exports&&(o=n.exports=r),o.jsexpr=r),"undefined"!=typeof window&&(window.jsexpr=r)}).call(this)},{"./index.js":2}],2:[function(r,t,n){var o=r("./lib/expression");t.exports=o},{"./lib/expression":3}],3:[function(r,t,n){var o=r("extend"),u=/\$\{[^\}]+\}/g;function i(r,t){var n,o=(n=r,t.split(".").forEach(function(r){null!=n&&null!=n&&(n=n[r])}),n||void 0);return void 0===o?"":o}function e(n){var r=n.match(u);r&&r.forEach(function(r){var t=r.replace(/[\$\{\}]/g,"").trim();n=n.replace(r,"__val(entry,'"+t+"')")});var t=new Function("entry","__val","return ("+n+")");return function(r){return t(r,i)}}function f(o){if("${JSON}"==o)return function(r){return JSON.stringify(r,null,2)};var e=[];return(o.match(u)||[]).forEach(function(t){var r=o.indexOf(t),n=o.substring(0,r);o=o.substring(r+t.length),e.push(n),e.push(function(r){return i(r,t.replace(/\$|\{|\}/g,""))})}),e.push(o),function(t){return e.map(function(r){return"string"==typeof r?r:r(t)}).join("")}}function c(n){var i=[];return function o(e,u){e&&Object.keys(e).forEach(function(r){var t=u+(u?".":"")+r,n=e[r];"string"==typeof n?i.push({path:t,fn:f(n)}):o(n,t)})}(n,""),function(t){var r=i.map(function(r){return{path:r.path,value:r.fn(t)}}).reduce(function(r,t){return r[t.path]=t.value,r},{});return function n(o,e,u){return o&&Object.keys(o).forEach(function(r){var t=u+(u?".":"")+r;e[t]&&(o[r]=e[t]),"object"==_typeof(o[r])&&n(o[r],e,t)}),o}(o(!0,{},n),r,"")}}t.exports={fn:e,eval:e,expr:function(r){return"object"==(void 0===r?"undefined":_typeof(r))?c(r):f(r)}}},{extend:4}],4:[function(r,t,n){var e=Object.prototype.hasOwnProperty,u=Object.prototype.toString,s=function(r){return"function"==typeof Array.isArray?Array.isArray(r):"[object Array]"===u.call(r)},l=function(r){if(!r||"[object Object]"!==u.call(r))return!1;var t,n=e.call(r,"constructor"),o=r.constructor&&r.constructor.prototype&&e.call(r.constructor.prototype,"isPrototypeOf");if(r.constructor&&!n&&!o)return!1;for(t in r);return void 0===t||e.call(r,t)};t.exports=function r(){var t,n,o,e,u,i,f=arguments[0],c=1,a=arguments.length,p=!1;for("boolean"==typeof f&&(p=f,f=arguments[1]||{},c=2),(null==f||"object"!==(void 0===f?"undefined":_typeof(f))&&"function"!=typeof f)&&(f={});c<a;++c)if(null!=(t=arguments[c]))for(n in t)o=f[n],f!==(e=t[n])&&(p&&e&&(l(e)||(u=s(e)))?(u?(u=!1,i=o&&s(o)?o:[]):i=o&&l(o)?o:{},f[n]=r(p,i,e)):void 0!==e&&(f[n]=e));return f}},{}]},{},[1]);
"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r};!function u(i,c,f){function a(t,r){if(!c[t]){if(!i[t]){var n="function"==typeof require&&require;if(!r&&n)return n(t,!0);if(p)return p(t,!0);var e=new Error("Cannot find module '"+t+"'");throw e.code="MODULE_NOT_FOUND",e}var o=c[t]={exports:{}};i[t][0].call(o.exports,function(r){return a(i[t][1][r]||r)},o,o.exports,u,i,c,f)}return c[t].exports}for(var p="function"==typeof require&&require,r=0;r<f.length;r++)a(f[r]);return a}({1:[function(t,n,e){(function(){this.jsexpr;var r=t("./index.js");void 0!==e&&(void 0!==n&&n.exports&&(e=n.exports=r),e.jsexpr=r),"undefined"!=typeof window&&(window.jsexpr=r)}).call(this)},{"./index.js":2}],2:[function(r,t,n){var e=r("./lib/expression");t.exports=e},{"./lib/expression":3}],3:[function(require,module,exports){var extend=require("extend"),RX=/\$\{[^\}]+\}/g;function _val(obj,key){try{return eval("this."+key)}catch(r){return}}function val(r,t){var n=_val.call(r,r,t);return void 0===n?"":n}function valwalk(n,e,o){return n&&Object.keys(n).forEach(function(r){var t=o+(o?".":"")+r;e[t]&&(n[r]=e[t]),"object"==_typeof(n[r])&&valwalk(n[r],e,t)}),n}function parse(n){var r=n.match(RX);r&&r.forEach(function(r){var t=r.replace(/[\$\{\}]/g,"").trim();n=n.replace(r,"__val(entry,'"+t+"')")});var t=new Function("entry","__val","return ("+n+")");return function(r){return t(r,val)}}function tokens(e){if("${JSON}"==e)return function(r){return JSON.stringify(r,null,2)};var o=[];return(e.match(RX)||[]).forEach(function(t){var r=e.indexOf(t),n=e.substring(0,r);e=e.substring(r+t.length),o.push(n),o.push(function(r){return val(r,t.replace(/\$|\{|\}/g,""))})}),o.push(e),function(t){return o.map(function(r){return"string"==typeof r?r:r(t)}).join("")}}function jsontokens(n){var i=[];return function e(o,u){o&&Object.keys(o).forEach(function(r){var t=u+(u?".":"")+r,n=o[r];"string"==typeof n?i.push({path:t,fn:tokens(n)}):e(n,t)})}(n,""),function(t){var r=i.map(function(r){return{path:r.path,value:r.fn(t)}}).reduce(function(r,t){return r[t.path]=t.value,r},{});return valwalk(extend(!0,{},n),r,"")}}module.exports={fn:parse,eval:parse,expr:function(r){return"object"==(void 0===r?"undefined":_typeof(r))?jsontokens(r):tokens(r)}}},{extend:4}],4:[function(r,t,n){var o=Object.prototype.hasOwnProperty,u=Object.prototype.toString,s=function(r){return"function"==typeof Array.isArray?Array.isArray(r):"[object Array]"===u.call(r)},l=function(r){if(!r||"[object Object]"!==u.call(r))return!1;var t,n=o.call(r,"constructor"),e=r.constructor&&r.constructor.prototype&&o.call(r.constructor.prototype,"isPrototypeOf");if(r.constructor&&!n&&!e)return!1;for(t in r);return void 0===t||o.call(r,t)};t.exports=function r(){var t,n,e,o,u,i,c=arguments[0],f=1,a=arguments.length,p=!1;for("boolean"==typeof c&&(p=c,c=arguments[1]||{},f=2),(null==c||"object"!==(void 0===c?"undefined":_typeof(c))&&"function"!=typeof c)&&(c={});f<a;++f)if(null!=(t=arguments[f]))for(n in t)e=c[n],c!==(o=t[n])&&(p&&o&&(l(o)||(u=s(o)))?(u?(u=!1,i=e&&s(e)?e:[]):i=e&&l(e)?e:{},c[n]=r(p,i,o)):void 0!==o&&(c[n]=o));return c}},{}]},{},[1]);

@@ -9,12 +9,11 @@ "use strict";

function _val(obj, key) {
var arr = key.split(".");
arr.forEach(function (key) {
if (obj == null || obj == undefined) return;else obj = obj[key];
});
return obj || undefined;
try {
return eval("this." + key);
} catch (err) {
return undefined;
}
}
function val(obj, key) {
var v = _val(obj, key);
var v = _val.call(obj, obj, key);
return v === undefined ? "" : v;

@@ -21,0 +20,0 @@ }

@@ -5,13 +5,11 @@ const extend = require("extend");

function _val(obj,key) {
var arr = key.split(".");
arr.forEach(key=>{
if(obj==null || obj==undefined) return;
else obj = obj[key];
});
return obj || undefined;
try {
return eval(`this.${key}`);
}catch(err) {
return undefined;
}
}
function val(obj,key) {
var v = _val(obj,key);
var v = _val.call(obj,obj,key);
return v===undefined? "" : v;

@@ -18,0 +16,0 @@ }

{
"name": "jsexpr",
"version": "0.2.1",
"version": "0.3.0",
"description": "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",
"main": "dist/node/index.js",
"scripts": {
"test-debug": "mocha --inspect-brk",
"test": "mocha"

@@ -9,0 +8,0 @@ },

# jsexpr
JSON Expressions!
[![Build Status](https://travis-ci.org/solzimer/jsexpr.svg?branch=master)](https://travis-ci.org/solzimer/jsexpr)
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.

@@ -4,0 +7,0 @@

const
expression = require("../dist/node"),
expression = require("../"),
assert = require('assert');

@@ -22,3 +22,3 @@

}
assert(error!=null,);
assert(error!=null);
});

@@ -29,3 +29,4 @@ });

let fn1 = expression.fn('(${a} + ${b}) / ${c} + ${d.e}');
let fn2 = expression.fn('${a} < ${d.e}');
let fn2 = expression.fn('(${a[1]} + ${b[2]}) / ${c[this.d.e]}');
let fn3 = expression.fn('${a} < ${d.e}');

@@ -40,2 +41,6 @@ it('Should perform operation', function() {

});
it('Should perform operation with arrays and `this`', function() {
let res = fn2({a:[1,5],b:[2,5,10,20],c:[4,3,4],d:{e:1}});
assert.equal(5,res);
});
it('Should fail operation with missing values', function() {

@@ -46,7 +51,7 @@ let res = fn1({d:{e:30}});

it('Should evaluate condition (true)', function() {
let res = fn2({a:5,d:{e:30}});
let res = fn3({a:5,d:{e:30}});
assert.equal(true,res);
});
it('Should evaluate condition (false)', function() {
let res = fn2({a:30,d:{e:30}});
let res = fn3({a:30,d:{e:30}});
assert.equal(false,res);

@@ -53,0 +58,0 @@ });

const
expression = require("../"),
expression = require("../lib/expression"),
assert = require('assert');

@@ -22,3 +22,3 @@

}
assert(error!=null,);
assert(error!=null);
});

@@ -29,3 +29,4 @@ });

let fn1 = expression.fn('(${a} + ${b}) / ${c} + ${d.e}');
let fn2 = expression.fn('${a} < ${d.e}');
let fn2 = expression.fn('(${a[1]} + ${b[2]}) / ${c[this.d.e]}');
let fn3 = expression.fn('${a} < ${d.e}');

@@ -40,2 +41,6 @@ it('Should perform operation', function() {

});
it('Should perform operation with arrays and `this`', function() {
let res = fn2({a:[1,5],b:[2,5,10,20],c:[4,3,4],d:{e:1}});
assert.equal(5,res);
});
it('Should fail operation with missing values', function() {

@@ -46,7 +51,7 @@ let res = fn1({d:{e:30}});

it('Should evaluate condition (true)', function() {
let res = fn2({a:5,d:{e:30}});
let res = fn3({a:5,d:{e:30}});
assert.equal(true,res);
});
it('Should evaluate condition (false)', function() {
let res = fn2({a:30,d:{e:30}});
let res = fn3({a:30,d:{e:30}});
assert.equal(false,res);

@@ -53,0 +58,0 @@ });

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc