yufan.me/src/helpers/tools.ts

25 lines
786 B
TypeScript
Raw Normal View History

import crypto from 'node:crypto';
2024-06-14 02:11:26 +08:00
export const makeToken = (
length: number,
characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
) => {
let result = '';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
};
export const urlJoin = (base: string, ...paths: string[]): string => {
return Array.from([base, ...paths])
.reduce((left, right) => left + (left.endsWith('/') || right.startsWith('/') ? '' : '/') + right)
.replace(/([^:]\/)\/+/g, '$1');
};
export const encodedEmail = (email: string): string =>
crypto.createHash('md5').update(email.trim().toLowerCase()).digest('hex');