diff --git a/dist/index.d.ts b/dist/index.d.ts index d29b64b..252aee2 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,9 +1,11 @@ type Options = { strict?: boolean; + keepTypeof?: boolean; }; declare class DryReplacer { data: object; - strict: boolean; + strict?: boolean; + keepTypeof?: boolean; constructor(data: object, options?: Options); replaceValue(key: string, value: any, data: object, template: object): void; recursiveReplace(data: object, template: any): object; diff --git a/dist/index.js b/dist/index.js index da9a280..e7a516c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,2 +1,2 @@ -var e=require("lodash.get"),r=require("lodash.set"),t=require("lodash.isarray"),n=require("lodash.isplainobject");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=/*#__PURE__*/a(e),o=/*#__PURE__*/a(r),u=/*#__PURE__*/a(t),l=/*#__PURE__*/a(n);function c(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t=e.length?{done:!0}:{done:!1,value:e[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(a.reverse());!(u=l()).done;){var s=u.value,f=s.replace(/[{}]/g,""),d=i.default(n,e),p=i.default(t,f),v=p;["undefined",void 0,null,"string"].includes(typeof p)&&(v=d.replace(s,this.strict?p:p||"")),["number"].includes(typeof p)&&(v=d.replace(s,p)),o.default(n,e,v)}},r.recursiveReplace=function(e,r){for(var t=this,n=0,a=Object.keys(r);ne.length)&&(t=e.length);for(var r=0,n=new Array(t);r=e.length?{done:!0}:{done:!1,value:e[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(a.reverse());!(u=l()).done;){var c=u.value,f=c.replace(/[{}]/g,""),d=i.default(n,e),p=i.default(r,f),v=p;["undefined",void 0,null,"string"].includes(typeof p)&&(v=d.replace(c,this.strict?p:p||"")),["number"].includes(typeof p)&&(v=d.replace(c,p),this.keepTypeof&&/^-?\.?_?\d+$/.test(v)&&(v=Number(v))),o.default(n,e,v)}},t.recursiveReplace=function(e,t){for(var r=this,n=0,a=Object.keys(t);n this.recursiveReplace(data, item))\n } else {\n this.replaceValue(key, value, data, template)\n }\n }\n return template\n }\n\n try(jsonToParse: string): object {\n let template = JSON.parse(jsonToParse)\n this.recursiveReplace(this.data, template)\n\n return template\n }\n}\n\nexport default DryReplacer\n"],"names":["DryReplacer","data","options","this","strict","_proto","prototype","replaceValue","key","value","template","matchedArray","String","match","_step","_iterator","_createForOfIteratorHelperLoose","reverse","done","item","patternKey","replace","spotting","get","valueFromData","newValue","undefined","includes","set","recursiveReplace","Object","keys","_i","_Object$keys","length","isPlainObject","isArray","map","_this","jsonToParse","JSON","parse"],"mappings":"oZAaE,WAAA,SAAAA,EAAYC,EAAcC,GAAiBC,KAH3CF,UAAI,EAAAE,KACJC,QAAkB,EAGhBD,KAAKF,KAAOA,EACZE,KAAKC,OAAgB,MAAPF,OAAO,EAAPA,EAASE,MACzB,CAAC,IAAAC,EAAAL,EAAAM,UAsDA,OAtDAD,EAEDE,aAAA,SAAaC,EAAaC,EAAYR,EAAcS,GAClD,IAAIC,EAAeC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAA,IAAuCG,EAAvCC,2qBAAAC,CAAiBL,EAAaM,aAASH,EAAAC,KAAAG,MAAE,CAAhCC,IAAAA,UACHC,EAAaD,EAAKE,QAAQ,QAAS,IAC3BC,EAAGC,EAAAA,QAAIb,EAAUF,GAEZgB,EAAGD,UAAItB,EAAMmB,GAC1BK,EAAWD,EAGb,CAAC,iBAAaE,EAAW,KAAM,UAAUC,gBAChCH,KAIPC,EAAWH,EAASD,QAAQF,EAD1BhB,KAAKC,OAC2BoB,EAEAA,GAAiB,KAInD,CAAC,UAAUG,gBAAgBH,KAC7BC,EAAWH,EAASD,QAAQF,EAAMK,IAGpCI,UAAIlB,EAAUF,EAAKiB,EACpB,CAEL,EAACpB,EAEDwB,iBAAA,SAAiB5B,EAAcS,GAC7B,eAAgBoB,EAAAA,EAAAA,EAAAA,OAAOC,KAAKrB,GAASsB,EAAAC,EAAAC,OAAAF,IAAE,CAAlC,MACHC,EAAAD,GAASvB,EAAGC,EAASF,GAEjB2B,EAAa,QAAC1B,GAChBN,KAAK0B,iBAAiB5B,EAAMQ,GACnB2B,UAAQ3B,GACjBA,EAAM4B,IAAI,SAAClB,GAAS,OAAAmB,EAAKT,iBAAiB5B,EAAMkB,EAAK,GAErDhB,KAAKI,aAAaC,EAAKC,EAAOR,EAAMS,EAEvC,CACD,QACF,EAACL,EAAA,IAED,SAAIkC,GACF,MAAeC,KAAKC,MAAMF,GAG1B,OAFApC,KAAK0B,iBAAiB1B,KAAKF,KAAMS,IAGnC,EAACV,CAAA,CAzDD"} \ No newline at end of file +{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import get from 'lodash.get'\nimport set from 'lodash.set'\nimport isArray from 'lodash.isarray'\nimport isPlainObject from 'lodash.isplainobject'\n\ntype Options = {\n strict?: boolean\n keepTypeof?: boolean\n}\n\nclass DryReplacer {\n data: object\n strict?: boolean = true\n keepTypeof?: boolean = false\n\n constructor(data: object, options?: Options) {\n this.data = data\n this.strict = options?.strict\n this.keepTypeof = options?.keepTypeof\n }\n\n replaceValue(key: string, value: any, data: object, template: object): void {\n let matchedArray = String(value).match(/{{.*?}}/g) // extract only {{}} pattern\n\n if (matchedArray) {\n for (let item of matchedArray.reverse()) {\n let patternKey = item.replace(/[{}]/g, '')\n let spotting = get(template, key)\n\n let valueFromData = get(data, patternKey)\n let newValue = valueFromData\n\n const isStringOrFalsy = [\n 'undefined',\n undefined,\n null,\n 'string',\n ].includes(typeof valueFromData)\n\n if (isStringOrFalsy) {\n if (this.strict) {\n newValue = spotting.replace(item, valueFromData)\n } else {\n newValue = spotting.replace(item, valueFromData || '')\n }\n }\n\n if (['number'].includes(typeof valueFromData)) {\n newValue = spotting.replace(item, valueFromData)\n\n if (this.keepTypeof && /^-?\\.?_?\\d+$/.test(newValue)) {\n newValue = Number(newValue)\n }\n }\n\n set(template, key, newValue)\n }\n }\n }\n\n recursiveReplace(data: object, template: any): object {\n for (let key of Object.keys(template)) {\n let value = template[key]\n\n if (isPlainObject(value)) {\n this.recursiveReplace(data, value)\n } else if (isArray(value)) {\n value.map((item) => this.recursiveReplace(data, item))\n } else {\n this.replaceValue(key, value, data, template)\n }\n }\n return template\n }\n\n try(jsonToParse: string): object {\n let template = JSON.parse(jsonToParse)\n this.recursiveReplace(this.data, template)\n\n return template\n }\n}\n\nexport default DryReplacer\n"],"names":["DryReplacer","data","options","strict","keepTypeof","this","replaceValue","key","value","template","matchedArray","String","match","reverse","item","patternKey","replace","spotting","get","newValue","valueFromData","undefined","includes","test","Number","set","_proto","recursiveReplace","_this","_i","_Object$keys","Object","keys","isPlainObject","isArray","map","jsonToParse","JSON","parse"],"mappings":"oZAeE,WAAA,SAAAA,EAAYC,EAAcC,GAJ1BD,KAAAA,iBACAE,QAAmB,EACnBC,KAAAA,YAAuB,EAGrBC,KAAKJ,KAAOA,EACZI,KAAKF,aAASD,SAAAA,EAASC,OACvBE,KAAKD,WAAaF,MAAAA,OAAAA,EAAAA,EAASE,UAC7B,CAAC,kBA6DA,SA3DDE,aAAA,SAAaC,EAAaC,EAAYP,EAAcQ,GAClD,IAAIC,EAAeC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAiBA,MAAAA,2qBAAAA,CAAAA,EAAaG,0BAAW,CAAhCC,IAAAA,UACHC,EAAaD,EAAKE,QAAQ,QAAS,IACnCC,EAAWC,EAAG,QAACT,EAAUF,KAETW,EAAAA,QAAIjB,EAAMc,GAClBI,EAAGC,EAES,CACtB,iBACAC,EACA,KACA,UACAC,gBAA6BF,KAI3BD,EAAWF,EAASD,QAAQF,EAD1BT,KAAKF,OAC2BiB,EAEAA,GAAiB,KAInD,CAAC,UAAUE,gBAAgBF,KAC7BD,EAAWF,EAASD,QAAQF,EAAMM,GAE9Bf,KAAKD,YAAc,eAAemB,KAAKJ,KACzCA,EAAWK,OAAOL,KAItBM,EAAG,QAAChB,EAAUF,EAAKY,EACpB,CAEL,EAACO,EAEDC,iBAAA,SAAiB1B,EAAcQ,GAC7B,IAAA,IAAAmB,EAAAvB,KAAAwB,EAAA,EAAAC,EAAgBC,OAAOC,KAAKvB,kBAAW,CAAlC,MACHqB,EAAAD,KAAYpB,EAASF,GAEjB0B,EAAa,QAACzB,GAChBH,KAAKsB,iBAAiB1B,EAAMO,GACnB0B,EAAAA,QAAQ1B,GACjBA,EAAM2B,IAAI,SAACrB,GAAI,SAAUa,iBAAiB1B,EAAMa,EAAK,GAErDT,KAAKC,aAAaC,EAAKC,EAAOP,EAAMQ,EAEvC,CACD,OACFA,CAAA,QAEA,SAAI2B,GACF,MAAeC,KAAKC,MAAMF,GAG1B,OAFA/B,KAAKsB,iBAAiBtB,KAAKJ,KAAMQ,IAGnC,EAACT,CAAA,CAjED"} \ No newline at end of file diff --git a/dist/index.mjs b/dist/index.mjs index 7e98ca3..f017db5 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -1,2 +1,2 @@ -import r from"lodash.get";import t from"lodash.set";import e from"lodash.isarray";import n from"lodash.isplainobject";function a(r,t){(null==t||t>r.length)&&(t=r.length);for(var e=0,n=new Array(t);e=r.length?{done:!0}:{done:!1,value:r[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(c.reverse());!(l=s()).done;){var u=l.value,f=u.replace(/[{}]/g,""),p=r(o,e),d=r(i,f),v=d;["undefined",void 0,null,"string"].includes(typeof d)&&(v=p.replace(u,this.strict?d:d||"")),["number"].includes(typeof d)&&(v=p.replace(u,d)),t(o,e,v)}},o.recursiveReplace=function(r,t){for(var a=this,i=0,o=Object.keys(t);ie.length)&&(r=e.length);for(var t=0,n=new Array(r);t=e.length?{done:!0}:{done:!1,value:e[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(s.reverse());!(l=c()).done;){var u=l.value,p=u.replace(/[{}]/g,""),f=e(a,t),d=e(o,p),v=d;["undefined",void 0,null,"string"].includes(typeof d)&&(v=f.replace(u,this.strict?d:d||"")),["number"].includes(typeof d)&&(v=f.replace(u,d),this.keepTypeof&&/^-?\.?_?\d+$/.test(v)&&(v=Number(v))),r(a,t,v)}},a.recursiveReplace=function(e,r){for(var i=this,o=0,a=Object.keys(r);o this.recursiveReplace(data, item))\n } else {\n this.replaceValue(key, value, data, template)\n }\n }\n return template\n }\n\n try(jsonToParse: string): object {\n let template = JSON.parse(jsonToParse)\n this.recursiveReplace(this.data, template)\n\n return template\n }\n}\n\nexport default DryReplacer\n"],"names":["DryReplacer","data","options","this","strict","_proto","prototype","replaceValue","key","value","template","matchedArray","String","match","_step","_iterator","_createForOfIteratorHelperLoose","reverse","done","item","patternKey","replace","spotting","get","valueFromData","newValue","undefined","includes","set","recursiveReplace","Object","keys","_i","_Object$keys","length","isPlainObject","isArray","map","_this","jsonToParse","JSON","parse"],"mappings":"gOASMA,mBAIJ,WAAA,SAAAA,EAAYC,EAAcC,GAAiBC,KAH3CF,UAAI,EAAAE,KACJC,QAAkB,EAGhBD,KAAKF,KAAOA,EACZE,KAAKC,OAAgB,MAAPF,OAAO,EAAPA,EAASE,MACzB,CAAC,IAAAC,EAAAL,EAAAM,UAsDA,OAtDAD,EAEDE,aAAA,SAAaC,EAAaC,EAAYR,EAAcS,GAClD,IAAIC,EAAeC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAA,IAAuCG,EAAvCC,2qBAAAC,CAAiBL,EAAaM,aAASH,EAAAC,KAAAG,MAAE,CAAhCC,IAAAA,UACHC,EAAaD,EAAKE,QAAQ,QAAS,IAC3BC,EAAGC,EAAIb,EAAUF,GAEZgB,EAAGD,EAAItB,EAAMmB,GAC1BK,EAAWD,EAGb,CAAC,iBAAaE,EAAW,KAAM,UAAUC,gBAChCH,KAIPC,EAAWH,EAASD,QAAQF,EAD1BhB,KAAKC,OAC2BoB,EAEAA,GAAiB,KAInD,CAAC,UAAUG,gBAAgBH,KAC7BC,EAAWH,EAASD,QAAQF,EAAMK,IAGpCI,EAAIlB,EAAUF,EAAKiB,EACpB,CAEL,EAACpB,EAEDwB,iBAAA,SAAiB5B,EAAcS,GAC7B,eAAgBoB,EAAAA,EAAAA,EAAAA,OAAOC,KAAKrB,GAASsB,EAAAC,EAAAC,OAAAF,IAAE,CAAlC,MACHC,EAAAD,GAASvB,EAAGC,EAASF,GAEjB2B,EAAc1B,GAChBN,KAAK0B,iBAAiB5B,EAAMQ,GACnB2B,EAAQ3B,GACjBA,EAAM4B,IAAI,SAAClB,GAAS,OAAAmB,EAAKT,iBAAiB5B,EAAMkB,EAAK,GAErDhB,KAAKI,aAAaC,EAAKC,EAAOR,EAAMS,EAEvC,CACD,QACF,EAACL,EAAA,IAED,SAAIkC,GACF,MAAeC,KAAKC,MAAMF,GAG1B,OAFApC,KAAK0B,iBAAiB1B,KAAKF,KAAMS,IAGnC,EAACV,CAAA,CAzDD"} \ No newline at end of file +{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["import get from 'lodash.get'\nimport set from 'lodash.set'\nimport isArray from 'lodash.isarray'\nimport isPlainObject from 'lodash.isplainobject'\n\ntype Options = {\n strict?: boolean\n keepTypeof?: boolean\n}\n\nclass DryReplacer {\n data: object\n strict?: boolean = true\n keepTypeof?: boolean = false\n\n constructor(data: object, options?: Options) {\n this.data = data\n this.strict = options?.strict\n this.keepTypeof = options?.keepTypeof\n }\n\n replaceValue(key: string, value: any, data: object, template: object): void {\n let matchedArray = String(value).match(/{{.*?}}/g) // extract only {{}} pattern\n\n if (matchedArray) {\n for (let item of matchedArray.reverse()) {\n let patternKey = item.replace(/[{}]/g, '')\n let spotting = get(template, key)\n\n let valueFromData = get(data, patternKey)\n let newValue = valueFromData\n\n const isStringOrFalsy = [\n 'undefined',\n undefined,\n null,\n 'string',\n ].includes(typeof valueFromData)\n\n if (isStringOrFalsy) {\n if (this.strict) {\n newValue = spotting.replace(item, valueFromData)\n } else {\n newValue = spotting.replace(item, valueFromData || '')\n }\n }\n\n if (['number'].includes(typeof valueFromData)) {\n newValue = spotting.replace(item, valueFromData)\n\n if (this.keepTypeof && /^-?\\.?_?\\d+$/.test(newValue)) {\n newValue = Number(newValue)\n }\n }\n\n set(template, key, newValue)\n }\n }\n }\n\n recursiveReplace(data: object, template: any): object {\n for (let key of Object.keys(template)) {\n let value = template[key]\n\n if (isPlainObject(value)) {\n this.recursiveReplace(data, value)\n } else if (isArray(value)) {\n value.map((item) => this.recursiveReplace(data, item))\n } else {\n this.replaceValue(key, value, data, template)\n }\n }\n return template\n }\n\n try(jsonToParse: string): object {\n let template = JSON.parse(jsonToParse)\n this.recursiveReplace(this.data, template)\n\n return template\n }\n}\n\nexport default DryReplacer\n"],"names":["DryReplacer","data","options","strict","keepTypeof","this","replaceValue","key","value","template","matchedArray","String","match","reverse","item","patternKey","replace","spotting","get","newValue","valueFromData","undefined","includes","test","Number","set","_proto","recursiveReplace","_this","_i","_Object$keys","Object","keys","isPlainObject","isArray","map","jsonToParse","JSON","parse"],"mappings":"gOAUMA,IAAAA,eAKJ,WAAA,SAAAA,EAAYC,EAAcC,GAJ1BD,KAAAA,iBACAE,QAAmB,EACnBC,KAAAA,YAAuB,EAGrBC,KAAKJ,KAAOA,EACZI,KAAKF,aAASD,SAAAA,EAASC,OACvBE,KAAKD,WAAaF,MAAAA,OAAAA,EAAAA,EAASE,UAC7B,CAAC,kBA6DA,SA3DDE,aAAA,SAAaC,EAAaC,EAAYP,EAAcQ,GAClD,IAAIC,EAAeC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAiBA,MAAAA,2qBAAAA,CAAAA,EAAaG,0BAAW,CAAhCC,IAAAA,UACHC,EAAaD,EAAKE,QAAQ,QAAS,IACnCC,EAAWC,EAAIT,EAAUF,KAETW,EAAIjB,EAAMc,GAClBI,EAAGC,EAES,CACtB,iBACAC,EACA,KACA,UACAC,gBAA6BF,KAI3BD,EAAWF,EAASD,QAAQF,EAD1BT,KAAKF,OAC2BiB,EAEAA,GAAiB,KAInD,CAAC,UAAUE,gBAAgBF,KAC7BD,EAAWF,EAASD,QAAQF,EAAMM,GAE9Bf,KAAKD,YAAc,eAAemB,KAAKJ,KACzCA,EAAWK,OAAOL,KAItBM,EAAIhB,EAAUF,EAAKY,EACpB,CAEL,EAACO,EAEDC,iBAAA,SAAiB1B,EAAcQ,GAC7B,IAAA,IAAAmB,EAAAvB,KAAAwB,EAAA,EAAAC,EAAgBC,OAAOC,KAAKvB,kBAAW,CAAlC,MACHqB,EAAAD,KAAYpB,EAASF,GAEjB0B,EAAczB,GAChBH,KAAKsB,iBAAiB1B,EAAMO,GACnB0B,EAAQ1B,GACjBA,EAAM2B,IAAI,SAACrB,GAAI,SAAUa,iBAAiB1B,EAAMa,EAAK,GAErDT,KAAKC,aAAaC,EAAKC,EAAOP,EAAMQ,EAEvC,CACD,OACFA,CAAA,QAEA,SAAI2B,GACF,MAAeC,KAAKC,MAAMF,GAG1B,OAFA/B,KAAKsB,iBAAiBtB,KAAKJ,KAAMQ,IAGnC,EAACT,CAAA,CAjED"} \ No newline at end of file diff --git a/dist/index.modern.mjs b/dist/index.modern.mjs index 6542ccd..bcf888d 100644 --- a/dist/index.modern.mjs +++ b/dist/index.modern.mjs @@ -1,2 +1,2 @@ -import e from"lodash.get";import t from"lodash.set";import r from"lodash.isarray";import s from"lodash.isplainobject";class i{constructor(e,t){this.data=void 0,this.strict=!0,this.data=e,this.strict=null==t?void 0:t.strict}replaceValue(r,s,i,a){let l=String(s).match(/{{.*?}}/g);if(l)for(let s of l.reverse()){let l=s.replace(/[{}]/g,""),c=e(a,r),o=e(i,l),p=o;["undefined",void 0,null,"string"].includes(typeof o)&&(p=c.replace(s,this.strict?o:o||"")),["number"].includes(typeof o)&&(p=c.replace(s,o)),t(a,r,p)}}recursiveReplace(e,t){for(let i of Object.keys(t)){let a=t[i];s(a)?this.recursiveReplace(e,a):r(a)?a.map(t=>this.recursiveReplace(e,t)):this.replaceValue(i,a,e,t)}return t}try(e){let t=JSON.parse(e);return this.recursiveReplace(this.data,t),t}}export{i as default}; +import e from"lodash.get";import t from"lodash.set";import r from"lodash.isarray";import s from"lodash.isplainobject";class i{constructor(e,t){this.data=void 0,this.strict=!0,this.keepTypeof=!1,this.data=e,this.strict=null==t?void 0:t.strict,this.keepTypeof=null==t?void 0:t.keepTypeof}replaceValue(r,s,i,l){let a=String(s).match(/{{.*?}}/g);if(a)for(let s of a.reverse()){let a=s.replace(/[{}]/g,""),o=e(l,r),p=e(i,a),c=p;["undefined",void 0,null,"string"].includes(typeof p)&&(c=o.replace(s,this.strict?p:p||"")),["number"].includes(typeof p)&&(c=o.replace(s,p),this.keepTypeof&&/^-?\.?_?\d+$/.test(c)&&(c=Number(c))),t(l,r,c)}}recursiveReplace(e,t){for(let i of Object.keys(t)){let l=t[i];s(l)?this.recursiveReplace(e,l):r(l)?l.map(t=>this.recursiveReplace(e,t)):this.replaceValue(i,l,e,t)}return t}try(e){let t=JSON.parse(e);return this.recursiveReplace(this.data,t),t}}export{i as default}; //# sourceMappingURL=index.modern.mjs.map diff --git a/dist/index.modern.mjs.map b/dist/index.modern.mjs.map index da306dd..1cbbdc4 100644 --- a/dist/index.modern.mjs.map +++ b/dist/index.modern.mjs.map @@ -1 +1 @@ -{"version":3,"file":"index.modern.mjs","sources":["../src/index.ts"],"sourcesContent":["import get from 'lodash.get'\nimport set from 'lodash.set'\nimport isArray from 'lodash.isarray'\nimport isPlainObject from 'lodash.isplainobject'\n\ntype Options = {\n strict?: boolean\n}\n\nclass DryReplacer {\n data: object\n strict: boolean = true\n\n constructor(data: object, options?: Options) {\n this.data = data\n this.strict = options?.strict\n }\n\n replaceValue(key: string, value: any, data: object, template: object): void {\n let matchedArray = String(value).match(/{{.*?}}/g) // extract only {{}} pattern\n\n if (matchedArray) {\n for (let item of matchedArray.reverse()) {\n let patternKey = item.replace(/[{}]/g, '')\n let spotting = get(template, key)\n\n let valueFromData = get(data, patternKey)\n let newValue = valueFromData\n\n if (\n ['undefined', undefined, null, 'string'].includes(\n typeof valueFromData\n )\n ) {\n if (this.strict) {\n newValue = spotting.replace(item, valueFromData)\n } else {\n newValue = spotting.replace(item, valueFromData || '')\n }\n }\n\n if (['number'].includes(typeof valueFromData)) {\n newValue = spotting.replace(item, valueFromData)\n }\n\n set(template, key, newValue)\n }\n }\n }\n\n recursiveReplace(data: object, template: any): object {\n for (let key of Object.keys(template)) {\n let value = template[key]\n\n if (isPlainObject(value)) {\n this.recursiveReplace(data, value)\n } else if (isArray(value)) {\n value.map((item) => this.recursiveReplace(data, item))\n } else {\n this.replaceValue(key, value, data, template)\n }\n }\n return template\n }\n\n try(jsonToParse: string): object {\n let template = JSON.parse(jsonToParse)\n this.recursiveReplace(this.data, template)\n\n return template\n }\n}\n\nexport default DryReplacer\n"],"names":["constructor","data","options","strict","this","replaceValue","key","value","template","matchedArray","String","match","item","reverse","patternKey","replace","spotting","get","valueFromData","newValue","undefined","includes","set","recursiveReplace","Object","keys","isPlainObject","isArray","map","try","jsonToParse","JSON","parse"],"mappings":"sHASA,QAIEA,YAAYC,EAAcC,GAH1BD,KAAAA,iBACAE,QAAkB,EAGhBC,KAAKH,KAAOA,EACZG,KAAKD,aAASD,SAAAA,EAASC,MACzB,CAEAE,aAAaC,EAAaC,EAAYN,EAAcO,GAClD,IAAgBC,EAAGC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAK,IAAQG,OAAiBC,UAAW,CACvC,IAAIC,EAAaF,EAAKG,QAAQ,QAAS,IACnCC,EAAWC,EAAIT,EAAUF,GAEzBY,EAAgBD,EAAIhB,EAAMa,GAClBK,EAAGD,EAGb,CAAC,iBAAaE,EAAW,KAAM,UAAUC,qBAKvCF,EAAWH,EAASD,QAAQH,EAD1BR,KAAKD,OAC2Be,EAEAA,GAAiB,KAInD,CAAC,UAAUG,gBAAgBH,KAC7BC,EAAWH,EAASD,QAAQH,EAAMM,IAGpCI,EAAId,EAAUF,EAAKa,EACpB,CAEL,CAEAI,iBAAiBtB,EAAcO,GAC7B,IAAK,SAAWgB,OAAOC,KAAKjB,GAAW,CACrC,IAASD,EAAGC,EAASF,GAEjBoB,EAAcnB,GAChBH,KAAKmB,iBAAiBtB,EAAMM,GACnBoB,EAAQpB,GACjBA,EAAMqB,IAAKhB,GAASR,KAAKmB,iBAAiBtB,EAAMW,IAEhDR,KAAKC,aAAaC,EAAKC,EAAON,EAAMO,EAEvC,CACD,QACF,CAEAqB,IAAIC,GACF,MAAeC,KAAKC,MAAMF,GAG1B,OAFA1B,KAAKmB,iBAAiBnB,KAAKH,KAAMO,IAGnC"} \ No newline at end of file +{"version":3,"file":"index.modern.mjs","sources":["../src/index.ts"],"sourcesContent":["import get from 'lodash.get'\nimport set from 'lodash.set'\nimport isArray from 'lodash.isarray'\nimport isPlainObject from 'lodash.isplainobject'\n\ntype Options = {\n strict?: boolean\n keepTypeof?: boolean\n}\n\nclass DryReplacer {\n data: object\n strict?: boolean = true\n keepTypeof?: boolean = false\n\n constructor(data: object, options?: Options) {\n this.data = data\n this.strict = options?.strict\n this.keepTypeof = options?.keepTypeof\n }\n\n replaceValue(key: string, value: any, data: object, template: object): void {\n let matchedArray = String(value).match(/{{.*?}}/g) // extract only {{}} pattern\n\n if (matchedArray) {\n for (let item of matchedArray.reverse()) {\n let patternKey = item.replace(/[{}]/g, '')\n let spotting = get(template, key)\n\n let valueFromData = get(data, patternKey)\n let newValue = valueFromData\n\n const isStringOrFalsy = [\n 'undefined',\n undefined,\n null,\n 'string',\n ].includes(typeof valueFromData)\n\n if (isStringOrFalsy) {\n if (this.strict) {\n newValue = spotting.replace(item, valueFromData)\n } else {\n newValue = spotting.replace(item, valueFromData || '')\n }\n }\n\n if (['number'].includes(typeof valueFromData)) {\n newValue = spotting.replace(item, valueFromData)\n\n if (this.keepTypeof && /^-?\\.?_?\\d+$/.test(newValue)) {\n newValue = Number(newValue)\n }\n }\n\n set(template, key, newValue)\n }\n }\n }\n\n recursiveReplace(data: object, template: any): object {\n for (let key of Object.keys(template)) {\n let value = template[key]\n\n if (isPlainObject(value)) {\n this.recursiveReplace(data, value)\n } else if (isArray(value)) {\n value.map((item) => this.recursiveReplace(data, item))\n } else {\n this.replaceValue(key, value, data, template)\n }\n }\n return template\n }\n\n try(jsonToParse: string): object {\n let template = JSON.parse(jsonToParse)\n this.recursiveReplace(this.data, template)\n\n return template\n }\n}\n\nexport default DryReplacer\n"],"names":["DryReplacer","constructor","data","options","this","strict","keepTypeof","replaceValue","key","value","template","matchedArray","String","match","item","reverse","patternKey","replace","get","valueFromData","newValue","undefined","includes","spotting","test","Number","set","recursiveReplace","keys","isPlainObject","isArray","map","try","jsonToParse","JSON","parse"],"mappings":"sHAUA,MAAMA,EAKJC,YAAYC,EAAcC,GAAiBC,KAJ3CF,UACAG,EAAAA,KAAAA,QAAmB,EAAID,KACvBE,YAAuB,EAGrBF,KAAKF,KAAOA,EACZE,KAAKC,OAASF,MAAAA,OAAAA,EAAAA,EAASE,OACvBD,KAAKE,WAAoB,MAAPH,OAAO,EAAPA,EAASG,UAC7B,CAEAC,aAAaC,EAAaC,EAAYP,EAAcQ,GAClD,IAAgBC,EAAGC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAK,IAAIG,KAAoBH,EAACI,UAAW,CACvC,IAAIC,EAAaF,EAAKG,QAAQ,QAAS,MACxBC,EAAIR,EAAUF,GAEZW,EAAGD,EAAIhB,EAAMc,GAC1BI,EAAWD,EAES,CACtB,iBACAE,EACA,KACA,UACAC,gBAAgBH,KAIdC,EAAWG,EAASN,QAAQH,EAD1BV,KAAKC,OAC2Bc,EAEAA,GAAiB,KAInD,CAAC,UAAUG,qBACbF,EAAWG,EAASN,QAAQH,EAAMK,GAE9Bf,KAAKE,YAAc,eAAekB,KAAKJ,KACzCA,EAAWK,OAAOL,KAItBM,EAAIhB,EAAUF,EAAKY,EACpB,CAEL,CAEAO,iBAAiBzB,EAAcQ,GAC7B,IAAK,IAAOF,YAAWoB,KAAKlB,GAAW,CACrC,MAAYA,EAASF,GAEjBqB,EAAcpB,GAChBL,KAAKuB,iBAAiBzB,EAAMO,GACnBqB,EAAQrB,GACjBA,EAAMsB,IAAKjB,GAASV,KAAKuB,iBAAiBzB,EAAMY,IAEhDV,KAAKG,aAAaC,EAAKC,EAAOP,EAAMQ,EAEvC,CACD,OACFA,CAAA,CAEAsB,IAAIC,GACF,MAAeC,KAAKC,MAAMF,GAG1B,OAFA7B,KAAKuB,iBAAiBvB,KAAKF,KAAMQ,IAGnC"} \ No newline at end of file diff --git a/dist/index.umd.js b/dist/index.umd.js index aa204d7..7df03dd 100644 --- a/dist/index.umd.js +++ b/dist/index.umd.js @@ -1,3 +1,3 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("lodash.get"),require("lodash.set"),require("lodash.isarray"),require("lodash.isplainobject")):"function"==typeof define&&define.amd?define(["lodash.get","lodash.set","lodash.isarray","lodash.isplainobject"],t):(e||self).dryReplacerJs=t(e.get,e.set,e.isArray,e.isPlainObject)}(this,function(e,t,r,n){function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=/*#__PURE__*/a(e),o=/*#__PURE__*/a(t),l=/*#__PURE__*/a(r),u=/*#__PURE__*/a(n);function s(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r=e.length?{done:!0}:{done:!1,value:e[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(a.reverse());!(l=u()).done;){var c=l.value,d=c.replace(/[{}]/g,""),f=i.default(n,e),p=i.default(r,d),h=p;["undefined",void 0,null,"string"].includes(typeof p)&&(h=f.replace(c,this.strict?p:p||"")),["number"].includes(typeof p)&&(h=f.replace(c,p)),o.default(n,e,h)}},t.recursiveReplace=function(e,t){for(var r=this,n=0,a=Object.keys(t);ne.length)&&(t=e.length);for(var r=0,n=new Array(t);r=e.length?{done:!0}:{done:!1,value:e[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(i.reverse());!(l=u()).done;){var c=l.value,f=c.replace(/[{}]/g,""),d=a.default(n,e),p=a.default(r,f),h=p;["undefined",void 0,null,"string"].includes(typeof p)&&(h=d.replace(c,this.strict?p:p||"")),["number"].includes(typeof p)&&(h=d.replace(c,p),this.keepTypeof&&/^-?\.?_?\d+$/.test(h)&&(h=Number(h))),o.default(n,e,h)}},t.recursiveReplace=function(e,t){for(var r=this,n=0,i=Object.keys(t);n this.recursiveReplace(data, item))\n } else {\n this.replaceValue(key, value, data, template)\n }\n }\n return template\n }\n\n try(jsonToParse: string): object {\n let template = JSON.parse(jsonToParse)\n this.recursiveReplace(this.data, template)\n\n return template\n }\n}\n\nexport default DryReplacer\n"],"names":["DryReplacer","data","options","this","strict","_proto","prototype","replaceValue","key","value","template","matchedArray","String","match","_step","_iterator","_createForOfIteratorHelperLoose","reverse","done","item","patternKey","replace","spotting","get","valueFromData","newValue","undefined","includes","set","recursiveReplace","Object","keys","_i","_Object$keys","length","isPlainObject","isArray","map","_this","jsonToParse","JSON","parse"],"mappings":";OAaE,WAAA,SAAAA,EAAYC,EAAcC,GAAiBC,KAH3CF,UAAI,EAAAE,KACJC,QAAkB,EAGhBD,KAAKF,KAAOA,EACZE,KAAKC,OAAgB,MAAPF,OAAO,EAAPA,EAASE,MACzB,CAAC,IAAAC,EAAAL,EAAAM,UAsDA,OAtDAD,EAEDE,aAAA,SAAaC,EAAaC,EAAYR,EAAcS,GAClD,IAAIC,EAAeC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAA,IAAuCG,EAAvCC,2qBAAAC,CAAiBL,EAAaM,aAASH,EAAAC,KAAAG,MAAE,CAAhCC,IAAAA,UACHC,EAAaD,EAAKE,QAAQ,QAAS,IAC3BC,EAAGC,EAAAA,QAAIb,EAAUF,GAEZgB,EAAGD,UAAItB,EAAMmB,GAC1BK,EAAWD,EAGb,CAAC,iBAAaE,EAAW,KAAM,UAAUC,gBAChCH,KAIPC,EAAWH,EAASD,QAAQF,EAD1BhB,KAAKC,OAC2BoB,EAEAA,GAAiB,KAInD,CAAC,UAAUG,gBAAgBH,KAC7BC,EAAWH,EAASD,QAAQF,EAAMK,IAGpCI,UAAIlB,EAAUF,EAAKiB,EACpB,CAEL,EAACpB,EAEDwB,iBAAA,SAAiB5B,EAAcS,GAC7B,eAAgBoB,EAAAA,EAAAA,EAAAA,OAAOC,KAAKrB,GAASsB,EAAAC,EAAAC,OAAAF,IAAE,CAAlC,MACHC,EAAAD,GAASvB,EAAGC,EAASF,GAEjB2B,EAAa,QAAC1B,GAChBN,KAAK0B,iBAAiB5B,EAAMQ,GACnB2B,UAAQ3B,GACjBA,EAAM4B,IAAI,SAAClB,GAAS,OAAAmB,EAAKT,iBAAiB5B,EAAMkB,EAAK,GAErDhB,KAAKI,aAAaC,EAAKC,EAAOR,EAAMS,EAEvC,CACD,QACF,EAACL,EAAA,IAED,SAAIkC,GACF,MAAeC,KAAKC,MAAMF,GAG1B,OAFApC,KAAK0B,iBAAiB1B,KAAKF,KAAMS,IAGnC,EAACV,CAAA,CAzDD"} \ No newline at end of file +{"version":3,"file":"index.umd.js","sources":["../src/index.ts"],"sourcesContent":["import get from 'lodash.get'\nimport set from 'lodash.set'\nimport isArray from 'lodash.isarray'\nimport isPlainObject from 'lodash.isplainobject'\n\ntype Options = {\n strict?: boolean\n keepTypeof?: boolean\n}\n\nclass DryReplacer {\n data: object\n strict?: boolean = true\n keepTypeof?: boolean = false\n\n constructor(data: object, options?: Options) {\n this.data = data\n this.strict = options?.strict\n this.keepTypeof = options?.keepTypeof\n }\n\n replaceValue(key: string, value: any, data: object, template: object): void {\n let matchedArray = String(value).match(/{{.*?}}/g) // extract only {{}} pattern\n\n if (matchedArray) {\n for (let item of matchedArray.reverse()) {\n let patternKey = item.replace(/[{}]/g, '')\n let spotting = get(template, key)\n\n let valueFromData = get(data, patternKey)\n let newValue = valueFromData\n\n const isStringOrFalsy = [\n 'undefined',\n undefined,\n null,\n 'string',\n ].includes(typeof valueFromData)\n\n if (isStringOrFalsy) {\n if (this.strict) {\n newValue = spotting.replace(item, valueFromData)\n } else {\n newValue = spotting.replace(item, valueFromData || '')\n }\n }\n\n if (['number'].includes(typeof valueFromData)) {\n newValue = spotting.replace(item, valueFromData)\n\n if (this.keepTypeof && /^-?\\.?_?\\d+$/.test(newValue)) {\n newValue = Number(newValue)\n }\n }\n\n set(template, key, newValue)\n }\n }\n }\n\n recursiveReplace(data: object, template: any): object {\n for (let key of Object.keys(template)) {\n let value = template[key]\n\n if (isPlainObject(value)) {\n this.recursiveReplace(data, value)\n } else if (isArray(value)) {\n value.map((item) => this.recursiveReplace(data, item))\n } else {\n this.replaceValue(key, value, data, template)\n }\n }\n return template\n }\n\n try(jsonToParse: string): object {\n let template = JSON.parse(jsonToParse)\n this.recursiveReplace(this.data, template)\n\n return template\n }\n}\n\nexport default DryReplacer\n"],"names":["DryReplacer","data","options","strict","keepTypeof","this","replaceValue","key","value","template","matchedArray","String","match","reverse","item","patternKey","replace","spotting","get","newValue","valueFromData","undefined","includes","test","Number","set","_proto","recursiveReplace","_this","_i","_Object$keys","Object","keys","isPlainObject","isArray","map","jsonToParse","JSON","parse"],"mappings":";OAeE,WAAA,SAAAA,EAAYC,EAAcC,GAJ1BD,KAAAA,iBACAE,QAAmB,EACnBC,KAAAA,YAAuB,EAGrBC,KAAKJ,KAAOA,EACZI,KAAKF,aAASD,SAAAA,EAASC,OACvBE,KAAKD,WAAaF,MAAAA,OAAAA,EAAAA,EAASE,UAC7B,CAAC,kBA6DA,SA3DDE,aAAA,SAAaC,EAAaC,EAAYP,EAAcQ,GAClD,IAAIC,EAAeC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAiBA,MAAAA,2qBAAAA,CAAAA,EAAaG,0BAAW,CAAhCC,IAAAA,UACHC,EAAaD,EAAKE,QAAQ,QAAS,IACnCC,EAAWC,EAAG,QAACT,EAAUF,KAETW,EAAAA,QAAIjB,EAAMc,GAClBI,EAAGC,EAES,CACtB,iBACAC,EACA,KACA,UACAC,gBAA6BF,KAI3BD,EAAWF,EAASD,QAAQF,EAD1BT,KAAKF,OAC2BiB,EAEAA,GAAiB,KAInD,CAAC,UAAUE,gBAAgBF,KAC7BD,EAAWF,EAASD,QAAQF,EAAMM,GAE9Bf,KAAKD,YAAc,eAAemB,KAAKJ,KACzCA,EAAWK,OAAOL,KAItBM,EAAG,QAAChB,EAAUF,EAAKY,EACpB,CAEL,EAACO,EAEDC,iBAAA,SAAiB1B,EAAcQ,GAC7B,IAAA,IAAAmB,EAAAvB,KAAAwB,EAAA,EAAAC,EAAgBC,OAAOC,KAAKvB,kBAAW,CAAlC,MACHqB,EAAAD,KAAYpB,EAASF,GAEjB0B,EAAa,QAACzB,GAChBH,KAAKsB,iBAAiB1B,EAAMO,GACnB0B,EAAAA,QAAQ1B,GACjBA,EAAM2B,IAAI,SAACrB,GAAI,SAAUa,iBAAiB1B,EAAMa,EAAK,GAErDT,KAAKC,aAAaC,EAAKC,EAAOP,EAAMQ,EAEvC,CACD,OACFA,CAAA,QAEA,SAAI2B,GACF,MAAeC,KAAKC,MAAMF,GAG1B,OAFA/B,KAAKsB,iBAAiBtB,KAAKJ,KAAMQ,IAGnC,EAACT,CAAA,CAjED"} \ No newline at end of file diff --git a/spec/replacer.spec.ts b/spec/replacer.spec.ts index f380b4e..beea7f7 100644 --- a/spec/replacer.spec.ts +++ b/spec/replacer.spec.ts @@ -352,5 +352,43 @@ describe('DryReplacer', () => { action: 'O numero é 10 com valor depois', }) }) + + it('should be return value if event numeric', () => { + const webhook = { + variables: { + action: 10, + }, + } + + let template = { + action: '{{variables.action}}', + } + + const replacer = new dryreplacer(webhook, { keepTypeof: true }) + let result = replacer.try(JSON.stringify(template)) + + expect(result).toMatchObject({ + action: 10, + }) + }) + + it('should be return value if event numeric', () => { + const webhook = { + variables: { + request_id: -110, + }, + } + + let template = { + action: '{{variables.request_id}}', + } + + const replacer = new dryreplacer(webhook, { keepTypeof: true }) + let result = replacer.try(JSON.stringify(template)) + + expect(result).toMatchObject({ + action: -110, + }) + }) }) }) diff --git a/src/index.ts b/src/index.ts index b98c89e..d762df6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,15 +5,18 @@ import isPlainObject from 'lodash.isplainobject' type Options = { strict?: boolean + keepTypeof?: boolean } class DryReplacer { data: object strict?: boolean = true + keepTypeof?: boolean = false constructor(data: object, options?: Options) { this.data = data this.strict = options?.strict + this.keepTypeof = options?.keepTypeof } replaceValue(key: string, value: any, data: object, template: object): void { @@ -44,6 +47,10 @@ class DryReplacer { if (['number'].includes(typeof valueFromData)) { newValue = spotting.replace(item, valueFromData) + + if (this.keepTypeof && /^-?\.?_?\d+$/.test(newValue)) { + newValue = Number(newValue) + } } set(template, key, newValue)