utils/src/misc.ts
ztimson 3deb536323
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
fixed gravatar default image
2024-03-20 20:44:53 -04:00

65 lines
1.8 KiB
TypeScript

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
* @param {string} def Default image, can be a link or '404', see: https://docs.gravatar.com/general/images/
* @returns {string} Gravatar URL
*/
export function gravatar(email: string, def='mp') {
if(!email) return '';
return `https://www.gravatar.com/avatar/${md5(email)}?d=${def}`;
}
/** 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: any = {};
split.forEach((q: any) => {
const [key, val] = q.split('=');
query[key] = val;
});
groups.query = query;
}
return groups;
}