2019-04-13 18:40:29 +02:00
|
|
|
const CHARS = '0123456789abcdef';
|
|
|
|
|
2023-04-08 21:41:06 +02:00
|
|
|
// same as object-id
|
|
|
|
export const meidRegExp = /^[0-9a-f]{24}$/;
|
|
|
|
|
2019-04-13 18:40:29 +02:00
|
|
|
function getTime(time: number) {
|
|
|
|
if (time < 0) time = 0;
|
|
|
|
if (time === 0) {
|
|
|
|
return CHARS[0];
|
|
|
|
}
|
|
|
|
|
2019-04-13 18:47:46 +02:00
|
|
|
time += 0x800000000000;
|
|
|
|
|
|
|
|
return time.toString(16).padStart(12, CHARS[0]);
|
2019-04-13 18:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getRandom() {
|
|
|
|
let str = '';
|
|
|
|
|
2019-04-13 18:47:46 +02:00
|
|
|
for (let i = 0; i < 12; i++) {
|
2019-04-13 18:40:29 +02:00
|
|
|
str += CHARS[Math.floor(Math.random() * CHARS.length)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function genMeid(date: Date): string {
|
2019-04-20 19:30:18 +02:00
|
|
|
return getTime(date.getTime()) + getRandom();
|
2019-04-13 18:40:29 +02:00
|
|
|
}
|
2023-04-08 21:41:06 +02:00
|
|
|
|
|
|
|
export function parseMeid(id: string): { date: Date; } {
|
|
|
|
return {
|
|
|
|
date: new Date(parseInt(id.slice(0, 12), 16) - 0x800000000000),
|
|
|
|
};
|
|
|
|
}
|