Added padding utility and updated logger object
All checks were successful
Build / Build NPM Project (push) Successful in 31s
Build / Tag Version (push) Successful in 7s
Build / Publish (push) Successful in 14s

This commit is contained in:
Zakary Timson 2024-02-28 23:43:07 -05:00
parent 24edc4a12d
commit 210f007e07
6 changed files with 25 additions and 9 deletions

View File

@ -25,9 +25,6 @@ jobs:
- name: Install Dependencies
run: npm i
- name: Build Project
run: npm run build
- name: Test
run: npm run test:coverage

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@ztimson/js-utilities",
"version": "0.0.0",
"version": "0.2.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@ztimson/js-utilities",
"version": "0.0.0",
"version": "0.2.1",
"license": "MIT",
"devDependencies": {
"@types/jest": "^29.5.12",

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/js-utilities",
"version": "0.2.1",
"version": "0.3.0",
"description": "JavaScript Utility library",
"author": "Zak Timson",
"license": "MIT",
@ -13,6 +13,7 @@
"module": "./dist/js-utilities.mjs",
"types": "./dist/src/index.d.ts",
"scripts": {
"postinstall": "npm run build",
"build": "vite build",
"test": "npx jest",
"test:coverage": "npx jest --coverage",

View File

@ -58,12 +58,22 @@ export type LoggerEvents = TypedEvents & {
export class Logger extends TypedEmitter<LoggerEvents> {
static LOG_LEVEL: LOG_LEVEL = LOG_LEVEL.INFO;
constructor(public readonly namespace: string) {
constructor(public readonly namespace?: string) {
super();
}
private pad(text: string, length: number, char: string, end = false) {
const l = length - text.length;
if(l <= 0) return text;
const padding = Array(~~(l / char.length)).fill(char).join('');
return !end ? padding + text : text + padding;
}
private format(...text: string[]): string {
return `${new Date().toISOString()} [${this.namespace}] ${text.join(' ')}`;
const now = new Date();
const timestamp = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${this.pad(now.getHours().toString(), 2, '0')}:${this.pad(now.getMinutes().toString(), 2, '0')}:${this.pad(now.getSeconds().toString(), 2, '0')}.${this.pad(now.getMilliseconds().toString(), 3, '0', true)}`;
return `${timestamp}${this.namespace ? ` [${this.namespace}]` : ''} ${text.join(' ')}`;
}
debug(...args: string[]) {
@ -86,7 +96,7 @@ export class Logger extends TypedEmitter<LoggerEvents> {
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
const str = this.format(...args);
Logger.emit(LOG_LEVEL.INFO, str);
console.info(CliForeground.CYAN + str + CliEffects.CLEAR);
console.info(CliForeground.BLUE + str + CliEffects.CLEAR);
}
}

View File

@ -52,6 +52,13 @@ export function insertAt(target: string, str: string, index: number): String {
return `${target.slice(0, index)}${str}${target.slice(index + 1)}`;
}
export function pad(text: string, length: number, char: string, start = true) {
const l = length - text.length;
if(l <= 0) return text;
const padding = Array(~~(l / char.length)).fill(char).join('');
return start ? padding + text : text + padding;
}
/**
* Generate a string of random characters.
*

View File

@ -8,6 +8,7 @@
"declarationMap": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"inlineSourceMap": true,
"lib": ["ESNext", "DOM"],
"module": "ES6",
"moduleResolution": "Node",