@canonical/utils 0.11.0 → 0.12.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.
package/README.md CHANGED
@@ -1,7 +1,100 @@
1
- ## Canonical Web Team Utils
1
+ # @canonical/utils
2
2
 
3
- This package contains a collection of utilities that are used across the Canonical Web Team's projects.
3
+ Utility functions for the Pragma design system. This package contains battle-tested helpers that have proven useful across multiple packages.
4
4
 
5
- ### Utils list
6
- - [debounce](./src/debounce/README.md)
7
- - [throttle](./src/throttle/README.md)
5
+ ## Installation
6
+
7
+ ```bash
8
+ bun add @canonical/utils
9
+ ```
10
+
11
+ ## Available Functions
12
+
13
+ ### debounce
14
+
15
+ Creates a debounced version of a function that waits until a specified delay has passed since the last call before executing. Useful for search inputs, resize handlers, and other high-frequency events.
16
+
17
+ ```typescript
18
+ import { debounce } from "@canonical/utils";
19
+
20
+ const debouncedSearch = debounce(async (query: string) => {
21
+ const response = await fetch(`/api/search?q=${query}`);
22
+ return response.json();
23
+ }, 300);
24
+
25
+ // Multiple rapid calls result in only one execution
26
+ await debouncedSearch("hello"); // Cancelled by next call
27
+ await debouncedSearch("hello w"); // Cancelled by next call
28
+ await debouncedSearch("hello world"); // Executes after 300ms
29
+
30
+ // Cancel a pending execution
31
+ const promise = debouncedSearch("query");
32
+ promise.cancel();
33
+ ```
34
+
35
+ The debounced function returns a promise and includes a `cancel` method. Each new call cancels any pending execution from previous calls.
36
+
37
+ ### throttle
38
+
39
+ Throttles a function to execute at most once per specified interval. Useful for scroll handlers, continuous input events, and rate-limited operations.
40
+
41
+ ```typescript
42
+ import { throttle } from "@canonical/utils";
43
+
44
+ const throttledResize = throttle(() => {
45
+ console.log("Window resized");
46
+ }, 500);
47
+
48
+ window.addEventListener("resize", throttledResize);
49
+ // Logs at most once every 500ms during continuous resizing
50
+ ```
51
+
52
+ ### humanizeNumber
53
+
54
+ Formats numbers for human readability with appropriate suffixes and precision.
55
+
56
+ ```typescript
57
+ import { humanizeNumber } from "@canonical/utils";
58
+
59
+ humanizeNumber(1234); // "1.2K"
60
+ humanizeNumber(1234567); // "1.2M"
61
+ ```
62
+
63
+ ### pluralize
64
+
65
+ Returns the singular or plural form of a word based on a count.
66
+
67
+ ```typescript
68
+ import { pluralize } from "@canonical/utils";
69
+
70
+ pluralize(1, "item", "items"); // "item"
71
+ pluralize(5, "item", "items"); // "items"
72
+ ```
73
+
74
+ ### casing
75
+
76
+ Converts strings between different case conventions.
77
+
78
+ ```typescript
79
+ import { casing } from "@canonical/utils";
80
+
81
+ casing.toCamelCase("hello-world"); // "helloWorld"
82
+ casing.toKebabCase("helloWorld"); // "hello-world"
83
+ ```
84
+
85
+ ### invariant
86
+
87
+ Throws an error if a condition is false. Useful for asserting assumptions in code.
88
+
89
+ ```typescript
90
+ import { invariant } from "@canonical/utils";
91
+
92
+ invariant(user != null, "User must be defined");
93
+ // TypeScript now knows user is not null
94
+ ```
95
+
96
+ ## Design Philosophy
97
+
98
+ Functions only enter this package after proving useful across multiple packages. Premature abstraction is actively avoided. If a utility is only needed in one place, it belongs in that package until a second use case emerges.
99
+
100
+ Each function is fully typed with comprehensive JSDoc comments. The package has no runtime dependencies, keeping bundle impact minimal.
package/dist/esm/index.js CHANGED
@@ -2,6 +2,7 @@ export { default as casing } from "./casing.js";
2
2
  export { default as debounce } from "./debounce.js";
3
3
  export { default as humanizeNumber } from "./humanizeNumber/index.js";
4
4
  export { default as invariant } from "./invariant.js";
5
+ export * from "./navigation/index.js";
5
6
  export { default as pluralize } from "./pluralize/index.js";
6
7
  export { default as throttle } from "./throttle.js";
7
8
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGpD,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEtD,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGpD,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEtD,cAAc,uBAAuB,CAAC;AAGtC,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { getItemId } from "./getItemId.js";
2
+ /**
3
+ * Annotate a navigation tree with parent references and depth
4
+ *
5
+ * Transforms Item tree into _Item tree with parentUrl and depth.
6
+ *
7
+ * @param root - The root item to annotate
8
+ * @param parentUrl - Parent URL (null for root)
9
+ * @param depth - Current depth (0 for root)
10
+ * @returns Annotated item tree
11
+ */
12
+ export function annotateTree(root, parentUrl = null, depth = 0) {
13
+ const { items, ...rest } = root;
14
+ const annotated = {
15
+ ...rest,
16
+ parentUrl,
17
+ depth,
18
+ };
19
+ if (items) {
20
+ annotated.items = items.map((child) => annotateTree(child, getItemId(root), depth + 1));
21
+ }
22
+ return annotated;
23
+ }
24
+ //# sourceMappingURL=annotateTree.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"annotateTree.js","sourceRoot":"","sources":["../../../src/navigation/annotateTree.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;;;;;;;;GASG;AACH,MAAM,UAAU,YAAY,CAC1B,IAAU,EACV,YAA2B,IAAI,EAC/B,KAAK,GAAG,CAAC;IAET,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,EAAE,GAAG,IAAI,CAAC;IAChC,MAAM,SAAS,GAAU;QACvB,GAAG,IAAI;QACP,SAAS;QACT,KAAK;KACN,CAAC;IACF,IAAI,KAAK,EAAE,CAAC;QACV,SAAS,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACpC,YAAY,CAAC,KAAK,EAAE,SAAS,CAAC,IAAI,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,CAChD,CAAC;IACJ,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Get the unique identifier for a navigation item (url or key)
3
+ *
4
+ * @param item - The navigation item
5
+ * @returns The item's url or key
6
+ * @throws Error if item has neither url nor key
7
+ */
8
+ export function getItemId(item) {
9
+ if (item.url)
10
+ return item.url;
11
+ if (item.key)
12
+ return item.key;
13
+ throw new Error("Item must have either url or key");
14
+ }
15
+ //# sourceMappingURL=getItemId.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getItemId.js","sourceRoot":"","sources":["../../../src/navigation/getItemId.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;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,KAAK,CAAC,kCAAkC,CAAC,CAAC;AACtD,CAAC"}
@@ -0,0 +1,4 @@
1
+ export { annotateTree } from "./annotateTree.js";
2
+ export { getItemId } from "./getItemId.js";
3
+ export { prepareIndex } from "./prepareIndex.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/navigation/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { getItemId } from "./getItemId.js";
2
+ /**
3
+ * Create an index for O(1) lookup of navigation items by URL or key
4
+ *
5
+ * @param root - The annotated root item
6
+ * @returns Index mapping URL/key to item
7
+ */
8
+ export function prepareIndex(root) {
9
+ const index = {};
10
+ const stack = [root];
11
+ while (stack.length > 0) {
12
+ const item = stack.pop();
13
+ if (!item)
14
+ continue;
15
+ const id = getItemId(item);
16
+ index[id] = item;
17
+ if (item.items) {
18
+ for (let i = item.items.length - 1; i >= 0; i--) {
19
+ stack.push(item.items[i]);
20
+ }
21
+ }
22
+ }
23
+ return index;
24
+ }
25
+ //# sourceMappingURL=prepareIndex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prepareIndex.js","sourceRoot":"","sources":["../../../src/navigation/prepareIndex.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,IAAW;IACtC,MAAM,KAAK,GAAW,EAAE,CAAC;IACzB,MAAM,KAAK,GAAY,CAAC,IAAI,CAAC,CAAC;IAC9B,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxB,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC;QACzB,IAAI,CAAC,IAAI;YAAE,SAAS;QACpB,MAAM,EAAE,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;QAC3B,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC;QACjB,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;gBAChD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;YAC5B,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -3,6 +3,7 @@ export { default as debounce } from "./debounce.js";
3
3
  export type * from "./humanizeNumber/index.js";
4
4
  export { default as humanizeNumber } from "./humanizeNumber/index.js";
5
5
  export { default as invariant } from "./invariant.js";
6
+ export * from "./navigation/index.js";
6
7
  export type * from "./pluralize/index.js";
7
8
  export { default as pluralize } from "./pluralize/index.js";
8
9
  export { default as throttle } from "./throttle.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEpD,mBAAmB,2BAA2B,CAAC;AAC/C,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACtD,mBAAmB,sBAAsB,CAAC;AAC1C,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEpD,mBAAmB,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEpD,mBAAmB,2BAA2B,CAAC;AAC/C,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEtD,cAAc,uBAAuB,CAAC;AAEtC,mBAAmB,sBAAsB,CAAC;AAC1C,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEpD,mBAAmB,YAAY,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { _Item, Item } from "@canonical/ds-types";
2
+ /**
3
+ * Annotate a navigation tree with parent references and depth
4
+ *
5
+ * Transforms Item tree into _Item tree with parentUrl and depth.
6
+ *
7
+ * @param root - The root item to annotate
8
+ * @param parentUrl - Parent URL (null for root)
9
+ * @param depth - Current depth (0 for root)
10
+ * @returns Annotated item tree
11
+ */
12
+ export declare function annotateTree(root: Item, parentUrl?: string | null, depth?: number): _Item;
13
+ //# sourceMappingURL=annotateTree.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"annotateTree.d.ts","sourceRoot":"","sources":["../../../src/navigation/annotateTree.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAGvD;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,IAAI,EACV,SAAS,GAAE,MAAM,GAAG,IAAW,EAC/B,KAAK,SAAI,GACR,KAAK,CAaP"}
@@ -0,0 +1,10 @@
1
+ import type { _Item, Item } from "@canonical/ds-types";
2
+ /**
3
+ * Get the unique identifier for a navigation item (url or key)
4
+ *
5
+ * @param item - The navigation item
6
+ * @returns The item's url or key
7
+ * @throws Error if item has neither url nor key
8
+ */
9
+ export declare function getItemId(item: Item | _Item): string;
10
+ //# sourceMappingURL=getItemId.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getItemId.d.ts","sourceRoot":"","sources":["../../../src/navigation/getItemId.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,GAAG,KAAK,GAAG,MAAM,CAIpD"}
@@ -0,0 +1,4 @@
1
+ export { annotateTree } from "./annotateTree.js";
2
+ export { getItemId } from "./getItemId.js";
3
+ export { prepareIndex } from "./prepareIndex.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/navigation/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { _Index, _Item } from "@canonical/ds-types";
2
+ /**
3
+ * Create an index for O(1) lookup of navigation items by URL or key
4
+ *
5
+ * @param root - The annotated root item
6
+ * @returns Index mapping URL/key to item
7
+ */
8
+ export declare function prepareIndex(root: _Item): _Index;
9
+ //# sourceMappingURL=prepareIndex.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prepareIndex.d.ts","sourceRoot":"","sources":["../../../src/navigation/prepareIndex.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAGzD;;;;;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/utils",
3
3
  "description": "Standard utility functions for Canonical's Web Engineering team",
4
- "version": "0.11.0",
4
+ "version": "0.12.0",
5
5
  "type": "module",
6
6
  "module": "dist/esm/index.js",
7
7
  "types": "dist/types/index.d.ts",
@@ -41,15 +41,18 @@
41
41
  "test:vitest": "vitest run",
42
42
  "test:vitest:watch": "vitest"
43
43
  },
44
+ "dependencies": {
45
+ "@canonical/ds-types": "^0.12.0"
46
+ },
44
47
  "devDependencies": {
45
48
  "@biomejs/biome": "2.3.11",
46
- "@canonical/biome-config": "^0.11.0",
47
- "@canonical/typescript-config-base": "^0.11.0",
48
- "@canonical/webarchitect": "^0.11.0",
49
+ "@canonical/biome-config": "^0.12.0",
50
+ "@canonical/typescript-config-base": "^0.12.0",
51
+ "@canonical/webarchitect": "^0.12.0",
49
52
  "globals": "^17.0.0",
50
53
  "typescript": "^5.9.3",
51
54
  "vite": "^7.3.1",
52
55
  "vitest": "^4.0.17"
53
56
  },
54
- "gitHead": "29125736059d1880b3d0fc277b218a63a2574f28"
57
+ "gitHead": "0b491caff8f797fef4ba4b7f5514a7c5b915a481"
55
58
  }