circuit-json-to-connectivity-map 0.0.2 → 0.0.4
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 +18 -2
- package/dist/index.d.ts +14 -6
- package/dist/index.js +71 -13
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -35,17 +35,31 @@ console.log(result)
|
|
|
35
35
|
### Generating Source Port Connectivity Map
|
|
36
36
|
|
|
37
37
|
```typescript
|
|
38
|
-
import {
|
|
38
|
+
import { getSourcePortConnectivityMapFromCircuitJson } from "circuit-json-to-connectivity-map"
|
|
39
39
|
import type { AnySoupElement } from "@tscircuit/soup"
|
|
40
40
|
|
|
41
41
|
const circuitJson: AnySoupElement[] = [
|
|
42
42
|
// Your circuit JSON data here
|
|
43
43
|
]
|
|
44
44
|
|
|
45
|
-
const connectivityMap =
|
|
45
|
+
const connectivityMap = getSourcePortConnectivityMapFromCircuitJson(circuitJson)
|
|
46
46
|
console.log(connectivityMap)
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
+
### Generating Full Connectivity Map
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { getFullConnectivityMapFromCircuitJson } from "circuit-json-to-connectivity-map"
|
|
53
|
+
import type { AnySoupElement } from "@tscircuit/soup"
|
|
54
|
+
|
|
55
|
+
const circuitJson: AnySoupElement[] = [
|
|
56
|
+
// Your circuit JSON data here
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
const fullConnectivityMap = getFullConnectivityMapFromCircuitJson(circuitJson)
|
|
60
|
+
console.log(fullConnectivityMap)
|
|
61
|
+
```
|
|
62
|
+
|
|
49
63
|
## Running Tests
|
|
50
64
|
|
|
51
65
|
To run the tests:
|
|
@@ -54,6 +68,8 @@ To run the tests:
|
|
|
54
68
|
bun test
|
|
55
69
|
```
|
|
56
70
|
|
|
71
|
+
This will run all tests, including the newly added test for `getFullConnectivityMapFromCircuitJson`.
|
|
72
|
+
|
|
57
73
|
## Development
|
|
58
74
|
|
|
59
75
|
This project uses [Bun](https://bun.sh) as its JavaScript runtime. Bun is a fast all-in-one JavaScript runtime.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import { AnySoupElement } from '@tscircuit/soup';
|
|
2
2
|
|
|
3
3
|
type NodeId = string;
|
|
4
|
-
declare function findConnectedNetworks(connections: Array<NodeId[]>):
|
|
5
|
-
netId: string;
|
|
6
|
-
connectedNodeIds: string[];
|
|
7
|
-
}>;
|
|
4
|
+
declare function findConnectedNetworks(connections: Array<NodeId[]>): Record<string, string[]>;
|
|
8
5
|
|
|
9
|
-
declare
|
|
6
|
+
declare class ConnectivityMap {
|
|
7
|
+
netMap: Record<string, string[]>;
|
|
8
|
+
idToNetMap: Record<string, string>;
|
|
9
|
+
constructor(netMap: Record<string, string[]>);
|
|
10
|
+
getIdsConnectedToNet(netId: string): string[];
|
|
11
|
+
getNetConnectedToId(id: string): string | undefined;
|
|
12
|
+
areIdsConnected(id1: string, id2: string): boolean;
|
|
13
|
+
}
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
declare const getSourcePortConnectivityMapFromCircuitJson: (circuitJson: AnySoupElement[]) => ConnectivityMap;
|
|
16
|
+
|
|
17
|
+
declare const getFullConnectivityMapFromCircuitJson: (circuitJson: AnySoupElement[]) => ConnectivityMap;
|
|
18
|
+
|
|
19
|
+
export { findConnectedNetworks, getFullConnectivityMapFromCircuitJson, getSourcePortConnectivityMapFromCircuitJson };
|
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ function findConnectedNetworks(connections) {
|
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
const newNetwork = /* @__PURE__ */ new Set();
|
|
12
|
-
networks.set(`
|
|
12
|
+
networks.set(`connectivity_net${netCounter++}`, newNetwork);
|
|
13
13
|
return newNetwork;
|
|
14
14
|
}
|
|
15
15
|
for (const connection of connections) {
|
|
@@ -33,29 +33,87 @@ function findConnectedNetworks(connections) {
|
|
|
33
33
|
network.add(nodeId);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
-
return
|
|
37
|
-
netId,
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
return Object.fromEntries(
|
|
37
|
+
Array.from(networks.entries()).map(([netId, connectedNodes]) => [
|
|
38
|
+
netId,
|
|
39
|
+
Array.from(connectedNodes)
|
|
40
|
+
])
|
|
41
|
+
);
|
|
40
42
|
}
|
|
41
43
|
|
|
44
|
+
// src/ConnectivityMap.ts
|
|
45
|
+
var ConnectivityMap = class {
|
|
46
|
+
netMap;
|
|
47
|
+
idToNetMap;
|
|
48
|
+
constructor(netMap) {
|
|
49
|
+
this.netMap = netMap;
|
|
50
|
+
this.idToNetMap = {};
|
|
51
|
+
for (const [netId, ids] of Object.entries(netMap)) {
|
|
52
|
+
for (const id of ids) {
|
|
53
|
+
this.idToNetMap[id] = netId;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
getIdsConnectedToNet(netId) {
|
|
58
|
+
return this.netMap[netId] || [];
|
|
59
|
+
}
|
|
60
|
+
getNetConnectedToId(id) {
|
|
61
|
+
return this.idToNetMap[id];
|
|
62
|
+
}
|
|
63
|
+
areIdsConnected(id1, id2) {
|
|
64
|
+
const netId1 = this.getNetConnectedToId(id1);
|
|
65
|
+
const netId2 = this.getNetConnectedToId(id2);
|
|
66
|
+
return netId1 === netId2 || netId2 === id1 || netId2 === id1;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
42
70
|
// src/getSourcePortConnectivityMapFromCircuitJson.ts
|
|
43
|
-
var
|
|
44
|
-
const connectivityMap = /* @__PURE__ */ new Map();
|
|
71
|
+
var getSourcePortConnectivityMapFromCircuitJson = (circuitJson) => {
|
|
45
72
|
const connections = [];
|
|
46
73
|
for (const element of circuitJson) {
|
|
47
74
|
if (element.type === "source_trace") {
|
|
48
|
-
connections.push(
|
|
75
|
+
connections.push([
|
|
76
|
+
...element.connected_source_port_ids ?? [],
|
|
77
|
+
...element.connected_source_net_ids ?? []
|
|
78
|
+
]);
|
|
49
79
|
}
|
|
50
80
|
}
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
81
|
+
const netMap = findConnectedNetworks(connections);
|
|
82
|
+
return new ConnectivityMap(netMap);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// src/getFullConnectivityMapFromCircuitJson.ts
|
|
86
|
+
var getFullConnectivityMapFromCircuitJson = (circuitJson) => {
|
|
87
|
+
const connections = [];
|
|
88
|
+
for (const element of circuitJson) {
|
|
89
|
+
if (element.type === "source_trace") {
|
|
90
|
+
connections.push([
|
|
91
|
+
...element.connected_source_port_ids ?? [],
|
|
92
|
+
...element.connected_source_net_ids ?? []
|
|
93
|
+
]);
|
|
94
|
+
} else if (element.type === "pcb_port") {
|
|
95
|
+
const { pcb_port_id, source_port_id } = element;
|
|
96
|
+
if (source_port_id && pcb_port_id) {
|
|
97
|
+
connections.push([source_port_id, pcb_port_id]);
|
|
98
|
+
}
|
|
99
|
+
} else if (element.type === "pcb_smtpad") {
|
|
100
|
+
const { pcb_smtpad_id, pcb_port_id } = element;
|
|
101
|
+
if (pcb_port_id && pcb_smtpad_id) {
|
|
102
|
+
connections.push([pcb_smtpad_id, pcb_port_id]);
|
|
103
|
+
}
|
|
104
|
+
} else if (element.type === "pcb_plated_hole") {
|
|
105
|
+
const { pcb_plated_hole_id, pcb_port_id } = element;
|
|
106
|
+
if (pcb_port_id && pcb_plated_hole_id) {
|
|
107
|
+
connections.push([pcb_plated_hole_id, pcb_port_id]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
54
110
|
}
|
|
55
|
-
|
|
111
|
+
const netMap = findConnectedNetworks(connections);
|
|
112
|
+
return new ConnectivityMap(netMap);
|
|
56
113
|
};
|
|
57
114
|
export {
|
|
58
115
|
findConnectedNetworks,
|
|
59
|
-
|
|
116
|
+
getFullConnectivityMapFromCircuitJson,
|
|
117
|
+
getSourcePortConnectivityMapFromCircuitJson
|
|
60
118
|
};
|
|
61
119
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/findConnectedNetworks.ts","../src/getSourcePortConnectivityMapFromCircuitJson.ts"],"sourcesContent":["type NodeId = string\n\nexport function findConnectedNetworks(\n connections: Array<NodeId[]>,\n):
|
|
1
|
+
{"version":3,"sources":["../src/findConnectedNetworks.ts","../src/ConnectivityMap.ts","../src/getSourcePortConnectivityMapFromCircuitJson.ts","../src/getFullConnectivityMapFromCircuitJson.ts"],"sourcesContent":["type NodeId = string\n\nexport function findConnectedNetworks(\n connections: Array<NodeId[]>,\n): Record<string, string[]> {\n const networks: Map<string, Set<string>> = new Map()\n let netCounter = 0\n\n function getOrCreateNetwork(nodeId: string): Set<string> {\n for (const [, network] of networks) {\n if (network.has(nodeId)) {\n return network\n }\n }\n const newNetwork = new Set<string>()\n networks.set(`connectivity_net${netCounter++}`, newNetwork)\n return newNetwork\n }\n\n for (const connection of connections) {\n let network: Set<string> | null = null\n\n for (const nodeId of connection) {\n if (!network) {\n network = getOrCreateNetwork(nodeId)\n } else if (!network.has(nodeId)) {\n const existingNetwork = getOrCreateNetwork(nodeId)\n if (existingNetwork !== network) {\n // Merge networks\n for (const node of existingNetwork) {\n network.add(node)\n }\n networks.delete(\n Array.from(networks.entries()).find(\n ([, net]) => net === existingNetwork,\n )![0],\n )\n }\n }\n network.add(nodeId)\n }\n }\n\n return Object.fromEntries(\n Array.from(networks.entries()).map(([netId, connectedNodes]) => [\n netId,\n Array.from(connectedNodes),\n ]),\n )\n}\n","export class ConnectivityMap {\n netMap: Record<string, string[]>\n\n idToNetMap: Record<string, string>\n\n constructor(netMap: Record<string, string[]>) {\n this.netMap = netMap\n this.idToNetMap = {}\n for (const [netId, ids] of Object.entries(netMap)) {\n for (const id of ids) {\n this.idToNetMap[id] = netId\n }\n }\n }\n\n getIdsConnectedToNet(netId: string): string[] {\n return this.netMap[netId] || []\n }\n\n getNetConnectedToId(id: string): string | undefined {\n return this.idToNetMap[id]\n }\n\n areIdsConnected(id1: string, id2: string): boolean {\n const netId1 = this.getNetConnectedToId(id1)\n const netId2 = this.getNetConnectedToId(id2)\n return netId1 === netId2 || netId2 === id1 || netId2 === id1\n }\n}\n","import type { AnySoupElement } from \"@tscircuit/soup\"\nimport { findConnectedNetworks } from \"./findConnectedNetworks\"\nimport { ConnectivityMap } from \"./ConnectivityMap\"\n\nexport const getSourcePortConnectivityMapFromCircuitJson = (\n circuitJson: AnySoupElement[],\n) => {\n const connections: string[][] = []\n\n for (const element of circuitJson) {\n if (element.type === \"source_trace\") {\n connections.push([\n ...(element.connected_source_port_ids ?? []),\n ...(element.connected_source_net_ids ?? []),\n ])\n }\n }\n\n const netMap = findConnectedNetworks(connections)\n\n return new ConnectivityMap(netMap)\n}\n","import type { AnySoupElement } from \"@tscircuit/soup\"\nimport { findConnectedNetworks } from \"./findConnectedNetworks\"\nimport { ConnectivityMap } from \"./ConnectivityMap\"\n\nexport const getFullConnectivityMapFromCircuitJson = (\n circuitJson: AnySoupElement[],\n) => {\n const connections: string[][] = []\n\n for (const element of circuitJson) {\n if (element.type === \"source_trace\") {\n connections.push([\n ...(element.connected_source_port_ids ?? []),\n ...(element.connected_source_net_ids ?? []),\n ])\n } else if (element.type === \"pcb_port\") {\n const { pcb_port_id, source_port_id } = element\n if (source_port_id && pcb_port_id) {\n connections.push([source_port_id, pcb_port_id])\n }\n } else if (element.type === \"pcb_smtpad\") {\n const { pcb_smtpad_id, pcb_port_id } = element\n if (pcb_port_id && pcb_smtpad_id) {\n connections.push([pcb_smtpad_id, pcb_port_id])\n }\n } else if (element.type === \"pcb_plated_hole\") {\n const { pcb_plated_hole_id, pcb_port_id } = element\n if (pcb_port_id && pcb_plated_hole_id) {\n connections.push([pcb_plated_hole_id, pcb_port_id])\n }\n }\n }\n\n const netMap = findConnectedNetworks(connections)\n\n return new Connect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C,oBAAY,KAAK,CAAC,eAAe,WAAW,CAAC;AAAA,MAC/C;AAAA,IACF,WAAW,QAAQ,SAAS,mBAAmB;AAC7C,YAAM,EAAE,oBAAoB,YAAY,IAAI;AAC5C,UAAI,eAAe,oBAAoB;AACrC,oBAAY,KAAK,CAAC,oBAAoB,WAAW,CAAC;AAAA,MACpD;AAAA,IACF;AAAA,EACF;AAEA,QAAM,SAAS,sBAAsB,WAAW;AAEhD,SAAO,IAAI,gBAAgB,MAAM;AACnC;","names":[]}
|
package/package.json
CHANGED
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
"name": "circuit-json-to-connectivity-map",
|
|
3
3
|
"module": "dist/index.js",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.4",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
8
8
|
],
|
|
9
9
|
"scripts": {
|
|
10
|
-
"build": "tsup-node src/index.ts --format esm --sourcemap --dts"
|
|
10
|
+
"build": "tsup-node src/index.ts --format esm --sourcemap --dts",
|
|
11
|
+
"format:check": "biome format .",
|
|
12
|
+
"format": "biome format --write ."
|
|
11
13
|
},
|
|
12
14
|
"devDependencies": {
|
|
13
15
|
"@tscircuit/soup": "^0.0.68",
|