Huge News!Announcing our $40M Series B led by Abstract Ventures.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 2.0.0-alpha.2 to 2.0.0-beta.0

1

CHANGELOG.md

@@ -11,2 +11,3 @@ ## 2.0.0

- removed `reset()`
- `suffix` is mandatory if function expression is used.
- `subKeyPointer` is restricted to dot(`.`), hash(`#`), underscore(`_`) & colon(`:`) (or it's multiple, like: `::` etc)

@@ -13,0 +14,0 @@ - `funcSpecifier` is fixed to equal(`=`)

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}=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=()=>"true"===process.env[debugEnv].toString().toLowerCase(),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="."){let n={};const _defineFixedProperties=e=>{Object.defineProperty(e,"funcSpecifier",{value:"=",writable:!1,enumerable:!1}),Object.defineProperty(e,"escapeSpecifier",{value:"*",writable:!1,enumerable:!1})};return n.updateOption=(e,t=!1)=>{const r=isArgument(e,"Object");for(let o in n)if(i=n[o],"function"!==type(i)){let i,s=`${envPrefix}${o.toUpperCase()}`;r&&(i=t?s:o),n[o]=r&&e.hasOwnProperty(i)?e[i]:process.env[s]||n[o]}var i;return n},n.clone=()=>{var e={...n};return _defineFixedProperties(e),e},n.cloneFrom=e=>("InterpolateOption"===type(e)&&(n.prefix=e.prefix,n.suffix=e.suffix,n.subKeyPointer=e.subKeyPointer),n),n[Symbol.toStringTag]="InterpolateOption",n.prefix=e,n.suffix=e&&!t?"":t,n.subKeyPointer=r,_defineFixedProperties(n),n},Interpolation=function(e=InterpolateOption()){let t,r,n,i,o={},s=null;const _updateRegex=()=>{r=_getParamRegex(),n=_getFuncRegex(s),i=_getEscapeRegex()},_updateOption=(e,t=!1)=>{s.updateOption(e,t),_updateRegex()},_getParamRegex=e=>new RegExp(RegexEscaped(s.prefix)+(isArgument(e,"string")?RegexEscaped(e):"")+"([\\s]*[\\w]+"+`(?:(${RegexEscaped(s.subKeyPointer)})`+"[\\w]+)*[\\s]*)"+RegexEscaped(s.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=s.clone();return e.prefix='"'+e.prefix.trim()+e.escapeSpecifier.trim(),e.suffix=e.suffix.trim()+'"',_getFuncRegex(e,!0)},_missingKeyKeepAlive=e=>`${s.prefix}${e}${s.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,o=!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(s.subKeyPointer).reduce((e,t)=>e[t]||{},e);return acceptedValueTypes.has(type(n))?n:r?_missingKeyKeepAlive(t):""})(e,t,o);_missingKeyKeepAlive(t)!==a&&r.test(a)&&(p=_flattenAndResolve(e,subtract(_getMatchSet(a.match(r)),new Set(Object.keys(p))),p,o),i=!0),n.test(a)&&(a=_getInterpolatedFunc(a,p)),i&&r.test(a)&&(a=_getInterpolated(a,p,o)),p[t]=a}),p},_containsOption=e=>Object.keys(e).some(e=>e.startsWith(envPrefix));return o.expand=(e,n=null,o=null)=>{let p=!1,a=!1;const c=type(e);isArgument(o,"Object")||isArgument(o,"InterpolateOption")?(o.prefix&&!o.suffix&&(o.suffix=""),_updateOption(o),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(`${s.prefix.trim()}${s.escapeSpecifier.trim()}`,`${s.prefix.trim()}`))),t=(e=>e.replace(i,(e,t)=>`${s.prefix}${t.trim()}${s.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&&(s.cloneFrom(t),_updateRegex()),c){case"Object":return JSON.parse(l);case"string":default:return l}},o.debug=(e=!0)=>(process.env[debugEnv]=e.toString(),o),"InterpolateOption"!==type(e)&&traceNThrow('Please provide "values"',BadRequest),o[Symbol.toStringTag]="Interpolation",s=e,s.updateOption(),null!==s&&(t=s.clone()),_updateRegex(),o};module.exports={InterpolateOption,Interpolation,interpolation:Interpolation()};
"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=()=>"true"===process.env[debugEnv].toString().toLowerCase(),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="."){let n={};const _checkSuffix=()=>{n.suffix=n.suffix?n.suffix:""},_defineFixedProperties=e=>{Object.defineProperty(e,"funcSpecifier",{value:"=",writable:!1,enumerable:!1}),Object.defineProperty(e,"escapeSpecifier",{value:"*",writable:!1,enumerable:!1})};return n.updateOption=(e,t=!1)=>{const r=isArgument(e,"Object");for(let s in n)if(i=n[s],"function"!==type(i)){let i,o=`${envPrefix}${s.toUpperCase()}`;r&&(i=t?o:s),n[s]=r&&e.hasOwnProperty(i)?e[i]:process.env[o]||n[s]}var i;return _checkSuffix(),n},n.clone=()=>{var e={...n};return _defineFixedProperties(e),e},n.cloneFrom=e=>("InterpolateOption"===type(e)&&(n.prefix=e.prefix,n.suffix=e.suffix,n.subKeyPointer=e.subKeyPointer),_checkSuffix(),n),n[Symbol.toStringTag]="InterpolateOption",n.prefix=e,n.suffix=t,n.subKeyPointer=r,_defineFixedProperties(n),_checkSuffix(),n},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]+)*"+`${o.suffix?spaces:""})`+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.toString(),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": "2.0.0-alpha.2",
"version": "2.0.0-beta.0",
"description": "Interpolate a Javascript Object or String with json - Advanced",

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

@@ -106,7 +106,7 @@ # interpolate-json [![NPM version](https://img.shields.io/npm/v/interpolate-json.svg?style=plastic)](https://www.npmjs.com/package/interpolate-json)

> Notice that `${==}` notation. It's a cool way to use JavaScript expression (not expression<u>_s_</u>, yet, just a single line).
> Notice that `${= =}` notation. It's a cool way to use JavaScript expression (not expression<u>_s_</u>, yet, just a single line).
## Definition
Syntax: `interpolation.expand(obj, values = null, options = {});`
Syntax: `interpolation.expand(obj, values = null, options = null);`

@@ -135,8 +135,7 @@ The `expand` function takes 3 parameters

{
debug: false,
prefix: '${',
suffix: '}',
subKeyPointer: '.',
funcSpecifier: '=',
escapeSpecifier: '*'
funcSpecifier: '=', // *read-only
escapeSpecifier: '*' // *read-only
}

@@ -143,0 +142,0 @@ ```

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