interpolate-json
Advanced tools
Comparing version 0.8.1 to 0.8.2
@@ -1,1 +0,1 @@ | ||
"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"),t=`[\\s\\w${escapeRegex("{}()+-*/%:|<>?,.$!&'#")}]+`,getFuncRegex=(e,r=!1)=>new RegExp(escapeRegex(e.prefix.trim())+(r?"((":"")+escapeRegex(e.funcSpecifier.trim())+(r?")?":"")+`(${t}`+`(?:((==|===)(${t})))*[\\s]*)`+(r?"(":"")+escapeRegex(e.funcSpecifier.trim())+(r?")?)":"")+escapeRegex(e.suffix.trim()),"g"),r={...e},log=t=>{e.debug&&console.log(`[dotconfig][debug]: ${t}`)},trace=t=>{e.debug&&console.trace(`[dotconfig][error]: ${t}`)},s=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,s=!1)=>(log(`Found match: ${e.match(t)}`),e.replace(t,(e,t)=>(t=t.trim(),r.hasOwnProperty(t)?r[t].toString():s?e:""))),getInterpolatedFunc=(e,t,r,s,i=!1)=>(log(`Found func match: ${e.match(t)}`),e.replace(t,(e,t)=>{let i={};return t=t.trim().replace(r,(e,t)=>(i[t]=s.hasOwnProperty(t)?s[t].toString():"",`$val['${t}']`)),new Function("$val",`return ${t}`)(i)})),flattenAndResolve=(e,t,r,i,n,c)=>{let a=c||{};return t.forEach(t=>{let c=!1,p=((e,t,r)=>{const i=t.split(r).reduce((e,t)=>e[t]||{},e);return s.has(type(i))?i:""})(e,t,n);r.test(p)&&(a=flattenAndResolve(e,subtract(getMatchSet(p.match(r),r),new Set(Object.keys(a))),r,i,n,a),c=!0),i.test(p)&&(p=getInterpolatedFunc(p,i,r,a)),c&&r.test(p)&&(p=getInterpolated(p,r,a)),a[t]=p}),a};return{[Symbol.toStringTag]:"Interpolate-Json",expand:(t,r=null,s={})=>{let i=!1;const n=type(t);s.prefix&&!s.suffix&&(s.suffix=""),s=((t,r,s,i)=>{let n={};return Object.keys(e).forEach(c=>{let a=`INTERPOLATE_OPTION_${c.toUpperCase()}`;n[c]=((...e)=>{let t;const r=e.length;for(let s=0;s<r;s++){let r=e[s];if(null!=r){t=r;break}}return t})(i[c],process.env[a],(s||{})[a],("Object"===t?r:{})[a],e[c])}),n})(n,t,r,s),e.debug=s.debug;const c=getParamRegex(s),a=getFuncRegex(s),p=(e=>{let t={...e};return t.prefix='"'+t.prefix.trim()+t.escapeSpecifier.trim(),t.suffix=t.suffix.trim()+'"',getFuncRegex(t,!0)})(s);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,s)),e=((e,t,r)=>e.replace(t,(e,t)=>`${r.prefix}${t.trim()}${r.suffix}`))(e,p,s));const u=extend({},t,r||{});if(t=e,c.test(t)){const e=t.match(c);r=flattenAndResolve(u,getMatchSet(e,c),c,a,s.subKeyPointer),log(`all values: ${JSON.stringify(r,null,2)}`),i=!0}break;case"string":if(log(`Input: "${t}"`),!c.test(t))break;i=!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(i?(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:(t=!0)=>(e.debug=t,this),reset:()=>(e={...r},this)}};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,uniqueCharsAsString=(...e)=>[...new Set(e.join("").split(""))].join(""),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"),t=["==","===","!=","!==","<","<=",">",">="],getFuncRegex=(e,r=!1)=>{let i=(s=e.subKeyPointer,`[\\s\\w${escapeRegex(uniqueCharsAsString("{}()+-*/%:|?,.$!&'#",s))}]+`);var s;return new RegExp(escapeRegex(e.prefix.trim())+(r?"((":"")+escapeRegex(e.funcSpecifier.trim())+(r?")?":"")+`([\\s]*${i}`+`(?:(${t.map(e=>escapeRegex(e)).join("|")})${i})*[\\s]*)`+(r?"(":"")+escapeRegex(e.funcSpecifier.trim())+(r?")?)":"")+escapeRegex(e.suffix.trim()),"g")},r={...e},log=t=>{e.debug&&console.log(`[dotconfig][debug]: ${t}`)},trace=t=>{e.debug&&console.trace(`[dotconfig][error]: ${t}`)},i=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,r,s,n,a)=>{let c=a||{};return t.forEach(t=>{let a=!1,p=((e,t,r)=>{const s=t.split(r).reduce((e,t)=>e[t]||{},e);return i.has(type(s))?s:""})(e,t,n);r.test(p)&&(c=flattenAndResolve(e,subtract(getMatchSet(p.match(r),r),new Set(Object.keys(c))),r,s,n,c),a=!0),s.test(p)&&(p=getInterpolatedFunc(p,s,r,c)),a&&r.test(p)&&(p=getInterpolated(p,r,c)),c[t]=p}),c};return{[Symbol.toStringTag]:"Interpolate-Json",expand:(t,r=null,i={})=>{let s=!1;const n=type(t);i.prefix&&!i.suffix&&(i.suffix=""),i=((t,r,i,s)=>{let n={};return Object.keys(e).forEach(a=>{let c=`INTERPOLATE_OPTION_${a.toUpperCase()}`;n[a]=((...e)=>{let t;const r=e.length;for(let i=0;i<r;i++){let r=e[i];if(null!=r){t=r;break}}return t})(s[a],process.env[c],(i||{})[c],("Object"===t?r:{})[c],e[a])}),n})(n,t,r,i),e.debug=i.debug;const a=getParamRegex(i),c=getFuncRegex(i),p=(e=>{let t={...e};return t.prefix='"'+t.prefix.trim()+t.escapeSpecifier.trim(),t.suffix=t.suffix.trim()+'"',getFuncRegex(t,!0)})(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,a.test(t)){const e=t.match(a);r=flattenAndResolve(u,getMatchSet(e,a),a,c,i.subKeyPointer),log(`all values: ${JSON.stringify(r,null,2)}`),s=!0}break;case"string":if(log(`Input: "${t}"`),!a.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,c,a,r),u=getInterpolated(u,a,r)):u=t,log(`after interpolation:\n${u}`),n){case"Object":return JSON.parse(u);case"string":default:return u}},debug:(t=!0)=>(e.debug=t,this),reset:()=>(e={...r},this)}};module.exports=main(); |
{ | ||
"name": "interpolate-json", | ||
"version": "0.8.1", | ||
"version": "0.8.2", | ||
"description": "Interpolate a Javascript Object or string with json - Advanced", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
15160