Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
3deb536323 | |||
f6b0e63751 | |||
d52f9cfdc3 | |||
0ffe3d6b13 | |||
d4bd054953 | |||
cf9bdca2ba | |||
0e2d720fdf | |||
061e27d92a | |||
63a5c5f6b7 | |||
64a0eac6df | |||
3e1341d876 | |||
b3457ed588 | |||
210f007e07 | |||
24edc4a12d | |||
d4996201cf | |||
74c89c3ed4 | |||
e4c7dea9a5 | |||
ffaf8558b2 |
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,9 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/js-utilities",
|
"name": "@ztimson/js-utilities",
|
||||||
"version": "0.0.0",
|
"version": "0.3.7",
|
||||||
"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"
|
||||||
@ -12,10 +13,10 @@
|
|||||||
"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",
|
"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",
|
||||||
|
@ -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();
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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';
|
||||||
|
100
src/logger.ts
100
src/logger.ts
@ -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,75 @@ export const CliBackground = {
|
|||||||
GREY: "\x1b[100m",
|
GREY: "\x1b[100m",
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Logger {
|
export enum LOG_LEVEL {
|
||||||
constructor(public readonly namespace: string) { }
|
VERBOSE,
|
||||||
|
DEBUG = 0,
|
||||||
|
LOG = 1,
|
||||||
|
INFO = 2,
|
||||||
|
WARN = 3,
|
||||||
|
ERROR = 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
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.VERBOSE;
|
||||||
|
|
||||||
|
constructor(public readonly namespace?: string) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
private pad(text: any, length: number, char: string, end = false) {
|
||||||
|
const t = text.toString();
|
||||||
|
const l = length - t.length;
|
||||||
|
if(l <= 0) return t;
|
||||||
|
const padding = Array(~~(l / char.length)).fill(char).join('');
|
||||||
|
return !end ? padding + t : t + 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[]) {
|
||||||
console.log(CliForeground.MAGENTA + this.format(...args) + CliEffects.CLEAR);
|
if(Logger.LOG_LEVEL > LOG_LEVEL.VERBOSE) return;
|
||||||
}
|
const str = this.format(...args);
|
||||||
|
Logger.emit(LOG_LEVEL.VERBOSE, str);
|
||||||
error(...args: string[]) {
|
console.debug(CliForeground.LIGHT_GREY + str + CliEffects.CLEAR);
|
||||||
console.log(CliForeground.RED + this.format(...args) + CliEffects.CLEAR);
|
|
||||||
}
|
|
||||||
|
|
||||||
info(...args: string[]) {
|
|
||||||
console.log(CliForeground.CYAN + this.format(...args) + CliEffects.CLEAR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log(...args: string[]) {
|
log(...args: string[]) {
|
||||||
console.log(CliEffects.CLEAR + this.format(...args));
|
if(Logger.LOG_LEVEL > LOG_LEVEL.INFO) return;
|
||||||
|
const str = this.format(...args);
|
||||||
|
Logger.emit(LOG_LEVEL.INFO, 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[]) {
|
||||||
console.log(CliForeground.YELLOW + this.format(...args) + CliEffects.CLEAR);
|
if(Logger.LOG_LEVEL > LOG_LEVEL.WARN) return;
|
||||||
|
const str = this.format(...args);
|
||||||
|
Logger.emit(LOG_LEVEL.WARN, str);
|
||||||
|
console.warn(CliForeground.YELLOW + str + CliEffects.CLEAR);
|
||||||
}
|
}
|
||||||
|
|
||||||
verbose(...args: string[]) {
|
error(...args: string[]) {
|
||||||
console.log(CliForeground.WHITE + this.format(...args) + CliEffects.CLEAR);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 */
|
||||||
|
72
src/redis.ts
72
src/redis.ts
@ -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);
|
|
||||||
// };
|
|
||||||
// }
|
|
@ -52,6 +52,14 @@ 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: any, length: number, char: string, start = true) {
|
||||||
|
const t = text.toString();
|
||||||
|
const l = length - t.length;
|
||||||
|
if(l <= 0) return t;
|
||||||
|
const padding = Array(~~(l / char.length)).fill(char).join('');
|
||||||
|
return start ? padding + t : t + padding;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a string of random characters.
|
* Generate a string of random characters.
|
||||||
*
|
*
|
||||||
|
@ -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 =>
|
||||||
|
@ -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