interpolate-json
Advanced tools
Comparing version 0.5.3 to 0.6.0
@@ -1,1 +0,1 @@ | ||
"use strict";const extend=require("extend"),type=require("type-detect"),{BadRequest}=require("./exceptions"),regexEscaped=/[\\/|#<>{}()[\]^$+*?.-:!]/g,escapeRegex=e=>"string"===type(e)?e.replace(regexEscaped,"\\$&"):e,main=()=>{let e={debug:!1,prefix:"${",suffix:"}",subKeyPointer:"."};const t={...e},log=t=>{e.debug&&console.log(`[dotconfig][debug]: ${t}`)},trace=t=>{e.debug&&console.trace(`[dotconfig][error]: ${t}`)},getMatchSet=(e,t)=>new Set(e.reduce((e,r)=>(e.push(r.replace(t,(e,t)=>t.trim())),e),[])),getInterpolated=(e,t,r,s=!1)=>(log(`Found match: ${e.match(t)}`),e.replace(t,(e,t)=>(t=t.trim(),r.hasOwnProperty(t)?r[t].toString():s?e:""))),flattenAndResolve=(e,t,r,s)=>{let n={};return t.forEach(t=>{let c=((e,t,r)=>{const s=t.split(r).reduce((e,t)=>e[t]||{},e);return"number"===type(s)||"string"===type(s)?s:""})(e,t,s);r.test(c)&&(c=getInterpolated(c,r,n,!0)),r.test(c)?(extend(n,flattenAndResolve(e,getMatchSet(c.match(r),r),r,s)),n[t]=getInterpolated(c,r,n)):n[t]=c}),n};return{[Symbol.toStringTag]:"Interpolate-Json",expand:(t,r=null,s={})=>{let n=!1;const c=type(t);s.prefix&&!s.suffix&&(s.suffix=""),s=extend({},e,s);const i=(o=s,new RegExp(escapeRegex(o.prefix.trim())+"([\\s]*[\\w]+(?:"+escapeRegex(o.subKeyPointer.trim())+"[\\w]+)*[\\s]*)"+escapeRegex(o.suffix.trim()),"g"));var o;switch(c){case"Object":const e=extend({},t,r||{});if(t=JSON.stringify(t),i.test(t)){const c=t.match(i);r=flattenAndResolve(e,getMatchSet(c,i),i,s.subKeyPointer),n=!0}break;case"string":if(log(`Input: "${t}"`),!i.test(t))break;n=!0,null===r&&((e,t)=>{throw trace(e),"Exception"===type(t)?new t(e):new Error(e)})('Please provide "values"',BadRequest);break;default:return trace(`Interpolation for ${c} has not yet been implemented`),t}const a=n?getInterpolated(t,i,r):t;switch(c){case"Object":return JSON.parse(a);case"string":default:return a}},debug:()=>{e.debug=!0},reset:()=>{e={...t}}}};module.exports=main(); | ||
"use strict";const extend=require("extend"),type=require("type-detect"),{BadRequest}=require("./exceptions"),{subtract}=require("./setoperations"),regexEscaped=/[=\\/|#<>{}()[\]^$+\-*?,.:!%]/g,escapeRegex=e=>"string"===type(e)?e.replace(regexEscaped,"\\$&"):e,main=()=>{let e={debug:!1,prefix:"${",suffix:"}",subKeyPointer:".",funcSpecifier:"=",escapeSpecifier:"*"};const getParamRegex=(e,t)=>new RegExp(escapeRegex(e.prefix.trim())+("string"===type(t)?escapeRegex(t.trim()):"")+"([\\s]*[\\w]+(?:"+escapeRegex(e.subKeyPointer.trim())+"[\\w]+)*[\\s]*)"+escapeRegex(e.suffix.trim()),"g"),getFuncRegex=e=>new RegExp(escapeRegex(e.prefix.trim())+escapeRegex(e.funcSpecifier.trim())+"([\\s\\w"+escapeRegex("{}()+-*/%:|<>?,.$!&'#")+"]+)"+escapeRegex(e.funcSpecifier.trim())+escapeRegex(e.suffix.trim()),"g"),t={...e},log=t=>{e.debug&&console.log(`[dotconfig][debug]: ${t}`)},trace=t=>{e.debug&&console.trace(`[dotconfig][error]: ${t}`)},r=new Set(["number","string","boolean"]),getMatchSet=(e,t)=>new Set(e.reduce((e,r)=>(e.push(r.replace(t,(e,t)=>t.trim())),e),[])),getInterpolated=(e,t,r,i=!1)=>(log(`Found match: ${e.match(t)}`),e.replace(t,(e,t)=>(t=t.trim(),r.hasOwnProperty(t)?r[t].toString():i?e:""))),getInterpolatedFunc=(e,t,r,i,s=!1)=>(log(`Found func match: ${e.match(t)}`),e.replace(t,(e,t)=>{let s={};return t=t.trim().replace(r,(e,t)=>(s[t]=i.hasOwnProperty(t)?i[t].toString():"",`$val['${t}']`)),new Function("$val",`return ${t}`)(s)})),flattenAndResolve=(e,t,i,s,n,c)=>{let a=c||{};return t.forEach(t=>{let c=!1,p=((e,t,i)=>{const s=t.split(i).reduce((e,t)=>e[t]||{},e);return r.has(type(s))?s:""})(e,t,n);i.test(p)&&(a=flattenAndResolve(e,subtract(getMatchSet(p.match(i),i),new Set(Object.keys(a))),i,s,n,a),c=!0),s.test(p)&&(p=getInterpolatedFunc(p,s,i,a)),c&&i.test(p)&&(p=getInterpolated(p,i,a)),a[t]=p}),a};return{[Symbol.toStringTag]:"Interpolate-Json",expand:(t,r=null,i={})=>{let s=!1;const n=type(t);i.prefix&&!i.suffix&&(i.suffix=""),i=extend({},e,i);const c=getParamRegex(i),a=getFuncRegex(i),p=(e=>{let t={...e};return t.prefix='"'+t.prefix.trim(),t.suffix=t.suffix.trim()+'"',getParamRegex(t,t.escapeSpecifier)})(i);switch(n){case"Object":let e=JSON.stringify(t);p.test(e)&&(t=JSON.parse(((e,t,r)=>e.replace(t,(e,t)=>e.replace(`${r.prefix.trim()}${r.escapeSpecifier.trim()}`,`${r.prefix.trim()}`)))(e,p,i)),e=((e,t,r)=>e.replace(t,(e,t)=>`${r.prefix}${t.trim()}${r.suffix}`))(e,p,i));const u=extend({},t,r||{});if(t=e,c.test(t)){const e=t.match(c);r=flattenAndResolve(u,getMatchSet(e,c),c,a,i.subKeyPointer),log(`all values: ${JSON.stringify(r,null,2)}`),s=!0}break;case"string":if(log(`Input: "${t}"`),!c.test(t))break;s=!0,null===r&&((e,t)=>{throw trace(e),"Exception"===type(t)?new t(e):new Error(e)})('Please provide "values"',BadRequest);break;default:return trace(`Interpolation for ${n} has not yet been implemented`),t}log(`before interpolation:\n${t}`);let u="";switch(s?(u=getInterpolatedFunc(t,a,c,r),u=getInterpolated(u,c,r)):u=t,log(`after interpolation:\n${u}`),n){case"Object":return JSON.parse(u);case"string":default:return u}},debug:()=>{e.debug=!0},reset:()=>{e={...t}}}};module.exports=main(); |
{ | ||
"name": "interpolate-json", | ||
"version": "0.5.3", | ||
"version": "0.6.0", | ||
"description": "Interpolate a Javascript (Node.js V8) Object or string with json - Advanced", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
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
7091
7
2