UNPKG

object-flatten-referencing

Version:

Flatten complex nested objects according to a reference objects

11 lines (9 loc) 4.96 kB
/** * @name object-flatten-referencing * @fileoverview Flatten complex nested objects according to a reference objects * @version 7.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/object-flatten-referencing/} */ import C from"rfdc";import{strIndexesOfPlus as S}from"str-indexes-of-plus";import{isMatch as P}from"matcher";import{arrayiffy as y}from"arrayiffy-if-string";import{existy as O,isStr as m,isPlainObject as b}from"codsen-utils";import A from"rfdc";import{isStr as w,isPlainObject as I}from"codsen-utils";var T=A(),W={wrapHeadsWith:"%%_",wrapTailsWith:"_%%",dontWrapKeys:[],dontWrapPaths:[],xhtml:!0,preventDoubleWrapping:!0,preventWrappingIfContains:[],objectKeyAndValueJoinChar:".",wrapGlobalFlipSwitch:!0,ignore:[],whatToDoWhenReferenceIsMissing:0,mergeArraysWithLineBreaks:!0,mergeWithoutTrailingBrIfLineContainsBr:!0,enforceStrictKeyset:!0};function j(g,u){let c={...W,...u};if(arguments.length===0||Object.keys(g).length===0)return[];let i=T(g),n=[];return I(i)&&Object.keys(i).forEach(a=>{I(i[a])&&(i[a]=j(i[a],c)),Array.isArray(i[a])&&(n=n.concat(i[a].map(p=>`${a}${c.objectKeyAndValueJoinChar}${p}`))),w(i[a])&&n.push(`${a}${c.objectKeyAndValueJoinChar}${i[a]}`)}),n}function v(g,u,c=!1,i=!1){let n={...W,...u};if(arguments.length===0||g.length===0)return"";let a=T(g),p="";if(a.length)if(i){for(let e=0,s=a.length;e<s;e++)if(w(a[e])){let l;l="",n.mergeArraysWithLineBreaks&&e>0&&(!n.mergeWithoutTrailingBrIfLineContainsBr||typeof a[e-1]!="string"||n.mergeWithoutTrailingBrIfLineContainsBr&&a[e-1]!==void 0&&!a[e-1].toLowerCase().includes("<br"))&&(l=`<br${n.xhtml?" /":""}>`),p+=`${l}${c?n.wrapHeadsWith:""}${a[e]}${c?n.wrapTailsWith:""}`}else if(Array.isArray(a[e])&&a[e].length&&a[e].every(w)){let l="";n.mergeArraysWithLineBreaks&&p.length&&(l=`<br${n.xhtml?" /":""}>`),p=a[e].reduce((d,t,o,r)=>{let f="";return o!==r.length-1&&(f=" "),d+(o===0?l:"")+(c?n.wrapHeadsWith:"")+t+(c?n.wrapTailsWith:"")+f},p)}}else p=a.reduce((e,s,l,d)=>{let t="";n.mergeArraysWithLineBreaks&&l>0&&(t=`<br${n.xhtml?" /":""}>`);let o="";return l!==d.length-1&&(o=" "),`${e}${l===0?t:""}${c?n.wrapHeadsWith:""}${s}${c?n.wrapTailsWith:""}${o}`},p);return p}var x="7.1.0";var D=C(),Q=x;function X(g,u,c){if(!b(g))throw new Error(`object-flatten-referencing/ofr(): [THROW_ID_01] the first input argument must be a plain object! It was given as ${JSON.stringify(g,null,4)} (its typeof was ${typeof g}).`);if(!b(u))throw new Error(`object-flatten-referencing/ofr(): [THROW_ID_02] the second input argument must be a plain object! It was given as ${JSON.stringify(u,null,4)} (its typeof was ${typeof u}).`);if(c&&!b(c))throw new Error(`object-flatten-referencing/ofr(): [THROW_ID_03] the third input argument must be a plain object! It was given as ${JSON.stringify(c,null,4)} (its typeof was ${typeof c}).`);let i={...W,...c};i.dontWrapKeys=y(i.dontWrapKeys),i.preventWrappingIfContains=y(i.preventWrappingIfContains),i.dontWrapPaths=y(i.dontWrapPaths),i.ignore=y(i.ignore),typeof i.whatToDoWhenReferenceIsMissing!="number"&&(i.whatToDoWhenReferenceIsMissing=+i.whatToDoWhenReferenceIsMissing||0);function n(a,p,e,s=!0,l=!0,d=""){let t=D(a),o=D(p);return e.wrapGlobalFlipSwitch||(s=!1),b(t)?Object.keys(t).forEach(r=>{let f=d+(d.length===0?r:`.${r}`);if(e.ignore.length===0||!e.ignore.includes(r)){if(e.wrapGlobalFlipSwitch&&(s=!0,e.dontWrapKeys.length&&(s=s&&!e.dontWrapKeys.some(h=>P(r,h,{caseSensitive:!0}))),e.dontWrapPaths.length&&(s=s&&!e.dontWrapPaths.some(h=>h===f)),e.preventWrappingIfContains.length&&typeof t[r]=="string"&&(s=s&&!e.preventWrappingIfContains.some(h=>t[r].includes(h)))),O(o[r])||!O(o[r])&&e.whatToDoWhenReferenceIsMissing===2)if(Array.isArray(t[r]))if(e.whatToDoWhenReferenceIsMissing===2||m(o[r]))t[r]=v(t[r],e,s,l);else{if(t[r].every(h=>typeof h=="string"||Array.isArray(h))){let h=!0;t[r].forEach($=>{Array.isArray($)&&!$.every(m)&&(h=!1)}),h&&(l=!1)}t[r]=n(t[r],o[r],e,s,l,f)}else b(t[r])?e.whatToDoWhenReferenceIsMissing===2||m(o[r])?t[r]=v(j(t[r],e),e,s,l):s?t[r]=n(t[r],o[r],e,s,l,f):t[r]=n(t[r],o[r],{...e,wrapGlobalFlipSwitch:!1},s,l,f):m(t[r])&&(t[r]=n(t[r],o[r],e,s,l,f));else if(typeof t[r]!=typeof o[r]&&e.whatToDoWhenReferenceIsMissing===1)throw new Error(`object-flatten-referencing/ofr(): [THROW_ID_06] resolvedReference object does not have the key ${r} and we need it. TIP: Turn off throwing via opts2.whatToDoWhenReferenceIsMissing.`)}}):Array.isArray(t)?Array.isArray(o)?t.forEach((r,f)=>{O(t[f])&&O(o[f])?t[f]=n(t[f],o[f],e,s,l,`${d}[${f}]`):t[f]=n(t[f],o[0],e,s,l,`${d}[${f}]`)}):m(o)&&(t=v(t,e,s,l)):m(t)&&t.length&&(e.wrapHeadsWith||e.wrapTailsWith)&&(!e.preventDoubleWrapping||(e.wrapHeadsWith===""||!S(t,e.wrapHeadsWith.trim()).length)&&(e.wrapTailsWith===""||!S(t,e.wrapTailsWith.trim()).length))&&(t=`${s?e.wrapHeadsWith:""}${t}${s?e.wrapTailsWith:""}`),t}return n(g,u,i)}export{y as arrayiffy,W as defaults,v as flattenArr,j as flattenObject,X as flattenReferencing,Q as version};