From 42f86d15edcd4b1ec6b035bd44b5aa653bfff2d8 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Thu, 12 Sep 2024 13:49:52 -0400 Subject: [PATCH] Adjust label types --- .../src/arches_vue_utils/types.ts | 12 ++++++++--- .../src/arches_vue_utils/utils.spec.ts | 15 ++++++-------- .../src/arches_vue_utils/utils.ts | 20 ++++++++++++------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/arches_vue_utils/src/arches_vue_utils/types.ts b/arches_vue_utils/src/arches_vue_utils/types.ts index 22c81df..b55773a 100644 --- a/arches_vue_utils/src/arches_vue_utils/types.ts +++ b/arches_vue_utils/src/arches_vue_utils/types.ts @@ -9,10 +9,16 @@ export interface Language { export interface Label { value: string; - languageCode: string; - valuetype: "prefLabel" | "altLabel" | "hiddenLabel"; + language_id: string; + valuetype_id: string; } -export interface Labellable { +export interface HasLabels { labels: Label[]; } + +export interface HasValues { + values: Label[]; +} + +export type Labellable = HasLabels | HasValues; diff --git a/arches_vue_utils/src/arches_vue_utils/utils.spec.ts b/arches_vue_utils/src/arches_vue_utils/utils.spec.ts index 7ef15e0..eb231d3 100644 --- a/arches_vue_utils/src/arches_vue_utils/utils.spec.ts +++ b/arches_vue_utils/src/arches_vue_utils/utils.spec.ts @@ -8,14 +8,11 @@ import { getItemLabel, rankLabel } from "@/arches_vue_utils/utils.ts"; import type { Label } from "@/arches_vue_utils/types"; // Test utils -const asLabel = ( - valuetype: "prefLabel" | "altLabel" | "hiddenLabel", - languageCode: string, -): Label => { +const asLabel = (valuetype_id: string, language_id: string): Label => { return { value: "arbitrary", - valuetype, - languageCode, + valuetype_id, + language_id, }; }; @@ -23,12 +20,12 @@ const systemLanguageCode = "en-ZA"; // arbitrary describe("rankLabel() util", () => { const rank = ( - valuetype: "prefLabel" | "altLabel" | "hiddenLabel", + valuetype_id: string, labelLanguageCode: string, desiredLanguageCode: string, ) => rankLabel( - asLabel(valuetype, labelLanguageCode), + asLabel(valuetype_id, labelLanguageCode), desiredLanguageCode, systemLanguageCode, ); @@ -82,7 +79,7 @@ describe("getItemLabel() util", () => { }, "fr", systemLanguageCode, - ).languageCode, + ).language_id, ).toEqual(systemLanguageCode); }); }); diff --git a/arches_vue_utils/src/arches_vue_utils/utils.ts b/arches_vue_utils/src/arches_vue_utils/utils.ts index 21be1bb..84eb362 100644 --- a/arches_vue_utils/src/arches_vue_utils/utils.ts +++ b/arches_vue_utils/src/arches_vue_utils/utils.ts @@ -1,6 +1,11 @@ import { ALT_LABEL, PREF_LABEL } from "@/arches_vue_utils/constants.ts"; -import type { Label, Labellable } from "@/arches_vue_utils/types"; +import type { + HasLabels, + HasValues, + Label, + Labellable, +} from "@/arches_vue_utils/types"; /* Port of rank_label in arches.app.utils.i18n python module */ export const rankLabel = ( @@ -9,15 +14,15 @@ export const rankLabel = ( systemLanguageCode: string, ): number => { let rank = 1; - if (label.valuetype === PREF_LABEL) { + if (label.valuetype_id === PREF_LABEL) { rank = 10; - } else if (label.valuetype === ALT_LABEL) { + } else if (label.valuetype_id === ALT_LABEL) { rank = 4; } // Some arches deployments may not have standardized capitalizations. - const labelLanguageFull = label.languageCode.toLowerCase(); - const labelLanguageNoRegion = label.languageCode + const labelLanguageFull = label.language_id.toLowerCase(); + const labelLanguageNoRegion = label.language_id .split(/[-_]/)[0] .toLowerCase(); const preferredLanguageFull = preferredLanguageCode.toLowerCase(); @@ -47,10 +52,11 @@ export const getItemLabel = ( preferredLanguageCode: string, systemLanguageCode: string, ): Label => { - if (!item.labels.length) { + const labels = (item as HasLabels).labels ?? (item as HasValues).values; + if (!labels.length) { throw new Error(); } - return item.labels.sort( + return labels.sort( (a, b) => rankLabel(b, preferredLanguageCode, systemLanguageCode) - rankLabel(a, preferredLanguageCode, systemLanguageCode),