New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

interpolate-json

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

interpolate-json - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0-alpha.1

CHANGELOG.md

2

lib/main.js

@@ -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,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.trim())).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);var u={};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)),u=extend(u,t,r||{}),t=e;break;case"string":log(`Input: "${t}"`),a.test(t)&&null===r&&((e,t)=>{throw trace(e),"Exception"===type(t)?new t(e):new Error(e)})('Please provide "values"',BadRequest),u=extend(u,r);break;default:return trace(`Interpolation for ${n} has not yet been implemented`),t}log(`before interpolation:\n${t}`);let o="";if(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}switch(s?(o=getInterpolatedFunc(t,c,a,r),o=getInterpolated(o,a,r)):o=t,log(`after interpolation:\n${o}`),n){case"Object":return JSON.parse(o);case"string":default:return o}},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}=require("./regexp-extensions"),spaces="[\\s]*",paramExpressionSet="[\\w]+",allowedFuncExpressions="{}()+-_*/%:|?,.$!&#'",funcExpressionSet=`[\\s\\w${RegexEscaped("{}()+-_*/%:|?,.$!&#'")}]+`,allowedOperators=["==","===","!=","!==","<","<=",">",">="],getOperatorSet=allowedOperators.map(e=>RegexEscaped(e)).join("|"),acceptedValueTypes=new Set(["number","string","boolean"]),isArgument=(e,t)=>e&&type(e)===(t||type(e)),isFunction=e=>"function"===type(e),envPrefix="INTERPOLATE_OPTION_",debugEnv=`${envPrefix}DEBUG`,isDebug=()=>process.env[debugEnv],log=e=>{isDebug()&&console.log(`[dotconfig][debug]: ${e}`)},trace=e=>{isDebug()&&console.trace(`[dotconfig][error]: ${e}`)},traceNThrow=(e,t)=>{throw trace(e),"Exception"===type(t)?new t(e):new Error(e)},InterpolateOption=function(e="${",t="}",r=".",n="=",i="*"){let s={};return s.updateOption=(e,t=!1)=>{const r=isArgument(e,"Object");for(let i in s)if(n=s[i],"function"!==type(n)){let n,o=`${envPrefix}${i.toUpperCase()}`;r&&(n=t?o:i),s[i]=r&&e.hasOwnProperty(n)?e[n]:process.env[o]||s[i]}var n;return s},s.clone=()=>({...s}),s.cloneFrom=e=>("InterpolateOption"===type(e)&&(s.prefix=e.prefix,s.suffix=e.suffix,s.subKeyPointer=e.subKeyPointer,s.funcSpecifier=e.funcSpecifier,s.escapeSpecifier=e.escapeSpecifier),s),s[Symbol.toStringTag]="InterpolateOption",s.prefix=e,s.suffix=e&&!t?"":t,s.subKeyPointer=r,s.funcSpecifier=n,s.escapeSpecifier=i,s},Interpolation=function(e=InterpolateOption()){let t,r,n,i,s={},o=null;const _updateRegex=()=>{r=_getParamRegex(),n=_getFuncRegex(o),i=_getEscapeRegex()},_updateOption=(e,t=!1)=>{o.updateOption(e,t),_updateRegex()},_getParamRegex=e=>new RegExp(RegexEscaped(o.prefix)+(isArgument(e,"string")?RegexEscaped(e):"")+"([\\s]*[\\w]+"+`(?:(${RegexEscaped(o.subKeyPointer)})`+"[\\w]+)*[\\s]*)"+RegexEscaped(o.suffix),"g"),_getFuncRegex=(e,t=!1)=>new RegExp(RegexEscaped(e.prefix)+(t?"((":"")+RegexEscaped(e.funcSpecifier)+(t?")?":"")+`([\\s]*${funcExpressionSet}`+`(?:(${getOperatorSet})${funcExpressionSet})*[\\s]*)`+(t?"(":"")+RegexEscaped(e.funcSpecifier)+(t?")?)":"")+RegexEscaped(e.suffix),"g"),_getEscapeRegex=()=>{let e=o.clone();return e.prefix='"'+e.prefix.trim()+e.escapeSpecifier.trim(),e.suffix=e.suffix.trim()+'"',_getFuncRegex(e,!0)},_missingKeyKeepAlive=e=>`${o.prefix}${e}${o.suffix}`,_getMatchSet=e=>new Set(e.reduce((e,t)=>(e.push(t.replace(r,(e,t)=>t.trim())),e),[])),_getInterpolated=(e,t,n=!1)=>(log(`Found match: ${e.match(r)}`),e.replace(r,(e,r)=>(r=r.trim(),t.hasOwnProperty(r)?t[r].toString():n?e:""))),_getInterpolatedFunc=(e,t,i=!1)=>(log(`Found func match: ${e.match(n)}`),e.replace(n,(e,n)=>{let i={};return n=n.trim().replace(r,(e,r)=>(i[r]=t.hasOwnProperty(r)?t[r].toString():"",`$val['${r}']`)),new Function("$val",`return ${n}`)(i)})),_flattenAndResolve=(e,t,i,s=!1)=>{let p=i||{};return t.forEach(t=>{if(p.hasOwnProperty(t))return;let i=!1,a=((e,t,r=!1)=>{const n=t.split(o.subKeyPointer).reduce((e,t)=>e[t]||{},e);return acceptedValueTypes.has(type(n))?n:r?_missingKeyKeepAlive(t):""})(e,t,s);_missingKeyKeepAlive(t)!==a&&r.test(a)&&(p=_flattenAndResolve(e,subtract(_getMatchSet(a.match(r)),new Set(Object.keys(p))),p,s),i=!0),n.test(a)&&(a=_getInterpolatedFunc(a,p)),i&&r.test(a)&&(a=_getInterpolated(a,p,s)),p[t]=a}),p},_containsOption=e=>Object.keys(e).some(e=>e.startsWith(envPrefix));return s.expand=(e,n=null,s=null)=>{let p=!1,a=!1;const c=type(e);isArgument(s,"Object")||isArgument(s,"InterpolateOption")?(s.prefix&&!s.suffix&&(s.suffix=""),_updateOption(s),a=!0):isArgument(n,"Object")&&_containsOption(n)?(_updateOption(n,!0),a=!0):isArgument(e,"Object")&&_containsOption(e)&&(_updateOption(e,!0),a=!0);var u={};switch(c){case"Object":let t=JSON.stringify(e);i.test(t)&&(e=JSON.parse(t.replace(i,(e,t)=>e.replace(`${o.prefix.trim()}${o.escapeSpecifier.trim()}`,`${o.prefix.trim()}`))),t=(e=>e.replace(i,(e,t)=>`${o.prefix}${t.trim()}${o.suffix}`))(t)),u=extend(u,e,n||{}),e=t;break;case"string":log(`Input: "${e}"`),r.test(e)&&null===n&&traceNThrow('Please provide "values"',BadRequest),u=extend(u,n);break;default:return trace(`Interpolation for ${c} has not yet been implemented`),e}log(`before interpolation:\n${e}`);let l="";if(r.test(e)){const t=e.match(r);n=_flattenAndResolve(u,_getMatchSet(t)),log(`all values: ${JSON.stringify(n,null,2)}`),p=!0}switch(p?(l=_getInterpolatedFunc(e,n),l=_getInterpolated(l,n)):l=e,log(`after interpolation:\n${l}`),a&&(o.cloneFrom(t),_updateRegex()),c){case"Object":return JSON.parse(l);case"string":default:return l}},s.debug=(e=!0)=>(process.env[debugEnv]=e,s),"InterpolateOption"!==type(e)&&traceNThrow('Please provide "values"',BadRequest),s[Symbol.toStringTag]="Interpolation",o=e,o.updateOption(),null!==o&&(t=o.clone()),_updateRegex(),s};module.exports={InterpolateOption,Interpolation,interpolation:Interpolation()};
{
"name": "interpolate-json",
"version": "1.0.1",
"version": "2.0.0-alpha.1",
"description": "Interpolate a Javascript Object or String with json - Advanced",

@@ -5,0 +5,0 @@ "main": "index.js",

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