Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
63a5c5f6b7 | |||
64a0eac6df | |||
3e1341d876 | |||
b3457ed588 | |||
210f007e07 | |||
24edc4a12d |
3
.github/workflows/build.yaml
vendored
3
.github/workflows/build.yaml
vendored
@ -25,9 +25,6 @@ jobs:
|
|||||||
- name: Install Dependencies
|
- name: Install Dependencies
|
||||||
run: npm i
|
run: npm i
|
||||||
|
|
||||||
- name: Build Project
|
|
||||||
run: npm run build
|
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: npm run test:coverage
|
run: npm run test:coverage
|
||||||
|
|
||||||
|
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/js-utilities",
|
"name": "@ztimson/js-utilities",
|
||||||
"version": "0.0.0",
|
"version": "0.2.1",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@ztimson/js-utilities",
|
"name": "@ztimson/js-utilities",
|
||||||
"version": "0.0.0",
|
"version": "0.2.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.5.12",
|
"@types/jest": "^29.5.12",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/js-utilities",
|
"name": "@ztimson/js-utilities",
|
||||||
"version": "0.2.0",
|
"version": "0.3.1",
|
||||||
"description": "JavaScript Utility library",
|
"description": "JavaScript Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@ -13,10 +13,11 @@
|
|||||||
"module": "./dist/js-utilities.mjs",
|
"module": "./dist/js-utilities.mjs",
|
||||||
"types": "./dist/src/index.d.ts",
|
"types": "./dist/src/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "vite build",
|
"postinstall": "npm run build",
|
||||||
|
"build": "npx vite build",
|
||||||
"test": "npx jest",
|
"test": "npx jest",
|
||||||
"test:coverage": "npx jest --coverage",
|
"test:coverage": "npx jest --coverage",
|
||||||
"watch": "vite build --watch"
|
"watch": "npx vite build --watch"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.5.12",
|
"@types/jest": "^29.5.12",
|
||||||
|
@ -58,46 +58,61 @@ export type LoggerEvents = TypedEvents & {
|
|||||||
export class Logger extends TypedEmitter<LoggerEvents> {
|
export class Logger extends TypedEmitter<LoggerEvents> {
|
||||||
static LOG_LEVEL: LOG_LEVEL = LOG_LEVEL.INFO;
|
static LOG_LEVEL: LOG_LEVEL = LOG_LEVEL.INFO;
|
||||||
|
|
||||||
constructor(public readonly namespace: string) {
|
constructor(public readonly namespace?: string) {
|
||||||
super();
|
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 {
|
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[]) {
|
debug(...args: string[]) {
|
||||||
if(LOG_LEVEL.VERBOSE >= Logger.LOG_LEVEL) {
|
if(LOG_LEVEL.VERBOSE >= Logger.LOG_LEVEL) {
|
||||||
Logger.emit(LOG_LEVEL.VERBOSE, ...args);
|
const str = this.format(...args);
|
||||||
console.debug(CliForeground.LIGHT_GREY + this.format(...args) + CliEffects.CLEAR);
|
Logger.emit(LOG_LEVEL.VERBOSE, str);
|
||||||
|
console.debug(CliForeground.LIGHT_GREY + str + CliEffects.CLEAR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
error(...args: string[]) {
|
error(...args: string[]) {
|
||||||
if(LOG_LEVEL.ERROR >= Logger.LOG_LEVEL) {
|
if(LOG_LEVEL.ERROR >= Logger.LOG_LEVEL) {
|
||||||
Logger.emit(LOG_LEVEL.ERROR, ...args);
|
const str = this.format(...args);
|
||||||
console.error(CliForeground.RED + this.format(...args) + CliEffects.CLEAR);
|
Logger.emit(LOG_LEVEL.ERROR, str);
|
||||||
|
console.error(CliForeground.RED + str + CliEffects.CLEAR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
info(...args: string[]) {
|
info(...args: string[]) {
|
||||||
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
|
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
|
||||||
Logger.emit(LOG_LEVEL.INFO, ...args);
|
const str = this.format(...args);
|
||||||
console.info(CliForeground.CYAN + this.format(...args) + CliEffects.CLEAR);
|
Logger.emit(LOG_LEVEL.INFO, str);
|
||||||
|
console.info(CliForeground.BLUE + str + CliEffects.CLEAR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log(...args: string[]) {
|
log(...args: string[]) {
|
||||||
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
|
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
|
||||||
Logger.emit(LOG_LEVEL.INFO, ...args);
|
const str = this.format(...args);
|
||||||
console.log(CliEffects.CLEAR + this.format(...args));
|
Logger.emit(LOG_LEVEL.INFO, str);
|
||||||
|
console.log(CliEffects.CLEAR + str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
warn(...args: string[]) {
|
warn(...args: string[]) {
|
||||||
if(LOG_LEVEL.WARN >= Logger.LOG_LEVEL) {
|
if(LOG_LEVEL.WARN >= Logger.LOG_LEVEL) {
|
||||||
Logger.emit(LOG_LEVEL.WARN, ...args);
|
const str = this.format(...args);
|
||||||
console.warn(CliForeground.YELLOW + this.format(...args) + CliEffects.CLEAR);
|
Logger.emit(LOG_LEVEL.WARN, str);
|
||||||
|
console.warn(CliForeground.YELLOW + str + CliEffects.CLEAR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,13 @@ export function insertAt(target: string, str: string, index: number): String {
|
|||||||
return `${target.slice(0, index)}${str}${target.slice(index + 1)}`;
|
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.
|
* Generate a string of random characters.
|
||||||
*
|
*
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
"declarationMap": true,
|
"declarationMap": true,
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
|
"inlineSourceMap": true,
|
||||||
"lib": ["ESNext", "DOM"],
|
"lib": ["ESNext", "DOM"],
|
||||||
"module": "ES6",
|
"module": "ES6",
|
||||||
"moduleResolution": "Node",
|
"moduleResolution": "Node",
|
||||||
|
Reference in New Issue
Block a user