Skip to content

Commit

Permalink
♻️ ConsoleSpan to include string in the type
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Dec 29, 2023
1 parent 43dd291 commit 1a60d4a
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 89 deletions.
7 changes: 6 additions & 1 deletion src/core/ConsoleSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { ConsoleObject } from "./consoleObject";
import { ConsoleFlush } from "./consoleFlush";
import { ConsoleGroup } from "./consoleGroup";

type ConsoleSpan = ConsoleText | ConsoleObject | ConsoleGroup | ConsoleFlush;
type ConsoleSpan =
| string
| ConsoleText
| ConsoleObject
| ConsoleGroup
| ConsoleFlush;

export default ConsoleSpan;
28 changes: 16 additions & 12 deletions src/core/consoleApply.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import ConsoleSpan from "./ConsoleSpan";
import ConsoleStyle from "./ConsoleStyle";
import { ConsoleText, consoleText } from "./consoleText";
import toFlatSpans from "../utils/toFlatSpans";

export default function consoleApply<T extends ConsoleSpan>(
spans: T[],
spans: T | T[] | T[][],
style: ConsoleStyle,
): T[] {
return spans.map((span) => {
return span.type === "text"
? {
...span,
style: {
...span.style,
...style,
},
}
: span;
});
return toFlatSpans(spans).map((span) => {
return typeof span === "string"
? consoleText(span, style)
: span.type === "text"
? {
...(span as ConsoleText),
style: {
...span.style,
...style,
},
}
: span;
}) as T[];
}
30 changes: 10 additions & 20 deletions src/core/consoleGroup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import arrayArg from "../utils/arrayArg";
import toFlatSpans from "../utils/toFlatSpans";
import ConsoleSpan from "./ConsoleSpan";
import { consoleText, ConsoleText } from "./consoleText";
import ensureConsoleText from "../utils/ensureConsoleText";
import { ConsoleText } from "./consoleText";
import consoleApply from "./consoleApply";

export interface ConsoleGroup {
type: "group";
expanded: boolean;
header: ConsoleText[];
header: (ConsoleText | string)[];
body: ConsoleSpan[];
}

Expand All @@ -16,25 +16,15 @@ export function consoleGroup({
body,
}: {
expanded?: boolean;
header?: string | ConsoleText | (string | ConsoleText)[];
body?: (ConsoleSpan | string)[] | string;
header?: ConsoleText | string | (ConsoleText | string)[];
body?: ConsoleSpan | ConsoleSpan[];
} = {}): ConsoleGroup {
return {
type: "group",
expanded: expanded ?? false,
header:
arrayArg(header).map((span) => {
const consoleText = ensureConsoleText(span);
return {
...consoleText,
style: {
fontWeight: "normal",
...consoleText.style,
},
};
}) ?? [],
body: arrayArg(body).map((span) =>
typeof span === "string" ? consoleText(span) : span,
),
header: consoleApply(toFlatSpans(header), {
fontWeight: "normal",
}),
body: toFlatSpans(body),
};
}
85 changes: 43 additions & 42 deletions src/core/consolePrint.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,71 @@
import ConsoleSpan from "./ConsoleSpan";
import { ConsoleText } from "./consoleText";
import ConsoleStyle from "./ConsoleStyle";
import arrayArg from "../utils/arrayArg";
import toFlatSpans from "../utils/toFlatSpans";

interface ConsoleBuffer {
text: string;
rest: (string | object)[];
}

export default function consolePrint(
...args: (ConsoleSpan | ConsoleSpan[])[]
...args: (ConsoleSpan | ConsoleSpan[] | ConsoleSpan[][])[]
): void {
let logBuffer: LogBuffer = {
const buffer: ConsoleBuffer = {
text: "",
rest: [],
};

const items = args.flatMap(arrayArg);
for (const item of items) {
if (item.type === "text") {
logBuffer.text += `%c${item.text}%c`;
logBuffer.rest.push(consoleStyleToString(item.style));
logBuffer.rest.push("");
} else if (item.type === "object") {
logBuffer.text += "%o";
logBuffer.rest.push(item.object);
} else if (item.type === "flush") {
flush(logBuffer);
} else if (item.type === "group") {
flush(logBuffer);
const spans = toFlatSpans(...args);
for (const span of spans) {
if (typeof span === "string" || span.type === "text") {
appendBuffer(buffer, [span]);
} else if (span.type === "object") {
buffer.text += "%o";
buffer.rest.push(span.object);
} else if (span.type === "flush") {
flushBuffer(buffer);
} else if (span.type === "group") {
flushBuffer(buffer);

const merged = mergeText(item.header);
if (item.expanded) {
console.group(merged.text, ...merged.rest);
appendBuffer(buffer, span.header);

if (span.expanded) {
console.group(buffer.text, ...buffer.rest);
} else {
console.groupCollapsed(merged.text, ...merged.rest);
console.groupCollapsed(buffer.text, ...buffer.rest);
}

consolePrint(item.body);
consolePrint(span.body);

console.groupEnd();
}
}

flush(logBuffer);
flushBuffer(buffer);
}

interface LogBuffer {
text: string;
rest: (string | object)[];
}

function flush(logBuffer: LogBuffer): void {
if (logBuffer.text !== "") {
console.log(logBuffer.text, ...logBuffer.rest);
function appendBuffer(
buffer: ConsoleBuffer,
spans: (ConsoleText | string)[],
): void {
for (const span of spans) {
if (typeof span === "string") {
buffer.text += span;
} else {
buffer.text += `%c${span.text}%c`;
buffer.rest.push(consoleStyleToString(span.style));
buffer.rest.push("");
}
}
logBuffer.text = "";
logBuffer.rest = [];
}

function mergeText(spans: ConsoleText[]): LogBuffer {
const merged: LogBuffer = {
text: "",
rest: [],
};
for (const span of spans) {
merged.text += `%c${span.text}%c`;
merged.rest.push(consoleStyleToString(span.style));
merged.rest.push("");
function flushBuffer(buffer: ConsoleBuffer): void {
if (buffer.text !== "") {
console.log(buffer.text, ...buffer.rest);
}
return merged;
buffer.text = "";
buffer.rest = [];
}

function consoleStyleToString(style: ConsoleStyle): string {
Expand Down
8 changes: 4 additions & 4 deletions src/extras/consoleOrderedList.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { consoleText, ConsoleText } from "../core/consoleText";
import ensureConsoleText from "../utils/ensureConsoleText";

export default function consoleOrderedList(
items: (string | ConsoleText)[],
export default function consoleUnorderedList(
spans: (string | ConsoleText)[],
): ConsoleText[] {
return items.flatMap((item, index) => {
return spans.flatMap((span, index) => {
const line = [
//
consoleText(`${index + 1}. `),
ensureConsoleText(item),
ensureConsoleText(span),
];
return index === 0 ? line : [consoleText("\n"), ...line];
});
Expand Down
6 changes: 3 additions & 3 deletions src/extras/consoleUnorderedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { consoleText, ConsoleText } from "../core/consoleText";
import ensureConsoleText from "../utils/ensureConsoleText";

export default function consoleUnorderedList(
items: (string | ConsoleText)[],
spans: (string | ConsoleText)[],
): ConsoleText[] {
return items.flatMap((item, index) => {
return spans.flatMap((span, index) => {
const line = [
//
consoleText("• "),
ensureConsoleText(item),
ensureConsoleText(span),
];
return index === 0 ? line : [consoleText("\n"), ...line];
});
Expand Down
7 changes: 0 additions & 7 deletions src/utils/arrayArg.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/utils/toFlatSpans.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ConsoleSpan } from "../../index";

export default function toFlatSpans<T extends ConsoleSpan>(
...args: (T | T[] | T[][] | undefined | null)[]
): T[] {
return args.flatMap((arg) => {
return arg === undefined || arg === null
? []
: Array.isArray(arg)
? (arg.flat(Infinity) as T[])
: [arg];
});
}

0 comments on commit 1a60d4a

Please sign in to comment.