Browse Source

refactor: add some useful helper util functions for rounding numbers or getting emoji flag

Kristian Vos 1 tháng trước cách đây
mục cha
commit
3d71b50993
1 tập tin đã thay đổi với 21 bổ sung0 xóa
  1. 21 0
      frontend/src/utils.ts

+ 21 - 0
frontend/src/utils.ts

@@ -76,5 +76,26 @@ export default {
 		const hour = `${date.getHours()}`.padStart(2, "0");
 		const minute = `${date.getMinutes()}`.padStart(2, "0");
 		return `${year}-${month}-${day} ${hour}:${minute}`;
+	},
+	// Turns for example 125953 into 125.95K
+	getNumberRounded: (number: number) => {
+		if (number > 1000000000 - 1)
+			return `${(number / 1000000000).toFixed(2)}B`;
+		if (number > 1000000 - 1) return `${(number / 1000000).toFixed(2)}M`;
+		if (number > 1000 - 1) return `${(number / 1000).toFixed(2)}K`;
+		return number;
+	},
+	// Based on https://stackoverflow.com/a/42235254 - ported to JavaScript
+	getEmojiFlagForCountryCode: (countryCode: string) => {
+		const flagOffset = 0x1f1e6;
+		const asciiOffset = 0x41;
+
+		const firstChar = countryCode.codePointAt(0) - asciiOffset + flagOffset;
+		const secondChar =
+			countryCode.codePointAt(1) - asciiOffset + flagOffset;
+
+		return (
+			String.fromCodePoint(firstChar) + String.fromCodePoint(secondChar)
+		);
 	}
 };