Compare commits

..

5 Commits
0.3.4 ... 0.3.8

Author SHA1 Message Date
77e6a40261 fixed gravatar default image
All checks were successful
Build / Build NPM Project (push) Successful in 14s
Build / Tag Version (push) Successful in 4s
Build / Publish (push) Successful in 7s
2024-03-20 20:45:41 -04:00
3deb536323 fixed gravatar default image
All checks were successful
Build / Build NPM Project (push) Successful in 14s
Build / Tag Version (push) Successful in 6s
Build / Publish (push) Successful in 7s
2024-03-20 20:44:53 -04:00
f6b0e63751 Updated gravatar function to supply default image
All checks were successful
Build / Build NPM Project (push) Successful in 20s
Build / Tag Version (push) Successful in 4s
Build / Publish (push) Successful in 8s
2024-03-20 19:26:13 -04:00
d52f9cfdc3 Fixed logger
All checks were successful
Build / Build NPM Project (push) Successful in 32s
Build / Tag Version (push) Successful in 10s
Build / Publish (push) Successful in 29s
2024-03-04 11:11:11 -05:00
0ffe3d6b13 Updated logger
All checks were successful
Build / Build NPM Project (push) Successful in 33s
Build / Tag Version (push) Successful in 8s
Build / Publish (push) Successful in 14s
2024-03-03 19:26:20 -05:00
5 changed files with 36 additions and 112 deletions

View File

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

View File

@ -4,7 +4,6 @@ export * from './errors';
export * from './logger'; export * from './logger';
export * from './misc'; export * from './misc';
export * from './objects'; export * from './objects';
// export * from './redis';
export * from './string'; export * from './string';
export * from './time'; export * from './time';
export * from './xhr'; export * from './xhr';

View File

@ -43,9 +43,11 @@ export const CliBackground = {
export enum LOG_LEVEL { export enum LOG_LEVEL {
VERBOSE, VERBOSE,
INFO, DEBUG = 0,
WARN, LOG = 1,
ERROR INFO = 2,
WARN = 3,
ERROR = 4,
} }
export type LoggerEvents = TypedEvents & { export type LoggerEvents = TypedEvents & {
@ -56,7 +58,7 @@ 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.VERBOSE;
constructor(public readonly namespace?: string) { constructor(public readonly namespace?: string) {
super(); super();
@ -70,7 +72,6 @@ export class Logger extends TypedEmitter<LoggerEvents> {
return !end ? padding + t : t + padding; return !end ? padding + t : t + padding;
} }
private format(...text: string[]): string { private format(...text: string[]): string {
const now = new Date(); 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)}`; 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)}`;
@ -78,42 +79,37 @@ export class Logger extends TypedEmitter<LoggerEvents> {
} }
debug(...args: string[]) { debug(...args: string[]) {
if(LOG_LEVEL.VERBOSE >= Logger.LOG_LEVEL) { if(Logger.LOG_LEVEL > LOG_LEVEL.VERBOSE) return;
const str = this.format(...args); const str = this.format(...args);
Logger.emit(LOG_LEVEL.VERBOSE, str); Logger.emit(LOG_LEVEL.VERBOSE, str);
console.debug(CliForeground.LIGHT_GREY + str + CliEffects.CLEAR); console.debug(CliForeground.LIGHT_GREY + str + CliEffects.CLEAR);
} }
}
error(...args: string[]) {
if(LOG_LEVEL.ERROR >= Logger.LOG_LEVEL) {
const str = this.format(...args);
Logger.emit(LOG_LEVEL.ERROR, str);
console.error(CliForeground.RED + str + CliEffects.CLEAR);
}
}
info(...args: string[]) {
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
const str = this.format(...args);
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(Logger.LOG_LEVEL > LOG_LEVEL.INFO) return;
const str = this.format(...args); const str = this.format(...args);
Logger.emit(LOG_LEVEL.INFO, str); Logger.emit(LOG_LEVEL.INFO, str);
console.log(CliEffects.CLEAR + str); console.log(CliEffects.CLEAR + str);
} }
info(...args: string[]) {
if(Logger.LOG_LEVEL > LOG_LEVEL.INFO) return;
const str = this.format(...args);
Logger.emit(LOG_LEVEL.INFO, str);
console.info(CliForeground.BLUE + str + CliEffects.CLEAR);
} }
warn(...args: string[]) { warn(...args: string[]) {
if(LOG_LEVEL.WARN >= Logger.LOG_LEVEL) { if(Logger.LOG_LEVEL > LOG_LEVEL.WARN) return;
const str = this.format(...args); const str = this.format(...args);
Logger.emit(LOG_LEVEL.WARN, str); Logger.emit(LOG_LEVEL.WARN, str);
console.warn(CliForeground.YELLOW + str + CliEffects.CLEAR); console.warn(CliForeground.YELLOW + str + CliEffects.CLEAR);
} }
error(...args: string[]) {
if(Logger.LOG_LEVEL > LOG_LEVEL.ERROR) return;
const str = this.format(...args);
Logger.emit(LOG_LEVEL.ERROR, str);
console.error(CliForeground.RED + str + CliEffects.CLEAR);
} }
} }

View File

@ -16,11 +16,12 @@ export function formEncode(data: any): string {
* Get profile image from Gravatar * Get profile image from Gravatar
* *
* @param {string} email Account email address * @param {string} email Account email address
* @param {string} def Default image, can be a link or '404', see: https://docs.gravatar.com/general/images/
* @returns {string} Gravatar URL * @returns {string} Gravatar URL
*/ */
export function gravatar(email: string) { export function gravatar(email: string, def='mp') {
if(!email) return ''; if(!email) return '';
return `https://www.gravatar.com/avatar/${md5(email)}`; return `https://www.gravatar.com/avatar/${md5(email)}?d=${def}`;
} }
/** Parts of a URL */ /** Parts of a URL */

View File

@ -1,72 +0,0 @@
// import {createClient, RedisClientType} from 'redis';
// import {environment} from '../environments/environment';
//
// export type RedisKey = string | string[];
//
// export let Redis!: RedisClientType & {
// // get/set shimmed for JSON
// jGet: (redisKey: string) => Promise<any>,
// jSet: (redisKey: string, obj: any, opts?: any) => Promise<void>,
// // hGet/hSet shimmed for objects
// oGet: (redisKey: string) => Promise<any>;
// oSet: (redisKey: string, obj: any) => Promise<void>,
// // Helpers
// findKeys: (filter: RedisKey) => Promise<string[]>,
// forEach: (filter: RedisKey, cb: (key: string) => any) => Promise<void>,
// };
//
// export async function connectRedis(retry = 3) {
// Redis = <any>createClient({
// url: `redis://host:port`
// });
// if(!Redis && retry > 0) {
// await connectRedis(retry - 1);
// } else if(!!Redis) {
// Redis.jGet = async (redisKey: string) => {
// const val = await Redis.get(redisKey);
// return val ? JSON.parse(val) || val : null;
// };
// Redis.jSet = (redisKey: string, obj: any, opts?: any) => {
// return Redis.set(redisKey, JSON.stringify(obj), opts).then(() => {});
// };
// Redis.oGet = async (redisKey: string) => {
// if(!(await Redis.hLen(redisKey))) return null;
// const val = await Redis.hGetAll(redisKey);
// Object.entries(val).forEach(([key, v]) => val[key] = JSON.parse(v));
// return val;
// };
// Redis.oSet = (redisKey: string, obj: any) => {
// const r = Redis.multi();
// Object.entries(obj).forEach(([key, val]) => {
// r.hSet(redisKey, key, JSON.stringify(val));
// });
// return r.exec().then(() => {});
// };
// Redis.findKeys = async (filter: RedisKey): Promise<string[]> => {
// const found: string[] = [];
// await Redis.forEach(filter, (key: string) => found.push(key));
// return found;
// }
// Redis.forEach = async (filter: RedisKey, cb: (key: string) => any): Promise<void> => {
// for await (const k of Redis.scanIterator({MATCH: createKey(filter)})) {
// const rtn = cb(k);
// if(rtn instanceof Promise) await rtn;
// }
// }
// await Redis.connect();
// }
// }
//
// export function createKey(...keys: (string | string[])[]) {
// return keys.flat().map(k => k == null ? '*' : k).join(':');
// }
//
// export function namespacedKey(namespace: string, key: RedisKey): string {
// return createKey(namespace, ...(Array.isArray(key) ? key : [key]));
// }
//
// export function nameSpacer(namespace: string) {
// return (key: RedisKey) => {
// return namespacedKey(namespace, key);
// };
// }