@homedev/framework 0.0.10 → 0.0.12
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 +4 -4
- package/dist/index.js +6 -4
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ export declare class Markdown extends StringBuilder {
|
|
|
32
32
|
bold(text: string): this;
|
|
33
33
|
italic(text: string): this;
|
|
34
34
|
item(text: string): this;
|
|
35
|
-
items(items: string[]): this;
|
|
35
|
+
items(items: string[] | Record<string, any>): this;
|
|
36
36
|
orderedList(items: string[], start?: number): this;
|
|
37
37
|
code(text: string): this;
|
|
38
38
|
horizontalRule(): this;
|
|
@@ -45,7 +45,7 @@ export declare class Markdown extends StringBuilder {
|
|
|
45
45
|
text: string;
|
|
46
46
|
checked: boolean;
|
|
47
47
|
}[]): this;
|
|
48
|
-
section(text: string): this;
|
|
48
|
+
section(text: string, level?: number): this;
|
|
49
49
|
endSection(): this;
|
|
50
50
|
setSectionLevel(level: number): this;
|
|
51
51
|
}
|
|
@@ -147,10 +147,10 @@ export declare class StringBuilder {
|
|
|
147
147
|
dedent(): this;
|
|
148
148
|
getCurrentIndent(): number;
|
|
149
149
|
getLines(): string[];
|
|
150
|
-
formatLine(line
|
|
150
|
+
formatLine(line?: string): string;
|
|
151
151
|
write(...args: any[]): this;
|
|
152
152
|
writeLine(...args: any[]): this;
|
|
153
|
-
writeMap<T>(items: T[], fn: (item: T, index: number, builder:
|
|
153
|
+
writeMap<T>(items: T[], fn: (item: T, index: number, builder: this) => string | this): this;
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
export declare interface StringBuilderOptions {
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
Array.prototype.findOrFail=function(t,n,r){let i=this.find(t,r);if(i===void 0)throw Error(n??"Element not found");return i};Array.flat=function(t){if(Array.isArray(t))return t.flat(2);else return[t]};Array.prototype.forEachAsync=async function(t){for(let n=0;n<this.length;n++)await t(this[n],n,this)};Array.prototype.groupBy=function(t){return this.reduce((n,r)=>{let i=t(r);if(!n[i])n[i]=[];return n[i].push(r),n},{})};Array.prototype.createInstances=function(t,...n){return this.map((r,i)=>new t(...n,r,i))};Array.prototype.mapAsync=async function(t){return Promise.all(this.map(t))};Array.prototype.nonNullMap=function(t){let n=[];for(let r=0;r<this.length;r++){let i=t(this[r],r,this);if(i!=null)n.push(i)}return n};Array.prototype.nonNullMapAsync=async function(t){let n=[];for(let r=0;r<this.length;r++){let i=await t(this[r],r,this);if(i!=null)n.push(i)}return n};Array.prototype.mergeAll=function(t){let n={};for(let r of this.toReversed())Object.merge(r,n,t);return n};Array.prototype.toObject=function(t,n){return Object.fromEntries(this.map((r)=>{let i=t(r),s=n?n(r):r;return[i,s]}))};Array.prototype.toObjectWithKey=function(t){return Object.fromEntries(this.map((n)=>[String(n[t]),n]))};Array.prototype.selectFor=function(t,n,r,i){let s=this.find(t,i);if(s===void 0)throw Error(r??"Element not found");return n(s)};Array.prototype.sortBy=function(t,n=!0){return this.slice().sort((r,i)=>{let s=t(r),e=t(i);if(s<e)return n?-1:1;if(s>e)return n?1:-1;return 0})};Array.prototype.unique=function(){return Array.from(new Set(this))};Array.prototype.uniqueBy=function(t){let n=new Set;return this.filter((r)=>{let i=t(r);if(n.has(i))return!1;else return n.add(i),!0})};var E;((o)=>{o[o.Explore=0]="Explore";o[o.SkipSiblings=1]="SkipSiblings";o[o.NoExplore=2]="NoExplore";o[o.ReplaceParent=3]="ReplaceParent";o[o.DeleteParent=4]="DeleteParent";o[o.MergeParent=5]="MergeParent";o[o.ReplaceItem=6]="ReplaceItem";o[o.DeleteItem=7]="DeleteItem"})(E||={});class T{action;by;skipSiblings;reexplore;constructor(t,n,r,i){this.action=t;this.by=n;this.skipSiblings=r;this.reexplore=i}static _explore=new T(0);static _noExplore=new T(2);static _skipSiblings=new T(1);static _deleteParent=new T(4);static ReplaceParent(t,n){return new T(3,t,void 0,n)}static SkipSiblings(){return T._skipSiblings}static Explore(){return T._explore}static NoExplore(){return T._noExplore}static DeleteParent(){return T._deleteParent}static MergeParent(t){return new T(5,t)}static ReplaceItem(t,n){return new T(6,t,n)}static DeleteItem(t){return new T(7,void 0,t)}}Object.navigate=async(t,n)=>{let r=new WeakSet,i=[],s=[],e=async(h,o,d)=>{let p=h===null,x=p?T.Explore():await n({key:h,value:o,path:i,parent:d,parents:s});if(o&&typeof o==="object"&&x.action===0){if(r.has(o))return x;if(r.add(o),!p)i.push(h),s.push(d);let g=o;while(await f(g,h,d))g=d[h];if(!p)s.pop(),i.pop()}return x},f=async(h,o,d)=>{let p=Object.keys(h),m=p.length;for(let x=0;x<m;x++){let b=p[x],w=h[b],g=await e(b,w,h),c=g.action;if(c===0||c===2)continue;if(c===6){if(h[b]=g.by,g.skipSiblings)return;continue}if(c===7){if(delete h[b],g.skipSiblings)return;continue}if(c===1)return;if(c===3){if(o===null)throw Error("Cannot replace root object");if(d[o]=g.by,g.reexplore)return!0;return}if(c===4){if(o===null)throw Error("Cannot delete root object");delete d[o];return}if(c===5)delete h[b],Object.assign(h,g.by)}};await e(null,t,null)};Object.find=async function(t,n){let r=[];return await Object.navigate(t,(i)=>{if(n([...i.path,i.key].join("."),i.value))r.push(i.value);return T.Explore()}),r};Object.merge=(t,n,r)=>{for(let i of Object.keys(n)){if(t[i]===void 0)continue;let s=n[i],e=t[i];if(typeof e!==typeof s||Array.isArray(e)!==Array.isArray(s)){let h=r?.mismatch?.(i,e,s,t,n);if(h!==void 0){n[i]=h;continue}throw Error(`Type mismatch: ${i}`)}let f=r?.mode?.(i,e,s,t,n)??n[".merge"]??"merge";switch(f){case"source":n[i]=e;continue;case"target":continue;case"skip":delete n[i],delete t[i];continue;case"merge":break;default:throw Error(`Unknown merge mode: ${f}`)}if(typeof e==="object")if(Array.isArray(e))s.push(...e);else{if(r?.navigate?.(i,e,s,t,n)===!1)continue;Object.merge(e,s,r)}else{if(e===s)continue;let h=r?.conflict?.(i,e,s,t,n);n[i]=h===void 0?s:h}}for(let i of Object.keys(t))if(n[i]===void 0)n[i]=t[i]};var O=/^\[([^=\]]*)([=]*)([^\]]*)\]$/,S=/^([^[\]]*)\[([^\]]*)]$/,a=(t,n,r,i)=>{if(t.startsWith("{")&&t.endsWith("}")){let s=t.substring(1,t.length-1);return Object.select(n,s,r)?.toString()}return i?t:void 0},$=(t,n,r,i)=>{let s=a(n,r,i);if(s)return Object.select(t,s,i);let e=O.exec(n);if(e){let[,h,o,d]=e,p=a(h.trim(),r,i,!0),m=a(d.trim(),r,i,!0);if(Array.isArray(t)&&(o==="="||o==="==")&&p)return t.find((x)=>x?.[p]==m)}let f=S.exec(n);if(f){let[,h,o]=f;return t?.[h]?.[o]}return t?.[n]};Object.select=(t,n,r)=>{let i=void 0,s=void 0,e=n??"",f=(m)=>{if(!m)return[m,!1];return m.endsWith("?")?[m.substring(0,m.length-1),!0]:[m,!1]},h=(m,x)=>{let[b,w]=f(x.pop());if(!b){if(r?.set!==void 0&&s!==void 0&&i!==void 0)i[s]=r.set;return m}let g=$(m,b,t,r);if(g===void 0){if(w||r?.optional||r?.defaultValue!==void 0)return;throw Error(`Unknown path element: "${b}" in "${e}"`)}return i=m,s=b,h(g,x)},o=e.splitNested(r?.separator??".","{","}"),d=void 0;if(o.length>0&&o[o.length-1].startsWith("?="))d=o.pop()?.substring(2);o.reverse();let p=h(t,o);if(p===void 0)return d??r?.defaultValue;return p};Object.deepClone=function(t){if(typeof structuredClone<"u")try{return structuredClone(t)}catch{}return JSON.parse(JSON.stringify(t))};Object.toList=function(t,n){return Object.entries(t).map(([r,i])=>({[n]:r,...i}))};Object.mapEntries=function(t,n){let i=Object.entries(t).map(([s,e])=>n(s,e));return Object.fromEntries(i)};global.AsyncFunction=Object.getPrototypeOf(async function(){}).constructor;global.defined=(t,n="Value is undefined")=>{if(t===void 0||t===null)throw Error(n);return t};String.prototype.tokenize=function(t,n=/\${([^}]+)}/g){return this.replace(n,(r,i)=>{let s=t[i];if(s===null||s===void 0)return"";if(typeof s==="string")return s;if(typeof s==="number"||typeof s==="boolean")return String(s);return String(s)})};String.prototype.toPascal=function(){return this.replace(/((?:[_ ]+)\w|^\w)/g,(t,n)=>n.toUpperCase()).replace(/[^a-zA-Z0-9 ]/g,"").replace(/[_ ]/g,"")};String.prototype.capitalize=function(){return this.replace(/((?:[ ]+)\w|^\w)/g,(t,n)=>n.toUpperCase())};String.prototype.toCamel=function(){return this.toPascal().replace(/((?:[ ]+\w)|^\w)/g,(t,n)=>n.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(t){switch(t){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: ${t}`)}};String.prototype.splitWithQuotes=function(t=","){let n=[],r=this.length,i=t.length,s=0,e=(f)=>{if(f+i>r)return!1;for(let h=0;h<i;h++)if(this[f+h]!==t[h])return!1;return!0};while(s<r){while(s<r&&this[s]===" ")s++;if(s>=r)break;let f="",h=this[s]==='"'||this[s]==="'";if(h){let o=this[s++];while(s<r&&this[s]!==o)f+=this[s++];if(s<r)s++}else{while(s<r&&!e(s)){let o=this[s];if(o==='"'||o==="'"){f+=o,s++;while(s<r&&this[s]!==o)f+=this[s++];if(s<r)f+=this[s++]}else f+=this[s++]}f=f.trim()}if(f)n.push({value:f,quoted:h});if(e(s))s+=i}return n};String.prototype.splitNested=function(t,n,r){let i=0,s="",e=[];for(let f of this){if(s+=f,f===n)i++;if(f===r)i--;if(i===0&&f===t)e.push(s.slice(0,-1)),s=""}if(s)e.push(s);return e};String.prototype.toStringBuilder=function(){return new u(this.split(`
|
|
2
2
|
`))};String.prototype.padBlock=function(t,n=`
|
|
3
3
|
`,r=" "){let i=r.repeat(t);return this.split(n).map((s)=>i+s).join(n)};String.prototype.stripIndent=function(){let t=this.split(/\r?\n/),n=t.filter((i)=>i.trim().length>0).map((i)=>/^[ \t]*/.exec(i)?.[0].length??0),r=n.length>0?Math.min(...n):0;if(r===0)return this;return t.map((i)=>i.slice(r)).join(`
|
|
4
|
-
`)};String.prototype.toHtmlLink=function(t){return`<a href="${t??this.toString()}">${this.toString()}</a>`};String.prototype.toRgb=function(){let t=this.toString().replace("#","");if(t.length!==6)throw Error("Invalid hex color");let n=parseInt(t.substring(0,2),16),r=parseInt(t.substring(2,4),16),i=parseInt(t.substring(4,6),16);return`rgb(${n.toString()}, ${r.toString()}, ${i.toString()})`};String.prototype.toArgb=function(){let t=this.toString().replace("#","");if(t.length!==8)throw Error("Invalid hex color");let n=parseInt(t.substring(0,2),16),r=parseInt(t.substring(2,4),16),i=parseInt(t.substring(4,6),16),s=parseInt(t.substring(6,8),16);return`argb(${n.toString()}, ${r.toString()}, ${i.toString()}, ${s.toString()})`};String.prototype.single=function(t){return this.replaceAll(new RegExp(`(${t})+`,"g"),"$1")};String.prototype.remove=function(t){return this.replaceAll(new RegExp(t,"g"),"")};String.prototype.toAlphanum=function(){return this.replace(/[^a-z0-9]/gi,"")};String.prototype.toAlpha=function(){return this.replace(/[^a-z]/gi,"")};class
|
|
5
|
-
`)}formatLine(t){
|
|
6
|
-
|
|
7
|
-
`)
|
|
4
|
+
`)};String.prototype.toHtmlLink=function(t){return`<a href="${t??this.toString()}">${this.toString()}</a>`};String.prototype.toRgb=function(){let t=this.toString().replace("#","");if(t.length!==6)throw Error("Invalid hex color");let n=parseInt(t.substring(0,2),16),r=parseInt(t.substring(2,4),16),i=parseInt(t.substring(4,6),16);return`rgb(${n.toString()}, ${r.toString()}, ${i.toString()})`};String.prototype.toArgb=function(){let t=this.toString().replace("#","");if(t.length!==8)throw Error("Invalid hex color");let n=parseInt(t.substring(0,2),16),r=parseInt(t.substring(2,4),16),i=parseInt(t.substring(4,6),16),s=parseInt(t.substring(6,8),16);return`argb(${n.toString()}, ${r.toString()}, ${i.toString()}, ${s.toString()})`};String.prototype.single=function(t){return this.replaceAll(new RegExp(`(${t})+`,"g"),"$1")};String.prototype.remove=function(t){return this.replaceAll(new RegExp(t,"g"),"")};String.prototype.toAlphanum=function(){return this.replace(/[^a-z0-9]/gi,"")};String.prototype.toAlpha=function(){return this.replace(/[^a-z]/gi,"")};class y{options;constructor(t){this.options={level:"INFO",showClass:!1,levels:["DEBUG","VERBOSE","INFO","WARN","ERROR"],timeStamp:!1,timeOptions:{second:"2-digit",minute:"2-digit",hour:"2-digit",day:"2-digit",month:"2-digit",year:"2-digit"},logFunction:(n,...r)=>{switch(n){case"ERROR":console.error(...r);break;case"WARN":console.warn(...r);break;case"DEBUG":console.debug(...r);break;case"VERBOSE":console.log(...r);break;default:console.log(...r)}},...t}}write(t,...n){if(this.options.level){let s=this.options.levels.indexOf(this.options.level)??-1,e=this.options.levels.indexOf(t);if(e===-1)throw Error(`Unknown log level: ${t}`);if(e<s)return}let r=[],i=[];if(this.options.timeStamp)i.push(new Date().toLocaleString(this.options.timeLocale,this.options.timeOptions));if(this.options.showClass)i.push(t);if(i.length>0)r.push(`[${i.join(" ")}]`);r.push(...n),this.options.logFunction(t,...r)}info(...t){this.write("INFO",...t)}error(...t){this.write("ERROR",...t)}warn(...t){this.write("WARN",...t)}debug(...t){this.write("DEBUG",...t)}verbose(...t){this.write("VERBOSE",...t)}}class u{#t=[];#n=0;options;constructor(t,n){if(t instanceof u)this.options={...t.options,...n},this.#t.push(...t.#t);else if(this.options=n??{},t)if(Array.isArray(t))this.#t.push(...t);else this.#t.push(t)}setOptions(t){return this.options=t,this}clear(){return this.#t.length=0,this}isEmpty(){return this.#t.length===0}toString(){return this.#t.join("")}indent(){return this.#n+=1,this}dedent(){if(this.#n>0)this.#n-=1;return this}getCurrentIndent(){return this.#n}getLines(){return this.toString().split(`
|
|
5
|
+
`)}formatLine(t){t??=`
|
|
6
|
+
`;let n=(this.options.indentChar??" ").repeat(this.#n*(this.options.indentSize??2));if(this.options.trim)t=t.trim();return n+(this.options.linePrefix??"")+t+(this.options.lineSuffix??"")+(this.options.lineSeparator??`
|
|
7
|
+
`)}write(...t){return this.#t.push(...t.flatMap((n)=>{if(n!==void 0){if(typeof n==="string")return n;if(Array.isArray(n))return n.flat();if(n instanceof u)return n.toString();if(typeof n==="function")return n(this);return String(n)}}).filter((n)=>n!==void 0)),this}writeLine(...t){return this.write(t.length>0?t.map((n)=>this.formatLine(n)):`
|
|
8
|
+
`)}writeMap(t,n){return t.forEach((r,i)=>n(r,i,this)),this}}class F extends u{header(t,n=1){return this.writeLine(`${"#".repeat(n)} ${t}`,"")}codeBlock(t,n){if(n??="",this.writeLine("```"+n),t!==void 0)this.writeLine(t).endCodeBlock();return this}endCodeBlock(){return this.writeLine("```")}link(t,n){return this.write(`[${t}](${n})`)}table(t,n){let r=t.map((e,f)=>{let h=e.length;return n.forEach((o)=>{if(o[f])h=Math.max(h,o[f].length)}),h}),i=t.map((e,f)=>e.padEnd(r[f]));this.writeLine("| "+i.join(" | ")+" |");let s=r.map((e)=>"-".repeat(e));return this.writeLine("| "+s.join(" | ")+" |"),n.forEach((e)=>{let f=e.map((h,o)=>h.padEnd(r[o]));this.writeLine("| "+f.join(" | ")+" |")}),this}blockquote(t){return t.split(`
|
|
9
|
+
`).forEach((r)=>this.writeLine("> "+r)),this}bold(t){return this.write(`**${t}**`)}italic(t){return this.write(`*${t}*`)}item(t){return this.writeLine(`- ${t}`)}items(t){if(Array.isArray(t))t.forEach((n)=>this.item(n));else Object.entries(t).forEach(([n,r])=>r!==void 0&&this.item(`${n}: ${r!==void 0?String(r):""}`));return this}orderedList(t,n=1){return t.forEach((r,i)=>this.writeLine(`${(i+n).toString()}. ${r}`)),this}code(t){return this.write(`\`${t}\``)}horizontalRule(){return this.writeLine("---")}image(t,n){return this.writeLine(``)}strikethrough(t){return this.write(`~~${t}~~`)}highlight(t){return this.write(`==${t}==`)}subscript(t){return this.write(`~${t}~`)}superscript(t){return this.write(`^${t}^`)}taskList(t){return t.forEach((n)=>this.writeLine(`- [${n.checked?"x":" "}] ${n.text}`)),this}#t=0;section(t,n){if(n!==void 0)this.#t=n;else this.#t++;return this.header(t,this.#t)}endSection(){if(this.#t>0)this.#t--;return this}setSectionLevel(t){return this.#t=t,this}}export{u as StringBuilder,T as NavigateResult,E as NavigateAction,F as Markdown,y as Logger};
|