-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprepare-datasets.ts
98 lines (85 loc) · 3.29 KB
/
prepare-datasets.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { DatasetData, StoryData } from '$types/veda';
import { optionAll } from '$components/common/browse-controls/constants';
const TAXONOMY_TOPICS = 'Topics';
const isDatasetData = (data: DatasetData | StoryData): data is DatasetData => {
return 'layers' in data;
};
interface FilterOptionsType {
search: string | null;
taxonomies: Record<string, string | string[]> | null;
sortField?: string | null;
sortDir?: string | null;
filterLayers?: boolean | null;
}
export function prepareDatasets(data: DatasetData[], options: FilterOptionsType): DatasetData[];
export function prepareDatasets(data: StoryData[], options: FilterOptionsType): StoryData[];
export function prepareDatasets (
data: DatasetData[] | StoryData[],
options: FilterOptionsType
) {
const { sortField, sortDir, search, taxonomies, filterLayers } = options;
let filtered = [...data];
// Does the free text search appear in specific fields?
if (search && search.length >= 3) {
const searchLower = search.toLowerCase();
// Function to check if searchLower is included in any of the string fields
const includesSearchLower = (str) => str.toLowerCase().includes(searchLower);
// Function to determine if a layer matches the search criteria
const layerMatchesSearch = (layer) =>
includesSearchLower(layer.stacCol) ||
includesSearchLower(layer.name) ||
includesSearchLower(layer.parentDataset.name) ||
includesSearchLower(layer.parentDataset.id) ||
includesSearchLower(layer.description);
filtered = filtered
.filter((d) => {
// Pre-calculate lowercased versions to use in comparisons
const idLower = d.id.toLowerCase();
const nameLower = d.name.toLowerCase();
const descriptionLower = d.description.toLowerCase();
const topicsTaxonomy = d.taxonomy.find((t) => t.name === TAXONOMY_TOPICS);
// Check if any of the conditions for including the item are met
return (
idLower.includes(searchLower) ||
nameLower.includes(searchLower) ||
descriptionLower.includes(searchLower) ||
(isDatasetData(d) && d.layers.some(layerMatchesSearch)) ||
topicsTaxonomy?.values.some((t) => includesSearchLower(t.name))
);
});
if (filterLayers)
filtered = filtered.map((d) => ({
...d,
layers: (isDatasetData(d) && d.layers.filter(layerMatchesSearch)),
})) as DatasetData[];
}
taxonomies &&
Object.entries(taxonomies).forEach(([name, value]) => {
if (!value.includes(optionAll.id)) {
filtered = filtered.filter((d) =>
d.taxonomy.some(
(t) => t.name === name && t.values.some((v) => value.includes(v.id))
)
);
}
});
sortField &&
/* eslint-disable-next-line fp/no-mutating-methods */
filtered.sort((a, b) => {
if (!a[sortField]) return Infinity;
return a[sortField]?.localeCompare(b[sortField]);
});
if (filterLayers && sortField) {
filtered = filtered.map((d) => ({
...d,
layers:
isDatasetData(d) &&
d.layers.sort((a, b) => a[sortField]?.localeCompare(b[sortField]) || 0)
})) as DatasetData[];
}
if (sortDir === 'desc') {
/* eslint-disable-next-line fp/no-mutating-methods */
filtered.reverse();
}
return filtered;
}