Skip to content
This repository was archived by the owner on Mar 2, 2026. It is now read-only.

Commit b53a75c

Browse files
committed
Add variables
1 parent ea57c81 commit b53a75c

10 files changed

Lines changed: 86 additions & 18 deletions

File tree

app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "addict",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"license": "MIT",
55
"type": "module",
66
"scripts": {

app/src/model/ddi.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
import { CATEGORY_SCHEME_ID, CATEGORY_ID, CATEGORY_LABEL, CATEGORY_SCHEME_LABEL } from "@utils/contants";
1+
import {
2+
CATEGORY_SCHEME_ID,
3+
CATEGORY_ID,
4+
CATEGORY_LABEL,
5+
CATEGORY_SCHEME_LABEL,
6+
VARIABLE_ID,
7+
VARIABLE_LABEL
8+
} from "@utils/contants";
29

3-
export type DDIObjectIDs = typeof CATEGORY_SCHEME_ID | typeof CATEGORY_ID;
4-
export type DDIObjectLabels = typeof CATEGORY_SCHEME_LABEL | typeof CATEGORY_LABEL;
10+
export type DDIObjectIDs = typeof CATEGORY_SCHEME_ID | typeof CATEGORY_ID | typeof VARIABLE_ID;
11+
12+
export type DDIObjectLabels =
13+
| typeof CATEGORY_SCHEME_LABEL
14+
| typeof CATEGORY_LABEL
15+
| typeof VARIABLE_LABEL;
516

617
export type DDIBaseObject = {
718
URN: string;

app/src/utils/badges.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { lighten } from "@mui/material/styles";
22

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

5-
import { CATEGORY_SCHEME_ID, CATEGORY_ID, CATEGORY_SCHEME_LABEL, CATEGORY_LABEL } from "./contants";
5+
import {
6+
CATEGORY_SCHEME_ID,
7+
CATEGORY_ID,
8+
CATEGORY_SCHEME_LABEL,
9+
CATEGORY_LABEL,
10+
VARIABLE_ID,
11+
VARIABLE_LABEL
12+
} from "./contants";
613

714
export const getLabelFromId = (id: DDIObjectIDs): DDIObjectLabels => {
815
if (id === CATEGORY_SCHEME_ID) {
@@ -11,13 +18,17 @@ export const getLabelFromId = (id: DDIObjectIDs): DDIObjectLabels => {
1118
if (id === CATEGORY_ID) {
1219
return CATEGORY_LABEL;
1320
}
21+
if (id === VARIABLE_ID) {
22+
return VARIABLE_LABEL;
23+
}
1424
throw new Error(`Unknow DDI object id: ${id}`);
1525
};
1626

1727
export const getBadgeColor =
1828
(baseColor: string) =>
1929
(id: DDIObjectIDs): string => {
2030
if (id === CATEGORY_SCHEME_ID) return lighten(baseColor, 0);
21-
if (id === CATEGORY_ID) return lighten(baseColor, 0.15);
31+
if (id === CATEGORY_ID) return lighten(baseColor, 0.1);
32+
if (id === VARIABLE_ID) return lighten(baseColor, 0.2);
2233
throw new Error(`Unknow id: ${id}`);
2334
};

app/src/utils/contants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import { DDIObjectIDs } from "@model/ddi";
33
/* Route path */
44
export const CATEGORY_SCHEME_ID = "category-scheme";
55
export const CATEGORY_ID = "category";
6+
export const VARIABLE_ID = "variable";
67

7-
export const DDI_OBJECTS = [CATEGORY_SCHEME_ID, CATEGORY_ID] as Array<DDIObjectIDs>;
8+
export const DDI_OBJECTS = [CATEGORY_SCHEME_ID, CATEGORY_ID, VARIABLE_ID] as Array<DDIObjectIDs>;
89

910
/* Supported DDI objects */
1011
export const CATEGORY_SCHEME_LABEL = "Category Scheme";
1112
export const CATEGORY_LABEL = "Category";
13+
export const VARIABLE_LABEL = "Variable";
1214

1315
/* XML */
1416

1517
export const CATEGORY_SCHEME_XML_PATH = "l:CategoryScheme";
1618
export const CATEGORY_XML_PATH = "l:Category";
19+
export const VARIABLE_XML_PATH = "l:Variable";

app/src/utils/xml/Category.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CATEGORY_ID, CATEGORY_XML_PATH, CATEGORY_SCHEME_ID } from "@utils/conta
22

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

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

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

28-
// Find parent
2928
const categoryScheme = category.closest("CategoryScheme") as Element;
3029
const parentURN = getElementURN(categoryScheme);
31-
const parentLabel = getElementLabel(categoryScheme);
32-
// TODO find parent
30+
const parentLabel = getElementContent(categoryScheme);
31+
3332
return {
3433
URN: getElementURN(category),
3534
labels: getLabelsByLang(labels),

app/src/utils/xml/CategoryScheme.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const getCategoryScheme = (xmlDoc: Document, id: string): DDIDetailledObj
2626
if (!categoryScheme) throw new Error(`Unknow Category Scheme: ${id}`);
2727
const labels = categoryScheme.getElementsByTagName("r:Content");
2828
const children = getCategories(categoryScheme);
29-
// TODO getCategories as child
29+
3030
// TODO find parent
3131
return {
3232
URN: getElementURN(categoryScheme),

app/src/utils/xml/Variable.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { VARIABLE_ID, VARIABLE_XML_PATH } from "@utils/contants";
2+
3+
import { DDIBaseObject, DDIDetailledObject } from "@model/ddi";
4+
5+
import { getCode, getElementURN, getLabelsByLang, getPreferedLabel } from "./common";
6+
7+
export const getVariables = (xmlDoc: Document | Element): DDIBaseObject[] => {
8+
const variables = xmlDoc.getElementsByTagName(VARIABLE_XML_PATH);
9+
return Array.from(variables).map(v => {
10+
const labels = v.getElementsByTagName("r:Content");
11+
return {
12+
URN: getElementURN(v),
13+
label: getPreferedLabel(getLabelsByLang(labels)),
14+
type: VARIABLE_ID
15+
};
16+
});
17+
};
18+
19+
export const getVariable = (xmlDoc: Document, id: string): DDIDetailledObject => {
20+
const variables = xmlDoc.getElementsByTagName(VARIABLE_XML_PATH);
21+
const variable = Array.from(variables).find(v => {
22+
const foundId = v.querySelector("ID")?.textContent;
23+
return id === foundId;
24+
});
25+
if (!variable) throw new Error(`Unknow Variable: ${id}`);
26+
const labels = variable.getElementsByTagName("r:Content");
27+
28+
return {
29+
URN: getElementURN(variable),
30+
labels: getLabelsByLang(labels),
31+
code: getCode(variable)
32+
};
33+
};

app/src/utils/xml/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const getElementURN = (e: Element): string => {
1919
return `${agency}:${id}:${version}`;
2020
};
2121

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

2525
export const getCode = (e: Element): string => new XMLSerializer().serializeToString(e);

app/src/utils/xml/index.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1-
import { CATEGORY_SCHEME_ID, CATEGORY_ID, CATEGORY_SCHEME_LABEL, CATEGORY_LABEL } from "@utils/contants";
1+
import {
2+
CATEGORY_SCHEME_ID,
3+
CATEGORY_ID,
4+
CATEGORY_SCHEME_LABEL,
5+
CATEGORY_LABEL,
6+
VARIABLE_ID,
7+
VARIABLE_LABEL
8+
} from "@utils/contants";
29

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

512
import { getCategories, getCategory } from "./Category";
613
import { getCategoryScheme, getCategorySchemes } from "./CategoryScheme";
14+
import { getVariable, getVariables } from "./Variable";
715

816
export const getDDIObjects = (xmlDoc: Document): DDIBaseObject[] => [
917
...getCategorySchemes(xmlDoc),
10-
...getCategories(xmlDoc)
18+
...getCategories(xmlDoc),
19+
...getVariables(xmlDoc)
1120
];
1221

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

1929
export const getTitle = (type: DDIObjectIDs): DDIObjectLabels => {
20-
if (type === "category-scheme") return CATEGORY_SCHEME_LABEL;
21-
if (type === "category") return CATEGORY_LABEL;
30+
if (type === CATEGORY_SCHEME_ID) return CATEGORY_SCHEME_LABEL;
31+
if (type === CATEGORY_ID) return CATEGORY_LABEL;
32+
if (type === VARIABLE_ID) return VARIABLE_LABEL;
2233
throw new Error(`Unknow type: ${type}`);
2334
};

app/yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1950,7 +1950,7 @@ react-router@6.28.0:
19501950
dependencies:
19511951
"@remix-run/router" "1.21.0"
19521952

1953-
react-syntax-highlighter@^15.5.13:
1953+
react-syntax-highlighter@^15.6.1:
19541954
version "15.6.1"
19551955
resolved "https://registry.yarnpkg.com/react-syntax-highlighter/-/react-syntax-highlighter-15.6.1.tgz#fa567cb0a9f96be7bbccf2c13a3c4b5657d9543e"
19561956
integrity sha512-OqJ2/vL7lEeV5zTJyG7kmARppUjiB9h9udl4qHQjjgEos66z00Ia0OckwYfRxCSFrW8RJIBnsBwQsHZbVPspqg==

0 commit comments

Comments
 (0)