@tscircuit/core 0.0.1156 → 0.0.1157

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.
Files changed (2) hide show
  1. package/dist/index.js +66 -72
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -19296,7 +19296,7 @@ import { identity as identity5 } from "transformation-matrix";
19296
19296
  var package_default = {
19297
19297
  name: "@tscircuit/core",
19298
19298
  type: "module",
19299
- version: "0.0.1155",
19299
+ version: "0.0.1156",
19300
19300
  types: "dist/index.d.ts",
19301
19301
  main: "dist/index.js",
19302
19302
  module: "dist/index.js",
@@ -23094,83 +23094,77 @@ import {
23094
23094
  } from "@tscircuit/props";
23095
23095
  import { unknown_error_finding_part as unknown_error_finding_part2 } from "circuit-json";
23096
23096
 
23097
- // lib/utils/connectors/rewriteToStandardUsbCPortHints.ts
23098
- var PIN_HINT_RE = /^(?:pin)?(\d+)$/i;
23099
- var USB_C_STANDARD_PORT_HINT_ALIASES = {
23100
- GND1: [],
23101
- VBUS1: [],
23102
- SBU2: [],
23103
- CC1: [],
23104
- DM2: ["DN2"],
23105
- DP1: [],
23106
- DM1: ["DN1"],
23107
- DP2: [],
23108
- SBU1: [],
23109
- CC2: [],
23110
- VBUS2: [],
23111
- GND2: [],
23112
- SHELL1: [],
23113
- SHELL2: [],
23114
- SHELL3: [],
23115
- SHELL4: []
23116
- };
23117
- var unique = (hints) => Array.from(new Set(hints));
23118
- var rewriteToStandardUsbCPortHints = (circuitJson) => {
23119
- const aliasToStandardHint = {};
23120
- for (const [standardHint, aliases] of Object.entries(
23121
- USB_C_STANDARD_PORT_HINT_ALIASES
23122
- )) {
23123
- aliasToStandardHint[standardHint] = standardHint;
23124
- for (const alias of aliases) {
23125
- aliasToStandardHint[alias] = standardHint;
23126
- }
23127
- }
23128
- const sourceAliasesByPin = /* @__PURE__ */ new Map();
23129
- for (const elm of circuitJson) {
23130
- if (elm && typeof elm === "object" && elm.type === "source_port" && typeof elm.pin_number === "number") {
23131
- const pin = elm.pin_number;
23132
- const hints = Array.isArray(elm.port_hints) ? elm.port_hints.filter((h) => typeof h === "string").map((h) => h.trim()).filter((h) => h.length > 0 && !PIN_HINT_RE.test(h)) : [];
23133
- if (hints.length > 0) sourceAliasesByPin.set(pin, unique(hints));
23134
- }
23135
- }
23136
- if (sourceAliasesByPin.size === 0) return circuitJson;
23137
- return circuitJson.map((elm) => {
23138
- if (!elm || typeof elm !== "object" || !("port_hints" in elm)) return elm;
23139
- if (!Array.isArray(elm.port_hints)) return elm;
23097
+ // lib/utils/connectors/convertCircuitJsonToUsbCStandardCircuitJson.ts
23098
+ var STANDARD_USB_C_PIN_LABELS = [
23099
+ { label: "GND1", aliases: [] },
23100
+ { label: "VBUS1", aliases: [] },
23101
+ { label: "CC1", aliases: [] },
23102
+ { label: "DP1", aliases: [] },
23103
+ { label: "DM1", aliases: ["DN1"] },
23104
+ { label: "SBU1", aliases: [] },
23105
+ { label: "SBU2", aliases: [] },
23106
+ { label: "DM2", aliases: ["DN2"] },
23107
+ { label: "DP2", aliases: [] },
23108
+ { label: "CC2", aliases: [] },
23109
+ { label: "VBUS2", aliases: [] },
23110
+ { label: "GND2", aliases: [] },
23111
+ { label: "SHELL1", aliases: ["MH1", "EH1", "MOUNT1"] },
23112
+ { label: "SHELL2", aliases: ["MH2", "EH2", "MOUNT2"] },
23113
+ { label: "SHELL3", aliases: ["MH3", "EH3", "MOUNT3"] },
23114
+ { label: "SHELL4", aliases: ["MH4", "EH4", "MOUNT4"] }
23115
+ ];
23116
+ var PIN_NUMBER_HINT_PATTERN = /^(?:pin)?(\d+)$/i;
23117
+ var dedupeHintsPreservingOrder = (hints) => Array.from(new Set(hints));
23118
+ var convertCircuitJsonToUsbCStandardCircuitJson = (partCircuitJson) => {
23119
+ const unassignedPorts = [];
23120
+ for (const elm of partCircuitJson) {
23121
+ if (elm.type !== "source_port") continue;
23122
+ const pinNumber = elm.pin_number;
23123
+ if (typeof pinNumber !== "number") continue;
23124
+ const upperCaseHints = /* @__PURE__ */ new Set();
23125
+ for (const hint of elm.port_hints ?? []) {
23126
+ upperCaseHints.add(hint.trim().toUpperCase());
23127
+ }
23128
+ unassignedPorts.push({ pinKey: `pin${pinNumber}`, upperCaseHints });
23129
+ }
23130
+ const canonicalHintsByPin = {};
23131
+ for (const { label, aliases } of STANDARD_USB_C_PIN_LABELS) {
23132
+ const canonicalAndAliasHintsUpper = [label, ...aliases].map(
23133
+ (s) => s.toUpperCase()
23134
+ );
23135
+ const matchIndex = unassignedPorts.findIndex(
23136
+ (port) => canonicalAndAliasHintsUpper.some((hint) => port.upperCaseHints.has(hint))
23137
+ );
23138
+ if (matchIndex === -1) continue;
23139
+ const { pinKey } = unassignedPorts[matchIndex];
23140
+ canonicalHintsByPin[pinKey] = [label];
23141
+ unassignedPorts.splice(matchIndex, 1);
23142
+ }
23143
+ if (Object.keys(canonicalHintsByPin).length === 0) return partCircuitJson;
23144
+ return partCircuitJson.map((elm) => {
23145
+ if (!("port_hints" in elm) || !Array.isArray(elm.port_hints)) return elm;
23140
23146
  const originalHints = elm.port_hints.filter((h) => typeof h === "string").map((h) => h.trim()).filter((h) => h.length > 0);
23141
23147
  if (originalHints.length === 0) return elm;
23142
- const pinNumbers = /* @__PURE__ */ new Set();
23148
+ const pinKeys = /* @__PURE__ */ new Set();
23143
23149
  if (elm.type === "source_port" && typeof elm.pin_number === "number") {
23144
- pinNumbers.add(elm.pin_number);
23150
+ pinKeys.add(`pin${elm.pin_number}`);
23145
23151
  }
23146
23152
  for (const hint of originalHints) {
23147
- const m = hint.match(PIN_HINT_RE);
23148
- if (m) pinNumbers.add(Number.parseInt(m[1], 10));
23149
- }
23150
- const sourceAliases = [];
23151
- for (const pin of pinNumbers) {
23152
- sourceAliases.push(...sourceAliasesByPin.get(pin) ?? []);
23153
- }
23154
- const standardHints = /* @__PURE__ */ new Set();
23155
- for (const hint of [...originalHints, ...sourceAliases]) {
23156
- const standard = aliasToStandardHint[hint.toUpperCase()];
23157
- if (standard) standardHints.add(standard);
23158
- }
23159
- const addedHints = [];
23160
- for (const standardHint of standardHints) {
23161
- addedHints.push(
23162
- standardHint,
23163
- ...USB_C_STANDARD_PORT_HINT_ALIASES[standardHint]
23164
- );
23153
+ const matchedPin = hint.match(PIN_NUMBER_HINT_PATTERN);
23154
+ if (!matchedPin) continue;
23155
+ pinKeys.add(`pin${Number.parseInt(matchedPin[1], 10)}`);
23165
23156
  }
23166
- const rewrittenPortHints = unique([
23167
- ...originalHints,
23168
- ...sourceAliases,
23169
- ...addedHints
23170
- ]);
23157
+ const canonicalHintsToAdd = [];
23158
+ for (const pinKey of pinKeys) {
23159
+ canonicalHintsToAdd.push(...canonicalHintsByPin[pinKey] ?? []);
23160
+ }
23161
+ if (canonicalHintsToAdd.length === 0) return elm;
23171
23162
  return {
23172
23163
  ...elm,
23173
- port_hints: rewrittenPortHints
23164
+ port_hints: dedupeHintsPreservingOrder([
23165
+ ...originalHints,
23166
+ ...canonicalHintsToAdd
23167
+ ])
23174
23168
  };
23175
23169
  });
23176
23170
  };
@@ -23316,8 +23310,8 @@ var Connector = class extends Chip {
23316
23310
  });
23317
23311
  }
23318
23312
  _addConnectorFootprintFromCircuitJson(standard, circuitJson) {
23319
- const rewrittenCircuitJson = standard === "usb_c" ? rewriteToStandardUsbCPortHints(circuitJson) : circuitJson;
23320
23313
  const props = this._getConnectorProps();
23314
+ const standardizedCircuitJson = standard === "usb_c" ? convertCircuitJsonToUsbCStandardCircuitJson(circuitJson) : circuitJson;
23321
23315
  const fpComponents = createComponentsFromCircuitJson(
23322
23316
  {
23323
23317
  componentName: this.name,
@@ -23326,7 +23320,7 @@ var Connector = class extends Chip {
23326
23320
  pinLabels: props.pinLabels,
23327
23321
  pcbPinLabels: props.pcbPinLabels
23328
23322
  },
23329
- rewrittenCircuitJson
23323
+ standardizedCircuitJson
23330
23324
  );
23331
23325
  this.addAll(fpComponents);
23332
23326
  this._markDirty("InitializePortsFromChildren");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tscircuit/core",
3
3
  "type": "module",
4
- "version": "0.0.1156",
4
+ "version": "0.0.1157",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",