From 74987e56a2e396529c03f4124133b05277d82b13 Mon Sep 17 00:00:00 2001 From: jamiefeiss Date: Mon, 4 Sep 2023 13:37:18 +1000 Subject: [PATCH] Moved sorting by title then IRI to a reusable function --- src/components/search/CatPrezSearchMap.vue | 16 ++--------- src/components/search/SpacePrezSearchMap.vue | 28 +++----------------- src/util/helpers.ts | 19 +++++++++++++ src/views/ItemListView.vue | 15 ++--------- src/views/PropTableView.vue | 22 +++++---------- 5 files changed, 33 insertions(+), 67 deletions(-) diff --git a/src/components/search/CatPrezSearchMap.vue b/src/components/search/CatPrezSearchMap.vue index e162714..fcf14d1 100644 --- a/src/components/search/CatPrezSearchMap.vue +++ b/src/components/search/CatPrezSearchMap.vue @@ -8,7 +8,7 @@ import { useApiRequest, useSparqlRequest } from "@/composables/api"; import { useRdfStore } from "@/composables/rdfStore"; import { catalogSpatialSearch, getThemesQuery } from "@/sparqlQueries/catalogSearch"; import { shapeQueryPart } from "@/util/mapSearchHelper" -import { copyToClipboard } from "@/util/helpers"; +import { copyToClipboard, sortByTitle } from "@/util/helpers"; import MapClient from "@/components/MapClient.vue"; import LoadingMessage from "@/components/LoadingMessage.vue"; import ErrorMessage from "@/components/ErrorMessage.vue"; @@ -137,19 +137,7 @@ async function getCatalogs() { } }, namedNode(qnameToIri("a")), namedNode(qnameToIri("dcat:Catalog")), null); - // sort by title first, then by IRI if no title - catalogOptions.sort((a, b) => { - if (a.title && b.title) { - return a.title.localeCompare(b.title); - } else if (a.title) { - return -1; - } else if (b.title) { - return 1; - } else { - return a.iri.localeCompare(b.iri); - } - }); - catalogs.value = catalogOptions; + catalogs.value = catalogOptions.sort(sortByTitle); } } diff --git a/src/components/search/SpacePrezSearchMap.vue b/src/components/search/SpacePrezSearchMap.vue index 018e7ac..cadd4ba 100644 --- a/src/components/search/SpacePrezSearchMap.vue +++ b/src/components/search/SpacePrezSearchMap.vue @@ -5,7 +5,7 @@ import { apiBaseUrlConfigKey, mapConfigKey, type MapConfig, type ProfileHeader } import { useUiStore } from "@/stores/ui"; import { useApiRequest, useConcurrentApiRequests, useSparqlRequest } from "@/composables/api"; import { useRdfStore } from "@/composables/rdfStore"; -import { copyToClipboard } from "@/util/helpers"; +import { copyToClipboard, sortByTitle } from "@/util/helpers"; import { AreaTypes, ShapeTypes, type Coords } from "@/components/MapClient.d"; import { enumToOptions } from "@/util/mapSearchHelper"; import MapClient from "@/components/MapClient.vue"; @@ -215,31 +215,11 @@ async function getDatasets() { }, subject, namedNode(qnameToIri("rdfs:member")), null); // sort by title first, then by IRI if no title - datasetOptions[subject.value].featureCollections.sort((a, b) => { - if (a.title && b.title) { - return a.title.localeCompare(b.title); - } else if (a.title) { - return -1; - } else if (b.title) { - return 1; - } else { - return a.iri.localeCompare(b.iri); - } - }); + datasetOptions[subject.value].featureCollections.sort(sortByTitle); }, namedNode(qnameToIri("a")), namedNode(qnameToIri("dcat:Dataset")), null); // sort by title first, then by IRI if no title - datasets.value = Object.values(datasetOptions).sort((a, b) => { - if (a.title && b.title) { - return a.title.localeCompare(b.title); - } else if (a.title) { - return -1; - } else if (b.title) { - return 1; - } else { - return a.iri.localeCompare(b.iri); - } - }); + datasets.value = Object.values(datasetOptions).sort(sortByTitle); } } @@ -343,7 +323,7 @@ onMounted(async () => { - +
  • diff --git a/src/util/helpers.ts b/src/util/helpers.ts index 7a7b9bd..d7a1ef1 100644 --- a/src/util/helpers.ts +++ b/src/util/helpers.ts @@ -29,3 +29,22 @@ export function ensureProfiles() { export function copyToClipboard(text: string) { navigator.clipboard.writeText(text.trim()); } + +/** + * Sorts an array of objects alphabetically first, and then by IRI if some elements lack a title + * + * @param a + * @param b + * @returns + */ +export const sortByTitle = (a: T, b: T): number => { + if (a.title && b.title) { + return a.title.localeCompare(b.title); + } else if (a.title) { + return -1; + } else if (b.title) { + return 1; + } else { + return a.iri.localeCompare(b.iri); + } +}; \ No newline at end of file diff --git a/src/views/ItemListView.vue b/src/views/ItemListView.vue index 8d9367e..7b47600 100644 --- a/src/views/ItemListView.vue +++ b/src/views/ItemListView.vue @@ -14,7 +14,7 @@ import PaginationComponent from "@/components/PaginationComponent.vue"; import { getPrezSystemLabel } from "@/util/prezSystemLabelMapping"; import SortableTabularList from "@/components/SortableTabularList.vue"; import LoadingMessage from "@/components/LoadingMessage.vue"; -import { ensureProfiles } from "@/util/helpers"; +import { ensureProfiles, sortByTitle } from "@/util/helpers"; const { namedNode } = DataFactory; @@ -227,18 +227,7 @@ function getProperties() { items.value.push(c); }); - // sort by title first, then by IRI if no title - items.value.sort((a, b) => { - if (a.title && b.title) { - return a.title.localeCompare(b.title); - } else if (a.title) { - return -1; - } else if (b.title) { - return 1; - } else { - return a.iri.localeCompare(b.iri); - } - }); + items.value.sort(sortByTitle); } function getIRILocalName(iri: string) { diff --git a/src/views/PropTableView.vue b/src/views/PropTableView.vue index 9aa43fd..a72ae6f 100644 --- a/src/views/PropTableView.vue +++ b/src/views/PropTableView.vue @@ -16,7 +16,7 @@ import { getPrezSystemLabel } from "@/util/prezSystemLabelMapping"; import MapClient from "@/components/MapClient.vue"; import SortableTabularList from "@/components/SortableTabularList.vue"; import LoadingMessage from "@/components/LoadingMessage.vue"; -import { ensureProfiles } from "@/util/helpers"; +import { ensureProfiles, sortByTitle } from "@/util/helpers"; const { namedNode } = DataFactory; @@ -320,17 +320,7 @@ function getChildren() { }, namedNode(item.value.iri), namedNode(childrenPredicate.value), null); // sort by title, then by IRI - children.value.sort((a, b) => { - if (a.title && b.title) { - return a.title.localeCompare(b.title); - } else if (a.title) { - return -1; - } else if (b.title) { - return 1; - } else { - return a.iri.localeCompare(b.iri); - } - }); + children.value.sort(sortByTitle); } } @@ -388,11 +378,11 @@ function getAllConcepts() { if (!!c.broader && c.broader !== "") { const parent = conceptArray[indexMap[c.broader]]; - parent.children = [...(parent.children || []), c].sort((a, b) => a.title.localeCompare(b.title)); + parent.children = [...(parent.children || []), c].sort(sortByTitle); parent.childrenCount = parent.children.length; } }); - conceptsList.sort((a, b) => a.title.localeCompare(b.title)); + conceptsList.sort(sortByTitle); concepts.value = conceptsList; } @@ -422,7 +412,7 @@ async function getTopConcepts(page: number = 1) { concepts.value.push(c); }, namedNode(item.value.iri), namedNode(conceptQnameToIri("skos:hasTopConcept")), null); - concepts.value.sort((a, b) => a.title.localeCompare(b.title)); + concepts.value.sort(sortByTitle); } } @@ -466,7 +456,7 @@ async function getNarrowers({ iriPath, link, page = 1 }: { iriPath: string, link parent!.children.push(c); }, namedNode(parent!.iri), namedNode(conceptQnameToIri("skos:narrower")), null); - parent!.children.sort((a, b) => a.title.localeCompare(b.title)); + parent!.children.sort(sortByTitle); } }