@homedev/fields 1.2.26 → 1.2.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +10 -9
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
`).
|
|
5
|
-
`)
|
|
6
|
-
`))
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
Array.prototype.findOrFail=function(r,i,f){let e=this.find(r,f);if(e===void 0)throw Error(i??"Element not found");return e};Array.flat=function(r){if(Array.isArray(r))return r.flat(2);else return[r]};Array.prototype.forEachAsync=async function(r){for(let i=0;i<this.length;i++)await r(this[i],i,this)};Array.prototype.groupBy=function(r){return this.reduce((i,f)=>{let e=r(f);if(!i[e])i[e]=[];return i[e].push(f),i},{})};Array.prototype.createInstances=function(r,...i){return this.map((f,e)=>new r(...i,f,e))};Array.prototype.mapAsync=async function(r){return Promise.all(this.map(r))};Array.prototype.nonNullMap=function(r){let i=[];for(let f=0;f<this.length;f++){let e=r(this[f],f,this);if(e!=null)i.push(e)}return i};Array.prototype.nonNullMapAsync=async function(r){let i=[];for(let f=0;f<this.length;f++){let e=await r(this[f],f,this);if(e!=null)i.push(e)}return i};Array.prototype.mergeAll=function(r){let i={};for(let f of this.toReversed())Object.merge(f,i,r);return i};Array.prototype.toObject=function(r,i){return Object.fromEntries(this.map((f)=>{let e=r(f),t=i?i(f):f;return[e,t]}))};Array.prototype.toObjectWithKey=function(r){return Object.fromEntries(this.map((i)=>[String(i[r]),i]))};Array.prototype.selectFor=function(r,i,f,e){let t=this.find(r,e);if(t===void 0)throw Error(f??"Element not found");return i(t)};Array.prototype.sortBy=function(r,i=!0){return this.slice().sort((f,e)=>{let t=r(f),h=r(e);if(t<h)return i?-1:1;if(t>h)return i?1:-1;return 0})};Array.prototype.unique=function(){return Array.from(new Set(this))};Array.prototype.uniqueBy=function(r){let i=new Set;return this.filter((f)=>{let e=r(f);if(i.has(e))return!1;else return i.add(e),!0})};var k;((r)=>{r[r.Explore=0]="Explore",r[r.SkipSiblings=1]="SkipSiblings",r[r.NoExplore=2]="NoExplore",r[r.ReplaceParent=3]="ReplaceParent",r[r.DeleteParent=4]="DeleteParent",r[r.MergeParent=5]="MergeParent",r[r.ReplaceItem=6]="ReplaceItem",r[r.DeleteItem=7]="DeleteItem"})(k||={});class E{action;by;skipSiblings;reexplore;constructor(r,i,f,e){this.action=r,this.by=i,this.skipSiblings=f,this.reexplore=e}static _explore=new E(0);static _noExplore=new E(2);static _skipSiblings=new E(1);static _deleteParent=new E(4);static ReplaceParent(r,i){return new E(3,r,void 0,i)}static SkipSiblings(){return E._skipSiblings}static Explore(){return E._explore}static NoExplore(){return E._noExplore}static DeleteParent(){return E._deleteParent}static MergeParent(r){return new E(5,r)}static ReplaceItem(r,i){return new E(6,r,i)}static DeleteItem(r){return new E(7,void 0,r)}}Object.navigate=async(r,i)=>{let f=new WeakSet,e=[],t=[],h=async(y,n,O)=>{let P=y===null,j=P?E.Explore():await i({key:y,value:n,path:e,parent:O,parents:t});if(n&&typeof n==="object"&&j.action===0){if(f.has(n))return j;if(f.add(n),!P)e.push(y),t.push(O);let F=n;while(await b(F,y,O))F=O[y];if(!P)t.pop(),e.pop()}return j},b=async(y,n,O)=>{let P=Object.keys(y),j=P.length;for(let F=0;F<j;F++){let d=P[F],$=y[d],S=await h(d,$,y),m=S.action;if(m===0||m===2)continue;if(m===6){if(y[d]=S.by,S.skipSiblings)return;continue}if(m===7){if(delete y[d],S.skipSiblings)return;continue}if(m===1)return;if(m===3){if(n===null)throw Error("Cannot replace root object");if(O[n]=S.by,S.reexplore)return!0;return}if(m===4){if(n===null)throw Error("Cannot delete root object");delete O[n];return}if(m===5)delete y[d],Object.assign(y,S.by)}};await h(null,r,null)};Object.find=async function(r,i){let f=[];return await Object.navigate(r,(e)=>{if(i([...e.path,e.key].join("."),e.value))f.push(e.value);return E.Explore()}),f};Object.merge=(r,i,f)=>{for(let e of Object.keys(i)){if(r[e]===void 0)continue;let t=i[e],h=r[e];if(typeof h!==typeof t||Array.isArray(h)!==Array.isArray(t)){let y=f?.mismatch?.(e,h,t,r,i);if(y!==void 0){i[e]=y;continue}throw Error(`Type mismatch: ${e}`)}let b=f?.mode?.(e,h,t,r,i)??i[".merge"]??"merge";switch(b){case"source":i[e]=h;continue;case"target":continue;case"skip":delete i[e],delete r[e];continue;case"merge":break;default:throw Error(`Unknown merge mode: ${b}`)}if(typeof h==="object")if(Array.isArray(h))t.push(...h);else{if(f?.navigate?.(e,h,t,r,i)===!1)continue;Object.merge(h,t,f)}else{if(h===t)continue;let y=f?.conflict?.(e,h,t,r,i);i[e]=y===void 0?t:y}}for(let e of Object.keys(r))if(i[e]===void 0)i[e]=r[e]};var v=/^\[([^=\]]*)([=]*)([^\]]*)\]$/,a=/^([^[\]]*)\[([^\]]*)]$/,l=(r,i,f,e)=>{if(r.startsWith("{")&&r.endsWith("}")){let t=r.substring(1,r.length-1);return Object.select(i,t,f)?.toString()}return e?r:void 0},rr=(r,i,f,e)=>{let t=l(i,f,e);if(t)return Object.select(r,t,e);let h=v.exec(i);if(h){let[,y,n,O]=h,P=l(y.trim(),f,e,!0),j=l(O.trim(),f,e,!0);if(Array.isArray(r)&&(n==="="||n==="==")&&P)return r.find((F)=>F?.[P]==j)}let b=a.exec(i);if(b){let[,y,n]=b;return r?.[y]?.[n]}return r?.[i]};Object.select=(r,i,f)=>{let e=void 0,t=void 0,h=i??"",b=(j)=>{if(!j)return[j,!1];return j.endsWith("?")?[j.substring(0,j.length-1),!0]:[j,!1]},y=(j,F)=>{let[d,$]=b(F.pop());if(!d){if(f?.set!==void 0&&t!==void 0&&e!==void 0)e[t]=f.set;return j}let S=rr(j,d,r,f);if(S===void 0){if($||f?.optional||f?.defaultValue!==void 0)return;throw Error(`Unknown path element: "${d}" in "${h}"`)}return e=j,t=d,y(S,F)},n=h.splitNested(f?.separator??".","{","}"),O=void 0;if(n.length>0&&n[n.length-1].startsWith("?="))O=n.pop()?.substring(2);n.reverse();let P=y(r,n);if(P===void 0)return O??f?.defaultValue;return P};Object.deepClone=function(r){if(typeof structuredClone<"u")try{return structuredClone(r)}catch{}return JSON.parse(JSON.stringify(r))};Object.toList=function(r,i){return Object.entries(r).map(([f,e])=>({[i]:f,...e}))};Object.mapEntries=function(r,i){let f=Object.entries(r).map(([e,t])=>i(e,t));return Object.fromEntries(f)};global.AsyncFunction=Object.getPrototypeOf(async function(){}).constructor;global.defined=(r,i="Value is undefined")=>{if(r===void 0||r===null)throw Error(i);return r};String.prototype.tokenize=function(r,i=/\${([^}]+)}/g){return this.replace(i,(f,e)=>{let t=r[e];if(t===null||t===void 0)return"";if(typeof t==="string")return t;if(typeof t==="number"||typeof t==="boolean")return String(t);return String(t)})};String.prototype.toPascal=function(){return this.replace(/((?:[_ ]+)\w|^\w)/g,(r,i)=>i.toUpperCase()).replace(/[_ ]/g,"")};String.prototype.capitalize=function(){return this.replace(/((?:[ ]+)\w|^\w)/g,(r,i)=>i.toUpperCase())};String.prototype.toCamel=function(){return this.toPascal().replace(/((?:[ ]+\w)|^\w)/g,(r,i)=>i.toLowerCase())};String.prototype.toSnake=function(){return this.replace(/([a-z])([A-Z])/g,"$1_$2").replace(/\s+/g,"_")};String.prototype.toKebab=function(){return this.replace(/([a-z])([A-Z])/g,"$1-$2").replace(/\s+/g,"-")};String.prototype.changeCase=function(r){switch(r){case"upper":return this.toUpperCase();case"lower":return this.toLowerCase();case"pascal":return this.toPascal();case"camel":return this.toCamel();case"snake":return this.toSnake();case"kebab":return this.toKebab();default:throw Error(`Unsupported case type: ${r}`)}};String.prototype.splitWithQuotes=function(r=","){let i=[],f=this.length,e=r.length,t=0,h=(b)=>{if(b+e>f)return!1;for(let y=0;y<e;y++)if(this[b+y]!==r[y])return!1;return!0};while(t<f){while(t<f&&this[t]===" ")t++;if(t>=f)break;let b="",y=this[t]==='"'||this[t]==="'";if(y){let n=this[t++];while(t<f&&this[t]!==n)b+=this[t++];if(t<f)t++}else{while(t<f&&!h(t)){let n=this[t];if(n==='"'||n==="'"){b+=n,t++;while(t<f&&this[t]!==n)b+=this[t++];if(t<f)b+=this[t++]}else b+=this[t++]}b=b.trim()}if(b)i.push({value:b,quoted:y});if(h(t))t+=e}return i};String.prototype.splitNested=function(r,i,f){let e=0,t="",h=[];for(let b of this){if(t+=b,b===i)e++;if(b===f)e--;if(e===0&&b===r)h.push(t.slice(0,-1)),t=""}if(t)h.push(t);return h};String.prototype.padBlock=function(r,i=`
|
|
2
|
+
`,f=" "){let e=f.repeat(r);return this.split(i).map((t)=>e+t).join(i)};String.prototype.stripIndent=function(){let r=this.split(/\r?\n/),i=r.filter((e)=>e.trim().length>0).map((e)=>/^[ \t]*/.exec(e)?.[0].length??0),f=i.length>0?Math.min(...i):0;if(f===0)return this;return r.map((e)=>e.slice(f)).join(`
|
|
3
|
+
`)};var c=(r,i)=>{if(typeof r.value==="string"){if(r.quoted)return r.value;if(r.value.startsWith("[")&&r.value.endsWith("]")){let f=r.value.slice(1,-1);if(f.trim()==="")return[];return f.split(";").map((e)=>{let t=e.trim();if(!isNaN(+t))return+t;return t})}if(r.value.startsWith("{")&&r.value.endsWith("}")){if(r.value.slice(1,-1).trim()==="")return{}}if(r.value==="null")return null;if(r.value==="undefined")return;if(r.value.startsWith("@"))return r.value.slice(1);if(!isNaN(+r.value))return+r.value}return i(r.value)},_=(r,i,f,e,t,h)=>{let b=e?.(r)??r,y=t(b);if(y[0])return y[1];if(!i){let n=Object.select(f,r),O=t(n);if(O[0])return O[1]}throw Error(h)},z=(r,i,f,e,t)=>{let h=typeof r.types==="function"?r.types(e,i.value,t):r.types?.[e];if(!h||h==="ref")return c(i,(y)=>Object.select(f,y));let b=c(i,()=>i.value);if(Array.isArray(h)){if(!h.includes(b))throw Error(`Argument ${e.toString()} must be one of [${h.join(", ")}]: ${b}`);return b}if(typeof h==="string"){if(h.endsWith("?")){let y=h.slice(0,-1);if(i.value==="null")return E.ReplaceItem(null);if(i.value==="undefined")return E.ReplaceItem(void 0);if(i.value==="")return"";h=y}switch(h){case"any":return b;case"array":return _(b,i.quoted,f,null,(y)=>[Array.isArray(y),y],`Argument ${e.toString()} must be an array: ${b}`);case"object":return _(b,i.quoted,f,null,(y)=>[typeof y==="object"&&y!==null,y],`Argument ${e.toString()} must be an object: ${b}`);case"number":return _(b,i.quoted,f,(y)=>Number(y),(y)=>[!isNaN(y),y],`Argument ${e.toString()} must be a number: ${b}`);case"boolean":return _(b,i.quoted,f,null,(y)=>{if([!0,"true","1",1,"yes","on"].includes(y))return[!0,!0];if([!1,"false","0",0,"no","off"].includes(y))return[!0,!1];if([null,"null","undefined",void 0,""].includes(y))return[!0,!1];if(Array.isArray(y))return[!0,y.length>0];return[!1,!1]},`Argument ${e.toString()} must be a boolean: ${b}`);case"string":return b.toString();default:throw Error(`Unknown type for argument ${e.toString()}: ${h}`)}}return h(b,f,e,i.quoted)};var G=(r,i)=>{let f=r?.[i];if(f===void 0)throw Error(`Unhandled field processor type: ${i}`);return typeof f==="function"?{options:{},fn:f}:{options:f,fn:f.fn}},q=(r)=>!r.quoted&&r.value.startsWith("."),ir=(r)=>{let i={};for(let f of r){if(!q(f))continue;let e=f.value.indexOf("="),t=e>-1?f.value.slice(1,e):f.value.slice(1),h=e>-1?f.value.slice(e+1):"";if(h.length>=2){let b=h[0],y=h[h.length-1];if(b==='"'&&y==='"'||b==="'"&&y==="'")h=h.slice(1,-1)}if(!i[t])i[t]=[];i[t].push(h)}return i},N=(r,i,f)=>{let e=(r??"").splitWithQuotes(),t=ir(e),h=e.filter((y)=>!q(y)),b=i.options.processArgs===!1?[]:h.map((y,n)=>z(i.options,y,f,n,h));return{rawArgs:h,parsedArgs:b,tags:t}};var fr=(r)=>{return r!==null&&typeof r==="object"&&"action"in r},w=(r,i)=>i.keys!==void 0&&r.key.startsWith(".")&&i.keys[r.key.substring(1)]!==void 0,p=async(r,i,f,e)=>{let{key:t,parent:h}=r,b=G(e.keys,t.substring(1));if(b.options.processArgs!==!1&&(typeof b.options.types==="function"||b.options.types?.[0]!==void 0))r.value=z(b.options,{value:r.value,quoted:!1},f,0,[]);if(typeof r.value!=="string")await J(r.value,{...e,root:{...f,parent:r.parent,parents:r.parents},filters:[...e.filters??[],...b.options.filters??[]]});let y=await b.fn({path:i,root:f,parent:h,key:t,options:e,value:r.value,args:[],rawArgs:[],tags:{}});return fr(y)?y:E.ReplaceParent(y)};var R=async(r,i,f,e)=>{if(!i?.onSection&&!i?.onSectionConfig&&!i?.sections)return[r,!1];let t=/<@\s*section(?:\s*:\s*([\w-]+))?\s*@>([\s\S]*?)<@\s*\/\s*section\s*@>/g,h,b=r,y=0,n=!1,O={content:b,root:e,path:f,options:i,section:{}};while((h=t.exec(r))!==null){n=!0;let[P,j,F]=h,d=h.index,$=d+P.length,S=r[$]===`
|
|
4
|
+
`;if(S)$++;let m=d+y,Q=$+y,M={},U=F,W=/(^|\r?\n)[ \t]*---[ \t]*(\r?\n|$)/m.exec(F);if(W){let Z=F.slice(0,W.index),o=W.index+W[0].length;U=F.slice(o);let Y=Z.stripIndent().trim();if(Y.length>0)try{M=i?.configParser?.(Y)??{}}catch(s){throw Error(`Failed to parse YAML config for section: ${s.message}`)}}else if(/^\r?\n/.test(U))U=U.replace(/^\r?\n/,"");U=U.stripIndent().replace(/(?:\r?\n[ \t]*)+$/,"").replace(/[ \t]+$/,"");let A={config:j?{...M,type:j}:M,content:U};if(O.content=b,O.section=A,Object.keys(A.config).length>0){if(await i.onSectionConfig?.(O)!==!1)await J(A.config,{...i,root:e})}let B=O.section.config.type?i.sections?.[O.section.config.type]:void 0,C;if(B)C=await B(O);else if(i.onSection)C=await i.onSection(O);if(A.trim)A.content=A.content.trim();if(A.indent)A.content=A.content.split(`
|
|
5
|
+
`).map((Z)=>" ".repeat(A.indent)+Z).join(`
|
|
6
|
+
`);let D="";if(C===!0||A.show)D=A.content;else if(typeof C==="string")D=C;if(S&&D!==""&&!D.endsWith(`
|
|
7
|
+
`))D+=`
|
|
8
|
+
`;let X=b.lastIndexOf(`
|
|
9
|
+
`,m-1),g=X===-1?0:X+1,H=b.slice(g,m).trim().length===0?g:m;b=b.slice(0,H)+D+b.slice(Q),y+=D.length-(Q-H),O.content=b}return[b,n]};var I=(r)=>{let i=[],f=0;while(f<r.length)if(r[f]==="$"&&r[f+1]==="("&&(f===0||r[f-1]!=="\\")){let e=f;f+=2;let t=1;for(;f<r.length;f++)if(r[f]==="(")t++;else if(r[f]===")"){if(t--,t===0){i.push([e,f]);break}}if(t!==0)throw Error(`Unmatched opening $( at position ${e.toString()}`);f++}else f++;return i};var K=Symbol.for("@homedev/fields:OverrideResult"),T=(r)=>({[K]:!0,value:r}),er=(r)=>{return r!==null&&typeof r==="object"&&K in r&&r[K]===!0},u=async(r,i,f)=>{for(let e=i.length-1;e>=0;e--){let[t,h]=i[e],b=r.slice(t+2,h),y=await u(b,I(b),f);if(typeof y==="object")return y;let n=await f(y,r);if(er(n))return n.value;r=r.slice(0,t)+n+r.slice(h+1)}return r},x=async(r,i)=>u(r,I(r),i);var tr=(r)=>{return r!==null&&typeof r==="object"&&"action"in r},yr=/([\w\d.-_/]+):(.+)/,hr=async(r,i,f,e)=>{let t=await(async()=>{let h=yr.exec(r);if(!h)return await Object.select(i,r);let[b,y,n]=h,O=G(e.sources,y),P=N(n,O,i);return await O.fn({args:P.parsedArgs,rawArgs:P.rawArgs,tags:P.tags,key:y,options:e,root:i,path:f,value:null})})();if(t===null||t===void 0)return"";if(typeof t==="object")return T(t);return t},L=async(r,i,f,e)=>{let t=await x(i,(h)=>hr(h,f,r,e));if(tr(t))return t;if(t===i)return E.Explore();if(typeof t==="string"){let h=await L(r,t,f,e);if(h.action!==k.Explore)return h;return E.ReplaceItem(t)}return E.ReplaceItem(t)};var J=async(r,i)=>{return i??={},await Object.navigate(r,async(f)=>{let e=[...f.path,f.key].join(".");if(i?.filters?.length&&i.filters.some((y)=>y.test(e)))return E.NoExplore();let t=f.path.length===0,h=f.parents.length>0?f.parents.toReversed():[];if(t){if(h.length>0)throw Error("Root object should not have a parent");h=i?.root?.parents?[i.root.parent,...i.root.parents.toReversed()]:[]}let b={...r,...i.root,this:f.parent,parent:h.length>0?h[0]:null,parents:h};try{if(f.key.startsWith("$"))return E.NoExplore();let y=E.Explore();if(typeof f.value==="string"){let n=f.value,O=!1;while(!0){let[P,j]=await R(n,i,e,b);if(O=O||j,y=await L(e,P,b,i),y.action===k.ReplaceItem&&typeof y.by==="string"){n=y.by;continue}if(y.action===k.Explore&&j&&P!==n)y=E.ReplaceItem(P);break}if(y.action===k.Explore&&n!==f.value)y=E.ReplaceItem(n);switch(y.action){case k.Explore:break;case k.ReplaceItem:f.value=y.by;break;default:return y}}if(w(f,i))y=await p(f,e,b,i);return y}catch(y){throw Error(`${y.message}
|
|
10
|
+
(${e})`)}}),r};var V=(r,i)=>i?{fn:(f)=>r(...f.args),types:i}:(f)=>r(...f.args),Hr=(r,i)=>Object.fromEntries(r.map((f)=>[f.name,V(f,i?.types?.[f.name])])),Yr=(r,i)=>Object.fromEntries((i?.keys??Object.keys(r)).map((f)=>[f,V(r[f],i?.types?.[f])]));export{J as processFields,Yr as makeFieldProcessorMap,Hr as makeFieldProcessorList,V as makeFieldProcessor,G as getProcessor,N as getArgs};
|