utils/src/misc.ts

28 lines
877 B
TypeScript
Raw Normal View History

2024-10-14 13:52:03 -04:00
import {PathEvent} from './path-events.ts';
2024-02-07 01:33:07 -05:00
import {md5} from './string';
/**
* 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/
2024-02-07 01:33:07 -05:00
* @returns {string} Gravatar URL
*/
2024-03-20 20:44:53 -04:00
export function gravatar(email: string, def='mp') {
2024-02-07 01:33:07 -05:00
if(!email) return '';
return `https://www.gravatar.com/avatar/${md5(email)}?d=${def}`;
2024-02-07 01:33:07 -05:00
}
2024-10-12 12:27:45 -04:00
/**
* Escape any regex special characters to avoid misinterpretation during search
*
* @param {string} value String which should be escaped
* @return {string} New escaped sequence
*/
2024-10-12 12:30:23 -04:00
export function escapeRegex(value: string) {
2024-10-12 12:27:45 -04:00
return value.replace(/[.*+?^${}()|\[\]\\]/g, '\\$&');
}
2024-10-14 13:52:03 -04:00
export type Listener = (event: PathEvent, ...args: any[]) => any;
export type Unsubscribe = () => void;