This commit is contained in:
2024-02-07 01:33:07 -05:00
commit c1af19d441
4088 changed files with 1260170 additions and 0 deletions

112
src/array.ts Normal file
View File

@ -0,0 +1,112 @@
import {dotNotation, isEqual} from './objects';
export function addUnique<T>(array: T[], el: T): T[] {
if(array.indexOf(el) === -1) array.push(el);
return array;
}
export function arrayDiff(a: any[], b: any[]): any[] {
return makeUnique([
...a.filter(v1 => !b.includes(v2 => isEqual(v1, v2))),
...b.filter(v1 => !a.includes(v2 => isEqual(v1, v2))),
]);
}
/**
* Provides a shorthand for sorting arrays of complex objects by a string property
*
* @example
* ```ts
* let arr = [{a: 'Apple', b: 123}, {a: 'Carrot', b: 789}, {a: 'banana', b: 456}];
* arr.sort(caseInsensitiveSort('a'));
* ```
*
* @param {string} prop - Name of property to use, supports dot notation
* @returns {(a, b) => (number)} - Function to handle sort (Meant to be passed to Array.prototype.sort or used in sortFn)
*/
export function caseInsensitiveSort(prop: string) {
return function (a: any, b: any) {
const aVal = dotNotation<string>(a, prop);
const bVal = dotNotation<string>(b, prop);
if(typeof aVal !== 'string' || typeof bVal !== 'string') return 1;
return aVal.toLowerCase().localeCompare(bVal.toLowerCase());
};
}
/**
* Recursively flatten nested arrays
*
* @example
* ```ts
* const arr = [
* {label: null, url: '/'},
* {label: 'Model Admin', url: '/model-admin'},
* [
* {label: 'Elements', url: '/model-admin/elements'},
* {label: 'Example', url: null}
* ]
* ];
*
* console.log(flattenArr(arr));
* // Output:
* [
* {label: null, url: '/'},
* {label: 'Model Admin', url: '/model-admin'},
* {label: 'Elements', url: '/model-admin/elements'},
* {label: 'Example', url: null}
* ]
* ```
*
* @param {any[]} arr - n-dimensional array
* @param {any[]} result - Internal use only -- Keeps track of recursion
* @returns {any[]} - Flattened array
*/
export function flattenArr(arr: any[], result: any[] = []): any[] {
arr.forEach(el => Array.isArray(el) ? flattenArr(el, result) : result.push(el));
return result;
}
/**
* Provides a shorthand for sorting arrays of complex objects
*
* @example
* ```ts
* let arr = [{a: {b: 2}}, {a: {b: 3}}, {a: {b: 1}}];
* arr.sort(sortByProp('a.b'));
* ```
*
* @param {string} prop - Name of property to use, supports dot notation
* @param {boolean} reverse - Reverse the order of the sort
* @returns {(a, b) => (number)} - Function to handle sort (Meant to be passed to Array.prototype.sort)
*/
export function sortByProp(prop: string, reverse = false) {
return function (a, b) {
const aVal = dotNotation<any>(a, prop);
const bVal = dotNotation<any>(b, prop);
if(typeof aVal == 'number' && typeof bVal == 'number')
return (reverse ? -1 : 1) * (aVal - bVal);
if(aVal > bVal) return reverse ? -1 : 1;
if(aVal < bVal) return reverse ? 1 : -1;
return 0;
};
}
export function findByProp(prop: string, value: any) {
return (v) => isEqual(v[prop], value);
}
export function makeUnique(arr: any[]) {
for(let i = arr.length - 1; i >= 0; i--) {
if(arr.slice(0, i).find(n => isEqual(n, arr[i]))) arr.splice(i, 1);
}
return arr;
}
/**
* Make sure value is an array, if it isn't wrap it in one.
* @param {T[] | T} value Value that should be an array
* @returns {T[]} Value in an array
*/
export function makeArray<T>(value: T | T[]): T[] {
return Array.isArray(value) ? value : [value];
}

28
src/emitter.ts Normal file
View File

@ -0,0 +1,28 @@
export type Listener<T> = (event: T) => any;
export class Emitter<T> {
private listeners: {[key: string]: Listener<T>} = {};
constructor() { }
emit(e: T) {
Object.values(this.listeners).forEach(l => l(e));
}
listen(fn: Listener<T>): () => {};
listen(key: string, fn: Listener<T>): () => {};
listen(keyOrFn: string | Listener<T>, fn?: Listener<T>): () => {} {
const func: any = fn ? fn : keyOrFn;
const key: string = typeof keyOrFn == 'string' ? keyOrFn :
`_${Object.keys(this.listeners).length.toString()}`;
this.listeners[<any>key] = func;
return () => delete this.listeners[<any>key];
}
once(fn: Listener<T>) {
const stop = this.listen(e => {
fn(e);
stop();
});
}
}

102
src/errors.ts Normal file
View File

@ -0,0 +1,102 @@
import {XHR} from './xhr';
XHR.addInterceptor((resp: Response, next: () => {}) => {
if(resp.status == 200) return next();
if(resp.status == 400) throw new BadRequestError(resp.statusText);
if(resp.status == 401) throw new UnauthorizedError(resp.statusText);
if(resp.status == 403) throw new ForbiddenError(resp.statusText);
if(resp.status == 404) throw new NotFoundError(resp.statusText);
if(resp.status == 500) throw new InternalServerError(resp.statusText);
throw new CustomError(resp.statusText, resp.status);
});
export class CustomError extends Error {
static code = 500;
private _code?: number;
get code(): number { return this._code || (<any>this).constructor.code; }
set code(c: number) { this._code = c; }
constructor(message?: string, code?: number) {
super(message);
if(code != null) this._code = code;
}
static from(err: Error): CustomError {
const code = Number((<any>err).statusCode) ?? Number((<any>err).code);
const newErr = new this(err.message || err.toString());
return Object.assign(newErr, {
stack: err.stack,
...err,
code: code ?? undefined
});
}
static instanceof(err: Error) {
return (<any>err).constructor.code != undefined;
}
toString() {
return this.message || super.toString();
}
}
export class BadRequestError extends CustomError {
static code = 400;
constructor(message: string = 'Bad Request') {
super(message);
}
static instanceof(err: Error) {
return (<any>err).constructor.code == this.code;
}
}
export class UnauthorizedError extends CustomError {
static code = 401;
constructor(message: string = 'Unauthorized') {
super(message);
}
static instanceof(err: Error) {
return (<any>err).constructor.code == this.code;
}
}
export class ForbiddenError extends CustomError {
static code = 403;
constructor(message: string = 'Forbidden') {
super(message);
}
static instanceof(err: Error) {
return (<any>err).constructor.code == this.code;
}
}
export class NotFoundError extends CustomError {
static code = 404;
constructor(message: string = 'Not Found') {
super(message);
}
static instanceof(err: Error) {
return (<any>err).constructor.code == this.code;
}
}
export class InternalServerError extends CustomError {
static code = 500;
constructor(message: string = 'Internal Server Error') {
super(message);
}
static instanceof(err: Error) {
return (<any>err).constructor.code == this.code;
}
}

10
src/index.ts Normal file
View File

@ -0,0 +1,10 @@
export * from './array';
export * from './emitter';
export * from './errors';
export * from './logger';
export * from './misc';
export * from './objects';
// export * from './redis';
export * from './string';
export * from './time';
export * from './xhr';

65
src/logger.ts Normal file
View File

@ -0,0 +1,65 @@
export const CliEffects = {
CLEAR: "\x1b[0m",
BRIGHT: "\x1b[1m",
DIM: "\x1b[2m",
UNDERSCORE: "\x1b[4m",
BLINK: "\x1b[5m",
REVERSE: "\x1b[7m",
HIDDEN: "\x1b[8m",
}
export const CliForeground = {
BLACK: "\x1b[30m",
RED: "\x1b[31m",
GREEN: "\x1b[32m",
YELLOW: "\x1b[33m",
BLUE: "\x1b[34m",
MAGENTA: "\x1b[35m",
CYAN: "\x1b[36m",
WHITE: "\x1b[37m",
GREY: "\x1b[90m",
}
export const CliBackground = {
BLACK: "\x1b[40m",
RED: "\x1b[41m",
GREEN: "\x1b[42m",
YELLOW: "\x1b[43m",
BLUE: "\x1b[44m",
MAGENTA: "\x1b[45m",
CYAN: "\x1b[46m",
WHITE: "\x1b[47m",
GREY: "\x1b[100m",
}
export class Logger {
constructor(public readonly namespace: string) { }
private format(...text: string[]): string {
return `${new Date().toISOString()} [${this.namespace}] ${text.join(' ')}`;
}
debug(...args: string[]) {
console.log(CliForeground.MAGENTA + this.format(...args) + CliEffects.CLEAR);
}
error(...args: string[]) {
console.log(CliForeground.RED + this.format(...args) + CliEffects.CLEAR);
}
info(...args: string[]) {
console.log(CliForeground.CYAN + this.format(...args) + CliEffects.CLEAR);
}
log(...args: string[]) {
console.log(CliEffects.CLEAR + this.format(...args));
}
warn(...args: string[]) {
console.log(CliForeground.YELLOW + this.format(...args) + CliEffects.CLEAR);
}
verbose(...args: string[]) {
console.log(CliForeground.WHITE + this.format(...args) + CliEffects.CLEAR);
}
}

63
src/misc.ts Normal file
View File

@ -0,0 +1,63 @@
import {md5} from './string';
/**
* Convert data into a form encoded format.
*
* @param {any} data - data to convert
* @returns {string} - Ecodeded form data
*/
export function formEncode(data: any): string {
return Object.entries(data).map(([key, value]) =>
encodeURIComponent(key) + '=' + encodeURIComponent(<any>value)
).join('&');
}
/**
* Get profile image from Gravatar
*
* @param {string} email Account email address
* @returns {string} Gravatar URL
*/
export function gravatar(email: string) {
if(!email) return '';
return `https://www.gravatar.com/avatar/${md5(email)}`;
}
/** Parts of a URL */
export type ParsedUrl = {
protocol?: string,
subdomain?: string,
domain: string,
host: string,
port?: number,
path?: string,
query?: {[name: string]: string}
fragment?: string
}
/**
*
* @param {string} url
* @returns {RegExpExecArray}
*/
export function urlParser(url: string): ParsedUrl {
const processed = new RegExp(
'(?:(?<protocol>[\\w\\d]+)\\:\\/\\/)?(?:(?<user>.+)\\@)?(?<host>(?<domain>[^:\\/\\?#@\\n]+)(?:\\:(?<port>\\d*))?)(?<path>\\/.*?)?(?:\\?(?<query>.*?))?(?:#(?<fragment>.*?))?$',
'gm').exec(url);
const groups: ParsedUrl = <any>processed?.groups ?? {};
const domains = groups.domain.split('.');
if(groups['port'] != null) groups.port = Number(groups.port);
if(domains.length > 2) {
groups.domain = domains.splice(-2, 2).join('.');
groups.subdomain = domains.join('.');
}
if(groups.query) {
const split = (<any>groups.query).split('&'), query = {};
split.forEach(q => {
const [key, val] = q.split('=');
query[key] = val;
});
groups.query = query;
}
return groups;
}

113
src/objects.ts Normal file
View File

@ -0,0 +1,113 @@
/**
* Removes any null values from an object in-place
*
* @example
* ```ts
* let test = {a: 0, b: false, c: null, d: 'abc'}
* console.log(clean(test)); // Output: {a: 0, b: false, d: 'abc'}
* ```
*
* @param {T} obj Object reference that will be cleaned
* @returns {Partial<T>} Cleaned object
*/
export function clean<T>(obj: T, includeNull = false): Partial<T> {
if(obj == null) throw new Error("Cannot clean a NULL value");
if(Array.isArray(obj)) {
obj = <any>obj.filter(o => o != null);
} else {
Object.entries(obj).forEach(([key, value]) => {
if(value === undefined || (includeNull || value === null)) delete (<any>obj)[key];
});
}
return <any>obj;
}
/**
* Create a deep copy of an object (vs. a shallow copy of references)
*
* Should be replaced by `structuredClone` once released.
*
* @param {T} value Object to copy
* @returns {T} Type
*/
export function deepCopy<T>(value: T): T {
return JSON.parse(JSON.stringify(value));
}
/**
* Get/set a property of an object using dot notation
*
* @example
* ```ts
* // Get a value
* const name = dotNotation<string>(person, 'firstName');
* const familyCarMake = dotNotation(family, 'cars[0].make');
* // Set a value
* dotNotation(family, 'cars[0].make', 'toyota');
* ```
*
* @type T Return type
* @param {Object} obj source object to search
* @param {string} prop property name (Dot notation & indexing allowed)
* @param {any} set Set object property to value, omit to fetch value instead
* @return {T} property value
*/
export function dotNotation<T>(obj: any, prop: string, set: T): T;
export function dotNotation<T>(obj: any, prop: string): T | undefined;
export function dotNotation<T>(obj: any, prop: string, set?: T): T | undefined {
if(obj == null || !prop) return undefined;
// Split property string by '.' or [index]
return <T>prop.split(/[.[\]]/g).filter(prop => prop.length).reduce((obj, prop, i, arr) => {
if(prop[0] == '"' || prop[0] == "'") prop = prop.slice(1, -1); // Take quotes out
if(!obj?.hasOwnProperty(prop)) {
if(set == undefined) return undefined;
obj[prop] = {};
}
if(set !== undefined && i == arr.length - 1)
return obj[prop] = set;
return obj[prop];
}, obj);
}
/**
* Check that an object has the following values
*
* @example
* ```ts
* const test = {a: 2, b: 2};
* includes(test, {a: 1}); // true
* includes(test, {b: 1, c: 3}); // false
* ```
*
* @param target Object to search
* @param values Criteria to check against
* @param allowMissing Only check the keys that are available on the target
* @returns {boolean} Does target include all the values
*/
export function includes(target: any, values: any, allowMissing = false): boolean {
if(target == undefined) return allowMissing;
if(Array.isArray(values)) return values.findIndex((e, i) => !includes(target[i], values[i], allowMissing)) == -1;
const type = typeof values;
if(type != typeof target) return false;
if(type == 'object') {
return Object.keys(values).find(key => !includes(target[key], values[key], allowMissing)) == null;
}
if(type == 'function') return target.toString() == values.toString();
return target == values;
}
/**
* Deep check if two objects are equal
*
* @param {any} a - first item to compare
* @param {any} b - second item to compare
* @returns {boolean} True if they match
*/
export function isEqual(a: any, b: any): boolean {
const ta = typeof a, tb = typeof b;
if((ta != 'object' || a == null) || (tb != 'object' || b == null))
return ta == 'function' && tb == 'function' ? a.toString() == b.toString() : a === b;
const keys = Object.keys(a);
if(keys.length != Object.keys(b).length) return false;
return Object.keys(a).every(key => isEqual(a[key], b[key]));
}

72
src/redis.ts Normal file
View File

@ -0,0 +1,72 @@
// 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);
// };
// }

153
src/string.ts Normal file
View File

@ -0,0 +1,153 @@
export function countChars(text: string, pattern: RegExp) {
return text.length - text.replaceAll(pattern, '').length;
}
export function createHex(length: number) {
return Array(length).fill(null).map(() => Math.round(Math.random() * 0xF).toString(16)).join('');
}
/**
* String of all letters
*
*/
const LETTER_LIST = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
/**
* String of all numbers
*
*/
const NUMBER_LIST = '0123456789';
/**
* String of all symbols
*/
const SYMBOL_LIST = '~`!@#$%^&*()_-+={[}]|\\:;"\'<,>.?/';
/**
* String of all letters, numbers & symbols
*/
const CHAR_LIST = LETTER_LIST + NUMBER_LIST + SYMBOL_LIST;
export function formatPhoneNumber(number: string) {
const parts = /(\+?1)?.*?(\d{3}).*?(\d{3}).*?(\d{4})/g.exec(number);
if(!parts) throw new Error(`Number cannot be parsed: ${number}`);
return `${parts[1] ?? ''} (${parts[2]}) ${parts[3]}-${parts[4]}`.trim();
}
/**
* Insert a string into another string at a given position
*
* @example
* ```
* console.log(insertAt('Hello world!', ' glorious', 5);
* // Output: Hello glorious world!
* ```
*
* @param {string} target - Parent string you want to modify
* @param {string} str - Value that will be injected to parent
* @param {number} index - Position to inject string at
* @returns {string} - New string
*/
export function insertAt(target: string, str: string, index: number): String {
return `${target.slice(0, index)}${str}${target.slice(index + 1)}`;
}
/**
* Generate a string of random characters.
*
* @example
* ```ts
* const random = randomString();
* const randomByte = randomString(8, "01")
* ```
*
* @param {number} length - length of generated string
* @param {string} pool - character pool to generate string from
* @return {string} generated string
*/
export function randomString(length: number, pool: string = CHAR_LIST): string {
return Array(length).fill(null).map(() => {
const n = ~~(Math.random() * pool.length);
return pool[n];
}).join('');
}
/**
* Generate a random string with fine control over letters, numbers & symbols
*
* @example
* ```ts
* const randomLetter = randomString(1, true);
* const randomChar = randomString(1, true, true, true);
* ```
*
* @param {number} length - length of generated string
* @param {boolean} letters - Add letters to pool
* @param {boolean} numbers - Add numbers to pool
* @param {boolean} symbols - Add symbols to pool
* @return {string} generated string
*/
export function randomStringBuilder(length: number, letters = false, numbers = false, symbols = false): string {
if(!letters && !numbers && !symbols) throw new Error('Must enable at least one: letters, numbers, symbols');
return Array(length).fill(null).map(() => {
let c;
do {
const type = ~~(Math.random() * 3);
if(letters && type == 0) {
c = LETTER_LIST[~~(Math.random() * LETTER_LIST.length)];
} else if(numbers && type == 1) {
c = NUMBER_LIST[~~(Math.random() * NUMBER_LIST.length)];
} else if(symbols && type == 2) {
c = SYMBOL_LIST[~~(Math.random() * SYMBOL_LIST.length)];
}
} while(!c);
return c;
}).join('');
}
/**
* Find all substrings that match a given pattern.
*
* Roughly based on `String.prototype.matchAll`.
*
* @param {string} value - String to search.
* @param {RegExp | string} regex - Regular expression to match.
* @return {RegExpExecArray[]} Found matches.
*/
export function matchAll(value: string, regex: RegExp | string): RegExpExecArray[] {
if(typeof regex === 'string') {
regex = new RegExp(regex, 'g');
}
// https://stackoverflow.com/a/60290199
if(!regex.global) {
throw new TypeError('Regular expression must be global.');
}
let ret: RegExpExecArray[] = [];
let match: RegExpExecArray | null;
while((match = regex.exec(value)) !== null) {
ret.push(match);
}
return ret;
}
/**
* Create MD5 hash using native javascript
* @param d String to hash
* @returns {string} Hashed string
*/
export function md5(d) {
var r = M(V(Y(X(d),8*d.length)));return r.toLowerCase()};function M(d){for(var _,m="0123456789ABCDEF",f="",r=0;r<d.length;r++)_=d.charCodeAt(r),f+=m.charAt(_>>>4&15)+m.charAt(15&_);return f}function X(d){for(var _=Array(d.length>>2),m=0;m<_.length;m++)_[m]=0;for(m=0;m<8*d.length;m+=8)_[m>>5]|=(255&d.charCodeAt(m/8))<<m%32;return _}function V(d){for(var _="",m=0;m<32*d.length;m+=8)_+=String.fromCharCode(d[m>>5]>>>m%32&255);return _}function Y(d,_){d[_>>5]|=128<<_%32,d[14+(_+64>>>9<<4)]=_;for(var m=1732584193,f=-271733879,r=-1732584194,i=271733878,n=0;n<d.length;n+=16){var h=m,t=f,g=r,e=i;f=md5_ii(f=md5_ii(f=md5_ii(f=md5_ii(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_ff(f=md5_ff(f=md5_ff(f=md5_ff(f,r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+0],7,-680876936),f,r,d[n+1],12,-389564586),m,f,d[n+2],17,606105819),i,m,d[n+3],22,-1044525330),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+4],7,-176418897),f,r,d[n+5],12,1200080426),m,f,d[n+6],17,-1473231341),i,m,d[n+7],22,-45705983),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+8],7,1770035416),f,r,d[n+9],12,-1958414417),m,f,d[n+10],17,-42063),i,m,d[n+11],22,-1990404162),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+12],7,1804603682),f,r,d[n+13],12,-40341101),m,f,d[n+14],17,-1502002290),i,m,d[n+15],22,1236535329),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+1],5,-165796510),f,r,d[n+6],9,-1069501632),m,f,d[n+11],14,643717713),i,m,d[n+0],20,-373897302),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+5],5,-701558691),f,r,d[n+10],9,38016083),m,f,d[n+15],14,-660478335),i,m,d[n+4],20,-405537848),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+9],5,568446438),f,r,d[n+14],9,-1019803690),m,f,d[n+3],14,-187363961),i,m,d[n+8],20,1163531501),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+13],5,-1444681467),f,r,d[n+2],9,-51403784),m,f,d[n+7],14,1735328473),i,m,d[n+12],20,-1926607734),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+5],4,-378558),f,r,d[n+8],11,-2022574463),m,f,d[n+11],16,1839030562),i,m,d[n+14],23,-35309556),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+1],4,-1530992060),f,r,d[n+4],11,1272893353),m,f,d[n+7],16,-155497632),i,m,d[n+10],23,-1094730640),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+13],4,681279174),f,r,d[n+0],11,-358537222),m,f,d[n+3],16,-722521979),i,m,d[n+6],23,76029189),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+9],4,-640364487),f,r,d[n+12],11,-421815835),m,f,d[n+15],16,530742520),i,m,d[n+2],23,-995338651),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+0],6,-198630844),f,r,d[n+7],10,1126891415),m,f,d[n+14],15,-1416354905),i,m,d[n+5],21,-57434055),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+12],6,1700485571),f,r,d[n+3],10,-1894986606),m,f,d[n+10],15,-1051523),i,m,d[n+1],21,-2054922799),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+8],6,1873313359),f,r,d[n+15],10,-30611744),m,f,d[n+6],15,-1560198380),i,m,d[n+13],21,1309151649),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+4],6,-145523070),f,r,d[n+11],10,-1120210379),m,f,d[n+2],15,718787259),i,m,d[n+9],21,-343485551),m=safe_add(m,h),f=safe_add(f,t),r=safe_add(r,g),i=safe_add(i,e)}return Array(m,f,r,i)}function md5_cmn(d,_,m,f,r,i){return safe_add(bit_rol(safe_add(safe_add(_,d),safe_add(f,i)),r),m)}function md5_ff(d,_,m,f,r,i,n){return md5_cmn(_&m|~_&f,d,_,r,i,n)}function md5_gg(d,_,m,f,r,i,n){return md5_cmn(_&f|m&~f,d,_,r,i,n)}function md5_hh(d,_,m,f,r,i,n){return md5_cmn(_^m^f,d,_,r,i,n)}function md5_ii(d,_,m,f,r,i,n){return md5_cmn(m^(_|~f),d,_,r,i,n)}function safe_add(d,_){var m=(65535&d)+(65535&_);return(d>>16)+(_>>16)+(m>>16)<<16|65535&m}function bit_rol(d,_){return d<<_|d>>>32-_
}
/**
* Check if email is valid
*
* @param {string} email - Target
* @returns {boolean} - Follows format
*/
export function validateEmail(email: string) {
return /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(email);
}

40
src/time.ts Normal file
View File

@ -0,0 +1,40 @@
/**
* Calculate the number of milliseconds until date/time
*
* @param {Date | number} date - Target
* @returns {number} - Number of milliseconds until target
*/
export function timeUntil(date: Date | number): number {
return (date instanceof Date ? date.getTime() : date) - (new Date()).getTime();
}
/**
* Use in conjunction with `await` to pause an async script
*
* @example
* ```ts
* async () => {
* ...
* await sleep(1000) // Pause for 1 second
* ...
* }
* ```
*
* @param {number} ms - Time to pause for in milliseconds
* @returns {Promise<unknown>} - Resolves promise when it's time to resume
*/
export function sleep(ms: number) {
return new Promise(res => setTimeout(res, ms));
}
export function formatDate(date: Date | number | string) {
const d = date instanceof Date ? date : new Date(date);
return new Intl.DateTimeFormat("en-us", {
weekday: "long",
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true
}).format(d);
}

92
src/xhr.ts Normal file
View File

@ -0,0 +1,92 @@
export type FetchInterceptor = (resp: Response, next: () => any) => any;
export class XHR<T> {
private static interceptors: {[key: number]: FetchInterceptor} = {};
static headers: Record<string, string | null> = {};
private interceptors: {[key: string]: FetchInterceptor} = {};
constructor(public readonly baseUrl: string,
public readonly headers: Record<string, string | null> = {}
) { }
static addInterceptor(fn: FetchInterceptor): () => {};
static addInterceptor(key: string, fn: FetchInterceptor): () => {};
static addInterceptor(keyOrFn: string | FetchInterceptor, fn?: FetchInterceptor): () => {} {
const func: any = fn ? fn : keyOrFn;
const key: string = typeof keyOrFn == 'string' ? keyOrFn :
`_${Object.keys(XHR.interceptors).length.toString()}`;
XHR.interceptors[<any>key] = func;
return () => delete XHR.interceptors[<any>key];
}
addInterceptor(fn: FetchInterceptor): () => {};
addInterceptor(key: string, fn: FetchInterceptor): () => {};
addInterceptor(keyOrFn: string | FetchInterceptor, fn?: FetchInterceptor): () => {} {
const func: any = fn ? fn : keyOrFn;
const key: string = typeof keyOrFn == 'string' ? keyOrFn :
`_${Object.keys(this.interceptors).length.toString()}`;
this.interceptors[<any>key] = func;
return () => delete this.interceptors[<any>key];
}
getInterceptors() {
return [...Object.values(XHR.interceptors), ...Object.values(this.interceptors)];
}
fetch<T2 = T>(href?: string, body?: any, opts: any = {}): Promise<T2> {
const headers = {
'Content-Type': (body && !(body instanceof FormData)) ? 'application/json' : undefined,
...XHR.headers,
...this.headers,
...opts.headers
};
Object.keys(headers).forEach(h => { if(!headers[h]) delete headers[h]; });
return fetch(`${this.baseUrl}${href || ''}`.replace(/([^:]\/)\/+/g, '$1'), {
headers,
method: opts.method || (body ? 'POST' : 'GET'),
body: (headers['Content-Type'].startsWith('application/json') && body) ? JSON.stringify(body) : body
}).then(async resp => {
for(let fn of this.getInterceptors()) {
const wait = new Promise(res =>
fn(resp, () => res(null)));
await wait;
}
if(resp.headers['Content-Type'] && resp.headers.get('Content-Type').startsWith('application/json'))
return await resp.json();
if(resp.headers['Content-Type'] && resp.headers.get('Content-Type').startsWith('text/plain'))
return await resp.text();
return resp;
});
}
delete<T2 = void>(url?: string, opts?: any): Promise<T2> {
return this.fetch(url, null, {method: 'delete', ...opts});
}
get<T2 = T>(url?: string, opts?: any): Promise<T2> {
return this.fetch(url, null, {method: 'get', ...opts});
}
patch<T2 = T>(data: T2, url?: string, opts?: any): Promise<T2> {
return this.fetch(url, data, {method: 'patch', ...opts});
}
post<T2 = T>(data: T2, url?: string, opts?: any): Promise<T2> {
return this.fetch(url, data, {method: 'post', ...opts});
}
put<T2 = T>(data: Partial<T2>, url?: string, opts?: any): Promise<T2> {
return this.fetch(url, data, {method: 'put', ...opts});
}
new<T2 = T>(href: string, headers: Record<string, string | null>): XHR<T2> {
const fetch = new XHR<T2>(`${this.baseUrl}${href}`, {
...this.headers,
...headers,
});
Object.entries(this.interceptors).map(([key, value]) =>
fetch.addInterceptor(key, value));
return fetch;
}
}