@canonical/design-system 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/.mcp.json +8 -0
  2. package/README.md +438 -0
  3. package/biome.json +6 -0
  4. package/bun.lock +349 -0
  5. package/data/apps/component/file_tree.ttl +8 -0
  6. package/data/apps/component/key_value_pair.ttl +8 -0
  7. package/data/apps/component/navigation.ttl +8 -0
  8. package/data/apps/component/skeleton_loading.ttl +8 -0
  9. package/data/apps/component/status_bar.ttl +8 -0
  10. package/data/apps/component/toolbar.ttl +8 -0
  11. package/data/apps/layout/application_layout.ttl +8 -0
  12. package/data/apps/layout/empty_layout.ttl +8 -0
  13. package/data/apps/layout/list_page_layout.ttl +8 -0
  14. package/data/apps/layout/settings_page_layout.ttl +8 -0
  15. package/data/apps/pattern/code_diff.ttl +8 -0
  16. package/data/apps/pattern/data_table.ttl +8 -0
  17. package/data/apps/pattern/destructive_modal.ttl +8 -0
  18. package/data/apps/pattern/key_value_summary.ttl +8 -0
  19. package/data/apps/pattern/keyboard_shortcuts.ttl +8 -0
  20. package/data/apps/pattern/notification_center.ttl +8 -0
  21. package/data/apps/pattern/side_panel.ttl +8 -0
  22. package/data/apps/pattern/sidepanel_stacked.ttl +8 -0
  23. package/data/apps.ttl +5 -0
  24. package/data/apps_wpe.ttl +5 -0
  25. package/data/documentation/layout/documentation_layout.ttl +8 -0
  26. package/data/documentation.ttl +5 -0
  27. package/data/global/component/accordion.ttl +14 -0
  28. package/data/global/component/announcement.ttl +8 -0
  29. package/data/global/component/badge.ttl +9 -0
  30. package/data/global/component/button.ttl +20 -0
  31. package/data/global/component/card.ttl +8 -0
  32. package/data/global/component/chip.ttl +8 -0
  33. package/data/global/component/code_snippet.ttl +8 -0
  34. package/data/global/component/contextual_menu.ttl +8 -0
  35. package/data/global/component/heading.ttl +14 -0
  36. package/data/global/component/icon.ttl +14 -0
  37. package/data/global/component/icon_in_container.ttl +8 -0
  38. package/data/global/component/keyboard_key.ttl +8 -0
  39. package/data/global/component/label.ttl +15 -0
  40. package/data/global/component/link.ttl +8 -0
  41. package/data/global/component/notification_inline.ttl +8 -0
  42. package/data/global/component/notification_toast.ttl +8 -0
  43. package/data/global/component/pagination.ttl +8 -0
  44. package/data/global/component/popover.ttl +8 -0
  45. package/data/global/component/rich_text_editor.ttl +8 -0
  46. package/data/global/component/search_bar.ttl +8 -0
  47. package/data/global/component/table.ttl +8 -0
  48. package/data/global/component/tabs.ttl +8 -0
  49. package/data/global/component/theme_switcher.ttl +8 -0
  50. package/data/global/component/tile.ttl +14 -0
  51. package/data/global/component/tooltip.ttl +8 -0
  52. package/data/global/group/buttons.ttl +8 -0
  53. package/data/global/group/cards.ttl +8 -0
  54. package/data/global/group/tiles.ttl +8 -0
  55. package/data/global/layout/error_layout.ttl +8 -0
  56. package/data/global/layout/form_layout.ttl +8 -0
  57. package/data/global/layout/identity_page_form.ttl +8 -0
  58. package/data/global/modifier/alpha.ttl +6 -0
  59. package/data/global/modifier/beta.ttl +6 -0
  60. package/data/global/modifier/caution.ttl +6 -0
  61. package/data/global/modifier/comfortable.ttl +6 -0
  62. package/data/global/modifier/completed.ttl +6 -0
  63. package/data/global/modifier/constructive.ttl +6 -0
  64. package/data/global/modifier/dark.ttl +6 -0
  65. package/data/global/modifier/dense.ttl +6 -0
  66. package/data/global/modifier/destructive.ttl +6 -0
  67. package/data/global/modifier/editable.ttl +6 -0
  68. package/data/global/modifier/elevated.ttl +6 -0
  69. package/data/global/modifier/error.ttl +6 -0
  70. package/data/global/modifier/experimental.ttl +6 -0
  71. package/data/global/modifier/failed.ttl +6 -0
  72. package/data/global/modifier/focused.ttl +6 -0
  73. package/data/global/modifier/highlighted.ttl +6 -0
  74. package/data/global/modifier/in_progress.ttl +6 -0
  75. package/data/global/modifier/information.ttl +6 -0
  76. package/data/global/modifier/light.ttl +6 -0
  77. package/data/global/modifier/loading.ttl +6 -0
  78. package/data/global/modifier/muted.ttl +6 -0
  79. package/data/global/modifier/paper.ttl +6 -0
  80. package/data/global/modifier/planned.ttl +6 -0
  81. package/data/global/modifier/primary.ttl +6 -0
  82. package/data/global/modifier/secondary.ttl +6 -0
  83. package/data/global/modifier/stable.ttl +6 -0
  84. package/data/global/modifier/success.ttl +6 -0
  85. package/data/global/modifier/tertiary.ttl +6 -0
  86. package/data/global/modifier/warning.ttl +6 -0
  87. package/data/global/modifier/wireframe.ttl +6 -0
  88. package/data/global/modifier_family/_color_luminosity.ttl +7 -0
  89. package/data/global/modifier_family/_emphasis.ttl +7 -0
  90. package/data/global/modifier_family/_other.ttl +7 -0
  91. package/data/global/modifier_family/anticipation.ttl +7 -0
  92. package/data/global/modifier_family/criticality.ttl +7 -0
  93. package/data/global/modifier_family/density.ttl +7 -0
  94. package/data/global/modifier_family/importance.ttl +7 -0
  95. package/data/global/modifier_family/lifecycle.ttl +7 -0
  96. package/data/global/modifier_family/release.ttl +7 -0
  97. package/data/global/pattern/breadcrumbs.ttl +14 -0
  98. package/data/global/pattern/info_table.ttl +8 -0
  99. package/data/global/pattern/modal.ttl +8 -0
  100. package/data/global/pattern/navigation.ttl +8 -0
  101. package/data/global/pattern/select_button.ttl +8 -0
  102. package/data/global/pattern/stepper.ttl +8 -0
  103. package/data/global/pattern/table_of_contents.ttl +8 -0
  104. package/data/global/pattern/timeline.ttl +14 -0
  105. package/data/global/subcomponent/accordion-item.ttl +19 -0
  106. package/data/global/subcomponent/breadcrumbs-item.ttl +19 -0
  107. package/data/global/subcomponent/card-content.ttl +14 -0
  108. package/data/global/subcomponent/card-header.ttl +14 -0
  109. package/data/global/subcomponent/card-image.ttl +20 -0
  110. package/data/global/subcomponent/divider.ttl +8 -0
  111. package/data/global/subcomponent/modal-content.ttl +8 -0
  112. package/data/global/subcomponent/modal-footer.ttl +8 -0
  113. package/data/global/subcomponent/modal-header.ttl +8 -0
  114. package/data/global/subcomponent/spinner.ttl +8 -0
  115. package/data/global/subcomponent/table-cell.ttl +8 -0
  116. package/data/global/subcomponent/table-row.ttl +8 -0
  117. package/data/global/subcomponent/tile-content.ttl +14 -0
  118. package/data/global/subcomponent/tile-header.ttl +14 -0
  119. package/data/global/subcomponent/timeline-content.ttl +14 -0
  120. package/data/global/subcomponent/timeline-event.ttl +31 -0
  121. package/data/global/subcomponent/timeline-footer.ttl +8 -0
  122. package/data/global/subcomponent/timeline-header.ttl +8 -0
  123. package/data/global.ttl +5 -0
  124. package/data/global_form/component/checkbox_input.ttl +8 -0
  125. package/data/global_form/component/choices_input.ttl +8 -0
  126. package/data/global_form/component/combobox_input.ttl +8 -0
  127. package/data/global_form/component/custom_choices_input.ttl +8 -0
  128. package/data/global_form/component/datepicker_input.ttl +8 -0
  129. package/data/global_form/component/rating_input.ttl +8 -0
  130. package/data/global_form/component/segmented_control_input.ttl +8 -0
  131. package/data/global_form/component/slider_input.ttl +8 -0
  132. package/data/global_form/component/switch_input.ttl +8 -0
  133. package/data/global_form/component/text_input.ttl +8 -0
  134. package/data/global_form/component/textarea_input.ttl +8 -0
  135. package/data/global_form/component/wysiwginput.ttl +8 -0
  136. package/data/global_form/pattern/field.ttl +8 -0
  137. package/data/global_form/pattern/form.ttl +8 -0
  138. package/data/global_form/subcomponent/field-description.ttl +8 -0
  139. package/data/global_form/subcomponent/field-label.ttl +8 -0
  140. package/data/global_form.ttl +5 -0
  141. package/data/site/component/quote.ttl +8 -0
  142. package/data/site/component/rule.ttl +8 -0
  143. package/data/site/component/section.ttl +8 -0
  144. package/data/site/layout/site_layout.ttl +8 -0
  145. package/data/site/pattern/hero-github.ttl +8 -0
  146. package/data/site/pattern/hero.ttl +8 -0
  147. package/data/site/pattern/info_table.ttl +8 -0
  148. package/data/site/pattern/skip_link.ttl +8 -0
  149. package/data/site.ttl +5 -0
  150. package/data/stores.ttl +5 -0
  151. package/definitions/ontology.ttl +498 -0
  152. package/package.json +42 -0
  153. package/session/A.DSL.md +429 -0
  154. package/session/B.NOTES.md +84 -0
  155. package/session/global.component.accordion.yaml +13 -0
  156. package/session/global.component.button.yaml +26 -0
  157. package/session/global.component.card.yaml +24 -0
  158. package/session/global.component.icon.yaml +7 -0
  159. package/session/global.component.label.yaml +7 -0
  160. package/session/global.component.tile.yaml +21 -0
  161. package/session/global.component.timeline.yaml +20 -0
  162. package/session/global.pattern.breadcrumbs.yaml +13 -0
  163. package/session/global.subcomponent.accordion-item.yaml +37 -0
  164. package/session/global.subcomponent.breadcrumbs-item.yaml +21 -0
  165. package/session/global.subcomponent.card-content.yaml +7 -0
  166. package/session/global.subcomponent.card-header.yaml +22 -0
  167. package/session/global.subcomponent.card-image.yaml +7 -0
  168. package/session/global.subcomponent.tile-content.yaml +7 -0
  169. package/session/global.subcomponent.tile-header.yaml +8 -0
  170. package/session/global.subcomponent.timeline-content.yaml +13 -0
  171. package/session/global.subcomponent.timeline-event.yaml +20 -0
  172. package/session/global.subcomponent.timeline-footer.yaml +9 -0
  173. package/session/global.subcomponent.timeline-header.yaml +18 -0
  174. package/skills/anatomy-author/ANATOMY_DSL_SPEC.md +423 -0
  175. package/skills/anatomy-author/SKILL.md +911 -0
  176. package/skills/component-specifier/SKILL.md +413 -0
  177. package/skills/design-auditor/SKILL.md +428 -0
  178. package/source.json +101 -0
  179. package/src/cli.ts +45 -0
  180. package/src/collect/generateTurtle.tests.ts +200 -0
  181. package/src/collect/generateTurtle.ts +187 -0
  182. package/src/collect/index.ts +4 -0
  183. package/src/collect/scanAnnotations.tests.ts +138 -0
  184. package/src/collect/scanAnnotations.ts +78 -0
  185. package/src/collect/types.ts +57 -0
  186. package/src/collect/validateCollectConfig.tests.ts +116 -0
  187. package/src/collect/validateCollectConfig.ts +74 -0
  188. package/src/collect-implementations.ts +99 -0
  189. package/src/commands/extract.tests.ts +41 -0
  190. package/src/commands/extract.ts +48 -0
  191. package/src/commands/index.ts +3 -0
  192. package/src/commands/list.tests.ts +26 -0
  193. package/src/commands/list.ts +26 -0
  194. package/src/commands/transform.tests.ts +28 -0
  195. package/src/commands/transform.ts +133 -0
  196. package/src/config/index.ts +2 -0
  197. package/src/config/source.schema.json +110 -0
  198. package/src/config/types.ts +97 -0
  199. package/src/config/validateConfig.tests.ts +46 -0
  200. package/src/config/validateConfig.ts +21 -0
  201. package/src/constants.ts +20 -0
  202. package/src/graph/GraphStore.tests.ts +157 -0
  203. package/src/graph/GraphStore.ts +200 -0
  204. package/src/graph/PrefixMap.tests.ts +129 -0
  205. package/src/graph/PrefixMap.ts +95 -0
  206. package/src/graph/buildUri.tests.ts +158 -0
  207. package/src/graph/buildUri.ts +69 -0
  208. package/src/graph/index.ts +4 -0
  209. package/src/graph/types.ts +20 -0
  210. package/src/providers/CodaProvider.tests.ts +241 -0
  211. package/src/providers/CodaProvider.ts +174 -0
  212. package/src/providers/CodaProvider.types.generated.ts +12236 -0
  213. package/src/providers/index.ts +18 -0
  214. package/src/serializers/JSONSerializer.tests.ts +79 -0
  215. package/src/serializers/JSONSerializer.ts +87 -0
  216. package/src/serializers/TTLSerializer.tests.ts +75 -0
  217. package/src/serializers/TTLSerializer.ts +99 -0
  218. package/src/serializers/index.ts +3 -0
  219. package/src/serializers/types.ts +16 -0
  220. package/src/transform/resolveReferences.tests.ts +63 -0
  221. package/src/transform/resolveReferences.ts +36 -0
  222. package/src/transform/resolveValue.tests.ts +76 -0
  223. package/src/transform/resolveValue.ts +87 -0
  224. package/src/transform/transform.tests.ts +99 -0
  225. package/src/transform/transform.ts +109 -0
  226. package/src/transform/transformRow.tests.ts +269 -0
  227. package/src/transform/transformRow.ts +220 -0
  228. package/src/transform/types.ts +26 -0
  229. package/tsconfig.json +8 -0
  230. package/vite.config.ts +11 -0
  231. package/vitest.config.ts +21 -0
@@ -0,0 +1,129 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { NAMESPACES } from "../constants.js";
3
+ import PrefixMap from "./PrefixMap.js";
4
+
5
+ describe("PrefixMap", () => {
6
+ describe("add and get", () => {
7
+ it("should store and retrieve a prefix", () => {
8
+ const pm = new PrefixMap();
9
+ pm.add("dso", NAMESPACES.dso);
10
+
11
+ expect(pm.get("dso")).toBe(NAMESPACES.dso);
12
+ });
13
+
14
+ it("should return undefined for unknown prefix", () => {
15
+ const pm = new PrefixMap();
16
+
17
+ expect(pm.get("unknown")).toBeUndefined();
18
+ });
19
+ });
20
+
21
+ describe("expand", () => {
22
+ it("should expand prefixed URI to full URI", () => {
23
+ const pm = new PrefixMap();
24
+ pm.add("dso", NAMESPACES.dso);
25
+
26
+ expect(pm.expand("dso:name")).toBe(`${NAMESPACES.dso}name`);
27
+ });
28
+
29
+ it("should return original if no colon", () => {
30
+ const pm = new PrefixMap();
31
+
32
+ expect(pm.expand("name")).toBe("name");
33
+ });
34
+
35
+ it("should return original if prefix not found", () => {
36
+ const pm = new PrefixMap();
37
+
38
+ expect(pm.expand("unknown:name")).toBe("unknown:name");
39
+ });
40
+
41
+ it("should preserve slashes in local name", () => {
42
+ const pm = new PrefixMap();
43
+ pm.add("ds", NAMESPACES.ds);
44
+
45
+ expect(pm.expand("ds:global/component/button")).toBe(
46
+ `${NAMESPACES.ds}global/component/button`,
47
+ );
48
+ });
49
+ });
50
+
51
+ describe("compact", () => {
52
+ it("should compact full URI to prefixed form", () => {
53
+ const pm = new PrefixMap();
54
+ pm.add("dso", NAMESPACES.dso);
55
+
56
+ expect(pm.compact(`${NAMESPACES.dso}name`)).toBe("dso:name");
57
+ });
58
+
59
+ it("should return original if no matching prefix", () => {
60
+ const pm = new PrefixMap();
61
+
62
+ expect(pm.compact("https://example.com/name")).toBe("https://example.com/name");
63
+ });
64
+
65
+ it("should use longest matching prefix", () => {
66
+ const pm = new PrefixMap();
67
+ pm.add("ex", "https://example.com/");
68
+ pm.add("exo", "https://example.com/ontology#");
69
+
70
+ expect(pm.compact("https://example.com/ontology#name")).toBe("exo:name");
71
+ });
72
+ });
73
+
74
+ describe("fromContext", () => {
75
+ it("should extract prefixes from JSON-LD context", () => {
76
+ const context = {
77
+ dso: NAMESPACES.dso,
78
+ ds: NAMESPACES.ds,
79
+ name: "dso:name",
80
+ };
81
+
82
+ const pm = PrefixMap.fromContext(context);
83
+
84
+ expect(pm.get("dso")).toBe(NAMESPACES.dso);
85
+ expect(pm.get("ds")).toBe(NAMESPACES.ds);
86
+ expect(pm.get("name")).toBeUndefined(); // Not a namespace
87
+ });
88
+
89
+ it("should ignore non-URI values", () => {
90
+ const context = {
91
+ dso: NAMESPACES.dso,
92
+ name: { "@id": "dso:name" },
93
+ };
94
+
95
+ const pm = PrefixMap.fromContext(context);
96
+
97
+ expect(pm.get("dso")).toBe(NAMESPACES.dso);
98
+ expect(pm.get("name")).toBeUndefined();
99
+ });
100
+ });
101
+
102
+ describe("entries", () => {
103
+ it("should iterate over all prefixes", () => {
104
+ const pm = new PrefixMap();
105
+ pm.add("dso", NAMESPACES.dso);
106
+ pm.add("ds", NAMESPACES.ds);
107
+
108
+ const entries = Array.from(pm.entries());
109
+
110
+ expect(entries).toContainEqual(["dso", NAMESPACES.dso]);
111
+ expect(entries).toContainEqual(["ds", NAMESPACES.ds]);
112
+ });
113
+ });
114
+
115
+ describe("toRecord", () => {
116
+ it("should convert to plain object for n3", () => {
117
+ const pm = new PrefixMap();
118
+ pm.add("dso", NAMESPACES.dso);
119
+ pm.add("ds", NAMESPACES.ds);
120
+
121
+ const record = pm.toRecord();
122
+
123
+ expect(record).toEqual({
124
+ dso: NAMESPACES.dso,
125
+ ds: NAMESPACES.ds,
126
+ });
127
+ });
128
+ });
129
+ });
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Manages RDF namespace prefixes for URI expansion and compaction
3
+ */
4
+ export default class PrefixMap {
5
+ private prefixes: Map<string, string> = new Map();
6
+
7
+ /**
8
+ * Create a PrefixMap from a JSON-LD context object
9
+ * Extracts only namespace URI mappings (strings starting with http)
10
+ */
11
+ static fromContext(context: Record<string, unknown>): PrefixMap {
12
+ const pm = new PrefixMap();
13
+ for (const [key, value] of Object.entries(context)) {
14
+ if (typeof value === "string" && value.startsWith("http")) {
15
+ pm.add(key, value);
16
+ }
17
+ }
18
+ return pm;
19
+ }
20
+
21
+ /**
22
+ * Add a prefix-namespace mapping
23
+ */
24
+ add(prefix: string, namespace: string): void {
25
+ this.prefixes.set(prefix, namespace);
26
+ }
27
+
28
+ /**
29
+ * Get the namespace for a prefix
30
+ */
31
+ get(prefix: string): string | undefined {
32
+ return this.prefixes.get(prefix);
33
+ }
34
+
35
+ /**
36
+ * Expand a prefixed URI (e.g., "dso:name") to full URI
37
+ */
38
+ expand(prefixedUri: string): string {
39
+ const colonIndex = prefixedUri.indexOf(":");
40
+ if (colonIndex === -1) {
41
+ return prefixedUri;
42
+ }
43
+
44
+ const prefix = prefixedUri.slice(0, colonIndex);
45
+ const localName = prefixedUri.slice(colonIndex + 1);
46
+ const namespace = this.prefixes.get(prefix);
47
+
48
+ if (!namespace) {
49
+ return prefixedUri;
50
+ }
51
+
52
+ return namespace + localName;
53
+ }
54
+
55
+ /**
56
+ * Compact a full URI to prefixed form if a matching prefix exists
57
+ * Uses the longest matching namespace
58
+ */
59
+ compact(fullUri: string): string {
60
+ let bestMatch: { prefix: string; namespace: string } | null = null;
61
+
62
+ for (const [prefix, namespace] of this.prefixes) {
63
+ if (fullUri.startsWith(namespace)) {
64
+ if (!bestMatch || namespace.length > bestMatch.namespace.length) {
65
+ bestMatch = { prefix, namespace };
66
+ }
67
+ }
68
+ }
69
+
70
+ if (!bestMatch) {
71
+ return fullUri;
72
+ }
73
+
74
+ const localName = fullUri.slice(bestMatch.namespace.length);
75
+ return `${bestMatch.prefix}:${localName}`;
76
+ }
77
+
78
+ /**
79
+ * Iterate over all prefix-namespace pairs
80
+ */
81
+ entries(): IterableIterator<[string, string]> {
82
+ return this.prefixes.entries();
83
+ }
84
+
85
+ /**
86
+ * Convert to plain object (for n3 Writer)
87
+ */
88
+ toRecord(): Record<string, string> {
89
+ const record: Record<string, string> = {};
90
+ for (const [prefix, namespace] of this.prefixes) {
91
+ record[prefix] = namespace;
92
+ }
93
+ return record;
94
+ }
95
+ }
@@ -0,0 +1,158 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import buildUri from "./buildUri.js";
3
+
4
+ describe("buildUri", () => {
5
+ describe("template interpolation", () => {
6
+ it("should interpolate simple template values", () => {
7
+ const template = "ds:component:{name}:{version}";
8
+ const row = { name: "button", version: "1.0.0" };
9
+
10
+ const uri = buildUri(template, row);
11
+
12
+ expect(uri).toBe("ds:component:button:1.0.0");
13
+ });
14
+
15
+ it("should handle multiple placeholders", () => {
16
+ const template = "ds:{tier}:{type}:{name}";
17
+ const row = { tier: "global", type: "component", name: "button" };
18
+
19
+ const uri = buildUri(template, row);
20
+
21
+ expect(uri).toBe("ds:global:component:button");
22
+ });
23
+ });
24
+
25
+ describe("URI-safe conversion", () => {
26
+ it("should convert to lowercase", () => {
27
+ const template = "ds:{name}";
28
+ const row = { name: "Button" };
29
+
30
+ const uri = buildUri(template, row);
31
+
32
+ expect(uri).toBe("ds:button");
33
+ });
34
+
35
+ it("should replace spaces with hyphens", () => {
36
+ const template = "ds:{name}";
37
+ const row = { name: "My Button" };
38
+
39
+ const uri = buildUri(template, row);
40
+
41
+ expect(uri).toBe("ds:my-button");
42
+ });
43
+
44
+ it("should preserve dots for versions", () => {
45
+ const template = "ds:{version}";
46
+ const row = { version: "1.0.0" };
47
+
48
+ const uri = buildUri(template, row);
49
+
50
+ expect(uri).toBe("ds:1.0.0");
51
+ });
52
+
53
+ it("should remove special characters", () => {
54
+ const template = "ds:{name}";
55
+ const row = { name: "Button (Primary)" };
56
+
57
+ const uri = buildUri(template, row);
58
+
59
+ expect(uri).toBe("ds:button-primary");
60
+ });
61
+ });
62
+
63
+ describe("single column template", () => {
64
+ it("should return raw value for single column reference", () => {
65
+ const template = "{uri}";
66
+ const row = { uri: "ds:global:component:button" };
67
+
68
+ const uri = buildUri(template, row);
69
+
70
+ expect(uri).toBe("ds:global:component:button");
71
+ });
72
+
73
+ it("should not transform single column value", () => {
74
+ const template = "{uri}";
75
+ const row = { uri: "ds:Global:Component:Button" };
76
+
77
+ const uri = buildUri(template, row);
78
+
79
+ expect(uri).toBe("ds:Global:Component:Button");
80
+ });
81
+ });
82
+
83
+ describe("missing values", () => {
84
+ it("should handle missing template values gracefully", () => {
85
+ const template = "ds:component:{name}:{version}";
86
+ const row = { name: "button" };
87
+
88
+ const uri = buildUri(template, row);
89
+
90
+ expect(uri).toBe("ds:component:button:");
91
+ });
92
+
93
+ it("should return empty string for null value in single column", () => {
94
+ const template = "{uri}";
95
+ const row = { uri: null };
96
+
97
+ const uri = buildUri(template, row);
98
+
99
+ expect(uri).toBe("");
100
+ });
101
+
102
+ it("should return empty string for undefined value in single column", () => {
103
+ const template = "{uri}";
104
+ const row = {};
105
+
106
+ const uri = buildUri(template, row);
107
+
108
+ expect(uri).toBe("");
109
+ });
110
+ });
111
+
112
+ describe("ontology namespace templates", () => {
113
+ it("should preserve case for dso: prefix", () => {
114
+ const template = "dso:{Name}";
115
+ const row = { Name: "Component" };
116
+
117
+ const uri = buildUri(template, row);
118
+
119
+ expect(uri).toBe("dso:Component");
120
+ });
121
+
122
+ it("should preserve case for rdf: prefix", () => {
123
+ const template = "rdf:{Type}";
124
+ const row = { Type: "Property" };
125
+
126
+ const uri = buildUri(template, row);
127
+
128
+ expect(uri).toBe("rdf:Property");
129
+ });
130
+
131
+ it("should preserve case for rdfs: prefix", () => {
132
+ const template = "rdfs:{Class}";
133
+ const row = { Class: "Resource" };
134
+
135
+ const uri = buildUri(template, row);
136
+
137
+ expect(uri).toBe("rdfs:Resource");
138
+ });
139
+
140
+ it("should still replace spaces with hyphens in ontology URIs", () => {
141
+ const template = "dso:{Name}";
142
+ const row = { Name: "My Component" };
143
+
144
+ const uri = buildUri(template, row);
145
+
146
+ expect(uri).toBe("dso:My-Component");
147
+ });
148
+
149
+ it("should remove special characters in ontology URIs", () => {
150
+ const template = "dso:{Name}";
151
+ const row = { Name: "Component (Type)" };
152
+
153
+ const uri = buildUri(template, row);
154
+
155
+ expect(uri).toBe("dso:Component-Type");
156
+ });
157
+ });
158
+ });
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Convert a string to URI-safe format
3
+ * - Converts to lowercase
4
+ * - Replaces spaces with hyphens
5
+ * - Keeps dots (for versions like 1.0.0)
6
+ * - Removes other special characters
7
+ */
8
+ function toUriSafe(str: string, lowercase = true): string {
9
+ let result = str.replace(/\s+/g, "-");
10
+ if (lowercase) {
11
+ result = result.toLowerCase();
12
+ result = result.replace(/[^a-z0-9.\-]/g, "");
13
+ } else {
14
+ result = result.replace(/[^a-zA-Z0-9.\-]/g, "");
15
+ }
16
+ return result;
17
+ }
18
+
19
+ /**
20
+ * Check if a template is for an ontology namespace URI
21
+ * Ontology URIs should preserve the case of class/property names
22
+ * e.g., "dso:{Name}" should produce "dso:Component", not "dso:component"
23
+ */
24
+ function isOntologyTemplate(template: string): boolean {
25
+ // Common ontology namespace prefixes
26
+ const ontologyPrefixes = ["dso:", "rdf:", "rdfs:", "owl:", "xsd:", "skos:"];
27
+ return ontologyPrefixes.some((prefix) => template.startsWith(prefix));
28
+ }
29
+
30
+ /**
31
+ * Build a URI from a template and row data
32
+ *
33
+ * Template uses column names in braces: {name}, {version}
34
+ *
35
+ * If template is just a single column reference like "{uri}",
36
+ * returns the raw value without transformation.
37
+ *
38
+ * For ontology templates (dso:, rdf:, etc.), preserves case of values.
39
+ *
40
+ * Otherwise, interpolates all placeholders and converts values
41
+ * to URI-safe format (lowercase, hyphens for spaces).
42
+ */
43
+ export default function buildUri(
44
+ template: string,
45
+ row: Record<string, unknown>,
46
+ ): string {
47
+ // If template is just a single column reference like "{uri}", return raw value
48
+ const singleColumnMatch = template.match(/^\{(\w+)\}$/);
49
+ if (singleColumnMatch) {
50
+ const value = row[singleColumnMatch[1]];
51
+ if (value === undefined || value === null) {
52
+ return "";
53
+ }
54
+ return String(value);
55
+ }
56
+
57
+ // Determine if we should preserve case (ontology URIs)
58
+ const preserveCase = isOntologyTemplate(template);
59
+
60
+ // Otherwise, interpolate and transform
61
+ return template.replace(/\{(\w+)\}/g, (_, key) => {
62
+ const value = row[key];
63
+ if (value === undefined || value === null) {
64
+ return "";
65
+ }
66
+ const strValue = String(value);
67
+ return toUriSafe(strValue, !preserveCase);
68
+ });
69
+ }
@@ -0,0 +1,4 @@
1
+ export { default as GraphStore } from "./GraphStore.js";
2
+ export { default as PrefixMap } from "./PrefixMap.js";
3
+ export { default as buildUri } from "./buildUri.js";
4
+ export type { Quad, Literal, Term } from "./types.js";
@@ -0,0 +1,20 @@
1
+ import type { Quad as N3Quad } from "n3";
2
+
3
+ /**
4
+ * Re-export n3 Quad type for use across the codebase
5
+ */
6
+ export type Quad = N3Quad;
7
+
8
+ /**
9
+ * Literal value with optional language or datatype
10
+ */
11
+ export interface Literal {
12
+ value: string;
13
+ language?: string;
14
+ datatype?: string;
15
+ }
16
+
17
+ /**
18
+ * Term can be a URI string or a Literal
19
+ */
20
+ export type Term = string | Literal;