-
Notifications
You must be signed in to change notification settings - Fork 1
/
id_utilities.ts
66 lines (56 loc) · 2.13 KB
/
id_utilities.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
export const regex = {
area: new RegExp("^[0-9]{2}-[0-9]{2}$"),
category: new RegExp("^[0-9]{2}$"),
item: new RegExp("^[0-9]{2}\\.[0-9]{2}$"),
/**
* @description
* Mostly for matching in Deno.walk
* Given an unknown ID, match anywhere in the string.
* @param id - any area, category, or item id
*/
matchId: (id: string): RegExp => {
if (isAreaId(id)) return RegExp(id + "\\s[^/]+$");
if (isCategoryId(id)) return RegExp("[^.-]" + id + "\\s[^/]+$");
if (isItemId(id)) return RegExp(id.replace(".", "\\."));
throw new Error("Invalid Location");
},
};
/**
* @description parse the id from a string. Meant for use in names, NOT paths.
* @param str - any string
*/
export const parseId = (str = ""): string => {
const id = str.split(" ")[0];
if (
regex.area.test(id) ||
regex.category.test(id) ||
regex.item.test(id)
) {
return id;
}
return "";
};
/** @description describes whether a string is an areaId (ex: 10-19) */
export const isAreaId = (str: string) => regex.area.test(str);
/** @description describes whether a string is an categoryId (ex: 10) */
export const isCategoryId = (str: string) => regex.category.test(str);
/** @description describes whether a string is an itemId (ex: 10.10) */
export const isItemId = (str: string) => regex.item.test(str);
/** @description describes whether a string is any kind of JD id */
export const isLocationId = (str: string) => (
isAreaId(str) ||
isCategoryId(str) ||
isItemId(str)
);
/** @description describes whether a filename is an areaId (ex: 10-19 MyArea) */
export const isAreaFilename = (str: string) => isAreaId(parseId(str));
/** @description describes whether a filename is an categoryId (ex: 10 MyCat) */
export const isCategoryFilename = (str: string) => isCategoryId(parseId(str));
/** @description describes whether a filename is an itemId (ex: 10.10 MyItem) */
export const isItemFilename = (str: string) => isItemId(parseId(str));
/** @description describes whether a filename is any kind of JD id */
export const isLocationFilename = (str: string) => (
isAreaFilename(str) ||
isCategoryFilename(str) ||
isItemFilename(str)
);