@canonical/utils 0.2.0-experimental.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 ADDED
@@ -0,0 +1,7 @@
1
+ ## Canonical Web Team Utils
2
+
3
+ This package contains a collection of utilities that are used across the Canonical Web Team's projects.
4
+
5
+ ### Utils list
6
+ - [debounce](./src/debounce/README.md)
7
+ - [throttle](./src/throttle/README.md)
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Creates a debounced version of a function, ensuring it is called only after a specified
3
+ * delay from the last invocation.
4
+ *
5
+ * @template F The type of the function to debounce
6
+ * @param fn - The function to debounce.
7
+ * @param delay - The time in milliseconds to wait before calling the function after the last call.
8
+ * @returns A debounced version of the function that returns a promise.
9
+ *
10
+ * @example
11
+ * // Example usage of the debounce function
12
+ * const debouncedFetchData = debounce(async (query: string) => {
13
+ * const response = await fetch(`/api/search?q=${query}`);
14
+ * return response.json();
15
+ * }, 300);
16
+ *
17
+ * // Calling the debounced function multiple times
18
+ * debouncedFetchData("hello").then(console.log); // Will only call fetch once after 300ms delay
19
+ * debouncedFetchData("world").then(console.log); // Will cancel the previous call and make a new one
20
+ */
21
+ export default function debounce(fn, delay) {
22
+ let timeoutId = null;
23
+ let promiseReject = null;
24
+ return (...args) => {
25
+ // Timer already exists, clear it and reject the promise
26
+ if (timeoutId) {
27
+ clearTimeout(timeoutId);
28
+ if (promiseReject) {
29
+ promiseReject();
30
+ }
31
+ }
32
+ return new Promise((resolve, reject) => {
33
+ promiseReject = reject;
34
+ timeoutId = setTimeout(async () => {
35
+ // Timer hasn't been cancelled, call the function
36
+ try {
37
+ resolve(fn(...args));
38
+ }
39
+ catch (error) {
40
+ reject(error);
41
+ }
42
+ }, delay);
43
+ });
44
+ };
45
+ }
46
+ //# sourceMappingURL=debounce.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debounce.js","sourceRoot":"","sources":["../../src/debounce.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAE9B,EAAK,EAAE,KAAa;IACpB,IAAI,SAAS,GAAyC,IAAI,CAAC;IAC3D,IAAI,aAAa,GAAuC,IAAI,CAAC;IAE7D,OAAO,CAAC,GAAG,IAAmB,EAA0B,EAAE;QACxD,wDAAwD;QACxD,IAAI,SAAS,EAAE,CAAC;YACd,YAAY,CAAC,SAAS,CAAC,CAAC;YACxB,IAAI,aAAa,EAAE,CAAC;gBAClB,aAAa,EAAE,CAAC;YAClB,CAAC;QACH,CAAC;QAED,OAAO,IAAI,OAAO,CAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACpD,aAAa,GAAG,MAAM,CAAC;YACvB,SAAS,GAAG,UAAU,CAAC,KAAK,IAAI,EAAE;gBAChC,iDAAiD;gBACjD,IAAI,CAAC;oBACH,OAAO,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;gBACvB,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,MAAM,CAAC,KAAK,CAAC,CAAC;gBAChB,CAAC;YACH,CAAC,EAAE,KAAK,CAAC,CAAC;QACZ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { default as debounce } from "./debounce.js";
2
+ export { default as throttle } from "./throttle.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Throttles a function invocation to at most once every `wait` milliseconds.
3
+ * @template T The type of the function to throttle
4
+ * @param func - The function to throttle
5
+ * @param wait - The time in milliseconds to wait between invocations
6
+ * @returns A throttled version of the function
7
+ * @see Throttling function calls, by Remy Sharp
8
+ * http://remysharp.com/2010/07/21/throttling-function-calls/
9
+ *
10
+ * @example
11
+ *
12
+ * window.addEventListener(
13
+ * 'resize',
14
+ * throttle(() => {
15
+ * console.log("window was resized!");
16
+ * }, 500)
17
+ * );
18
+ */
19
+ export default function throttle(func, wait) {
20
+ let timer = null;
21
+ return function (...args) {
22
+ if (timer)
23
+ clearTimeout(timer);
24
+ timer = setTimeout(() => {
25
+ func.apply(this, args);
26
+ }, wait);
27
+ };
28
+ }
29
+ //# sourceMappingURL=throttle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throttle.js","sourceRoot":"","sources":["../../src/throttle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAE9B,IAAO,EAAE,IAAY;IACrB,IAAI,KAAK,GAA0B,IAAI,CAAC;IACxC,OAAO,UAAyB,GAAG,IAAmB;QACpD,IAAI,KAAK;YAAE,YAAY,CAAC,KAAK,CAAC,CAAC;QAC/B,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;YACtB,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACzB,CAAC,EAAE,IAAI,CAAC,CAAC;IACX,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Creates a debounced version of a function, ensuring it is called only after a specified
3
+ * delay from the last invocation.
4
+ *
5
+ * @template F The type of the function to debounce
6
+ * @param fn - The function to debounce.
7
+ * @param delay - The time in milliseconds to wait before calling the function after the last call.
8
+ * @returns A debounced version of the function that returns a promise.
9
+ *
10
+ * @example
11
+ * // Example usage of the debounce function
12
+ * const debouncedFetchData = debounce(async (query: string) => {
13
+ * const response = await fetch(`/api/search?q=${query}`);
14
+ * return response.json();
15
+ * }, 300);
16
+ *
17
+ * // Calling the debounced function multiple times
18
+ * debouncedFetchData("hello").then(console.log); // Will only call fetch once after 300ms delay
19
+ * debouncedFetchData("world").then(console.log); // Will cancel the previous call and make a new one
20
+ */
21
+ export default function debounce<F extends (...args: Parameters<F>) => ReturnType<F>>(fn: F, delay: number): (...args: Parameters<F>) => Promise<ReturnType<F>>;
22
+ //# sourceMappingURL=debounce.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debounce.d.ts","sourceRoot":"","sources":["../../src/debounce.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAC9B,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,EACnD,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAyB1E"}
@@ -0,0 +1,3 @@
1
+ export { default as debounce } from "./debounce.js";
2
+ export { default as throttle } from "./throttle.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Throttles a function invocation to at most once every `wait` milliseconds.
3
+ * @template T The type of the function to throttle
4
+ * @param func - The function to throttle
5
+ * @param wait - The time in milliseconds to wait between invocations
6
+ * @returns A throttled version of the function
7
+ * @see Throttling function calls, by Remy Sharp
8
+ * http://remysharp.com/2010/07/21/throttling-function-calls/
9
+ *
10
+ * @example
11
+ *
12
+ * window.addEventListener(
13
+ * 'resize',
14
+ * throttle(() => {
15
+ * console.log("window was resized!");
16
+ * }, 500)
17
+ * );
18
+ */
19
+ export default function throttle<T extends (...args: Parameters<T>) => ReturnType<T>>(func: T, wait: number): (...args: Parameters<T>) => void;
20
+ //# sourceMappingURL=throttle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"throttle.d.ts","sourceRoot":"","sources":["../../src/throttle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAC9B,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,EACnD,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,IAAI,CAQzD"}
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@canonical/utils",
3
+ "description": "Standard utility functions for Canonical's Web Engineering team",
4
+ "version": "0.2.0-experimental.0",
5
+ "type": "module",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/types/index.d.ts",
8
+ "files": ["dist"],
9
+ "author": {
10
+ "email": "webteam@canonical.com",
11
+ "name": "Canonical Webteam"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/canonical/ds25"
16
+ },
17
+ "license": "LGPL-3.0",
18
+ "bugs": {
19
+ "url": "https://github.com/canonical/ds25/issues"
20
+ },
21
+ "homepage": "https://github.com/canonical/ds25#readme",
22
+ "scripts": {
23
+ "build": "tsc -p tsconfig.build.json",
24
+ "lint": "biome lint src/*.{ts,tsx}",
25
+ "format": "bun run format:biome",
26
+ "format:biome": "biome format src/*.{ts,tsx}",
27
+ "check": "bun run check:biome && bun run check:ts",
28
+ "check:fix": "bun run check:biome:fix",
29
+ "check:biome": "biome check src/* *.json",
30
+ "check:biome:fix": "biome check --write src/* *.json",
31
+ "check:ts": "tsc --noEmit"
32
+ },
33
+ "devDependencies": {
34
+ "@biomejs/biome": "^1.9.2",
35
+ "@canonical/biome-config": "^0.1.0-experimental.0",
36
+ "@canonical/typescript-config-base": "^0.2.0-experimental.0",
37
+ "typescript": "^5.5.3"
38
+ }
39
+ }