-
Notifications
You must be signed in to change notification settings - Fork 1
/
format_id.ts
47 lines (42 loc) · 1.41 KB
/
format_id.ts
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/** @description Ensure itemId is formatted correctly ("10.00") */
export function getFormattedItemId({ itemId }: { itemId: string }): string {
return formatItemId(Number(itemId));
}
/**
* @description
* Given values for item OR categoryId, correctly format a categoryId ("10")
*/
export function getFormattedCategoryId(
{ itemId, categoryId }: { itemId?: string; categoryId?: string },
): string {
if (categoryId) return formatCategoryId(Number(categoryId));
if (itemId) return formatCategoryId(Math.floor(Number(itemId)));
throw new Error("getFormattedCategoryId: no id given");
}
/**
* @description
* Given values for itemId OR categoryId OR areaId, correctly format an areaId
*/
export function getFormattedAreaId(
{ itemId, categoryId, areaId }: {
itemId?: string;
categoryId?: string;
areaId?: string;
},
): string {
if (areaId) return areaId;
if (categoryId) return getAreaIdFromCategoryId(Number(categoryId));
if (itemId) return getAreaIdFromCategoryId(Math.floor(Number(itemId)));
throw new Error("getFormattedAreaId: no id given");
}
function getAreaIdFromCategoryId(category: number): string {
const start = Math.floor(category / 10) * 10;
const end = start + 9;
return `${start}-${end}`;
}
function formatCategoryId(category: number): string {
return category < 10 ? "0" + category : String(category);
}
function formatItemId(id: number): string {
return String(id.toFixed(2));
}