@autoplay/utils
Advanced tools
Comparing version 0.0.17 to 0.0.18
@@ -6,2 +6,4 @@ export * from "./internal/invariant"; | ||
export * from "./internal/match"; | ||
export * from "./internal/parseJSON"; | ||
export * from "./internal/createErrorObj"; | ||
export * from "./internal/staticCheck"; | ||
@@ -13,3 +15,2 @@ export * from "./internal/tightJsonStringify"; | ||
export * from "./internal/z/index"; | ||
export * from "./internal/errorToString"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -7,2 +7,4 @@ // Invariant errors with stack trace fixing | ||
export * from "./internal/match"; | ||
export * from "./internal/parseJSON"; | ||
export * from "./internal/createErrorObj"; | ||
export * from "./internal/staticCheck"; | ||
@@ -17,3 +19,1 @@ export * from "./internal/tightJsonStringify"; | ||
export * from "./internal/z/index"; | ||
// Error stringification including Zod types | ||
export * from "./internal/errorToString"; |
@@ -7,4 +7,13 @@ /** | ||
found: any; | ||
records: any; | ||
cause: any; | ||
constructor(message: string); | ||
toJSON(): { | ||
message: string; | ||
found: any; | ||
cause: any; | ||
records: any; | ||
stack: string | undefined; | ||
}; | ||
} | ||
//# sourceMappingURL=DevError.d.ts.map |
@@ -5,3 +5,3 @@ // regexes to remove lines from thrown error stacktraces | ||
const AT_INVARIANT_RE = /^\s*(at|[^@]+@) (?:Object\.)?invariant.+/gm; | ||
const AT_INVARIANT_MORE_RE = /^\s*at.+(?:invariant|asError).+/gm; | ||
const AT_INVARIANT_MORE_RE = /^\s*at.+(?:invariant|asError|createErrorObj).+/gm; | ||
const AT_TEST_HELPERS_RE = /^\s*(at|[^@]+@).+test\-helpers.+/gm; | ||
@@ -30,2 +30,11 @@ // const AT_WEB_MODULES = /^\s*(at|[^@]+@).+(web_modules|\-[a-f0-9]{8}\.js).*/gm | ||
} | ||
toJSON() { | ||
return { | ||
message: this.message, | ||
found: this.found, | ||
cause: this.cause, | ||
records: this.records, | ||
stack: this.stack, | ||
}; | ||
} | ||
} |
@@ -34,6 +34,7 @@ import { DevError } from "./DevError"; | ||
asError(): DevError; | ||
private _templateDisplay; | ||
toDisplay(): string; | ||
toString(): string; | ||
toJSON(key?: string): any[]; | ||
toJSON(): any[]; | ||
} | ||
//# sourceMappingURL=DevString.d.ts.map |
@@ -42,10 +42,18 @@ import { DevError } from "./DevError"; | ||
asError() { | ||
return new DevError(this.toDisplay()); | ||
const err = new DevError(this._templateDisplay()); | ||
if (this._values) { | ||
err.cause = this._values.cause; | ||
err.records = this._values.records; | ||
} | ||
return err; | ||
} | ||
// Notice that `"" + {toString() { return 1}} === "1"` | ||
toDisplay() { | ||
_templateDisplay() { | ||
const stringSubs = this._subs.map((sub) => devStringify(sub, true)); | ||
let display = typeof this._templateOrID === "number" | ||
return typeof this._templateOrID === "number" | ||
? `#${this._templateOrID}: ${stringSubs.join("; ")}` // if dev calls are replaced with message identifiers (this is speculative) | ||
: String.raw(this._templateOrID, ...stringSubs); | ||
} | ||
// Notice that `"" + {toString() { return 1}} === "1"` | ||
toDisplay() { | ||
let display = this._templateDisplay(); | ||
if (this._values) { | ||
@@ -65,3 +73,3 @@ if (this._values.cause) { | ||
} | ||
toJSON(key) { | ||
toJSON() { | ||
return typeof this._templateOrID === "number" | ||
@@ -68,0 +76,0 @@ ? this._values == null |
@@ -41,3 +41,3 @@ import { DevString } from "./DevString"; | ||
}); | ||
return display ? cleanNewlinesAndStacks(json.replace(/"([^"]+)":/g, "$1:")) : json; | ||
return display ? cleanNewlinesAndStacks(json.replace(/\\?"([^"]+)\\?":/g, "$1:")) : json; | ||
} | ||
@@ -56,4 +56,4 @@ } | ||
.replace(/(.+?)"(.*\\n(.(?!\\"))+|\\")*"/gm, (_fullMatch, beforeQuote, inside) => { | ||
return beforeQuote + `"` + inside.split(/\\n/g).join("\n" + " ".repeat(beforeQuote.length)) + '"'; | ||
return beforeQuote + (inside ? `"${inside.split(/\\n/g).join("\n" + " ".repeat(beforeQuote.length))}"` : '""'); | ||
})); | ||
} |
@@ -1,3 +0,2 @@ | ||
import { DevString } from "./DevString"; | ||
import { z } from "./z/index"; | ||
import type { DevString } from "./DevString"; | ||
declare type AllowedMessageTypes = DevString | string | number | object; | ||
@@ -19,4 +18,3 @@ /** | ||
export declare function invariantThrow(message: (() => AllowedMessageTypes) | AllowedMessageTypes, butFoundInstead?: any): never; | ||
/** Used with our (Autoplay) custom ZodChoice */ | ||
export declare function invariantChoiceIs<TVariants extends Record<string, any>, VariantName extends Extract<keyof TVariants, string>>(choice: z.ZodChoiceContainer<TVariants>, key: VariantName, name?: string): TVariants[VariantName]; | ||
export declare function identity<T>(x: T): T; | ||
/** Asserts that the `left` strict equals the `right` */ | ||
@@ -23,0 +21,0 @@ export declare function invariantEq(left: any, |
@@ -33,9 +33,3 @@ import { DevError } from "./DevError"; | ||
} | ||
/** Used with our (Autoplay) custom ZodChoice */ | ||
export function invariantChoiceIs(choice, key, name = "choice") { | ||
return choice.matchPartial({ [key]: identity }, (other) => { | ||
invariantThrow(`Expected ${name} to be "${key}"`, other); | ||
}); | ||
} | ||
function identity(x) { | ||
export function identity(x) { | ||
return x; | ||
@@ -42,0 +36,0 @@ } |
@@ -8,4 +8,6 @@ /** | ||
* during runtime. | ||
* | ||
* This might be replaceable with TypeScript 4.9's `satisfies` operator. | ||
*/ | ||
export declare function staticCheck<T>(x: T): T; | ||
//# sourceMappingURL=staticCheck.d.ts.map |
@@ -8,2 +8,4 @@ /** | ||
* during runtime. | ||
* | ||
* This might be replaceable with TypeScript 4.9's `satisfies` operator. | ||
*/ | ||
@@ -10,0 +12,0 @@ export function staticCheck(x) { |
/** Strings you're willing to send to the user with minor formatting capabilities. */ | ||
export declare class UsrString { | ||
readonly _marks: UsrMarkKind[]; | ||
readonly _children: UsrStringChild[]; | ||
constructor(_marks: UsrMarkKind[], _children: UsrStringChild[]); | ||
readonly marks: UsrMarkKind[]; | ||
readonly children: UsrStringChild[]; | ||
constructor(marks: UsrMarkKind[], children: UsrStringChild[]); | ||
toString(): string; | ||
@@ -13,4 +13,11 @@ toMarkdown(): string; | ||
export declare const usr: UsrConstructor; | ||
declare type UsrMarkKind = "bold" | "em" | { | ||
link: string; | ||
declare type UsrMarkKind = { | ||
type: "bold"; | ||
} | { | ||
type: "em"; | ||
} | { | ||
type: "link"; | ||
link: { | ||
href: string; | ||
}; | ||
}; | ||
@@ -17,0 +24,0 @@ declare type UsrStringChild = string | number | UsrString; |
/** Strings you're willing to send to the user with minor formatting capabilities. */ | ||
export class UsrString { | ||
constructor(_marks, _children) { | ||
this._marks = _marks; | ||
this._children = _children; | ||
constructor(marks, children) { | ||
this.marks = marks; | ||
this.children = children; | ||
} | ||
@@ -14,6 +14,6 @@ toString() { | ||
toPlainText() { | ||
let disp = this._children.map((c) => c.toString()).filter(Boolean); | ||
for (const m of this._marks) { | ||
if (typeof m === "object") { | ||
disp = [...disp, " (", m.link, ")"]; | ||
let disp = this.children.map((c) => c.toString()).filter(Boolean); | ||
for (const m of this.marks) { | ||
if (m.type === "link") { | ||
disp = [...disp, " (", m.link.href, ")"]; | ||
} | ||
@@ -27,10 +27,10 @@ } | ||
_flattenedMd() { | ||
let disp = this._children.flatMap((c) => c instanceof UsrString ? c._flattenedMd() : [c]).filter(Boolean); | ||
for (const m of this._marks) { | ||
if (m === "bold") | ||
let disp = this.children.flatMap((c) => (c instanceof UsrString ? c._flattenedMd() : [c])).filter(Boolean); | ||
for (const m of this.marks) { | ||
if (m.type === "bold") | ||
disp = ["**", ...disp, "**"]; | ||
else if (m === "em") | ||
else if (m.type === "em") | ||
disp = ["*", ...disp, "*"]; | ||
else | ||
disp = ["[", ...disp, "](", m.link, ")"]; | ||
disp = ["[", ...disp, "](", m.link.href, ")"]; | ||
} | ||
@@ -43,8 +43,8 @@ return disp; | ||
text, | ||
em: _usr.bind(null, ["em"]), | ||
bold: _usr.bind(null, ["bold"]), | ||
link: (displayText, href) => _usr([{ link: href.toString() }], displayText), | ||
em: _usr.bind(null, [{ type: "em" }]), | ||
bold: _usr.bind(null, [{ type: "bold" }]), | ||
link: (displayText, href) => _usr([{ type: "link", link: { href: href.toString() } }], displayText), | ||
}); | ||
function _usr(_marks, template, ...elements) { | ||
return new UsrString(_marks, Array.isArray(template) ? template.flatMap((a, i) => (i < elements.length ? [a, elements[i]] : [a])) : [template]); | ||
function _usr(marks, template, ...elements) { | ||
return new UsrString(marks, Array.isArray(template) ? template.flatMap((a, i) => (i < elements.length ? [a, elements[i]] : [a])) : [template]); | ||
} |
export * as z from "./z"; | ||
export * from "./invariantChoiceIs"; | ||
export * from "./errorToString"; | ||
//# sourceMappingURL=index.d.ts.map |
export * as z from "./z"; | ||
// Extra invariant assertion | ||
export * from "./invariantChoiceIs"; | ||
// Error stringification including Zod types | ||
export * from "./errorToString"; |
{ | ||
"name": "@autoplay/utils", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"description": "Utilities designed for error management, parsing, and TypeScript domain modeling.", | ||
@@ -20,3 +20,3 @@ "keywords": [], | ||
], | ||
"author": "Autoplay Team <dev@autoplay.so>", | ||
"author": "Cole Lawrence <cole@colelawrence.com>", | ||
"repository": { | ||
@@ -38,5 +38,5 @@ "type": "git", | ||
"tslib": "^2.4", | ||
"typescript": "^4.7.4", | ||
"zod": "^3.17.10" | ||
"typescript": "^4.8.4", | ||
"zod": "^3.19.1" | ||
} | ||
} |
@@ -7,2 +7,4 @@ // Invariant errors with stack trace fixing | ||
export * from "./internal/match"; | ||
export * from "./internal/parseJSON"; | ||
export * from "./internal/createErrorObj"; | ||
export * from "./internal/staticCheck"; | ||
@@ -17,3 +19,1 @@ export * from "./internal/tightJsonStringify"; | ||
export * from "./internal/z/index"; | ||
// Error stringification including Zod types | ||
export * from "./internal/errorToString"; |
@@ -5,3 +5,3 @@ // regexes to remove lines from thrown error stacktraces | ||
const AT_INVARIANT_RE = /^\s*(at|[^@]+@) (?:Object\.)?invariant.+/gm; | ||
const AT_INVARIANT_MORE_RE = /^\s*at.+(?:invariant|asError).+/gm; | ||
const AT_INVARIANT_MORE_RE = /^\s*at.+(?:invariant|asError|createErrorObj).+/gm; | ||
const AT_TEST_HELPERS_RE = /^\s*(at|[^@]+@).+test\-helpers.+/gm; | ||
@@ -17,2 +17,4 @@ // const AT_WEB_MODULES = /^\s*(at|[^@]+@).+(web_modules|\-[a-f0-9]{8}\.js).*/gm | ||
found: any; | ||
records: any; | ||
cause: any; | ||
constructor(message: string) { | ||
@@ -33,2 +35,12 @@ super(message); | ||
} | ||
toJSON() { | ||
return { | ||
message: this.message, | ||
found: this.found, | ||
cause: this.cause, | ||
records: this.records, | ||
stack: this.stack, | ||
}; | ||
} | ||
} |
import { DevError } from "./DevError"; | ||
import { devStringify } from "./devStringify"; | ||
import { invariantThrow } from "./invariant"; | ||
@@ -60,12 +59,20 @@ /** | ||
asError(): DevError { | ||
return new DevError(this.toDisplay()); | ||
const err = new DevError(this._templateDisplay()); | ||
if (this._values) { | ||
err.cause = this._values.cause | ||
err.records = this._values.records | ||
} | ||
return err | ||
} | ||
private _templateDisplay(): string { | ||
const stringSubs = this._subs.map((sub) => devStringify(sub, true)); | ||
return typeof this._templateOrID === "number" | ||
? `#${this._templateOrID}: ${stringSubs.join("; ")}` // if dev calls are replaced with message identifiers (this is speculative) | ||
: String.raw(this._templateOrID as TemplateStringsArray, ...stringSubs); | ||
} | ||
// Notice that `"" + {toString() { return 1}} === "1"` | ||
toDisplay() { | ||
const stringSubs = this._subs.map((sub) => devStringify(sub, true)); | ||
let display = | ||
typeof this._templateOrID === "number" | ||
? `#${this._templateOrID}: ${stringSubs.join("; ")}` // if dev calls are replaced with message identifiers (this is speculative) | ||
: String.raw(this._templateOrID as TemplateStringsArray, ...stringSubs); | ||
let display = this._templateDisplay() | ||
if (this._values) { | ||
@@ -87,3 +94,3 @@ if (this._values.cause) { | ||
toJSON(key?: string) { | ||
toJSON() { | ||
return typeof this._templateOrID === "number" | ||
@@ -90,0 +97,0 @@ ? this._values == null |
@@ -39,3 +39,3 @@ import { DevString } from "./DevString"; | ||
}); | ||
return display ? cleanNewlinesAndStacks(json.replace(/"([^"]+)":/g, "$1:")) : json; | ||
return display ? cleanNewlinesAndStacks(json.replace(/\\?"([^"]+)\\?":/g, "$1:")) : json; | ||
} | ||
@@ -55,5 +55,5 @@ } catch (err) { | ||
.replace(/(.+?)"(.*\\n(.(?!\\"))+|\\")*"/gm, (_fullMatch, beforeQuote, inside) => { | ||
return beforeQuote + `"` + inside.split(/\\n/g).join("\n" + " ".repeat(beforeQuote.length)) + '"'; | ||
return beforeQuote + (inside ? `"${inside.split(/\\n/g).join("\n" + " ".repeat(beforeQuote.length))}"` : '""'); | ||
}) | ||
); | ||
} |
import { DevError } from "./DevError"; | ||
import { DevString } from "./DevString"; | ||
import type { DevString } from "./DevString"; | ||
import { devStringify } from "./devStringify"; | ||
import { z } from "./z/index"; | ||
@@ -46,13 +45,3 @@ type AllowedMessageTypes = DevString | string | number | object; | ||
/** Used with our (Autoplay) custom ZodChoice */ | ||
export function invariantChoiceIs< | ||
TVariants extends Record<string, any>, | ||
VariantName extends Extract<keyof TVariants, string>, | ||
>(choice: z.ZodChoiceContainer<TVariants>, key: VariantName, name = "choice"): TVariants[VariantName] { | ||
return choice.matchPartial({ [key]: identity } as Partial<any>, (other) => { | ||
invariantThrow(`Expected ${name} to be "${key}"`, other); | ||
}); | ||
} | ||
function identity<T>(x: T): T { | ||
export function identity<T>(x: T): T { | ||
return x; | ||
@@ -111,3 +100,2 @@ } | ||
class InvariantError extends DevError { | ||
found: any; | ||
constructor(message: string, found?: any) { | ||
@@ -114,0 +102,0 @@ super(message); |
@@ -8,2 +8,4 @@ /** | ||
* during runtime. | ||
* | ||
* This might be replaceable with TypeScript 4.9's `satisfies` operator. | ||
*/ | ||
@@ -10,0 +12,0 @@ export function staticCheck<T>(x: T): T { |
/** Strings you're willing to send to the user with minor formatting capabilities. */ | ||
export class UsrString { | ||
constructor(public readonly _marks: UsrMarkKind[], public readonly _children: UsrStringChild[]) {} | ||
constructor(public readonly marks: UsrMarkKind[], public readonly children: UsrStringChild[]) {} | ||
toString(): string { | ||
@@ -11,6 +11,6 @@ return this.toPlainText(); | ||
toPlainText(): string { | ||
let disp = this._children.map((c) => c.toString()).filter(Boolean); | ||
for (const m of this._marks) { | ||
if (typeof m === "object") { | ||
disp = [...disp, " (", m.link, ")"]; | ||
let disp = this.children.map((c) => c.toString()).filter(Boolean); | ||
for (const m of this.marks) { | ||
if (m.type === "link") { | ||
disp = [...disp, " (", m.link.href, ")"]; | ||
} | ||
@@ -21,10 +21,10 @@ } | ||
toJSON(key?: unknown): unknown { | ||
return this._flattenedMd() | ||
return this._flattenedMd(); | ||
} | ||
private _flattenedMd(): (string | number)[] { | ||
let disp = this._children.flatMap((c) => c instanceof UsrString ? c._flattenedMd() : [c]).filter(Boolean); | ||
for (const m of this._marks) { | ||
if (m === "bold") disp = ["**", ...disp, "**"]; | ||
else if (m === "em") disp = ["*", ...disp, "*"]; | ||
else disp = ["[", ...disp, "](", m.link, ")"]; | ||
let disp = this.children.flatMap((c) => (c instanceof UsrString ? c._flattenedMd() : [c])).filter(Boolean); | ||
for (const m of this.marks) { | ||
if (m.type === "bold") disp = ["**", ...disp, "**"]; | ||
else if (m.type === "em") disp = ["*", ...disp, "*"]; | ||
else disp = ["[", ...disp, "](", m.link.href, ")"]; | ||
} | ||
@@ -38,16 +38,21 @@ return disp; | ||
text, | ||
em: _usr.bind(null, ["em"]), | ||
bold: _usr.bind(null, ["bold"]), | ||
link: (displayText: UsrStringChild, href: string) => _usr([{ link: href.toString() }], displayText), | ||
em: _usr.bind(null, [{ type: "em" }]), | ||
bold: _usr.bind(null, [{ type: "bold" }]), | ||
link: (displayText: UsrStringChild, href: string | URL) => | ||
_usr([{ type: "link", link: { href: href.toString() } }], displayText), | ||
}); | ||
type UsrMarkKind = | ||
| "bold" | ||
| "em" | ||
| { type: "bold" } | ||
| { type: "em" } | ||
| { | ||
link: string; | ||
type: "link"; | ||
link: { | ||
href: string; | ||
}; | ||
}; | ||
type UsrStringChild = string | number | UsrString; | ||
function _usr( | ||
_marks: UsrMarkKind[], | ||
marks: UsrMarkKind[], | ||
template: TemplateStringsArray | UsrStringChild, | ||
@@ -57,3 +62,3 @@ ...elements: UsrStringChild[] | ||
return new UsrString( | ||
_marks, | ||
marks, | ||
Array.isArray(template) ? template.flatMap((a, i) => (i < elements.length ? [a, elements[i]] : [a])) : [template], | ||
@@ -70,3 +75,3 @@ ); | ||
bold: UsrFn; | ||
link(displayText: UsrStringChild, href: string | URL): UsrString | ||
link(displayText: UsrStringChild, href: string | URL): UsrString; | ||
} |
export * as z from "./z"; | ||
// Extra invariant assertion | ||
export * from "./invariantChoiceIs"; | ||
// Error stringification including Zod types | ||
export * from "./errorToString"; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
107607
84
2304