@tscircuit/circuit-json-util 0.0.48 → 0.0.50

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.js CHANGED
@@ -1,5 +1,76 @@
1
1
  // lib/cju.ts
2
2
  import * as Soup from "circuit-json";
3
+
4
+ // lib/subtree.ts
5
+ function connect(map, a, b) {
6
+ if (!a || !b)
7
+ return;
8
+ let setA = map.get(a);
9
+ if (!setA) {
10
+ setA = /* @__PURE__ */ new Set();
11
+ map.set(a, setA);
12
+ }
13
+ setA.add(b);
14
+ let setB = map.get(b);
15
+ if (!setB) {
16
+ setB = /* @__PURE__ */ new Set();
17
+ map.set(b, setB);
18
+ }
19
+ setB.add(a);
20
+ }
21
+ function buildSubtree(soup, opts) {
22
+ if (!opts.subcircuit_id && !opts.source_group_id)
23
+ return [...soup];
24
+ const idMap = /* @__PURE__ */ new Map();
25
+ for (const elm of soup) {
26
+ const idVal = elm[`${elm.type}_id`];
27
+ if (typeof idVal === "string") {
28
+ idMap.set(idVal, elm);
29
+ }
30
+ }
31
+ const adj = /* @__PURE__ */ new Map();
32
+ for (const elm of soup) {
33
+ const entries = Object.entries(elm);
34
+ for (const [key, val] of entries) {
35
+ if (key.endsWith("_id") && typeof val === "string") {
36
+ const other = idMap.get(val);
37
+ connect(adj, elm, other);
38
+ } else if (key.endsWith("_ids") && Array.isArray(val)) {
39
+ for (const v of val) {
40
+ if (typeof v === "string") {
41
+ const other = idMap.get(v);
42
+ connect(adj, elm, other);
43
+ }
44
+ }
45
+ }
46
+ }
47
+ }
48
+ const queue = [];
49
+ const included = /* @__PURE__ */ new Set();
50
+ for (const elm of soup) {
51
+ if (opts.subcircuit_id && elm.subcircuit_id === opts.subcircuit_id || opts.source_group_id && (elm.source_group_id === opts.source_group_id || Array.isArray(elm.member_source_group_ids) && elm.member_source_group_ids.includes(
52
+ opts.source_group_id
53
+ ))) {
54
+ queue.push(elm);
55
+ included.add(elm);
56
+ }
57
+ }
58
+ while (queue.length > 0) {
59
+ const elm = queue.shift();
60
+ const neighbors = adj.get(elm);
61
+ if (!neighbors)
62
+ continue;
63
+ for (const n of neighbors) {
64
+ if (!included.has(n)) {
65
+ included.add(n);
66
+ queue.push(n);
67
+ }
68
+ }
69
+ }
70
+ return soup.filter((e) => included.has(e));
71
+ }
72
+
73
+ // lib/cju.ts
3
74
  var cju = (circuitJsonInput, options = {}) => {
4
75
  const circuitJson = circuitJsonInput;
5
76
  let internalStore = circuitJson._internal_store;
@@ -37,6 +108,9 @@ var cju = (circuitJsonInput, options = {}) => {
37
108
  if (prop === "editCount") {
38
109
  return internalStore.editCount;
39
110
  }
111
+ if (prop === "subtree") {
112
+ return (opts) => cju(buildSubtree(circuitJson, opts), options);
113
+ }
40
114
  const component_type = prop;
41
115
  return {
42
116
  get: (id) => circuitJson.find(
@@ -800,7 +874,10 @@ var transformSchematicElements = (elms, matrix) => {
800
874
  };
801
875
  var transformPCBElement = (elm, matrix) => {
802
876
  if (elm.type === "pcb_plated_hole" || elm.type === "pcb_hole" || elm.type === "pcb_via" || elm.type === "pcb_smtpad" || elm.type === "pcb_port") {
803
- const { x, y } = applyToPoint(matrix, { x: elm.x, y: elm.y });
877
+ const { x, y } = applyToPoint(matrix, {
878
+ x: Number(elm.x),
879
+ y: Number(elm.y)
880
+ });
804
881
  elm.x = x;
805
882
  elm.y = y;
806
883
  } else if (elm.type === "pcb_keepout" || elm.type === "pcb_board") {
@@ -1074,18 +1151,18 @@ var getBoundsOfPcbElements = (elements) => {
1074
1151
  let width;
1075
1152
  let height;
1076
1153
  if ("x" in elm && "y" in elm) {
1077
- centerX = elm.x;
1078
- centerY = elm.y;
1154
+ centerX = Number(elm.x);
1155
+ centerY = Number(elm.y);
1079
1156
  }
1080
1157
  if ("outer_diameter" in elm) {
1081
- width = elm.outer_diameter;
1082
- height = elm.outer_diameter;
1158
+ width = Number(elm.outer_diameter);
1159
+ height = Number(elm.outer_diameter);
1083
1160
  }
1084
1161
  if ("width" in elm) {
1085
- width = elm.width;
1162
+ width = Number(elm.width);
1086
1163
  }
1087
1164
  if ("height" in elm) {
1088
- height = elm.height;
1165
+ height = Number(elm.height);
1089
1166
  }
1090
1167
  if ("center" in elm) {
1091
1168
  centerX = elm.center.x;