svg-toolbox 1.0.2 → 1.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.
@@ -49,11 +49,6 @@ module.exports = async function (pathA, pathB, diffFilePath) {
49
49
  // Compare the images and get the number of different pixels
50
50
  const numDiffPixels = pixelmatch(img1.data, img2.data, diff.data, width, height, { threshold: 0.1 });
51
51
 
52
- // Ensure the diff directory exists
53
- if (!fs.existsSync('diff')) {
54
- fs.mkdirSync('diff', { recursive: true });
55
- }
56
-
57
52
  // Write the diff image to a buffer
58
53
  const diffPngBuffer = PNG.sync.write(diff);
59
54
 
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @file applyRemoveNanCoordinates.js
3
+ * @description This module provides a function to parse and normalize the 'd' attribute of all path elements in an SVG content.
4
+ * @requires jsdom - JavaScript DOM library
5
+ * @module applyRemoveNanCoordinates
6
+ * @author pipi
7
+ */
8
+
9
+ const { JSDOM } = require('jsdom');
10
+
11
+ /**
12
+ * Parses and normalizes the 'd' attribute of all path elements in an SVG content.
13
+ * @param {*} svgContent
14
+ * @returns
15
+ */
16
+ module.exports = function (svgContent) {
17
+ // Create a DOM from the SVG content
18
+ const dom = new JSDOM(svgContent, {
19
+ contentType: 'image/svg+xml' // Set content type to SVG
20
+ });
21
+ const document = dom.window.document;
22
+ const svgElement = document.querySelector('svg');
23
+ const paths = svgElement.querySelectorAll('path');
24
+
25
+ // Iterate over each path element
26
+ Array.from(paths).forEach(path => {
27
+ const d = path.getAttribute('d'); // Get the 'd' attribute
28
+ const commands = d.split(/(?=[MLHVCSQTAZ])/).map(command => {
29
+ const type = command[0]; // Command type (e.g., M, L, H, etc.)
30
+ const params = command.slice(1).trim().split(/[\s,]+/).filter(Number); // Command parameters
31
+ return { type, params };
32
+ }).filter(command => (command.type === 'Z' || command.params.length > 0)); // Filter out invalid commands
33
+
34
+ // Reconstruct the 'd' attribute
35
+ const modifiedD = commands.map(command => {
36
+ return command.type + command.params.join(' ');
37
+ }).join('');
38
+ path.setAttribute('d', modifiedD); // Set the modified 'd' attribute
39
+ });
40
+
41
+ // Get the new SVG content
42
+ const newSvgContent = svgElement.outerHTML.trim();
43
+
44
+ return newSvgContent;
45
+ }
package/lib/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  const svg2Png = require('./applySvg2Png');
2
2
  const diffSvg = require('./applyDiffSvg');
3
+ const removeEmptyCoordinates = require('./applyRemoveNanCoordinates');
3
4
 
4
5
  module.exports = {
5
6
  svg2Png,
6
- diffSvg
7
+ diffSvg,
8
+ removeEmptyCoordinates,
7
9
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svg-toolbox",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "This library provides some SVG-related tools",
5
5
  "main": "lib/index.js",
6
6
  "repository": "https://github.com/SteamedBread2333/svg-toolbox",