Skip to content

Commit

Permalink
feat: add boolean value in message helper to configure line break for…
Browse files Browse the repository at this point in the history
… all position
  • Loading branch information
adbayb committed Nov 17, 2024
1 parent 65bac40 commit e5b2458
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-rings-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"termost": minor
---

Message helper accepts now a boolean to configure line breaks at once for all positions.
46 changes: 34 additions & 12 deletions termost/src/helpers/stdout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,43 @@ export const format = (
* @param options - The configuration object to define the display type and/or override the default label.
* @param options.label - The label to display.
* @param options.type - The message type.
* @param options.lineBreakByPosition - Configure line break addition.
* @param options.lineBreakByPosition.start - Configure line break addition in a start trailing position (by default, true).
* @param options.lineBreakByPosition.end - Configure line break addition in an end trailing position (by default, false).
* @param options.lineBreak - Configure line break addition.
* @example
* message("message to log");
*/
export const message = (
content: Error | string,
{
label: optionLabel,
lineBreakByPosition: optionLineBreakByPosition,
lineBreak: optionlineBreak,
type: optionType,
}: {
label?: string | false;
lineBreakByPosition?: { end: boolean; start: boolean };
lineBreak?: LineBreakByPosition | boolean;
type?: MessageType;
} = {},
) => {
const isTextualContent = typeof content === "string";
const type = optionType ?? (isTextualContent ? "information" : "error");
const { color, defaultLabel, icon, method } = formatPropertiesByType[type];
const lineBreakStart = optionLineBreakByPosition?.start ?? true;
const lineBreakEnd = optionLineBreakByPosition?.end ?? false;

const getLineBreak = (): LineBreakByPosition => {
if (optionlineBreak === undefined) {
return {
end: false,
start: true,
};
}

if (isRecord(optionlineBreak)) {
return optionlineBreak;
}

return {
end: optionlineBreak,
start: optionlineBreak,
};
};

const getLabel = () => {
if (optionLabel === false) {
Expand All @@ -73,9 +87,11 @@ export const message = (
return optionLabel ?? defaultLabel;
};

const lineBreak = getLineBreak();

method(
format(
`${lineBreakStart ? "\n" : ""}${icon} ${getLabel()}${lineBreakEnd ? "\n" : ""}`,
`${lineBreak.start ? "\n" : ""}${icon} ${getLabel()}${lineBreak.end ? "\n" : ""}`,
{
color,
modifiers: ["bold"],
Expand All @@ -87,16 +103,22 @@ export const message = (
method(isTextualContent ? format(` ${content}`, { color }) : content);
};

const compose = <T>(...fns: ((a: T) => T)[]) => {
if (!fns[0])
type LineBreakByPosition = { end: boolean; start: boolean };

const isRecord = (value: unknown): value is Record<string, unknown> => {
return typeof value === "object" && value !== null && !Array.isArray(value);
};

const compose = <T>(...functions: ((a: T) => T)[]) => {
if (!functions[0])
throw new Error(
"No function is provided, defeating the purpose of composing functions. Make sure to provide at least one function as an argument.",
);

return fns.reduce<(a: T) => T>(
return functions.reduce<(a: T) => T>(
(previousFunction, nextFunction) => (value) =>
previousFunction(nextFunction(value)),
fns[0],
functions[0],
);
};

Expand Down

0 comments on commit e5b2458

Please sign in to comment.