easyeda 0.0.381 → 0.0.382

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/main.cjs CHANGED
@@ -1078,7 +1078,7 @@ var import_promises = __toESM(require("fs/promises"), 1);
1078
1078
  var package_default = {
1079
1079
  name: "easyeda",
1080
1080
  type: "module",
1081
- version: "0.0.380",
1081
+ version: "0.0.381",
1082
1082
  files: [
1083
1083
  "dist"
1084
1084
  ],
@@ -13136,8 +13136,10 @@ var Polygon$1 = class Polygon {
13136
13136
  return edge;
13137
13137
  }
13138
13138
  /**
13139
- * Split polygon into array of polygons, where each polygon is an outer face with all
13140
- * containing inner faces
13139
+ * Split polygon into an array of islands with their contained holes.
13140
+ * The largest face defines the outer orientation; opposite-oriented faces represent voids.
13141
+ * Holes not contained in any island are omitted from the result, without changing
13142
+ * their orientation or the input polygon. This method does not clip overlapping faces.
13141
13143
  * @returns {Flatten.Polygon[]}
13142
13144
  */
13143
13145
  splitToIslands() {
@@ -13182,7 +13184,7 @@ var Polygon$1 = class Polygon {
13182
13184
  }
13183
13185
  /**
13184
13186
  * Helper method to check if face is outer face of the polygon
13185
- * @param face
13187
+ * @param {Flatten.Face} face - face to check
13186
13188
  * @returns {boolean}
13187
13189
  */
13188
13190
  isOuter(face) {
@@ -13967,14 +13969,41 @@ var Distance = class _Distance {
13967
13969
  };
13968
13970
  Flatten.Distance = Distance;
13969
13971
  var { Multiline: Multiline2, Point: Point2, Segment: Segment2, Polygon: Polygon2 } = Flatten;
13972
+ function splitComponents(str) {
13973
+ const components = [];
13974
+ let depth = 0;
13975
+ let start = 0;
13976
+ for (let i = 0; i < str.length; i++) {
13977
+ if (str[i] === "(") depth++;
13978
+ if (str[i] === ")") depth--;
13979
+ if (depth < 0) throw Flatten.Errors.ILLEGAL_PARAMETERS;
13980
+ if (str[i] === "," && depth === 0) {
13981
+ components.push(str.slice(start, i).trim());
13982
+ start = i + 1;
13983
+ }
13984
+ }
13985
+ if (depth !== 0) throw Flatten.Errors.ILLEGAL_PARAMETERS;
13986
+ components.push(str.slice(start).trim());
13987
+ return components;
13988
+ }
13989
+ function unwrap(str) {
13990
+ str = str.trim();
13991
+ if (str === "EMPTY") throw new Error("Empty geometry not supported");
13992
+ if (!str.startsWith("(") || !str.endsWith(")")) {
13993
+ throw Flatten.Errors.ILLEGAL_PARAMETERS;
13994
+ }
13995
+ return str.slice(1, -1).trim();
13996
+ }
13970
13997
  function parseSinglePoint(pointStr) {
13971
- return new Point2(pointStr.split(" ").map(Number));
13998
+ pointStr = pointStr.trim();
13999
+ if (pointStr === "EMPTY") throw new Error("Empty geometry not supported");
14000
+ return new Point2(pointStr.split(/\s+/).map(Number));
13972
14001
  }
13973
14002
  function parseMultiPoint(multipointStr) {
13974
- return multipointStr.split(", ").map(parseSinglePoint);
14003
+ return splitComponents(multipointStr).map((str) => parseSinglePoint(str.startsWith("(") ? unwrap(str) : str));
13975
14004
  }
13976
14005
  function parseLineString(lineStr) {
13977
- const points = parseMultiPoint(lineStr);
14006
+ const points = splitComponents(lineStr).map(parseSinglePoint);
13978
14007
  let segments = [];
13979
14008
  for (let i = 0; i < points.length - 1; i++) {
13980
14009
  segments.push(new Segment2(points[i], points[i + 1]));
@@ -13982,45 +14011,31 @@ function parseLineString(lineStr) {
13982
14011
  return new Multiline2(segments);
13983
14012
  }
13984
14013
  function parseMultiLineString(multilineStr) {
13985
- const lineStrings = multilineStr.replace(/\(\(/, "").replace(/\)\)$/, "").split("), (");
13986
- return lineStrings.map(parseLineString);
14014
+ return splitComponents(multilineStr).map((str) => parseLineString(unwrap(str)));
13987
14015
  }
13988
14016
  function parseSinglePolygon(polygonStr) {
13989
- const facesStr = polygonStr.replace(/\(\(/, "").replace(/\)\)$/, "").split("), (");
13990
14017
  const polygon2 = new Polygon2();
14018
+ if (polygonStr.trim() === "EMPTY") return polygon2;
14019
+ const facesStr = splitComponents(unwrap(polygonStr));
13991
14020
  let orientation2;
13992
- facesStr.forEach((facesStr2, idx) => {
13993
- let points = facesStr2.split(", ").map((coordStr) => {
13994
- return new Point2(coordStr.split(" ").map(Number));
13995
- });
14021
+ facesStr.forEach((faceStr, idx) => {
14022
+ const points = splitComponents(unwrap(faceStr)).map(parseSinglePoint);
13996
14023
  const face = polygon2.addFace(points);
13997
14024
  if (idx === 0) {
13998
14025
  orientation2 = face.orientation();
13999
- } else {
14000
- if (face.orientation() === orientation2) {
14001
- face.reverse();
14002
- }
14026
+ } else if (face.orientation() === orientation2) {
14027
+ face.reverse();
14003
14028
  }
14004
14029
  });
14005
14030
  return polygon2;
14006
14031
  }
14007
- function parseMutliPolygon(multiPolygonString) {
14008
- const polygonStrings = multiPolygonString.split(/\)\), \(\(/).map((polygon3) => "((" + polygon3 + "))");
14009
- const polygons = polygonStrings.map(parseSinglePolygon);
14032
+ function parseMultiPolygon(multiPolygonStr) {
14010
14033
  const polygon2 = new Polygon2();
14011
- const faces = polygons.reduce((acc, polygon3) => [...acc, ...polygon3?.faces], []);
14012
- faces.forEach((face) => polygon2.addFace([...face?.shapes]));
14034
+ splitComponents(multiPolygonStr).map(parseSinglePolygon).forEach((part) => {
14035
+ part.faces.forEach((face) => polygon2.addFace([...face.shapes]));
14036
+ });
14013
14037
  return polygon2;
14014
14038
  }
14015
- function parsePolygon(wkt) {
14016
- if (wkt.startsWith("POLYGON")) {
14017
- const polygonStr = wkt.replace(/^POLYGON /, "");
14018
- return parseSinglePolygon(polygonStr);
14019
- } else {
14020
- const multiPolygonString = wkt.replace(/^MULTIPOLYGON \(\(\((.*)\)\)\)$/, "$1");
14021
- return parseMutliPolygon(multiPolygonString);
14022
- }
14023
- }
14024
14039
  function parseArrayOfPoints(str) {
14025
14040
  const arr = str.split("\n").map((x) => x.match(/\(([^)]+)\)/)[1]);
14026
14041
  return arr.map(parseSinglePoint);
@@ -14030,28 +14045,39 @@ function parseArrayOfLineStrings(str) {
14030
14045
  return arr.map(parseLineString).reduce((acc, x) => [...acc, ...x], []);
14031
14046
  }
14032
14047
  function parseWKT(str) {
14033
- if (str.startsWith("POINT")) {
14034
- const pointStr = str.replace(/^POINT \(/, "").replace(/\)$/, "");
14035
- return parseSinglePoint(pointStr);
14036
- } else if (str.startsWith("MULTIPOINT")) {
14037
- const multiPointStr = str.replace(/^MULTIPOINT \(/, "").replace(/\)$/, "");
14038
- return parseMultiPoint(multiPointStr);
14039
- } else if (str.startsWith("LINESTRING")) {
14040
- const lineStr = str.replace(/^LINESTRING \(/, "").replace(/\)$/, "");
14041
- return parseLineString(lineStr);
14042
- } else if (str.startsWith("MULTILINESTRING")) {
14043
- const multilineStr = str.replace(/^MULTILINESTRING /, "");
14044
- return parseMultiLineString(multilineStr);
14045
- } else if (str.startsWith("POLYGON") || str.startsWith("MULTIPOLYGON")) {
14046
- return parsePolygon(str);
14047
- } else if (str.startsWith("GEOMETRYCOLLECTION")) {
14048
- const regex = /(?<type>POINT|LINESTRING|POLYGON|MULTIPOINT|MULTILINESTRING|MULTIPOLYGON) \((?:[^\(\)]|\([^\)]*\))*\)/g;
14049
- const wktArray = str.match(regex);
14050
- if (wktArray[0].startsWith("GEOMETRYCOLLECTION")) {
14051
- wktArray[0] = wktArray[0].replace("GEOMETRYCOLLECTION (", "");
14052
- }
14053
- const flArray = wktArray.map(parseWKT).map((x) => x instanceof Array ? x : [x]);
14054
- return flArray.reduce((acc, x) => [...acc, ...x], []);
14048
+ const records = str.trim().split("\n");
14049
+ if (records.length > 1) {
14050
+ if (records.every((record) => /^\s*POINT\s*\([^()]*\)\s*$/.test(record))) {
14051
+ return parseArrayOfPoints(str.trim());
14052
+ }
14053
+ if (records.every((record) => /^\s*LINESTRING\s*\([^()]*\)\s*$/.test(record))) {
14054
+ return parseArrayOfLineStrings(str.trim());
14055
+ }
14056
+ }
14057
+ const geometry = str.trim().match(/^(POINT|MULTIPOINT|LINESTRING|MULTILINESTRING|POLYGON|MULTIPOLYGON|GEOMETRYCOLLECTION)\s*(EMPTY|\([\s\S]*\))$/);
14058
+ if (geometry) {
14059
+ const [, type, body] = geometry;
14060
+ if (body === "EMPTY") {
14061
+ if (type === "POLYGON") return new Polygon2();
14062
+ throw new Error("Empty geometry not supported");
14063
+ }
14064
+ const content = unwrap(body);
14065
+ switch (type) {
14066
+ case "POINT":
14067
+ return parseSinglePoint(content);
14068
+ case "MULTIPOINT":
14069
+ return parseMultiPoint(content);
14070
+ case "LINESTRING":
14071
+ return parseLineString(content);
14072
+ case "MULTILINESTRING":
14073
+ return parseMultiLineString(content);
14074
+ case "POLYGON":
14075
+ return parseSinglePolygon(body);
14076
+ case "MULTIPOLYGON":
14077
+ return parseMultiPolygon(content);
14078
+ case "GEOMETRYCOLLECTION":
14079
+ return splitComponents(content).map(parseWKT).reduce((acc, shapes) => acc.concat(shapes), []);
14080
+ }
14055
14081
  } else if (isArrayOfPoints(str)) {
14056
14082
  return parseArrayOfPoints(str);
14057
14083
  } else if (isArrayOfLines(str)) {
@@ -15616,30 +15642,37 @@ var expandEasyEdaPathData = (pathData) => {
15616
15642
  const command = commandMatch[1]?.toUpperCase();
15617
15643
  const values = commandMatch[2]?.match(/[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][-+]?\d+)?/g)?.map(Number) ?? [];
15618
15644
  if ((command === "M" || command === "L") && values.length >= 2) {
15619
- currentPoint = { x: values[0], y: values[1] };
15620
- rawRoute.push(currentPoint);
15645
+ for (let offset = 0; offset + 1 < values.length; offset += 2) {
15646
+ currentPoint = { x: values[offset], y: values[offset + 1] };
15647
+ rawRoute.push(currentPoint);
15648
+ }
15621
15649
  } else if (command === "A" && currentPoint && values.length >= 7) {
15622
- const [radiusX, , , largeArcFlag, sweepFlag, endX, endY] = values;
15623
- const generatedArcRoute = generateArcFromSweep(
15624
- currentPoint.x,
15625
- currentPoint.y,
15626
- endX,
15627
- endY,
15628
- radiusX,
15629
- largeArcFlag === 1,
15630
- sweepFlag === 1
15631
- );
15632
- const maxArcSegments = 16;
15633
- const arcRoute = generatedArcRoute.length <= maxArcSegments + 1 ? generatedArcRoute : Array.from(
15634
- { length: maxArcSegments + 1 },
15635
- (_, pointIndex) => generatedArcRoute.at(
15636
- Math.round(
15637
- pointIndex * (generatedArcRoute.length - 1) / maxArcSegments
15650
+ for (let offset = 0; offset + 6 < values.length; offset += 7) {
15651
+ const [radiusX, , , largeArcFlag, sweepFlag, endX, endY] = values.slice(
15652
+ offset,
15653
+ offset + 7
15654
+ );
15655
+ const generatedArcRoute = generateArcFromSweep(
15656
+ currentPoint.x,
15657
+ currentPoint.y,
15658
+ endX,
15659
+ endY,
15660
+ radiusX,
15661
+ largeArcFlag === 1,
15662
+ sweepFlag === 1
15663
+ );
15664
+ const maxArcSegments = 16;
15665
+ const arcRoute = generatedArcRoute.length <= maxArcSegments + 1 ? generatedArcRoute : Array.from(
15666
+ { length: maxArcSegments + 1 },
15667
+ (_, pointIndex) => generatedArcRoute.at(
15668
+ Math.round(
15669
+ pointIndex * (generatedArcRoute.length - 1) / maxArcSegments
15670
+ )
15638
15671
  )
15639
- )
15640
- ).filter((point4) => !!point4);
15641
- rawRoute.push(...arcRoute.slice(1));
15642
- currentPoint = { x: endX, y: endY };
15672
+ ).filter((point4) => !!point4);
15673
+ rawRoute.push(...arcRoute.slice(1));
15674
+ currentPoint = { x: endX, y: endY };
15675
+ }
15643
15676
  }
15644
15677
  }
15645
15678
  return rawRoute;
@@ -17491,7 +17524,7 @@ var PolygonShapeOutputSchema = import_zod223.z.object({
17491
17524
  lineColor: import_zod223.z.string(),
17492
17525
  id: import_zod223.z.string()
17493
17526
  });
17494
- var parsePolygon2 = (str) => {
17527
+ var parsePolygon = (str) => {
17495
17528
  const [, ...rest] = str.split("~");
17496
17529
  const [pointsStr, lineColor, lineWidth, , fillColor, id] = rest;
17497
17530
  return {
@@ -17503,7 +17536,7 @@ var parsePolygon2 = (str) => {
17503
17536
  id
17504
17537
  };
17505
17538
  };
17506
- var PolygonShapeSchema = import_zod223.z.string().startsWith("PG~").transform(parsePolygon2).pipe(PolygonShapeOutputSchema);
17539
+ var PolygonShapeSchema = import_zod223.z.string().startsWith("PG~").transform(parsePolygon).pipe(PolygonShapeOutputSchema);
17507
17540
  var PathShapeOutputSchema = import_zod223.z.object({
17508
17541
  type: import_zod223.z.literal("PATH"),
17509
17542
  pathData: import_zod223.z.string(),