@nesso-how/formats 0.1.0-alpha.28 → 0.1.0-alpha.30

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/dist/index.d.ts CHANGED
@@ -11,6 +11,13 @@ export interface NessoGraphFile {
11
11
  display?: Partial<GraphDisplaySettings>;
12
12
  }
13
13
  export declare function serializeGraph(file: NessoGraphFile): string;
14
+ /**
15
+ * Parse and structurally validate a graph file. Files are user-editable (and
16
+ * importable from anywhere), so each element is checked before it can reach
17
+ * the store: a node without a valid id/position crashes the canvas and would
18
+ * then be re-persisted. Partial node data (e.g. hand-written files with only
19
+ * `text`) is normalized with fresh review fields.
20
+ */
14
21
  export declare function deserializeGraph(json: string): NessoGraphFile;
15
22
  /** Strip personal FSRS / review history for shareable graph export. Keeps text, elaboration, layout. */
16
23
  export declare function nodesForGraphShareExport(nodes: Node<ConceptNodeData>[]): Node<ConceptNodeData>[];
package/dist/index.js CHANGED
@@ -2,15 +2,53 @@ import { defaultConceptReviewFields } from '@nesso-how/types';
2
2
  export function serializeGraph(file) {
3
3
  return JSON.stringify(file, null, 2);
4
4
  }
5
+ function asRecord(value) {
6
+ return typeof value === 'object' && value !== null ? value : null;
7
+ }
8
+ /**
9
+ * Parse and structurally validate a graph file. Files are user-editable (and
10
+ * importable from anywhere), so each element is checked before it can reach
11
+ * the store: a node without a valid id/position crashes the canvas and would
12
+ * then be re-persisted. Partial node data (e.g. hand-written files with only
13
+ * `text`) is normalized with fresh review fields.
14
+ */
5
15
  export function deserializeGraph(json) {
6
16
  const data = JSON.parse(json);
7
- if (typeof data !== 'object' ||
8
- data === null ||
9
- !Array.isArray(data.nodes) ||
10
- !Array.isArray(data.edges)) {
17
+ const root = asRecord(data);
18
+ if (!root || !Array.isArray(root.nodes) || !Array.isArray(root.edges)) {
11
19
  throw new Error('Invalid Nesso graph file: missing nodes or edges array');
12
20
  }
13
- return data;
21
+ const review = defaultConceptReviewFields();
22
+ const nodes = root.nodes.map((value, i) => {
23
+ const node = asRecord(value);
24
+ const pos = node ? asRecord(node.position) : null;
25
+ if (!node ||
26
+ typeof node.id !== 'string' ||
27
+ node.id === '' ||
28
+ !pos ||
29
+ typeof pos.x !== 'number' ||
30
+ !Number.isFinite(pos.x) ||
31
+ typeof pos.y !== 'number' ||
32
+ !Number.isFinite(pos.y)) {
33
+ throw new Error(`Invalid Nesso graph file: node ${i} is missing a valid id or position`);
34
+ }
35
+ const d = asRecord(node.data) ?? {};
36
+ return {
37
+ ...node,
38
+ data: { ...review, ...d, text: typeof d.text === 'string' ? d.text : '' },
39
+ };
40
+ });
41
+ const edges = root.edges.map((value, i) => {
42
+ const edge = asRecord(value);
43
+ if (!edge ||
44
+ typeof edge.id !== 'string' ||
45
+ typeof edge.source !== 'string' ||
46
+ typeof edge.target !== 'string') {
47
+ throw new Error(`Invalid Nesso graph file: edge ${i} is missing id, source or target`);
48
+ }
49
+ return edge;
50
+ });
51
+ return { ...root, nodes, edges };
14
52
  }
15
53
  /** Strip personal FSRS / review history for shareable graph export. Keeps text, elaboration, layout. */
16
54
  export function nodesForGraphShareExport(nodes) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nesso-how/formats",
3
- "version": "0.1.0-alpha.28",
3
+ "version": "0.1.0-alpha.30",
4
4
  "description": "Nesso graph serialization formats — JSON serialize/deserialize",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -22,7 +22,7 @@
22
22
  }
23
23
  },
24
24
  "dependencies": {
25
- "@nesso-how/types": "0.1.0-alpha.28"
25
+ "@nesso-how/types": "0.1.0-alpha.30"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "@xyflow/react": "^12.6.4"