@pie-element/shared-utils 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.
@@ -0,0 +1,7 @@
1
+ /**
2
+ * PIE Shared Utilities
3
+ *
4
+ * Framework-agnostic utility functions for PIE elements.
5
+ */
6
+ export * from './utils';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,SAAS,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,70 @@
1
+ function c(e) {
2
+ const t = typeof process < "u" && process.env?.DEBUG?.includes(e);
3
+ return (...r) => {
4
+ t && console.log(`[${e}]`, ...r);
5
+ };
6
+ }
7
+ function a(e) {
8
+ return !e || typeof e == "object" && Object.keys(e).length === 0;
9
+ }
10
+ function l(e, t) {
11
+ return e.mode === "evaluate" && t;
12
+ }
13
+ function y(e, t) {
14
+ return e.mode === "evaluate" && t && e.role === "instructor";
15
+ }
16
+ function d(e, t, r) {
17
+ return Math.min(Math.max(e, t), r);
18
+ }
19
+ function g() {
20
+ return crypto.randomUUID();
21
+ }
22
+ function h(e, t) {
23
+ let r;
24
+ return (...o) => {
25
+ clearTimeout(r), r = setTimeout(() => e(...o), t);
26
+ };
27
+ }
28
+ function p(e, t) {
29
+ const r = [...e], o = t ? f(t) : Math.random;
30
+ for (let n = r.length - 1; n > 0; n--) {
31
+ const i = Math.floor(o() * (n + 1));
32
+ [r[n], r[i]] = [r[i], r[n]];
33
+ }
34
+ return r;
35
+ }
36
+ function f(e) {
37
+ let t = e;
38
+ return () => (t = (t * 9301 + 49297) % 233280, t / 233280);
39
+ }
40
+ function m(e, t, r = {}) {
41
+ const { skipUndefined: o = !0 } = r;
42
+ for (const [n, i] of Object.entries(t))
43
+ o && i === void 0 || (e[n] = i);
44
+ }
45
+ function v(e, t) {
46
+ if (e === t) return !0;
47
+ if (!e || !t || typeof e != "object" || typeof t != "object") return e === t;
48
+ const r = e, o = t;
49
+ if ("answers" in r && "answers" in o) {
50
+ const n = r.answers, i = o.answers;
51
+ return !Array.isArray(n) || !Array.isArray(i) || n.length !== i.length ? !1 : JSON.stringify(n) === JSON.stringify(i);
52
+ }
53
+ if ("value" in r && "value" in o) {
54
+ const n = r.value, i = o.value;
55
+ return Array.isArray(n) && Array.isArray(i) ? n.length !== i.length ? !1 : n.length === 0 || typeof n[0] != "object" ? n.every((u, s) => u === i[s]) : JSON.stringify(n) === JSON.stringify(i) : typeof n == "object" && typeof i == "object" ? JSON.stringify(n) === JSON.stringify(i) : n === i;
56
+ }
57
+ return JSON.stringify(e) === JSON.stringify(t);
58
+ }
59
+ export {
60
+ m as assignProps,
61
+ d as clamp,
62
+ h as debounce,
63
+ c as debug,
64
+ a as isEmpty,
65
+ v as sessionsEqual,
66
+ l as showFeedback,
67
+ y as showRationale,
68
+ p as shuffle,
69
+ g as uuid
70
+ };
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Common utilities for PIE elements
3
+ */
4
+ import type { PieEnvironment, PieSession } from '@pie-element/shared-types';
5
+ /**
6
+ * Simple debug logger factory
7
+ * Replacement for the 'debug' npm package
8
+ *
9
+ * Usage:
10
+ * const log = debug('my-component');
11
+ * log('message', data); // Only logs if DEBUG environment includes 'my-component'
12
+ */
13
+ export declare function debug(namespace: string): (...args: unknown[]) => void;
14
+ /**
15
+ * Check if session is empty
16
+ */
17
+ export declare function isEmpty(session: PieSession | null | undefined): boolean;
18
+ /**
19
+ * Check if feedback should be shown based on mode and configuration
20
+ */
21
+ export declare function showFeedback(env: PieEnvironment, feedbackEnabled: boolean): boolean;
22
+ /**
23
+ * Check if rationale should be shown based on mode and configuration
24
+ */
25
+ export declare function showRationale(env: PieEnvironment, rationaleEnabled: boolean): boolean;
26
+ /**
27
+ * Clamp a number between min and max
28
+ */
29
+ export declare function clamp(value: number, min: number, max: number): number;
30
+ /**
31
+ * Generate a random UUID (v4)
32
+ */
33
+ export declare function uuid(): string;
34
+ /**
35
+ * Debounce function calls
36
+ */
37
+ export declare function debounce<T extends (...args: unknown[]) => unknown>(fn: T, delay: number): (...args: Parameters<T>) => void;
38
+ /**
39
+ * Shuffle array using Fisher-Yates algorithm
40
+ */
41
+ export declare function shuffle<T>(array: T[], seed?: number): T[];
42
+ export type AssignPropsOptions = {
43
+ /**
44
+ * When true (default), keys with `undefined` values are skipped.
45
+ * This prevents accidentally overwriting existing values on the element.
46
+ *
47
+ * Note: `null` is still assigned (it is often a meaningful value).
48
+ */
49
+ skipUndefined?: boolean;
50
+ };
51
+ /**
52
+ * Assign a set of JS properties onto a DOM element.
53
+ *
54
+ * This is the preferred way to pass values into our custom elements, especially
55
+ * Svelte custom elements, where camelCase props do not map cleanly via HTML attributes.
56
+ */
57
+ export declare function assignProps(node: HTMLElement, props: Record<string, unknown>, options?: AssignPropsOptions): void;
58
+ /**
59
+ * Smart equality check for PIE sessions
60
+ * Optimized for common drag-drop session structures to avoid expensive JSON.stringify
61
+ * comparisons in reactive effects.
62
+ *
63
+ * @param a - First session to compare
64
+ * @param b - Second session to compare
65
+ * @returns true if sessions are equal, false otherwise
66
+ */
67
+ export declare function sessionsEqual(a: unknown, b: unknown): boolean;
68
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5E;;;;;;;GAOG;AACH,wBAAgB,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CASrE;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,OAAO,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAIvE;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,cAAc,EAAE,eAAe,EAAE,OAAO,GAAG,OAAO,CAEnF;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,cAAc,EAAE,gBAAgB,EAAE,OAAO,GAAG,OAAO,CAErF;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAErE;AAED;;GAEG;AACH,wBAAgB,IAAI,IAAI,MAAM,CAE7B;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,EAChE,EAAE,EAAE,CAAC,EACL,KAAK,EAAE,MAAM,GACZ,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,IAAI,CAMlC;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,CAAC,EAAE,CAUzD;AAaD,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;;;;OAKG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,WAAW,EACjB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9B,OAAO,GAAE,kBAAuB,GAC/B,IAAI,CAMN;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAsD7D"}
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@pie-element/shared-utils",
3
+ "version": "0.1.0",
4
+ "description": "Shared utilities for PIE elements",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "rm -rf dist tsconfig.tsbuildinfo && vite build && tsc --emitDeclarationOnly",
19
+ "dev": "vite build --watch",
20
+ "test": "vitest",
21
+ "clean": "rm -rf dist"
22
+ },
23
+ "dependencies": {
24
+ "@pie-element/shared-types": "workspace:*"
25
+ },
26
+ "devDependencies": {
27
+ "@types/bun": "latest",
28
+ "typescript": "^5.9.3",
29
+ "vite": "^7.2.2",
30
+ "vitest": "^4.0.17"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "license": "MIT"
36
+ }