Compare commits

..

No commits in common. "d4996201cfd41f2c16ae4218775ea4a589b20e4d" and "e4c7dea9a56f287bc009e0b7829604a0baca4e87" have entirely different histories.

3 changed files with 25 additions and 90 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@ztimson/js-utilities", "name": "@ztimson/js-utilities",
"version": "0.2.0", "version": "0.1.0",
"description": "JavaScript Utility library", "description": "JavaScript Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@ -2,44 +2,17 @@ export type Listener = (...args: any[]) => any;
export type TypedEvents = {[k in string | symbol]: Listener} & {'*': (event: string, ...args: any[]) => any}; export type TypedEvents = {[k in string | symbol]: Listener} & {'*': (event: string, ...args: any[]) => any};
export class TypedEmitter<T extends TypedEvents = TypedEvents> { export class TypedEmitter<T extends TypedEvents = TypedEvents> {
private static listeners: {[key: string]: Listener[]} = {}; private listeners: { [key in keyof T]?: Function[] } = {};
private listeners: { [key in keyof T]?: Listener[] } = {};
static emit(event: any, ...args: any[]) {
(this.listeners['*'] || []).forEach(l => l(event, ...args));
(this.listeners[event.toString()] || []).forEach(l => l(...args));
};
static off(event: any, listener: Listener) {
const e = event.toString();
this.listeners[e] = (this.listeners[e] || []).filter(l => l === listener);
}
static on(event: any, listener: Listener) {
const e = event.toString();
if(!this.listeners[e]) this.listeners[e] = [];
this.listeners[e]?.push(listener);
return () => this.off(event, listener);
}
static once(event: any, listener?: Listener): Promise<any> {
return new Promise(res => {
const unsubscribe = this.on(event, <any>((...args: any) => {
res(args.length == 1 ? args[0] : args);
if(listener) listener(...args);
unsubscribe();
}));
});
}
emit<K extends keyof T>(event: K, ...args: Parameters<T[K]>) { emit<K extends keyof T>(event: K, ...args: Parameters<T[K]>) {
(this.listeners['*'] || []).forEach(l => l(event, ...args)); (this.listeners['*'] || []).forEach(l => l(event, ...args));
(this.listeners[event] || []).forEach(l => l(...args)); (this.listeners[event] || []).forEach(l => l(...args));
}; };
off<K extends keyof T = string>(event: K, listener: T[K]) { off<K extends keyof T = string>(event: K, listener: T[K]): this {
console.log('cleared');
this.listeners[event] = (this.listeners[event] || []).filter(l => l === listener); this.listeners[event] = (this.listeners[event] || []).filter(l => l === listener);
return this;
} }
on<K extends keyof T = string>(event: K, listener: T[K]) { on<K extends keyof T = string>(event: K, listener: T[K]) {

View File

@ -1,5 +1,3 @@
import {TypedEmitter, TypedEvents} from './emitter';
export const CliEffects = { export const CliEffects = {
CLEAR: "\x1b[0m", CLEAR: "\x1b[0m",
BRIGHT: "\x1b[1m", BRIGHT: "\x1b[1m",
@ -11,22 +9,15 @@ export const CliEffects = {
} }
export const CliForeground = { export const CliForeground = {
BLACK: '\x1b[30m', BLACK: "\x1b[30m",
RED: '\x1b[31m', RED: "\x1b[31m",
GREEN: '\x1b[32m', GREEN: "\x1b[32m",
YELLOW: '\x1b[33m', YELLOW: "\x1b[33m",
BLUE: '\x1b[34m', BLUE: "\x1b[34m",
MAGENTA: '\x1b[35m', MAGENTA: "\x1b[35m",
CYAN: '\x1b[36m', CYAN: "\x1b[36m",
LIGHT_GREY: '\x1b[37m', WHITE: "\x1b[37m",
GREY: '\x1b[90m', GREY: "\x1b[90m",
LIGHT_RED: '\x1b[91m',
LIGHT_GREEN: '\x1b[92m',
LIGHT_YELLOW: '\x1b[93m',
LIGHT_BLUE: '\x1b[94m',
LIGHT_MAGENTA: '\x1b[95m',
LIGHT_CYAN: '\x1b[96m',
WHITE: '\x1b[97m',
} }
export const CliBackground = { export const CliBackground = {
@ -41,63 +32,34 @@ export const CliBackground = {
GREY: "\x1b[100m", GREY: "\x1b[100m",
} }
export enum LOG_LEVEL { export class Logger {
VERBOSE, constructor(public readonly namespace: string) { }
INFO,
WARN,
ERROR
}
export type LoggerEvents = TypedEvents & {
'VERBOSE': (...args: any[]) => any;
'INFO': (...args: any[]) => any;
'WARN': (...args: any[]) => any;
'ERROR': (...args: any[]) => any;
};
export class Logger extends TypedEmitter<LoggerEvents> {
static LOG_LEVEL: LOG_LEVEL = LOG_LEVEL.INFO;
constructor(public readonly namespace: string) {
super();
}
private format(...text: string[]): string { private format(...text: string[]): string {
return `${new Date().toISOString()} [${this.namespace}] ${text.join(' ')}`; return `${new Date().toISOString()} [${this.namespace}] ${text.join(' ')}`;
} }
debug(...args: string[]) { debug(...args: string[]) {
if(LOG_LEVEL.VERBOSE >= Logger.LOG_LEVEL) { console.log(CliForeground.MAGENTA + this.format(...args) + CliEffects.CLEAR);
Logger.emit(LOG_LEVEL.VERBOSE, ...args);
console.debug(CliForeground.LIGHT_GREY + this.format(...args) + CliEffects.CLEAR);
}
} }
error(...args: string[]) { error(...args: string[]) {
if(LOG_LEVEL.ERROR >= Logger.LOG_LEVEL) { console.log(CliForeground.RED + this.format(...args) + CliEffects.CLEAR);
Logger.emit(LOG_LEVEL.ERROR, ...args);
console.error(CliForeground.RED + this.format(...args) + CliEffects.CLEAR);
}
} }
info(...args: string[]) { info(...args: string[]) {
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) { console.log(CliForeground.CYAN + this.format(...args) + CliEffects.CLEAR);
Logger.emit(LOG_LEVEL.INFO, ...args);
console.info(CliForeground.CYAN + this.format(...args) + CliEffects.CLEAR);
}
} }
log(...args: string[]) { log(...args: string[]) {
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
Logger.emit(LOG_LEVEL.INFO, ...args);
console.log(CliEffects.CLEAR + this.format(...args)); console.log(CliEffects.CLEAR + this.format(...args));
} }
}
warn(...args: string[]) { warn(...args: string[]) {
if(LOG_LEVEL.WARN >= Logger.LOG_LEVEL) { console.log(CliForeground.YELLOW + this.format(...args) + CliEffects.CLEAR);
Logger.emit(LOG_LEVEL.WARN, ...args);
console.warn(CliForeground.YELLOW + this.format(...args) + CliEffects.CLEAR);
} }
verbose(...args: string[]) {
console.log(CliForeground.WHITE + this.format(...args) + CliEffects.CLEAR);
} }
} }