@tscircuit/circuit-json-util 0.0.41

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 tscircuit Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # @tscircuit/circuit-json-util
2
+
3
+ > Previously released as `@tscircuit/soup-util`
4
+
5
+ This is a small utility library for working with [circuit json](https://github.com/tscircuit/circuit-json)
6
+
7
+ It reduces the amount of code to retrieve or join elements from circuit json, it also neatly handles all the typing.
8
+
9
+ ## Standard Usage
10
+
11
+ ```ts
12
+ import { su } from "@tscircuit/circuit-json-util"
13
+
14
+ const circuitJson = [
15
+ /* [ { type: "source_component", ... }, ... ] */
16
+ ]
17
+
18
+ const pcb_component = su(circuitJson).pcb_component.get("1234")
19
+
20
+ const source_component = su(circuitJson).source_component.getUsing({
21
+ pcb_component_id: "123",
22
+ })
23
+
24
+ const schematic_component = su(circuitJson).schematic_component.getWhere({
25
+ width: 1,
26
+ })
27
+
28
+ const source_traces = su(circuitJson).source_trace.list({
29
+ source_component_id: "123",
30
+ })
31
+ ```
32
+
33
+ ## Optimized Indexed Version
34
+
35
+ For large circuit json, the library provides an optimized version with indexing for faster lookups:
36
+
37
+ ```ts
38
+ import { suIndexed } from "@tscircuit/circuit-json-util"
39
+
40
+ const circuitJson = [
41
+ /* large soup with many elements */
42
+ ]
43
+
44
+ // Configure the indexes you want to use
45
+ const indexedSu = suIndexed(circuitJson, {
46
+ indexConfig: {
47
+ byId: true, // Index by element ID for fast .get() operations
48
+ byType: true, // Index by element type for fast .list() operations
49
+ byRelation: true, // Index relation fields (fields ending with _id)
50
+ bySubcircuit: true, // Index by subcircuit_id for fast subcircuit filtering
51
+ byCustomField: ["name", "ftype"], // Index specific fields you query often
52
+ },
53
+ })
54
+
55
+ // Use the same API as the standard version, but with much better performance
56
+ const pcb_component = indexedSu.pcb_component.get("1234") // O(1) lookup
57
+
58
+ // Fast filtering by subcircuit
59
+ const subcircuitElements = indexedSu.source_component.list({
60
+ subcircuit_id: "main",
61
+ })
62
+ ```
63
+
64
+ The indexed version maintains the same API as the standard version but provides significant performance improvements, especially for large circuit json arrays.