@canonical/ds-types 0.15.0 → 0.15.1

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.
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Get the unique identifier for an item (url or key)
3
+ *
4
+ * @throws Error if item has neither url nor key
5
+ */
6
+ export function getItemId(item) {
7
+ if (item.url)
8
+ return item.url;
9
+ if (item.key)
10
+ return item.key;
11
+ throw new Error("Item must have either url or key");
12
+ }
13
+ /**
14
+ * Annotate a navigation tree with parent references and depth
15
+ *
16
+ * Transforms Item tree into _Item tree with parentUrl and depth.
17
+ *
18
+ * @param root - The root item to annotate
19
+ * @param parentUrl - Parent URL (null for root)
20
+ * @param depth - Current depth (0 for root)
21
+ * @returns Annotated item tree
22
+ */
23
+ export function annotateTree(root, parentUrl = null, depth = 0) {
24
+ const { items, ...rest } = root;
25
+ const annotated = {
26
+ ...rest,
27
+ parentUrl,
28
+ depth,
29
+ };
30
+ if (items) {
31
+ annotated.items = items.map((child) => annotateTree(child, getItemId(root), depth + 1));
32
+ }
33
+ return annotated;
34
+ }
35
+ /**
36
+ * Create an index for O(1) lookup of items by URL or key
37
+ *
38
+ * @param root - The annotated root item
39
+ * @returns Index mapping URL/key to item
40
+ */
41
+ export function prepareIndex(root) {
42
+ const index = {};
43
+ const stack = [root];
44
+ while (stack.length > 0) {
45
+ const item = stack.pop();
46
+ if (!item)
47
+ continue;
48
+ const id = getItemId(item);
49
+ index[id] = item;
50
+ if (item.items) {
51
+ for (let i = item.items.length - 1; i >= 0; i--) {
52
+ stack.push(item.items[i]);
53
+ }
54
+ }
55
+ }
56
+ return index;
57
+ }
58
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/navigation/helpers.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,IAAkB;IAC1C,IAAI,IAAI,CAAC,GAAG;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AAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;YAC5B,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type { _Index, _Item, Item } from "./types.js";
2
+ /**
3
+ * Get the unique identifier for an item (url or key)
4
+ *
5
+ * @throws Error if item has neither url nor key
6
+ */
7
+ export declare function getItemId(item: Item | _Item): string;
8
+ /**
9
+ * Annotate a navigation tree with parent references and depth
10
+ *
11
+ * Transforms Item tree into _Item tree with parentUrl and depth.
12
+ *
13
+ * @param root - The root item to annotate
14
+ * @param parentUrl - Parent URL (null for root)
15
+ * @param depth - Current depth (0 for root)
16
+ * @returns Annotated item tree
17
+ */
18
+ export declare function annotateTree(root: Item, parentUrl?: string | null, depth?: number): _Item;
19
+ /**
20
+ * Create an index for O(1) lookup of items by URL or key
21
+ *
22
+ * @param root - The annotated root item
23
+ * @returns Index mapping URL/key to item
24
+ */
25
+ export declare function prepareIndex(root: _Item): _Index;
26
+ //# sourceMappingURL=helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/navigation/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAEtD;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,GAAG,KAAK,GAAG,MAAM,CAIpD;AAED;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,IAAI,EACV,SAAS,GAAE,MAAM,GAAG,IAAW,EAC/B,KAAK,SAAI,GACR,KAAK,CAaP;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,KAAK,GAAG,MAAM,CAehD"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@canonical/ds-types",
3
3
  "description": "Types and related type utilities for Pragma.",
4
- "version": "0.15.0",
4
+ "version": "0.15.1",
5
5
  "type": "module",
6
6
  "main": "dist/esm/index.js",
7
7
  "module": "dist/esm/index.js",
@@ -44,9 +44,9 @@
44
44
  "devDependencies": {
45
45
  "@biomejs/biome": "2.3.14",
46
46
  "@canonical/biome-config": "^0.15.0",
47
- "@canonical/typescript-config": "^0.15.0",
48
- "@canonical/webarchitect": "^0.15.0",
47
+ "@canonical/typescript-config": "^0.15.1",
48
+ "@canonical/webarchitect": "^0.15.1",
49
49
  "typescript": "^5.9.3"
50
50
  },
51
- "gitHead": "60c6f6f355d4a80ce110d8dd44c429695301ad86"
51
+ "gitHead": "d57f21b67013701f6b4f74c5cd595775a17a2259"
52
52
  }