All files svgshapes2svgpath.js

11.76% Statements 2/17
0% Branches 0/50
0% Functions 0/5
11.76% Lines 2/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136      1x               1x                                                                                                                                                                                                                                                        
/* eslint-disable prefer-template,complexity */
'use strict';
 
const svgShapesToPath = {
  rectToPath: svgShapesToPathRectToPath,
  polylineToPath: svgShapesToPathPolylineToPath,
  lineToPath: svgShapesToPathLineToPath,
  circleToPath: svgShapesToPathCircleToPath,
  polygonToPath: svgShapesToPathPolygonToPath,
};
 
module.exports = svgShapesToPath;
 
// Shapes helpers (should also move elsewhere)
function svgShapesToPathRectToPath(attributes) {
  const x = 'undefined' !== typeof attributes.x ? parseFloat(attributes.x) : 0;
  const y = 'undefined' !== typeof attributes.y ? parseFloat(attributes.y) : 0;
  const width =
    'undefined' !== typeof attributes.width ? parseFloat(attributes.width) : 0;
  const height =
    'undefined' !== typeof attributes.height
      ? parseFloat(attributes.height)
      : 0;
  const rx =
    'undefined' !== typeof attributes.rx
      ? parseFloat(attributes.rx)
      : 'undefined' !== typeof attributes.ry
      ? parseFloat(attributes.ry)
      : 0;
  const ry =
    'undefined' !== typeof attributes.ry ? parseFloat(attributes.ry) : rx;
 
  return (
    '' +
    // start at the left corner
    'M' +
    (x + rx) +
    ' ' +
    y +
    // top line
    'h' +
    (width - rx * 2) +
    // upper right corner
    (rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx + ' ' + ry : '') +
    // Draw right side
    'v' +
    (height - ry * 2) +
    // Draw bottom right corner
    (rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx * -1 + ' ' + ry : '') +
    // Down the down side
    'h' +
    (width - rx * 2) * -1 +
    // Draw bottom right corner
    (rx || ry
      ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx * -1 + ' ' + ry * -1
      : '') +
    // Down the left side
    'v' +
    (height - ry * 2) * -1 +
    // Draw bottom right corner
    (rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx + ' ' + ry * -1 : '') +
    // Close path
    'z'
  );
}
 
function svgShapesToPathPolylineToPath(attributes) {
  return 'M' + attributes.points;
}
 
function svgShapesToPathLineToPath(attributes) {
  // Move to the line start
  return (
    '' +
    'M' +
    (parseFloat(attributes.x1) || 0).toString(10) +
    ' ' +
    (parseFloat(attributes.y1) || 0).toString(10) +
    ' ' +
    ((parseFloat(attributes.x1) || 0) + 1).toString(10) +
    ' ' +
    ((parseFloat(attributes.y1) || 0) + 1).toString(10) +
    ' ' +
    ((parseFloat(attributes.x2) || 0) + 1).toString(10) +
    ' ' +
    ((parseFloat(attributes.y2) || 0) + 1).toString(10) +
    ' ' +
    (parseFloat(attributes.x2) || 0).toString(10) +
    ' ' +
    (parseFloat(attributes.y2) || 0).toString(10) +
    'Z'
  );
}
 
function svgShapesToPathCircleToPath(attributes) {
  const cx = parseFloat(attributes.cx);
  const cy = parseFloat(attributes.cy);
  const rx =
    'undefined' !== typeof attributes.rx
      ? parseFloat(attributes.rx)
      : parseFloat(attributes.r);
  const ry =
    'undefined' !== typeof attributes.ry
      ? parseFloat(attributes.ry)
      : parseFloat(attributes.r);
 
  // use two A commands because one command which returns to origin is invalid
  return (
    '' +
    'M' +
    (cx - rx) +
    ',' +
    cy +
    'A' +
    rx +
    ',' +
    ry +
    ' 0,0,0 ' +
    (cx + rx) +
    ',' +
    cy +
    'A' +
    rx +
    ',' +
    ry +
    ' 0,0,0 ' +
    (cx - rx) +
    ',' +
    cy
  );
}
 
function svgShapesToPathPolygonToPath(attributes) {
  return 'M' + attributes.points + 'Z';
}