Skip to content

Commit

Permalink
feat: changed multiple commands to be used directly with console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
dworak-dev committed May 31, 2023
1 parent 8a816a5 commit dc5176b
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 51 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"prepare": "husky install",
"test": "jest"
"test": "jest",
"test:watch": "jest --watch"
},
"publishConfig": {
"access": "public"
Expand Down
31 changes: 31 additions & 0 deletions src/getTimeString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @file src/getTimeString.test.ts
* @author dworac <mail@dworac.com>
*
* This file contains the tests for the getTimeString function.
*/

import getTimeString from "./getTimeString";

describe("getTimeString", () => {
test("should return a string", () => {
const result = getTimeString();
expect(typeof result).toBe("string");
});

test("should return a string formatted as [hh:mm:ss]", () => {
const result = getTimeString();
const timeRegex = /\[\d{2}:\d{2}:\d{2}\]/;
expect(timeRegex.test(result)).toBeTruthy();
});

test("should return a string that starts with a [", () => {
const result = getTimeString();
expect(result.charAt(0)).toBe("[");
});

test("should return a string that ends with a ]", () => {
const result = getTimeString();
expect(result.charAt(result.length - 1)).toBe("]");
});
});
39 changes: 39 additions & 0 deletions src/getTimeString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @file src/getTimeString.ts
* @author dworac <mail@dworac.com>
*
* This file contains the getTimeString function.
*/

/**
* Pads a date part with a leading zero if necessary.
*
* @param {number} datePart - The date part to pad.
* @returns {string} The padded date part.
*/
export const padDatePart = (datePart: number) => {
const { length } = datePart.toString();
const padding = 2 - length;
return `${"0".repeat(padding)}${datePart.toString()}`;
};

/**
* Returns a string containing the current time in the format [hh:mm:ss].
*
* @returns {string} The current time.
* @example
* const time = getTimeString();
* console.log(time); // [12:34:56]
*/
const getTimeString = () => {
const date = new Date();
const hours = padDatePart(date.getHours());
const minutes = padDatePart(date.getMinutes());
const seconds = padDatePart(date.getSeconds());

const time = `${hours}:${minutes}:${seconds}`;

return `[${time}]`;
};

export default getTimeString;
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import Logger from "./logger";

// Logger.logInfo("hey", "another");
// Logger.logError("hey");
// Logger.logSuccess("hey");
// Logger.logInfo("first", "second");
// Logger.logSuccess("first", "second", 2, new Date());
// Logger.logError("first", "second", new Error("test"));

export default Logger;
16 changes: 8 additions & 8 deletions src/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,32 @@ describe("Logger", () => {
const message = "Info message";
Logger.logInfo(message);
expect(consoleSpy).toHaveBeenCalled();
expect(consoleSpy.mock.calls[0][0]).toContain(message);
expect(consoleSpy.mock.calls[0][0]).toContain("[INFO]");
expect(consoleSpy.mock.calls[0][1]).toContain("[INFO]");
expect(consoleSpy.mock.calls[0][2]).toContain(message);
});

it("should log success messages correctly", () => {
const message = "Success message";
Logger.logSuccess(message);
expect(consoleSpy).toHaveBeenCalled();
expect(consoleSpy.mock.calls[0][0]).toContain(message);
expect(consoleSpy.mock.calls[0][0]).toContain("[SUCCESS]");
expect(consoleSpy.mock.calls[0][1]).toContain("[SUCCESS]");
expect(consoleSpy.mock.calls[0][2]).toContain(message);
});

it("should log error messages correctly", () => {
const message = "Error message";
Logger.logError(message);
expect(consoleSpy).toHaveBeenCalled();
expect(consoleSpy.mock.calls[0][0]).toContain(message);
expect(consoleSpy.mock.calls[0][0]).toContain("[ERROR]");
expect(consoleSpy.mock.calls[0][1]).toContain("[ERROR]");
expect(consoleSpy.mock.calls[0][2]).toContain(message);
});

it("should log Error objects correctly", () => {
const error = new Error("Error object");
Logger.logError(error);
expect(consoleSpy).toHaveBeenCalled();
// expect(consoleSpy.mock.calls[0][0]).toContain(error.stack);
expect(consoleSpy.mock.calls[0][0]).toContain("[ERROR]");
expect(consoleSpy.mock.calls[0][1]).toContain("[ERROR]");
});

it("should log Error without stack objects correctly", () => {
Expand All @@ -55,6 +55,6 @@ describe("Logger", () => {
Logger.logError(error);
expect(consoleSpy).toHaveBeenCalled();
// expect(consoleSpy.mock.calls[0][0]).toContain(error.stack);
expect(consoleSpy.mock.calls[0][0]).toContain("[ERROR]");
expect(consoleSpy.mock.calls[0][1]).toContain("[ERROR]");
});
});
56 changes: 17 additions & 39 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,12 @@
* This file contains the Logger class.
*/
import chalk from "chalk";

const padDatePart = (part: number) => {
const { length } = part.toString();
const padding = 2 - length;
return `${"0".repeat(padding)}${part.toString()}`;
};

const getTimeString = () => {
const date = new Date();
const hours = padDatePart(date.getHours());
const minutes = padDatePart(date.getMinutes());
const seconds = padDatePart(date.getSeconds());

const time = `${hours}:${minutes}:${seconds}`;

return chalk.gray(`[${time}] `);
};
import getTimeString from "./getTimeString";

const paddPrefix = (prefix: string) => {
const { length } = prefix;
const padding = 9 - length;
return `${" ".repeat(padding)}${prefix} `;
return `${" ".repeat(padding)}${prefix}`;
};

/**
Expand All @@ -45,46 +29,40 @@ class Logger {
/**
* Logs an info message.
*
* @param {string[]} messages - Messages to be printed with blue highlight.
* @param {any[]} messages - Messages to be printed with blue highlight.
*/
static logInfo(...messages: string[]) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static logInfo(...messages: any[]) {
const prefix = chalk.blue(paddPrefix("[INFO]"));
const msg = chalk.white(messages.join(" "));
const time = chalk.gray(getTimeString());

const log = `${getTimeString()}${prefix}${msg}`;
console.log(log);
console.log(time, prefix, ...messages);
}

/**
* Logs a success message.
*
* @param {string[]} messages - Messages to be printed with green highlight.
* @param {any[]} messages - Messages to be printed with green highlight.
*/
static logSuccess(...messages: string[]) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static logSuccess(...messages: any[]) {
const prefix = chalk.green(paddPrefix("[SUCCESS]"));
const msg = chalk.white(messages.join(" "));
const time = chalk.gray(getTimeString());

const log = `${getTimeString()}${prefix}${msg}`;
console.log(log);
console.log(time, prefix, ...messages);
}

/**
* Logs an error message.
*
* @param {string} message - Message to be printed with a red highlight.
* @param {any[]} messages - Message to be printed with a red highlight.
*/
static logError(message: string | Error) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static logError(...messages: any[]) {
const prefix = chalk.red(paddPrefix("[ERROR]"));
let msg = "";

if (typeof message === "string") {
msg = chalk.white(message);
} else {
msg = chalk.white(message.stack || message.message);
}
const time = chalk.gray(getTimeString());

const log = `${getTimeString()}${prefix}${msg}`;
console.log(log);
console.log(time, prefix, ...messages);
}
}

Expand Down

0 comments on commit dc5176b

Please sign in to comment.