summaryrefslogtreecommitdiff
path: root/src/misc/id/object-id.ts
blob: 392ea4330108c2c6f876df8d05e1e5176fddf040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const CHARS = '0123456789abcdef';

function getTime(time: number) {
	if (time < 0) time = 0;
	if (time === 0) {
		return CHARS[0];
	}

	time = Math.floor(time / 1000);

	return time.toString(16).padStart(8, CHARS[0]);
}

function getRandom() {
	let str = '';

	for (let i = 0; i < 16; i++) {
		str += CHARS[Math.floor(Math.random() * CHARS.length)];
	}

	return str;
}

export function genObjectId(date: Date): string {
	return getTime(date.getTime()) + getRandom();
}