Skip to content

Commit

Permalink
Adjust label types
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Oct 2, 2024
1 parent d3afae7 commit 3b6cd6f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
12 changes: 9 additions & 3 deletions arches_vue_utils/src/arches_vue_utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
15 changes: 6 additions & 9 deletions arches_vue_utils/src/arches_vue_utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,24 @@ 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,
};
};

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,
);
Expand Down Expand Up @@ -82,7 +79,7 @@ describe("getItemLabel() util", () => {
},
"fr",
systemLanguageCode,
).languageCode,
).language_id,
).toEqual(systemLanguageCode);
});
});
20 changes: 13 additions & 7 deletions arches_vue_utils/src/arches_vue_utils/utils.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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();
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 3b6cd6f

Please sign in to comment.