Skip to content

Commit

Permalink
Add variables
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoLaval committed Dec 7, 2024
1 parent ea57c81 commit b53a75c
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "addict",
"version": "0.2.0",
"version": "0.3.0",
"license": "MIT",
"type": "module",
"scripts": {
Expand Down
17 changes: 14 additions & 3 deletions app/src/model/ddi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { CATEGORY_SCHEME_ID, CATEGORY_ID, CATEGORY_LABEL, CATEGORY_SCHEME_LABEL } from "@utils/contants";
import {
CATEGORY_SCHEME_ID,
CATEGORY_ID,
CATEGORY_LABEL,
CATEGORY_SCHEME_LABEL,
VARIABLE_ID,
VARIABLE_LABEL
} from "@utils/contants";

export type DDIObjectIDs = typeof CATEGORY_SCHEME_ID | typeof CATEGORY_ID;
export type DDIObjectLabels = typeof CATEGORY_SCHEME_LABEL | typeof CATEGORY_LABEL;
export type DDIObjectIDs = typeof CATEGORY_SCHEME_ID | typeof CATEGORY_ID | typeof VARIABLE_ID;

export type DDIObjectLabels =
| typeof CATEGORY_SCHEME_LABEL
| typeof CATEGORY_LABEL
| typeof VARIABLE_LABEL;

export type DDIBaseObject = {
URN: string;
Expand Down
15 changes: 13 additions & 2 deletions app/src/utils/badges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { lighten } from "@mui/material/styles";

import { DDIObjectIDs, DDIObjectLabels } from "@model/ddi";

import { CATEGORY_SCHEME_ID, CATEGORY_ID, CATEGORY_SCHEME_LABEL, CATEGORY_LABEL } from "./contants";
import {
CATEGORY_SCHEME_ID,
CATEGORY_ID,
CATEGORY_SCHEME_LABEL,
CATEGORY_LABEL,
VARIABLE_ID,
VARIABLE_LABEL
} from "./contants";

export const getLabelFromId = (id: DDIObjectIDs): DDIObjectLabels => {
if (id === CATEGORY_SCHEME_ID) {
Expand All @@ -11,13 +18,17 @@ export const getLabelFromId = (id: DDIObjectIDs): DDIObjectLabels => {
if (id === CATEGORY_ID) {
return CATEGORY_LABEL;
}
if (id === VARIABLE_ID) {
return VARIABLE_LABEL;
}
throw new Error(`Unknow DDI object id: ${id}`);
};

export const getBadgeColor =
(baseColor: string) =>
(id: DDIObjectIDs): string => {
if (id === CATEGORY_SCHEME_ID) return lighten(baseColor, 0);
if (id === CATEGORY_ID) return lighten(baseColor, 0.15);
if (id === CATEGORY_ID) return lighten(baseColor, 0.1);
if (id === VARIABLE_ID) return lighten(baseColor, 0.2);
throw new Error(`Unknow id: ${id}`);
};
5 changes: 4 additions & 1 deletion app/src/utils/contants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { DDIObjectIDs } from "@model/ddi";
/* Route path */
export const CATEGORY_SCHEME_ID = "category-scheme";
export const CATEGORY_ID = "category";
export const VARIABLE_ID = "variable";

export const DDI_OBJECTS = [CATEGORY_SCHEME_ID, CATEGORY_ID] as Array<DDIObjectIDs>;
export const DDI_OBJECTS = [CATEGORY_SCHEME_ID, CATEGORY_ID, VARIABLE_ID] as Array<DDIObjectIDs>;

/* Supported DDI objects */
export const CATEGORY_SCHEME_LABEL = "Category Scheme";
export const CATEGORY_LABEL = "Category";
export const VARIABLE_LABEL = "Variable";

/* XML */

export const CATEGORY_SCHEME_XML_PATH = "l:CategoryScheme";
export const CATEGORY_XML_PATH = "l:Category";
export const VARIABLE_XML_PATH = "l:Variable";
7 changes: 3 additions & 4 deletions app/src/utils/xml/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CATEGORY_ID, CATEGORY_XML_PATH, CATEGORY_SCHEME_ID } from "@utils/conta

import { DDIBaseObject, DDIDetailledObject } from "@model/ddi";

import { getCode, getElementLabel, getElementURN, getLabelsByLang, getPreferedLabel } from "./common";
import { getCode, getElementContent, getElementURN, getLabelsByLang, getPreferedLabel } from "./common";

export const getCategories = (xmlDoc: Document | Element): DDIBaseObject[] => {
const categories = xmlDoc.getElementsByTagName(CATEGORY_XML_PATH);
Expand All @@ -25,11 +25,10 @@ export const getCategory = (xmlDoc: Document, id: string): DDIDetailledObject =>
if (!category) throw new Error(`Unknow Category Scheme: ${id}`);
const labels = category.getElementsByTagName("r:Content");

// Find parent
const categoryScheme = category.closest("CategoryScheme") as Element;
const parentURN = getElementURN(categoryScheme);
const parentLabel = getElementLabel(categoryScheme);
// TODO find parent
const parentLabel = getElementContent(categoryScheme);

return {
URN: getElementURN(category),
labels: getLabelsByLang(labels),
Expand Down
2 changes: 1 addition & 1 deletion app/src/utils/xml/CategoryScheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const getCategoryScheme = (xmlDoc: Document, id: string): DDIDetailledObj
if (!categoryScheme) throw new Error(`Unknow Category Scheme: ${id}`);
const labels = categoryScheme.getElementsByTagName("r:Content");
const children = getCategories(categoryScheme);
// TODO getCategories as child

// TODO find parent
return {
URN: getElementURN(categoryScheme),
Expand Down
33 changes: 33 additions & 0 deletions app/src/utils/xml/Variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { VARIABLE_ID, VARIABLE_XML_PATH } from "@utils/contants";

import { DDIBaseObject, DDIDetailledObject } from "@model/ddi";

import { getCode, getElementURN, getLabelsByLang, getPreferedLabel } from "./common";

export const getVariables = (xmlDoc: Document | Element): DDIBaseObject[] => {
const variables = xmlDoc.getElementsByTagName(VARIABLE_XML_PATH);
return Array.from(variables).map(v => {
const labels = v.getElementsByTagName("r:Content");
return {
URN: getElementURN(v),
label: getPreferedLabel(getLabelsByLang(labels)),
type: VARIABLE_ID
};
});
};

export const getVariable = (xmlDoc: Document, id: string): DDIDetailledObject => {
const variables = xmlDoc.getElementsByTagName(VARIABLE_XML_PATH);
const variable = Array.from(variables).find(v => {
const foundId = v.querySelector("ID")?.textContent;
return id === foundId;
});
if (!variable) throw new Error(`Unknow Variable: ${id}`);
const labels = variable.getElementsByTagName("r:Content");

return {
URN: getElementURN(variable),
labels: getLabelsByLang(labels),
code: getCode(variable)
};
};
2 changes: 1 addition & 1 deletion app/src/utils/xml/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const getElementURN = (e: Element): string => {
return `${agency}:${id}:${version}`;
};

export const getElementLabel = (e: Element): string =>
export const getElementContent = (e: Element): string =>
getLabelsByLang(e.getElementsByTagName("r:Content"))[PREFERED_LANGUAGE];

export const getCode = (e: Element): string => new XMLSerializer().serializeToString(e);
19 changes: 15 additions & 4 deletions app/src/utils/xml/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import { CATEGORY_SCHEME_ID, CATEGORY_ID, CATEGORY_SCHEME_LABEL, CATEGORY_LABEL } from "@utils/contants";
import {
CATEGORY_SCHEME_ID,
CATEGORY_ID,
CATEGORY_SCHEME_LABEL,
CATEGORY_LABEL,
VARIABLE_ID,
VARIABLE_LABEL
} from "@utils/contants";

import { DDIBaseObject, DDIDetailledObject, DDIObjectIDs, DDIObjectLabels } from "@model/ddi";

import { getCategories, getCategory } from "./Category";
import { getCategoryScheme, getCategorySchemes } from "./CategoryScheme";
import { getVariable, getVariables } from "./Variable";

export const getDDIObjects = (xmlDoc: Document): DDIBaseObject[] => [
...getCategorySchemes(xmlDoc),
...getCategories(xmlDoc)
...getCategories(xmlDoc),
...getVariables(xmlDoc)
];

export const getDDIObject = (content: Document, type: DDIObjectIDs, id: string): DDIDetailledObject => {
if (type === CATEGORY_SCHEME_ID) return getCategoryScheme(content, id);
if (type === CATEGORY_ID) return getCategory(content, id);
if (type === VARIABLE_ID) return getVariable(content, id);
throw new Error(`Unknow DDI object type: ${type}`);
};

export const getTitle = (type: DDIObjectIDs): DDIObjectLabels => {
if (type === "category-scheme") return CATEGORY_SCHEME_LABEL;
if (type === "category") return CATEGORY_LABEL;
if (type === CATEGORY_SCHEME_ID) return CATEGORY_SCHEME_LABEL;
if (type === CATEGORY_ID) return CATEGORY_LABEL;
if (type === VARIABLE_ID) return VARIABLE_LABEL;
throw new Error(`Unknow type: ${type}`);
};
2 changes: 1 addition & 1 deletion app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1950,7 +1950,7 @@ [email protected]:
dependencies:
"@remix-run/router" "1.21.0"

react-syntax-highlighter@^15.5.13:
react-syntax-highlighter@^15.6.1:
version "15.6.1"
resolved "https://registry.yarnpkg.com/react-syntax-highlighter/-/react-syntax-highlighter-15.6.1.tgz#fa567cb0a9f96be7bbccf2c13a3c4b5657d9543e"
integrity sha512-OqJ2/vL7lEeV5zTJyG7kmARppUjiB9h9udl4qHQjjgEos66z00Ia0OckwYfRxCSFrW8RJIBnsBwQsHZbVPspqg==
Expand Down

0 comments on commit b53a75c

Please sign in to comment.