Compare commits

..

4 Commits
0.0.0 ... 0.2.0

Author SHA1 Message Date
d4996201cf Added static events to the emitter & added it to the logger
All checks were successful
Build / Build NPM Project (push) Successful in 33s
Build / Tag Version (push) Successful in 10s
Build / Publish (push) Successful in 16s
2024-02-28 19:19:24 -05:00
74c89c3ed4 Updated logger to use log levels 2024-02-28 19:08:51 -05:00
e4c7dea9a5 Added typed emitter
All checks were successful
Build / Build NPM Project (push) Successful in 31s
Build / Tag Version (push) Successful in 7s
Build / Publish (push) Successful in 12s
2024-02-23 21:20:44 -05:00
ffaf8558b2 Fixed xhr
All checks were successful
Build / Build NPM Project (push) Successful in 15s
Build / Tag Version (push) Successful in 5s
Build / Publish (push) Successful in 13s
2024-02-07 23:14:37 -05:00
4 changed files with 111 additions and 40 deletions

View File

@ -1,9 +1,10 @@
{ {
"name": "@ztimson/js-utilities", "name": "@ztimson/js-utilities",
"version": "0.0.0", "version": "0.2.0",
"description": "JavaScript Utility library", "description": "JavaScript Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",
"private": false,
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://git.zakscode.com/ztimson/js-utilities" "url": "https://git.zakscode.com/ztimson/js-utilities"

View File

@ -1,28 +1,60 @@
export type Listener<T> = (event: T) => any; export type Listener = (...args: any[]) => any;
export type TypedEvents = {[k in string | symbol]: Listener} & {'*': (event: string, ...args: any[]) => any};
export class Emitter<T> { export class TypedEmitter<T extends TypedEvents = TypedEvents> {
private listeners: {[key: string]: Listener<T>} = {}; private static listeners: {[key: string]: Listener[]} = {};
constructor() { } private listeners: { [key in keyof T]?: Listener[] } = {};
emit(e: T) { static emit(event: any, ...args: any[]) {
Object.values(this.listeners).forEach(l => l(e)); (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);
} }
listen(fn: Listener<T>): () => {}; static on(event: any, listener: Listener) {
listen(key: string, fn: Listener<T>): () => {}; const e = event.toString();
listen(keyOrFn: string | Listener<T>, fn?: Listener<T>): () => {} { if(!this.listeners[e]) this.listeners[e] = [];
const func: any = fn ? fn : keyOrFn; this.listeners[e]?.push(listener);
const key: string = typeof keyOrFn == 'string' ? keyOrFn : return () => this.off(event, listener);
`_${Object.keys(this.listeners).length.toString()}`;
this.listeners[<any>key] = func;
return () => delete this.listeners[<any>key];
} }
once(fn: Listener<T>) { static once(event: any, listener?: Listener): Promise<any> {
const stop = this.listen(e => { return new Promise(res => {
fn(e); const unsubscribe = this.on(event, <any>((...args: any) => {
stop(); res(args.length == 1 ? args[0] : args);
if(listener) listener(...args);
unsubscribe();
}));
});
}
emit<K extends keyof T>(event: K, ...args: Parameters<T[K]>) {
(this.listeners['*'] || []).forEach(l => l(event, ...args));
(this.listeners[event] || []).forEach(l => l(...args));
};
off<K extends keyof T = string>(event: K, listener: T[K]) {
this.listeners[event] = (this.listeners[event] || []).filter(l => l === listener);
}
on<K extends keyof T = string>(event: K, listener: T[K]) {
if(!this.listeners[event]) this.listeners[event] = [];
this.listeners[event]?.push(listener);
return () => this.off(event, listener);
}
once<K extends keyof T = string>(event: K, listener?: T[K]): 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();
}));
}); });
} }
} }

View File

@ -1,3 +1,5 @@
import {TypedEmitter, TypedEvents} from './emitter';
export const CliEffects = { export const CliEffects = {
CLEAR: "\x1b[0m", CLEAR: "\x1b[0m",
BRIGHT: "\x1b[1m", BRIGHT: "\x1b[1m",
@ -9,15 +11,22 @@ 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',
WHITE: "\x1b[37m", LIGHT_GREY: '\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 = {
@ -32,34 +41,63 @@ export const CliBackground = {
GREY: "\x1b[100m", GREY: "\x1b[100m",
} }
export class Logger { export enum LOG_LEVEL {
constructor(public readonly namespace: string) { } VERBOSE,
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[]) {
console.log(CliForeground.MAGENTA + this.format(...args) + CliEffects.CLEAR); if(LOG_LEVEL.VERBOSE >= Logger.LOG_LEVEL) {
Logger.emit(LOG_LEVEL.VERBOSE, ...args);
console.debug(CliForeground.LIGHT_GREY + this.format(...args) + CliEffects.CLEAR);
}
} }
error(...args: string[]) { error(...args: string[]) {
console.log(CliForeground.RED + this.format(...args) + CliEffects.CLEAR); if(LOG_LEVEL.ERROR >= Logger.LOG_LEVEL) {
Logger.emit(LOG_LEVEL.ERROR, ...args);
console.error(CliForeground.RED + this.format(...args) + CliEffects.CLEAR);
}
} }
info(...args: string[]) { info(...args: string[]) {
console.log(CliForeground.CYAN + this.format(...args) + CliEffects.CLEAR); if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
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[]) {
console.log(CliForeground.YELLOW + this.format(...args) + CliEffects.CLEAR); if(LOG_LEVEL.WARN >= Logger.LOG_LEVEL) {
} 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);
} }
} }

View File

@ -45,7 +45,7 @@ export class XHR<T> {
return fetch(`${this.baseUrl}${href || ''}`.replace(/([^:]\/)\/+/g, '$1'), { return fetch(`${this.baseUrl}${href || ''}`.replace(/([^:]\/)\/+/g, '$1'), {
headers, headers,
method: opts.method || (body ? 'POST' : 'GET'), method: opts.method || (body ? 'POST' : 'GET'),
body: (headers['Content-Type'].startsWith('application/json') && body) ? JSON.stringify(body) : body body: (headers['Content-Type']?.startsWith('application/json') && body) ? JSON.stringify(body) : body
}).then(async resp => { }).then(async resp => {
for(let fn of this.getInterceptors()) { for(let fn of this.getInterceptors()) {
const wait = new Promise(res => const wait = new Promise(res =>