all repos — discord-utils @ fe1626583ce351ffa2bf22c5d83dd638aa409753

src/utils/time.ts (view raw)

 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
export function parseDuration(input: string): number {
	const matches = input.match(/(\d+)([smhd])/g) || [];
	let totalSeconds = 0;

	for (const match of matches) {
		const value = Number.parseInt(match.match(/\d+/)?.[0] || "0", 10);
		const unit = match.match(/[smhd]/)?.[0];

		switch (unit) {
			case "s":
				totalSeconds += value;
				break;
			case "m":
				totalSeconds += value * 60;
				break;
			case "h":
				totalSeconds += value * 3600;
				break;
			case "d":
				totalSeconds += value * 86400;
				break;
		}
	}

	return totalSeconds;
}