Comparing version 0.4.0 to 0.5.0
@@ -88,7 +88,7 @@ "use strict"; | ||
if (!src) return src; | ||
Object.keys(src).forEach(function (k) { | ||
for (var k in src) { | ||
var newpath = "" + path + (path ? '.' : '') + k; | ||
if (ops[newpath]) src[k] = ops[newpath]; | ||
if (_typeof(src[k]) == "object") EVALS.valwalk(src[k], ops, newpath); | ||
}); | ||
var rop = ops[newpath]; | ||
if (rop !== undefined) src[k] = rop;else if (_typeof(src[k]) == "object") EVALS.valwalk(src[k], ops, newpath); | ||
}; | ||
return src; | ||
@@ -141,3 +141,4 @@ } | ||
function jsontokens(json) { | ||
var ops = []; | ||
var ops = [], | ||
len = 0; | ||
@@ -158,11 +159,10 @@ function walk(json, path) { | ||
walk(json, ""); | ||
len = ops.length; | ||
return function (entry) { | ||
var map = ops.map(function (op) { | ||
return { path: op.path, value: op.fn(entry) }; | ||
}).reduce(function (map, op) { | ||
map[op.path] = op.value; | ||
return map; | ||
}, {}); | ||
var map = {}; | ||
for (var i = 0; i < len; i++) { | ||
var op = ops[i]; | ||
map[op.path] = op.fn(entry); | ||
} | ||
return EVALS.valwalk(extend(true, {}, json), map, ""); | ||
@@ -175,4 +175,8 @@ }; | ||
eval: parse, | ||
expr: function expr(input) { | ||
if ((typeof input === "undefined" ? "undefined" : _typeof(input)) == "object") return jsontokens(input);else return tokens(input); | ||
expr: function expr(input, replace) { | ||
if ((typeof input === "undefined" ? "undefined" : _typeof(input)) == "object") { | ||
return jsontokens(input, replace); | ||
} else { | ||
return tokens(input); | ||
} | ||
} | ||
@@ -179,0 +183,0 @@ }; |
@@ -1,3 +0,3 @@ | ||
/*! jsexpr 2018-06-12 */ | ||
/*! jsexpr 2018-07-05 */ | ||
"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};!function u(i,c,f){function a(n,t){if(!c[n]){if(!i[n]){var r="function"==typeof require&&require;if(!t&&r)return r(n,!0);if(l)return l(n,!0);var e=new Error("Cannot find module '"+n+"'");throw e.code="MODULE_NOT_FOUND",e}var o=c[n]={exports:{}};i[n][0].call(o.exports,function(t){return a(i[n][1][t]||t)},o,o.exports,u,i,c,f)}return c[n].exports}for(var l="function"==typeof require&&require,t=0;t<f.length;t++)a(f[t]);return a}({1:[function(n,r,e){(function(){this.jsexpr;var t=n("./index.js");void 0!==e&&(void 0!==r&&r.exports&&(e=r.exports=t),e.jsexpr=t),"undefined"!=typeof window&&(window.jsexpr=t)}).call(this)},{"./index.js":2}],2:[function(t,n,r){var e=t("./lib/expression");n.exports=e},{"./lib/expression":3}],3:[function(require,module,exports){var extend=require("extend"),RX=/\$\{[^\}]+\}/g,RX_RPL_PARSE=/[\$\{\}]/g,RX_RPL_TOKEN=/\$|\{|\}/g,CACHE={};function cacheeval(obj,key){if(!CACHE[key]){var fn=eval("(function(){\n\t\t\treturn function() {\n\t\t\t\ttry {\n\t\t\t\t\treturn this."+key+";\n\t\t\t\t}catch(err) {\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t})()");CACHE[key]=fn}return CACHE[key].call(obj)}function fneval(obj,key){try{return eval("this."+key)}catch(t){return}}var EVALS={eval:function(t,n){var r=fneval.call(t,t,n);return void 0===r?"":r},iteval:function(n,t){t.split(".").forEach(function(t){null!=n&&null!=n&&(n=n[t])});var r=n||void 0;return void 0===r?"":r},ceval:function(t,n){var r=cacheeval(t,n);return void 0===r?"":r},valwalk:function(r,e,o){return r&&Object.keys(r).forEach(function(t){var n=o+(o?".":"")+t;e[n]&&(r[t]=e[n]),"object"==_typeof(r[t])&&EVALS.valwalk(r[t],e,n)}),r}};function parse(r,n){n=n||"ceval";var t=r.match(RX);t&&t.forEach(function(t){var n=t.replace(RX_RPL_PARSE,"").trim();r=r.replace(t,"__val(entry,'"+n+"')")});var e=new Function("entry","__val","return ("+r+")");return function(t){return e(t,EVALS[n])}}function tokens(e,o){if(o=o||"ceval","${JSON}"==e)return function(t){return JSON.stringify(t,null,2)};var u=[];return(e.match(RX)||[]).forEach(function(n){var t=e.indexOf(n),r=e.substring(0,t);e=e.substring(t+n.length),u.push(r),u.push(function(t){return EVALS[o](t,n.replace(RX_RPL_TOKEN,""))})}),u.push(e),function(n){return u.map(function(t){return"string"==typeof t?t:t(n)}).join("")}}function jsontokens(r){var i=[];return function e(o,u){o&&Object.keys(o).forEach(function(t){var n=u+(u?".":"")+t,r=o[t];"string"==typeof r?i.push({path:n,fn:tokens(r)}):e(r,n)})}(r,""),function(n){var t=i.map(function(t){return{path:t.path,value:t.fn(n)}}).reduce(function(t,n){return t[n.path]=n.value,t},{});return EVALS.valwalk(extend(!0,{},r),t,"")}}module.exports={fn:parse,eval:parse,expr:function(t){return"object"==(void 0===t?"undefined":_typeof(t))?jsontokens(t):tokens(t)}}},{extend:4}],4:[function(t,n,r){var o=Object.prototype.hasOwnProperty,u=Object.prototype.toString,p=function(t){return"function"==typeof Array.isArray?Array.isArray(t):"[object Array]"===u.call(t)},s=function(t){if(!t||"[object Object]"!==u.call(t))return!1;var n,r=o.call(t,"constructor"),e=t.constructor&&t.constructor.prototype&&o.call(t.constructor.prototype,"isPrototypeOf");if(t.constructor&&!r&&!e)return!1;for(n in t);return void 0===n||o.call(t,n)};n.exports=function t(){var n,r,e,o,u,i,c=arguments[0],f=1,a=arguments.length,l=!1;for("boolean"==typeof c&&(l=c,c=arguments[1]||{},f=2),(null==c||"object"!==(void 0===c?"undefined":_typeof(c))&&"function"!=typeof c)&&(c={});f<a;++f)if(null!=(n=arguments[f]))for(r in n)e=c[r],c!==(o=n[r])&&(l&&o&&(s(o)||(u=p(o)))?(u?(u=!1,i=e&&p(e)?e:[]):i=e&&s(e)?e:{},c[r]=t(l,i,o)):void 0!==o&&(c[r]=o));return c}},{}]},{},[1]); | ||
"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};!function u(i,c,f){function a(n,t){if(!c[n]){if(!i[n]){var r="function"==typeof require&&require;if(!t&&r)return r(n,!0);if(l)return l(n,!0);var e=new Error("Cannot find module '"+n+"'");throw e.code="MODULE_NOT_FOUND",e}var o=c[n]={exports:{}};i[n][0].call(o.exports,function(t){return a(i[n][1][t]||t)},o,o.exports,u,i,c,f)}return c[n].exports}for(var l="function"==typeof require&&require,t=0;t<f.length;t++)a(f[t]);return a}({1:[function(n,r,e){(function(){this.jsexpr;var t=n("./index.js");void 0!==e&&(void 0!==r&&r.exports&&(e=r.exports=t),e.jsexpr=t),"undefined"!=typeof window&&(window.jsexpr=t)}).call(this)},{"./index.js":2}],2:[function(t,n,r){var e=t("./lib/expression");n.exports=e},{"./lib/expression":3}],3:[function(require,module,exports){var extend=require("extend"),RX=/\$\{[^\}]+\}/g,RX_RPL_PARSE=/[\$\{\}]/g,RX_RPL_TOKEN=/\$|\{|\}/g,CACHE={};function cacheeval(obj,key){if(!CACHE[key]){var fn=eval("(function(){\n\t\t\treturn function() {\n\t\t\t\ttry {\n\t\t\t\t\treturn this."+key+";\n\t\t\t\t}catch(err) {\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t})()");CACHE[key]=fn}return CACHE[key].call(obj)}function fneval(obj,key){try{return eval("this."+key)}catch(t){return}}var EVALS={eval:function(t,n){var r=fneval.call(t,t,n);return void 0===r?"":r},iteval:function(n,t){t.split(".").forEach(function(t){null!=n&&null!=n&&(n=n[t])});var r=n||void 0;return void 0===r?"":r},ceval:function(t,n){var r=cacheeval(t,n);return void 0===r?"":r},valwalk:function(t,n,r){if(!t)return t;for(var e in t){var o=r+(r?".":"")+e,u=n[o];void 0!==u?t[e]=u:"object"==_typeof(t[e])&&EVALS.valwalk(t[e],n,o)}return t}};function parse(r,n){n=n||"ceval";var t=r.match(RX);t&&t.forEach(function(t){var n=t.replace(RX_RPL_PARSE,"").trim();r=r.replace(t,"__val(entry,'"+n+"')")});var e=new Function("entry","__val","return ("+r+")");return function(t){return e(t,EVALS[n])}}function tokens(e,o){if(o=o||"ceval","${JSON}"==e)return function(t){return JSON.stringify(t,null,2)};var u=[];return(e.match(RX)||[]).forEach(function(n){var t=e.indexOf(n),r=e.substring(0,t);e=e.substring(t+n.length),u.push(r),u.push(function(t){return EVALS[o](t,n.replace(RX_RPL_TOKEN,""))})}),u.push(e),function(n){return u.map(function(t){return"string"==typeof t?t:t(n)}).join("")}}function jsontokens(o){var u,i=[];return function e(o,u){o&&Object.keys(o).forEach(function(t){var n=u+(u?".":"")+t,r=o[t];"string"==typeof r?i.push({path:n,fn:tokens(r)}):e(r,n)})}(o,""),u=i.length,function(t){for(var n={},r=0;r<u;r++){var e=i[r];n[e.path]=e.fn(t)}return EVALS.valwalk(extend(!0,{},o),n,"")}}module.exports={fn:parse,eval:parse,expr:function(t,n){return"object"==(void 0===t?"undefined":_typeof(t))?jsontokens(t,n):tokens(t)}}},{extend:4}],4:[function(t,n,r){var o=Object.prototype.hasOwnProperty,u=Object.prototype.toString,s=function(t){return"function"==typeof Array.isArray?Array.isArray(t):"[object Array]"===u.call(t)},p=function(t){if(!t||"[object Object]"!==u.call(t))return!1;var n,r=o.call(t,"constructor"),e=t.constructor&&t.constructor.prototype&&o.call(t.constructor.prototype,"isPrototypeOf");if(t.constructor&&!r&&!e)return!1;for(n in t);return void 0===n||o.call(t,n)};n.exports=function t(){var n,r,e,o,u,i,c=arguments[0],f=1,a=arguments.length,l=!1;for("boolean"==typeof c&&(l=c,c=arguments[1]||{},f=2),(null==c||"object"!==(void 0===c?"undefined":_typeof(c))&&"function"!=typeof c)&&(c={});f<a;++f)if(null!=(n=arguments[f]))for(r in n)e=c[r],c!==(o=n[r])&&(l&&o&&(p(o)||(u=s(o)))?(u?(u=!1,i=e&&s(e)?e:[]):i=e&&p(e)?e:{},c[r]=t(l,i,o)):void 0!==o&&(c[r]=o));return c}},{}]},{},[1]); |
@@ -48,7 +48,7 @@ "use strict"; | ||
if (!src) return src; | ||
Object.keys(src).forEach(function (k) { | ||
for (var k in src) { | ||
var newpath = "" + path + (path ? '.' : '') + k; | ||
if (ops[newpath]) src[k] = ops[newpath]; | ||
if (_typeof(src[k]) == "object") EVALS.valwalk(src[k], ops, newpath); | ||
}); | ||
var rop = ops[newpath]; | ||
if (rop !== undefined) src[k] = rop;else if (_typeof(src[k]) == "object") EVALS.valwalk(src[k], ops, newpath); | ||
}; | ||
return src; | ||
@@ -101,3 +101,4 @@ } | ||
function jsontokens(json) { | ||
var ops = []; | ||
var ops = [], | ||
len = 0; | ||
@@ -118,11 +119,10 @@ function walk(json, path) { | ||
walk(json, ""); | ||
len = ops.length; | ||
return function (entry) { | ||
var map = ops.map(function (op) { | ||
return { path: op.path, value: op.fn(entry) }; | ||
}).reduce(function (map, op) { | ||
map[op.path] = op.value; | ||
return map; | ||
}, {}); | ||
var map = {}; | ||
for (var i = 0; i < len; i++) { | ||
var op = ops[i]; | ||
map[op.path] = op.fn(entry); | ||
} | ||
return EVALS.valwalk(extend(true, {}, json), map, ""); | ||
@@ -135,6 +135,10 @@ }; | ||
eval: parse, | ||
expr: function expr(input) { | ||
if ((typeof input === "undefined" ? "undefined" : _typeof(input)) == "object") return jsontokens(input);else return tokens(input); | ||
expr: function expr(input, replace) { | ||
if ((typeof input === "undefined" ? "undefined" : _typeof(input)) == "object") { | ||
return jsontokens(input, replace); | ||
} else { | ||
return tokens(input); | ||
} | ||
} | ||
}; | ||
//# sourceMappingURL=expression.js.map |
@@ -55,9 +55,10 @@ const extend = require("extend"); | ||
if(!src) return src; | ||
Object.keys(src).forEach(k=>{ | ||
for(let k in src) { | ||
let newpath = `${path}${path?'.':''}${k}`; | ||
if(ops[newpath]) | ||
src[k] = ops[newpath]; | ||
if(typeof(src[k])=="object") | ||
EVALS.valwalk(src[k],ops,newpath); | ||
}); | ||
let rop = ops[newpath]; | ||
if(rop!==undefined) | ||
src[k] = rop; | ||
else if(typeof(src[k])=="object") | ||
EVALS.valwalk(src[k],ops,newpath); | ||
}; | ||
return src; | ||
@@ -106,3 +107,3 @@ } | ||
function jsontokens(json) { | ||
let ops = []; | ||
let ops = [], len = 0; | ||
@@ -124,11 +125,10 @@ function walk(json,path) { | ||
walk(json,""); | ||
len = ops.length; | ||
return function(entry) { | ||
let map = ops.map(op=>{ | ||
return {path:op.path, value:op.fn(entry)} | ||
}).reduce((map,op)=>{ | ||
map[op.path] = op.value; | ||
return map; | ||
},{}); | ||
let map = {}; | ||
for(let i=0;i<len;i++) { | ||
let op = ops[i]; | ||
map[op.path] = op.fn(entry); | ||
} | ||
return EVALS.valwalk(extend(true,{},json),map,""); | ||
@@ -141,8 +141,10 @@ } | ||
eval : parse, | ||
expr(input){ | ||
if(typeof(input)=="object") | ||
return jsontokens(input); | ||
else | ||
expr(input,replace){ | ||
if(typeof(input)=="object") { | ||
return jsontokens(input,replace); | ||
} | ||
else { | ||
return tokens(input); | ||
} | ||
} | ||
} |
{ | ||
"name": "jsexpr", | ||
"version": "0.4.0", | ||
"version": "0.5.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", | ||
@@ -5,0 +5,0 @@ "main": "dist/node/index.js", |
16
test.js
const expr = require('./index'); | ||
let fn1 = expr.eval('(${a} + ${b}) / ${c} + ${d.e}'); | ||
console.log(fn1); | ||
console.log(fn1({a:4,b:6,c:10,d:{e:4}})); | ||
var expr1 = expr.expr({time : "${client}/${address.host}:${address.port}", data : {request : {headers : "${headers}"}}}); | ||
var input1 = {client : "HOST001", address : {host : "localhost", port : 8080}, headers : "Content-Type: application/json"} | ||
var output1 = {time: 'HOST001/localhost:8080', data: {request: {headers: 'Content-Type: application/json'}}} | ||
var input2 = {client : "HOST002", address : {host : "localhost", port : 8443}, headers : "Content-Type: text/html"} | ||
var output2 = {time: 'HOST002/localhost:8443', data: {request: {headers: 'Content-Type: text/html'}}} | ||
console.log(JSON.stringify(output1,null,2)); | ||
console.log(JSON.stringify(expr1(input1),null,2)); | ||
console.log(JSON.stringify(output2,null,2)); | ||
console.log(JSON.stringify(expr1(input2),null,2)); |
@@ -75,3 +75,2 @@ const | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54959
24
720