From 2b856851a27a4269c36b8bcc7edcc6c4eb544550 Mon Sep 17 00:00:00 2001 From: thadeu Date: Wed, 27 Sep 2023 19:44:34 +0000 Subject: [PATCH] added support to strict newValue --- README.md | 8 +++++++- dist/index.d.ts | 6 +++++- dist/index.js | 2 +- dist/index.js.map | 2 +- dist/index.mjs | 2 +- dist/index.mjs.map | 2 +- dist/index.modern.mjs | 2 +- dist/index.modern.mjs.map | 2 +- dist/index.umd.js | 4 ++-- dist/index.umd.js.map | 2 +- spec/replacer.spec.ts | 24 ++++++++++++++++++++++++ src/index.ts | 19 +++++++++++++++---- 12 files changed, 60 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 84e938a..69cfb7e 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,12 @@ or $ npm i dry-replacer-js@latest ``` +or directly from Github + +```bash +$ pnpm i thadeu/dry-replacer-js +``` + ## Usage ```js @@ -76,7 +82,7 @@ let template = { code: 'fixedvalue', } -const replacer = new dryreplacer(data) +const replacer = new dryreplacer(data, { strict: true }) let result = replacer.try(JSON.stringify(template, null, 4)) ``` diff --git a/dist/index.d.ts b/dist/index.d.ts index a433527..d29b64b 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,6 +1,10 @@ +type Options = { + strict?: boolean; +}; declare class DryReplacer { data: object; - constructor(data: object); + strict: boolean; + constructor(data: object, options?: Options); replaceValue(key: string, value: any, data: object, template: object): void; recursiveReplace(data: object, template: any): object; try(jsonToParse: string): object; diff --git a/dist/index.js b/dist/index.js index 4057071..ef63cd3 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"),a=require("lodash.isplainobject");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=/*#__PURE__*/n(e),i=/*#__PURE__*/n(r),u=/*#__PURE__*/n(t),l=/*#__PURE__*/n(a);function c(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,a=new Array(r);t=e.length?{done:!0}:{done:!1,value:e[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(n.reverse());!(u=l()).done;){var s=u.value,f=s.replace(/[{}]/g,""),d=o.default(a,e),p=o.default(t,f),v=p;["string"].includes(typeof p)&&(v=d.replace(s,p)),i.default(a,e,v)}},r.recursiveReplace=function(e,r){for(var t=this,a=0,n=Object.keys(r);ae.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),v=i.default(t,f),p=v;["undefined",void 0,null,"string"].includes(typeof v)&&(p=d.replace(s,this.strict?v:v||"")),o.default(n,e,p)}},r.recursiveReplace=function(e,r){for(var t=this,n=0,a=Object.keys(r);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":["data","this","_proto","DryReplacer","prototype","replaceValue","key","value","template","matchedArray","String","match","_step","_iterator","_createForOfIteratorHelperLoose","reverse","done","item","patternKey","replace","spotting","get","valueFromData","newValue","includes","set","recursiveReplace","_this","_i","_Object$keys","Object","keys","length","isPlainObject","isArray","map","jsonToParse","JSON","parse"],"mappings":"oZAKiB,WAGf,SAAYA,EAAAA,GAAYC,KAFxBD,UAAI,EAGFC,KAAKD,KAAOA,CACd,CAAC,IAAAE,EAAAC,EAAAC,UA0CA,OA1CAF,EAEDG,aAAA,SAAaC,EAAaC,EAAYP,EAAcQ,GAClD,IAAgBC,EAAGC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAA,IAAyCG,EAAzCC,2qBAAAC,CAAiBL,EAAaM,aAAWH,EAAAC,KAAAG,MAAA,CAAA,IAA5BC,EAAAL,EAAAL,MACPW,EAAaD,EAAKE,QAAQ,QAAS,IACnCC,EAAWC,EAAAA,QAAIb,EAAUF,GAEZgB,EAAGD,EAAG,QAACrB,EAAMkB,GAClBK,EAAGD,EAEX,CAAC,UAAUE,gBAAgBF,KAC7BC,EAAWH,EAASD,QAAQF,EAAMK,IAGpCG,UAAIjB,EAAUF,EAAKiB,EACpB,CAEL,EAEAG,EAAAA,iBAAA,SAAiB1B,EAAcQ,GAC7B,IAAA,IAAAmB,EAAA1B,KAAA2B,EAAA,EAAAC,EAAgBC,OAAOC,KAAKvB,GAAWoB,EAAAC,EAAAG,OAAAJ,IAAA,CAAlC,IAAOtB,EAAAuB,EAAAD,GACNrB,EAAQC,EAASF,GAEjB2B,EAAAA,QAAc1B,GAChBN,KAAKyB,iBAAiB1B,EAAMO,GACnB2B,UAAQ3B,GACjBA,EAAM4B,IAAI,SAAClB,GAAS,OAAAU,EAAKD,iBAAiB1B,EAAMiB,EAAK,GAErDhB,KAAKI,aAAaC,EAAKC,EAAOP,EAAMQ,EAEvC,CACD,OAAOA,CACT,EAACN,EAAA,IAED,SAAIkC,GACF,IAAI5B,EAAW6B,KAAKC,MAAMF,GAG1B,OAFAnC,KAAKyB,iBAAiBzB,KAAKD,KAAMQ,GAE1BA,CACT,EAACL,CAAA,CA/Cc"} \ 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}\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 (['undefined', undefined, null, 'string'].includes(typeof valueFromData)) {\n if (this.strict) {\n newValue = spotting.replace(item, valueFromData)\n } else {\n newValue = spotting.replace(item, valueFromData || '')\n }\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":["data","options","strict","this","_proto","DryReplacer","prototype","replaceValue","key","value","template","matchedArray","String","match","_step","reverse","_iterator","done","patternKey","item","replace","get","newValue","valueFromData","undefined","includes","spotting","set","recursiveReplace","_this","Object","keys","_Object$keys","_i","isPlainObject","isArray","map","jsonToParse","JSON","parse"],"mappings":"oZASiB,WAIf,SAAYA,EAAAA,EAAcC,GAH1BD,KAAAA,iBACAE,QAAkB,EAGhBC,KAAKH,KAAOA,EACZG,KAAKD,OAAgB,MAAPD,OAAO,EAAPA,EAASC,MACzB,CAAC,IAAAE,EAAAC,EAAAC,UA+CA,OA/CAF,EAEDG,aAAA,SAAaC,EAAaC,EAAYT,EAAcU,GAClD,IAAgBC,EAAGC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAiBA,IAAwBG,EAAxBH,2qBAAAA,CAAAA,EAAaI,aAAWD,EAAAE,KAAAC,MAAA,CAAA,MACvCH,EAAAL,MAAcS,EAAGC,EAAKC,QAAQ,QAAS,MACxBC,EAAG,QAACX,EAAUF,KAETa,EAAAA,QAAIrB,EAAMkB,GAClBI,EAAGC,EAEX,CAAC,iBAAaC,EAAW,KAAM,UAAUC,gBAAgBF,KAEzDD,EAAWI,EAASN,QAAQD,EAD1BhB,KAAKD,OAC2BqB,EAEAA,GAAiB,KAKvDI,UAAIjB,EAAUF,EAAKc,EACpB,CAEL,EAEAM,EAAAA,iBAAA,SAAiB5B,EAAcU,GAC7B,IAD0C,IAAAmB,EAAA1B,KAC1B2B,EAAAA,EAAAA,EAAAA,OAAOC,KAAKrB,kBAAW,CAAlC,IAAOF,EAAAwB,EAAAC,GACDxB,EAAGC,EAASF,GAEjB0B,EAAAA,QAAczB,GAChBN,KAAKyB,iBAAiB5B,EAAMS,GACnB0B,EAAO,QAAC1B,GACjBA,EAAM2B,IAAI,SAACjB,GAAI,SAAUS,iBAAiB5B,EAAMmB,EAAK,GAErDhB,KAAKI,aAAaC,EAAKC,EAAOT,EAAMU,EAEvC,CACD,OACFA,CAAA,QAEA,SAAI2B,GACF,IAAY3B,EAAG4B,KAAKC,MAAMF,GAG1B,OAFAlC,KAAKyB,iBAAiBzB,KAAKH,KAAMU,GAE1BA,CACT,EAACL,CAAA,CAtDc"} \ No newline at end of file diff --git a/dist/index.mjs b/dist/index.mjs index f7d591f..ddbea13 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -1,2 +1,2 @@ -import r from"lodash.get";import e from"lodash.set";import t from"lodash.isarray";import a from"lodash.isplainobject";function n(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,a=new Array(e);t=r.length?{done:!0}:{done:!1,value:r[a++]}}}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=u()).done;){var s=l.value,f=s.replace(/[{}]/g,""),p=r(i,t),v=r(o,f),d=v;["string"].includes(typeof v)&&(d=p.replace(s,v)),e(i,t,d)}},i.recursiveReplace=function(r,e){for(var n=this,o=0,i=Object.keys(e);or.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||"")),t(o,e,v)}},o.recursiveReplace=function(r,t){for(var a=this,i=0,o=Object.keys(t);i 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","this","_proto","prototype","replaceValue","key","value","template","matchedArray","String","match","_step","_iterator","_createForOfIteratorHelperLoose","reverse","done","item","patternKey","replace","spotting","get","valueFromData","newValue","includes","set","recursiveReplace","_this","_i","_Object$keys","Object","keys","length","isPlainObject","isArray","map","jsonToParse","JSON","parse"],"mappings":"gOAKMA,IAAWA,eAAA,WAGf,SAAYC,EAAAA,GAAYC,KAFxBD,UAAI,EAGFC,KAAKD,KAAOA,CACd,CAAC,IAAAE,EAAAH,EAAAI,UA0CA,OA1CAD,EAEDE,aAAA,SAAaC,EAAaC,EAAYN,EAAcO,GAClD,IAAgBC,EAAGC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAA,IAAyCG,EAAzCC,2qBAAAC,CAAiBL,EAAaM,aAAWH,EAAAC,KAAAG,MAAA,CAAA,IAA5BC,EAAAL,EAAAL,MACPW,EAAaD,EAAKE,QAAQ,QAAS,IACnCC,EAAWC,EAAIb,EAAUF,GAEZgB,EAAGD,EAAIpB,EAAMiB,GAClBK,EAAGD,EAEX,CAAC,UAAUE,gBAAgBF,KAC7BC,EAAWH,EAASD,QAAQF,EAAMK,IAGpCG,EAAIjB,EAAUF,EAAKiB,EACpB,CAEL,EAEAG,EAAAA,iBAAA,SAAiBzB,EAAcO,GAC7B,IAAA,IAAAmB,EAAAzB,KAAA0B,EAAA,EAAAC,EAAgBC,OAAOC,KAAKvB,GAAWoB,EAAAC,EAAAG,OAAAJ,IAAA,CAAlC,IAAOtB,EAAAuB,EAAAD,GACNrB,EAAQC,EAASF,GAEjB2B,EAAc1B,GAChBL,KAAKwB,iBAAiBzB,EAAMM,GACnB2B,EAAQ3B,GACjBA,EAAM4B,IAAI,SAAClB,GAAS,OAAAU,EAAKD,iBAAiBzB,EAAMgB,EAAK,GAErDf,KAAKG,aAAaC,EAAKC,EAAON,EAAMO,EAEvC,CACD,OAAOA,CACT,EAACL,EAAA,IAED,SAAIiC,GACF,IAAI5B,EAAW6B,KAAKC,MAAMF,GAG1B,OAFAlC,KAAKwB,iBAAiBxB,KAAKD,KAAMO,GAE1BA,CACT,EAACR,CAAA,CA/Cc"} \ 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}\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 (['undefined', undefined, null, 'string'].includes(typeof valueFromData)) {\n if (this.strict) {\n newValue = spotting.replace(item, valueFromData)\n } else {\n newValue = spotting.replace(item, valueFromData || '')\n }\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","this","_proto","prototype","replaceValue","key","value","template","matchedArray","String","match","_step","reverse","_iterator","done","patternKey","item","replace","get","newValue","valueFromData","undefined","includes","spotting","set","recursiveReplace","_this","Object","keys","_Object$keys","_i","isPlainObject","isArray","map","jsonToParse","JSON","parse"],"mappings":"gOASMA,IAAWA,eAAA,WAIf,SAAYC,EAAAA,EAAcC,GAH1BD,KAAAA,iBACAE,QAAkB,EAGhBC,KAAKH,KAAOA,EACZG,KAAKD,OAAgB,MAAPD,OAAO,EAAPA,EAASC,MACzB,CAAC,IAAAE,EAAAL,EAAAM,UA+CA,OA/CAD,EAEDE,aAAA,SAAaC,EAAaC,EAAYR,EAAcS,GAClD,IAAgBC,EAAGC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAiBA,IAAwBG,EAAxBH,2qBAAAA,CAAAA,EAAaI,aAAWD,EAAAE,KAAAC,MAAA,CAAA,MACvCH,EAAAL,MAAcS,EAAGC,EAAKC,QAAQ,QAAS,MACxBC,EAAIX,EAAUF,KAETa,EAAIpB,EAAMiB,GAClBI,EAAGC,EAEX,CAAC,iBAAaC,EAAW,KAAM,UAAUC,gBAAgBF,KAEzDD,EAAWI,EAASN,QAAQD,EAD1Bf,KAAKD,OAC2BoB,EAEAA,GAAiB,KAKvDI,EAAIjB,EAAUF,EAAKc,EACpB,CAEL,EAEAM,EAAAA,iBAAA,SAAiB3B,EAAcS,GAC7B,IAD0C,IAAAmB,EAAAzB,KAC1B0B,EAAAA,EAAAA,EAAAA,OAAOC,KAAKrB,kBAAW,CAAlC,IAAOF,EAAAwB,EAAAC,GACDxB,EAAGC,EAASF,GAEjB0B,EAAczB,GAChBL,KAAKwB,iBAAiB3B,EAAMQ,GACnB0B,EAAQ1B,GACjBA,EAAM2B,IAAI,SAACjB,GAAI,SAAUS,iBAAiB3B,EAAMkB,EAAK,GAErDf,KAAKG,aAAaC,EAAKC,EAAOR,EAAMS,EAEvC,CACD,OACFA,CAAA,QAEA,SAAI2B,GACF,IAAY3B,EAAG4B,KAAKC,MAAMF,GAG1B,OAFAjC,KAAKwB,iBAAiBxB,KAAKH,KAAMS,GAE1BA,CACT,EAACV,CAAA,CAtDc"} \ No newline at end of file diff --git a/dist/index.modern.mjs b/dist/index.modern.mjs index aa60473..6a2f0a3 100644 --- a/dist/index.modern.mjs +++ b/dist/index.modern.mjs @@ -1,2 +1,2 @@ -import e from"lodash.get";import r from"lodash.set";import t from"lodash.isarray";import a from"lodash.isplainobject";class s{constructor(e){this.data=void 0,this.data=e}replaceValue(t,a,s,l){let i=String(a).match(/{{.*?}}/g);if(i)for(let a of i.reverse()){let i=a.replace(/[{}]/g,""),o=e(l,t),c=e(s,i),p=c;["string"].includes(typeof c)&&(p=o.replace(a,c)),r(l,t,p)}}recursiveReplace(e,r){for(let s of Object.keys(r)){let l=r[s];a(l)?this.recursiveReplace(e,l):t(l)?l.map(r=>this.recursiveReplace(e,r)):this.replaceValue(s,l,e,r)}return r}try(e){let r=JSON.parse(e);return this.recursiveReplace(this.data,r),r}}export{s 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.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,""),o=e(a,r),c=e(i,l),p=c;["undefined",void 0,null,"string"].includes(typeof c)&&(p=o.replace(s,this.strict?c:c||"")),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}; //# sourceMappingURL=index.modern.mjs.map diff --git a/dist/index.modern.mjs.map b/dist/index.modern.mjs.map index 0aa4fb2..5476dc4 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\nclass DryReplacer {\n data: object\n\n constructor(data: object) {\n this.data = data\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 (['string'].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":["DryReplacer","constructor","data","this","replaceValue","key","value","template","matchedArray","String","match","item","reverse","patternKey","replace","spotting","get","valueFromData","newValue","includes","set","recursiveReplace","Object","keys","isPlainObject","isArray","map","try","jsonToParse","JSON","parse"],"mappings":"sHAKA,MAAiBA,EAGfC,YAAYC,GAFZA,KAAAA,YAGEC,KAAKD,KAAOA,CACd,CAEAE,aAAaC,EAAaC,EAAYJ,EAAcK,GAClD,IAAgBC,EAAGC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAK,IAAIG,KAAQH,EAAaI,UAAW,CACvC,IAAcC,EAAGF,EAAKG,QAAQ,QAAS,IAC3BC,EAAGC,EAAIT,EAAUF,GAEzBY,EAAgBD,EAAId,EAAMW,GAC1BK,EAAWD,EAEX,CAAC,UAAUE,gBAAgBF,KAC7BC,EAAWH,EAASD,QAAQH,EAAMM,IAGpCG,EAAIb,EAAUF,EAAKa,EACpB,CAEL,CAEAG,iBAAiBnB,EAAcK,GAC7B,IAAK,IAAIF,KAAOiB,OAAOC,KAAKhB,GAAW,CACrC,IAAID,EAAQC,EAASF,GAEjBmB,EAAclB,GAChBH,KAAKkB,iBAAiBnB,EAAMI,GACnBmB,EAAQnB,GACjBA,EAAMoB,IAAKf,GAASR,KAAKkB,iBAAiBnB,EAAMS,IAEhDR,KAAKC,aAAaC,EAAKC,EAAOJ,EAAMK,EAEvC,CACD,OAAOA,CACT,CAEAoB,IAAIC,GACF,IAAIrB,EAAWsB,KAAKC,MAAMF,GAG1B,OAFAzB,KAAKkB,iBAAiBlB,KAAKD,KAAMK,GAE1BA,CACT"} \ 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}\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 (['undefined', undefined, null, 'string'].includes(typeof valueFromData)) {\n if (this.strict) {\n newValue = spotting.replace(item, valueFromData)\n } else {\n newValue = spotting.replace(item, valueFromData || '')\n }\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","replaceValue","key","value","template","matchedArray","String","match","reverse","patternKey","item","replace","spotting","get","valueFromData","newValue","undefined","includes","set","recursiveReplace","Object","keys","isPlainObject","isArray","map","try","jsonToParse","JSON","parse"],"mappings":"sHASA,MAAMA,EAIJC,YAAYC,EAAcC,QAH1BD,UAAI,EAAAE,KACJC,QAAkB,EAGhBD,KAAKF,KAAOA,EACZE,KAAKC,OAAgB,MAAPF,OAAO,EAAPA,EAASE,MACzB,CAEAC,aAAaC,EAAaC,EAAYN,EAAcO,GAClD,IAAgBC,EAAGC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAK,SAAYA,EAAaG,UAAW,CACvC,IAAIC,EAAaC,EAAKC,QAAQ,QAAS,IACnCC,EAAWC,EAAIT,EAAUF,GAEzBY,EAAgBD,EAAIhB,EAAMY,GAClBM,EAAGD,EAEX,CAAC,iBAAaE,EAAW,KAAM,UAAUC,gBAA6BH,KAEtEC,EAAWH,EAASD,QAAQD,EAD1BX,KAAKC,OAC2Bc,EAEAA,GAAiB,KAKvDI,EAAId,EAAUF,EAAKa,EACpB,CAEL,CAEAI,iBAAiBtB,EAAcO,GAC7B,IAAK,IAAOF,KAAUkB,OAACC,KAAKjB,GAAW,CACrC,IAAID,EAAQC,EAASF,GAEjBoB,EAAcnB,GAChBJ,KAAKoB,iBAAiBtB,EAAMM,GACnBoB,EAAQpB,GACjBA,EAAMqB,IAAKd,GAASX,KAAKoB,iBAAiBtB,EAAMa,IAEhDX,KAAKE,aAAaC,EAAKC,EAAON,EAAMO,EAEvC,CACD,OACFA,CAAA,CAEAqB,IAAIC,GACF,IAAYtB,EAAGuB,KAAKC,MAAMF,GAG1B,OAFA3B,KAAKoB,iBAAiBpB,KAAKF,KAAMO,GAE1BA,CACT"} \ No newline at end of file diff --git a/dist/index.umd.js b/dist/index.umd.js index ef0bba4..c6cbcd9 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 o=/*#__PURE__*/a(e),i=/*#__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,f=c.replace(/[{}]/g,""),d=o.default(n,e),p=o.default(r,f),h=p;["string"].includes(typeof p)&&(h=d.replace(c,p)),i.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.")}(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||"")),o.default(n,e,h)}},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":["data","this","_proto","DryReplacer","prototype","replaceValue","key","value","template","matchedArray","String","match","_step","_iterator","_createForOfIteratorHelperLoose","reverse","done","item","patternKey","replace","spotting","get","valueFromData","newValue","includes","set","recursiveReplace","_this","_i","_Object$keys","Object","keys","length","isPlainObject","isArray","map","jsonToParse","JSON","parse"],"mappings":";OAKiB,WAGf,SAAYA,EAAAA,GAAYC,KAFxBD,UAAI,EAGFC,KAAKD,KAAOA,CACd,CAAC,IAAAE,EAAAC,EAAAC,UA0CA,OA1CAF,EAEDG,aAAA,SAAaC,EAAaC,EAAYP,EAAcQ,GAClD,IAAgBC,EAAGC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAA,IAAyCG,EAAzCC,2qBAAAC,CAAiBL,EAAaM,aAAWH,EAAAC,KAAAG,MAAA,CAAA,IAA5BC,EAAAL,EAAAL,MACPW,EAAaD,EAAKE,QAAQ,QAAS,IACnCC,EAAWC,EAAAA,QAAIb,EAAUF,GAEZgB,EAAGD,EAAG,QAACrB,EAAMkB,GAClBK,EAAGD,EAEX,CAAC,UAAUE,gBAAgBF,KAC7BC,EAAWH,EAASD,QAAQF,EAAMK,IAGpCG,UAAIjB,EAAUF,EAAKiB,EACpB,CAEL,EAEAG,EAAAA,iBAAA,SAAiB1B,EAAcQ,GAC7B,IAAA,IAAAmB,EAAA1B,KAAA2B,EAAA,EAAAC,EAAgBC,OAAOC,KAAKvB,GAAWoB,EAAAC,EAAAG,OAAAJ,IAAA,CAAlC,IAAOtB,EAAAuB,EAAAD,GACNrB,EAAQC,EAASF,GAEjB2B,EAAAA,QAAc1B,GAChBN,KAAKyB,iBAAiB1B,EAAMO,GACnB2B,UAAQ3B,GACjBA,EAAM4B,IAAI,SAAClB,GAAS,OAAAU,EAAKD,iBAAiB1B,EAAMiB,EAAK,GAErDhB,KAAKI,aAAaC,EAAKC,EAAOP,EAAMQ,EAEvC,CACD,OAAOA,CACT,EAACN,EAAA,IAED,SAAIkC,GACF,IAAI5B,EAAW6B,KAAKC,MAAMF,GAG1B,OAFAnC,KAAKyB,iBAAiBzB,KAAKD,KAAMQ,GAE1BA,CACT,EAACL,CAAA,CA/Cc"} \ 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}\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 (['undefined', undefined, null, 'string'].includes(typeof valueFromData)) {\n if (this.strict) {\n newValue = spotting.replace(item, valueFromData)\n } else {\n newValue = spotting.replace(item, valueFromData || '')\n }\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":["data","options","strict","this","_proto","DryReplacer","prototype","replaceValue","key","value","template","matchedArray","String","match","_step","reverse","_iterator","done","patternKey","item","replace","get","newValue","valueFromData","undefined","includes","spotting","set","recursiveReplace","_this","Object","keys","_Object$keys","_i","isPlainObject","isArray","map","jsonToParse","JSON","parse"],"mappings":";OASiB,WAIf,SAAYA,EAAAA,EAAcC,GAH1BD,KAAAA,iBACAE,QAAkB,EAGhBC,KAAKH,KAAOA,EACZG,KAAKD,OAAgB,MAAPD,OAAO,EAAPA,EAASC,MACzB,CAAC,IAAAE,EAAAC,EAAAC,UA+CA,OA/CAF,EAEDG,aAAA,SAAaC,EAAaC,EAAYT,EAAcU,GAClD,IAAgBC,EAAGC,OAAOH,GAAOI,MAAM,YAEvC,GAAIF,EACF,IAAiBA,IAAwBG,EAAxBH,2qBAAAA,CAAAA,EAAaI,aAAWD,EAAAE,KAAAC,MAAA,CAAA,MACvCH,EAAAL,MAAcS,EAAGC,EAAKC,QAAQ,QAAS,MACxBC,EAAG,QAACX,EAAUF,KAETa,EAAAA,QAAIrB,EAAMkB,GAClBI,EAAGC,EAEX,CAAC,iBAAaC,EAAW,KAAM,UAAUC,gBAAgBF,KAEzDD,EAAWI,EAASN,QAAQD,EAD1BhB,KAAKD,OAC2BqB,EAEAA,GAAiB,KAKvDI,UAAIjB,EAAUF,EAAKc,EACpB,CAEL,EAEAM,EAAAA,iBAAA,SAAiB5B,EAAcU,GAC7B,IAD0C,IAAAmB,EAAA1B,KAC1B2B,EAAAA,EAAAA,EAAAA,OAAOC,KAAKrB,kBAAW,CAAlC,IAAOF,EAAAwB,EAAAC,GACDxB,EAAGC,EAASF,GAEjB0B,EAAAA,QAAczB,GAChBN,KAAKyB,iBAAiB5B,EAAMS,GACnB0B,EAAO,QAAC1B,GACjBA,EAAM2B,IAAI,SAACjB,GAAI,SAAUS,iBAAiB5B,EAAMmB,EAAK,GAErDhB,KAAKI,aAAaC,EAAKC,EAAOT,EAAMU,EAEvC,CACD,OACFA,CAAA,QAEA,SAAI2B,GACF,IAAY3B,EAAG4B,KAAKC,MAAMF,GAG1B,OAFAlC,KAAKyB,iBAAiBzB,KAAKH,KAAMU,GAE1BA,CACT,EAACL,CAAA,CAtDc"} \ No newline at end of file diff --git a/spec/replacer.spec.ts b/spec/replacer.spec.ts index c492677..240a448 100644 --- a/spec/replacer.spec.ts +++ b/spec/replacer.spec.ts @@ -39,6 +39,30 @@ const data = { } describe('DryReplacer', () => { + describe('use case undefined value', () => { + it('case undefined', () => { + let data = { + cliente_nome: 'Thadeu' + } + + let template = { + interaction: { + action: { + prompt: '{{nome}} infelizmente foi negado', + option: '400', + }, + }, + } + + const replacer = new dryreplacer(data, { options: { strict: false }}) + let result: any = replacer.try(JSON.stringify(template)) + + let prompt = result?.interaction?.action?.prompt + + expect(prompt).toBe(' infelizmente foi negado') + }) + }) + describe('root replaces', () => { test('fixed value', () => { let template = { diff --git a/src/index.ts b/src/index.ts index 511d8ac..1395cde 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,11 +3,17 @@ import set from 'lodash.set' import isArray from 'lodash.isarray' import isPlainObject from 'lodash.isplainobject' +type Options = { + strict?: boolean +} + class DryReplacer { data: object + strict: boolean = true - constructor(data: object) { + constructor(data: object, options?: Options) { this.data = data + this.strict = options?.strict } replaceValue(key: string, value: any, data: object, template: object): void { @@ -20,11 +26,16 @@ class DryReplacer { let valueFromData = get(data, patternKey) let newValue = valueFromData - - if (['string'].includes(typeof valueFromData)) { - newValue = spotting.replace(item, valueFromData) + + if (['undefined', undefined, null, 'string'].includes(typeof valueFromData)) { + if (this.strict) { + newValue = spotting.replace(item, valueFromData) + } else { + newValue = spotting.replace(item, valueFromData || '') + } } + set(template, key, newValue) } }