@homedev/framework 0.0.6 → 0.0.8
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 +35 -39
- package/dist/index.js +7 -9
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Guards against undefined and null values.
|
|
3
|
-
* Throws an error if the value is nullish, otherwise returns the value.
|
|
4
|
-
*
|
|
5
|
-
* @param value - The value to check
|
|
6
|
-
* @param message - Custom error message (default: 'Value is undefined')
|
|
7
|
-
* @returns The value if it is not nullish
|
|
8
|
-
* @throws {Error} When value is undefined or null
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```typescript
|
|
12
|
-
* const value = defined(maybeUndefined, 'Required value missing')
|
|
13
|
-
* // value is guaranteed to be non-nullish here
|
|
14
|
-
*
|
|
15
|
-
* defined(0) // 0 (falsy but not nullish)
|
|
16
|
-
* defined(false) // false (falsy but not nullish)
|
|
17
|
-
* defined(null) // throws Error: Value is undefined
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
declare function defined_2<T>(value: T | undefined | null, message?: string): T;
|
|
21
|
-
export { defined_2 as defined }
|
|
22
|
-
|
|
23
1
|
export declare type LogFunction = (className: string, ...args: any[]) => void;
|
|
24
2
|
|
|
25
3
|
export declare class Logger {
|
|
@@ -46,14 +24,15 @@ export declare interface LoggerOptions {
|
|
|
46
24
|
export declare class Markdown extends StringBuilder {
|
|
47
25
|
#private;
|
|
48
26
|
header(text: string, level?: number): this;
|
|
49
|
-
codeBlock(code
|
|
27
|
+
codeBlock(code?: string, language?: string): this;
|
|
28
|
+
endCodeBlock(): this;
|
|
50
29
|
link(text: string, url: string): this;
|
|
51
30
|
table(headers: string[], rows: string[][]): this;
|
|
52
31
|
blockquote(text: string): this;
|
|
53
32
|
bold(text: string): this;
|
|
54
33
|
italic(text: string): this;
|
|
55
34
|
item(text: string): this;
|
|
56
|
-
|
|
35
|
+
items(items: string[]): this;
|
|
57
36
|
orderedList(items: string[], start?: number): this;
|
|
58
37
|
code(text: string): this;
|
|
59
38
|
horizontalRule(): this;
|
|
@@ -158,12 +137,29 @@ export declare interface SelectOptions {
|
|
|
158
137
|
|
|
159
138
|
export declare class StringBuilder {
|
|
160
139
|
#private;
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
140
|
+
options: StringBuilderOptions;
|
|
141
|
+
constructor(initial?: string | string[] | StringBuilder, options?: StringBuilderOptions);
|
|
142
|
+
setOptions(options: StringBuilderOptions): this;
|
|
164
143
|
clear(): this;
|
|
165
144
|
isEmpty(): boolean;
|
|
166
145
|
toString(): string;
|
|
146
|
+
indent(): this;
|
|
147
|
+
dedent(): this;
|
|
148
|
+
getCurrentIndent(): number;
|
|
149
|
+
getLines(): string[];
|
|
150
|
+
formatLine(line: string): string;
|
|
151
|
+
write(...args: any[]): this;
|
|
152
|
+
writeLine(...args: any[]): this;
|
|
153
|
+
writeMap<T>(items: T[], fn: (item: T, index: number, builder: StringBuilder) => string | StringBuilder): this;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export declare interface StringBuilderOptions {
|
|
157
|
+
indentChar?: string;
|
|
158
|
+
indentSize?: number;
|
|
159
|
+
trim?: boolean;
|
|
160
|
+
lineSeparator?: string;
|
|
161
|
+
linePrefix?: string;
|
|
162
|
+
lineSuffix?: string;
|
|
167
163
|
}
|
|
168
164
|
|
|
169
165
|
export { }
|
|
@@ -178,6 +174,18 @@ declare global {
|
|
|
178
174
|
var defined: <T>(value: T | undefined | null, msg?: string) => T;
|
|
179
175
|
}
|
|
180
176
|
|
|
177
|
+
declare global {
|
|
178
|
+
interface ObjectConstructor {
|
|
179
|
+
mapEntries<T extends object, U>(obj: T, callback: (key: keyof T, value: T[keyof T]) => [string | number | symbol, U]): Record<string | number | symbol, U>;
|
|
180
|
+
toList<T extends object, U extends object, X extends string>(obj: T, key: X): (Record<X, string> & U)[];
|
|
181
|
+
deepClone<T extends object>(obj: T): T;
|
|
182
|
+
merge(source: any, target: any, options?: MergeOptions): void;
|
|
183
|
+
navigate(o: any, cb: NavigateCallback): Promise<void>;
|
|
184
|
+
find(from: any, cb: (path: string, value: any) => boolean): Promise<any[]>;
|
|
185
|
+
select(from: any, path: string, options?: SelectOptions): any;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
181
189
|
declare global {
|
|
182
190
|
interface Array<T> {
|
|
183
191
|
mapAsync<U>(callback: (value: T, index: number, array: T[]) => Promise<U>): Promise<U[]>;
|
|
@@ -200,18 +208,6 @@ declare global {
|
|
|
200
208
|
}
|
|
201
209
|
}
|
|
202
210
|
|
|
203
|
-
declare global {
|
|
204
|
-
interface ObjectConstructor {
|
|
205
|
-
mapEntries<T extends object, U>(obj: T, callback: (key: keyof T, value: T[keyof T]) => [string | number | symbol, U]): Record<string | number | symbol, U>;
|
|
206
|
-
toList<T extends object, U extends object, X extends string>(obj: T, key: X): (Record<X, string> & U)[];
|
|
207
|
-
deepClone<T extends object>(obj: T): T;
|
|
208
|
-
merge(source: any, target: any, options?: MergeOptions): void;
|
|
209
|
-
navigate(o: any, cb: NavigateCallback): Promise<void>;
|
|
210
|
-
find(from: any, cb: (path: string, value: any) => boolean): Promise<any[]>;
|
|
211
|
-
select(from: any, path: string, options?: SelectOptions): any;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
211
|
declare global {
|
|
216
212
|
interface String {
|
|
217
213
|
capitalize(): string;
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
Array.prototype.findOrFail=function(n,r
|
|
2
|
-
`))};String.prototype.padBlock=function(n
|
|
3
|
-
`,
|
|
4
|
-
`)};String.prototype.toHtmlLink=function(
|
|
5
|
-
|
|
6
|
-
`}))
|
|
7
|
-
${n}
|
|
8
|
-
\`\`\``)}link(n,r){return this.append(`[${n}](${r})`)}table(n,r){let t=n.map((e,h)=>{let d=e.length;return r.forEach((i)=>{if(i[h])d=Math.max(d,i[h].length)}),d}),s=n.map((e,h)=>e.padEnd(t[h]));this.appendLine("| "+s.join(" | ")+" |");let o=t.map((e)=>"-".repeat(e));return this.appendLine("| "+o.join(" | ")+" |"),r.forEach((e)=>{let h=e.map((d,i)=>d.padEnd(t[i]));this.appendLine("| "+h.join(" | ")+" |")}),this}blockquote(n){let r=n.split(`
|
|
9
|
-
`);this.appendLine("> "+r[0]);for(let t=1;t<r.length;t++)this.appendLine("> "+r[t]);return this}bold(n){return this.append(`**${n}**`)}italic(n){return this.append(`*${n}*`)}item(n){return this.appendLine(`- ${n}`)}list(n){return n.forEach((r)=>this.item(r)),this}orderedList(n,r=1){return n.forEach((t,s)=>this.appendLine(`${(s+r).toString()}. ${t}`)),this}code(n){return this.append(`\`${n}\``)}horizontalRule(){return this.appendLine("---")}image(n,r){return this.appendLine(``)}strikethrough(n){return this.append(`~~${n}~~`)}highlight(n){return this.append(`==${n}==`)}subscript(n){return this.append(`~${n}~`)}superscript(n){return this.append(`^${n}^`)}taskList(n){return n.forEach((r)=>this.appendLine(`- [${r.checked?"x":" "}] ${r.text}`)),this}#n=0;section(n){return this.#n++,this.header(n,this.#n)}endSection(){if(this.#n>0)this.#n--;return this}setSectionLevel(n){return this.#n=n,this}}export{v as defined,u as StringBuilder,T as NavigateResult,E as NavigateAction,$ as Markdown,L as Logger};
|
|
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 O;((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"})(O||={});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,x)=>{let p=h===null,g=p?T.Explore():await n({key:h,value:o,path:i,parent:x,parents:s});if(o&&typeof o==="object"&&g.action===0){if(r.has(o))return g;if(r.add(o),!p)i.push(h),s.push(x);let d=o;while(await f(d,h,x))d=x[h];if(!p)s.pop(),i.pop()}return g},f=async(h,o,x)=>{let p=Object.keys(h),m=p.length;for(let g=0;g<m;g++){let b=p[g],u=h[b],d=await e(b,u,h),w=d.action;if(w===0||w===2)continue;if(w===6){if(h[b]=d.by,d.skipSiblings)return;continue}if(w===7){if(delete h[b],d.skipSiblings)return;continue}if(w===1)return;if(w===3){if(o===null)throw Error("Cannot replace root object");if(x[o]=d.by,d.reexplore)return!0;return}if(w===4){if(o===null)throw Error("Cannot delete root object");delete x[o];return}if(w===5)delete h[b],Object.assign(h,d.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 F=/^\[([^=\]]*)([=]*)([^\]]*)\]$/,L=/^([^[\]]*)\[([^\]]*)]$/,E=(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},a=(t,n,r,i)=>{let s=E(n,r,i);if(s)return Object.select(t,s,i);let e=F.exec(n);if(e){let[,h,o,x]=e,p=E(h.trim(),r,i,!0),m=E(x.trim(),r,i,!0);if(Array.isArray(t)&&(o==="="||o==="==")&&p)return t.find((g)=>g?.[p]==m)}let f=L.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,g)=>{let[b,u]=f(g.pop());if(!b){if(r?.set!==void 0&&s!==void 0&&i!==void 0)i[s]=r.set;return m}let d=a(m,b,t,r);if(d===void 0){if(u||r?.optional||r?.defaultValue!==void 0)return;throw Error(`Unknown path element: "${b}" in "${e}"`)}return i=m,s=b,h(d,g)},o=e.splitNested(r?.separator??".","{","}"),x=void 0;if(o.length>0&&o[o.length-1].startsWith("?="))x=o.pop()?.substring(2);o.reverse();let p=h(t,o);if(p===void 0)return x??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 c(this.split(`
|
|
2
|
+
`))};String.prototype.padBlock=function(t,n=`
|
|
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>`};class B{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 c{#t=[];#n=0;options;constructor(t,n){if(t instanceof c)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){let n=(this.options.indentChar??" ").repeat(this.#n*(this.options.indentSize??2));if(this.options.trim)t=t.trim();return(this.options.linePrefix??"")+n+t+(this.options.lineSuffix??"")}write(...t){return this.#t.push(...t.flatMap((n)=>{if(n===void 0)return;if(typeof n==="string")return n;if(n instanceof c)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.flatMap((n)=>{return[this.formatLine(n),this.options.lineSeparator??`
|
|
6
|
+
`]}))}writeMap(t,n){return t.forEach((r,i)=>this.write(n(r,i,this))),this}}class U extends c{header(t,n=1){return this.writeLine(`${"#".repeat(n)} ${t}`)}codeBlock(t,n){if(this.write("```"),n!==void 0)this.write(n);if(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){let n=t.split(`
|
|
7
|
+
`);this.writeLine("> "+n[0]);for(let r=1;r<n.length;r++)this.writeLine("> "+n[r]);return this}bold(t){return this.write(`**${t}**`)}italic(t){return this.write(`*${t}*`)}item(t){return this.writeLine(`- ${t}`)}items(t){return t.forEach((n)=>this.item(n)),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){return this.#t++,this.header(t,this.#t)}endSection(){if(this.#t>0)this.#t--;return this}setSectionLevel(t){return this.#t=t,this}}export{c as StringBuilder,T as NavigateResult,O as NavigateAction,U as Markdown,B as Logger};
|