@canonical/ds-types
Advanced tools
| /** | ||
| * Get the unique identifier for an item (url or key) | ||
| * | ||
| * @throws Error if item has neither url nor key | ||
| */ | ||
| export function getItemId(item) { | ||
| if (item.url) | ||
| return item.url; | ||
| if (item.key) | ||
| return item.key; | ||
| throw new Error("Item must have either url or key"); | ||
| } | ||
| /** | ||
| * Annotate a navigation tree with parent references and depth | ||
| * | ||
| * Transforms Item tree into _Item tree with parentUrl and depth. | ||
| * | ||
| * @param root - The root item to annotate | ||
| * @param parentUrl - Parent URL (null for root) | ||
| * @param depth - Current depth (0 for root) | ||
| * @returns Annotated item tree | ||
| */ | ||
| export function annotateTree(root, parentUrl = null, depth = 0) { | ||
| const { items, ...rest } = root; | ||
| const annotated = { | ||
| ...rest, | ||
| parentUrl, | ||
| depth, | ||
| }; | ||
| if (items) { | ||
| annotated.items = items.map((child) => annotateTree(child, getItemId(root), depth + 1)); | ||
| } | ||
| return annotated; | ||
| } | ||
| /** | ||
| * Create an index for O(1) lookup of items by URL or key | ||
| * | ||
| * @param root - The annotated root item | ||
| * @returns Index mapping URL/key to item | ||
| */ | ||
| export function prepareIndex(root) { | ||
| const index = {}; | ||
| const stack = [root]; | ||
| while (stack.length > 0) { | ||
| const item = stack.pop(); | ||
| if (!item) | ||
| continue; | ||
| const id = getItemId(item); | ||
| index[id] = item; | ||
| if (item.items) { | ||
| for (let i = item.items.length - 1; i >= 0; i--) { | ||
| stack.push(item.items[i]); | ||
| } | ||
| } | ||
| } | ||
| return index; | ||
| } | ||
| //# sourceMappingURL=helpers.js.map |
| {"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;QAAE,OAAO,IAAI,CAAC,GAAG,CAAC;IAC9B,IAAI,IAAI,CAAC,GAAG;QAAE,OAAO,IAAI,CAAC,GAAG,CAAC;IAC9B,MAAM,IAAI,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|
| import type { _Index, _Item, Item } from "./types.js"; | ||
| /** | ||
| * Get the unique identifier for an item (url or key) | ||
| * | ||
| * @throws Error if item has neither url nor key | ||
| */ | ||
| export declare function getItemId(item: Item | _Item): string; | ||
| /** | ||
| * Annotate a navigation tree with parent references and depth | ||
| * | ||
| * Transforms Item tree into _Item tree with parentUrl and depth. | ||
| * | ||
| * @param root - The root item to annotate | ||
| * @param parentUrl - Parent URL (null for root) | ||
| * @param depth - Current depth (0 for root) | ||
| * @returns Annotated item tree | ||
| */ | ||
| export declare function annotateTree(root: Item, parentUrl?: string | null, depth?: number): _Item; | ||
| /** | ||
| * Create an index for O(1) lookup of items by URL or key | ||
| * | ||
| * @param root - The annotated root item | ||
| * @returns Index mapping URL/key to item | ||
| */ | ||
| export declare function prepareIndex(root: _Item): _Index; | ||
| //# sourceMappingURL=helpers.d.ts.map |
| {"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"} |
+4
-4
| { | ||
| "name": "@canonical/ds-types", | ||
| "description": "Types and related type utilities for Pragma.", | ||
| "version": "0.15.0", | ||
| "version": "0.15.1", | ||
| "type": "module", | ||
@@ -47,7 +47,7 @@ "main": "dist/esm/index.js", | ||
| "@canonical/biome-config": "^0.15.0", | ||
| "@canonical/typescript-config": "^0.15.0", | ||
| "@canonical/webarchitect": "^0.15.0", | ||
| "@canonical/typescript-config": "^0.15.1", | ||
| "@canonical/webarchitect": "^0.15.1", | ||
| "typescript": "^5.9.3" | ||
| }, | ||
| "gitHead": "60c6f6f355d4a80ce110d8dd44c429695301ad86" | ||
| "gitHead": "d57f21b67013701f6b4f74c5cd595775a17a2259" | ||
| } |
25319
20.15%31
14.81%226
56.94%