svg-path-commander 1.0.5 → 2.0.0-alpha2

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 (131) hide show
  1. package/README.md +26 -4
  2. package/dist/svg-path-commander.cjs +2 -0
  3. package/dist/svg-path-commander.cjs.map +1 -0
  4. package/dist/svg-path-commander.d.ts +454 -0
  5. package/dist/svg-path-commander.js +2 -3762
  6. package/dist/svg-path-commander.js.map +1 -0
  7. package/dist/svg-path-commander.mjs +1194 -0
  8. package/dist/svg-path-commander.mjs.map +1 -0
  9. package/package.json +39 -42
  10. package/src/convert/pathToAbsolute.ts +101 -0
  11. package/src/convert/{pathToCurve.js → pathToCurve.ts} +12 -14
  12. package/src/convert/pathToRelative.ts +92 -0
  13. package/src/convert/pathToString.ts +17 -0
  14. package/src/{svg-path-commander.js → index.ts} +143 -66
  15. package/src/interface.ts +129 -0
  16. package/src/math/distanceSquareRoot.ts +13 -0
  17. package/src/math/midPoint.ts +16 -0
  18. package/src/math/{polygonArea.js → polygonArea.ts} +6 -4
  19. package/src/math/{polygonLength.js → polygonLength.ts} +5 -3
  20. package/src/math/rotateVector.ts +16 -0
  21. package/src/options/options.ts +9 -0
  22. package/src/parser/error.ts +2 -0
  23. package/src/parser/finalizeSegment.ts +31 -0
  24. package/src/parser/{invalidPathValue.js → invalidPathValue.ts} +0 -0
  25. package/src/parser/{isArcCommand.js → isArcCommand.ts} +5 -4
  26. package/src/parser/isDigit.ts +12 -0
  27. package/src/parser/isDigitStart.ts +14 -0
  28. package/src/parser/isPathCommand.ts +28 -0
  29. package/src/parser/isSpace.ts +23 -0
  30. package/src/parser/paramsCount.ts +16 -0
  31. package/src/parser/paramsParser.ts +14 -0
  32. package/src/parser/{parsePathString.js → parsePathString.ts} +13 -7
  33. package/src/parser/pathParser.ts +29 -0
  34. package/src/parser/{scanFlag.js → scanFlag.ts} +8 -5
  35. package/src/parser/{scanParam.js → scanParam.ts} +12 -11
  36. package/src/parser/{scanSegment.js → scanSegment.ts} +9 -5
  37. package/src/parser/{skipSpaces.js → skipSpaces.ts} +5 -3
  38. package/src/process/{arcToCubic.js → arcToCubic.ts} +42 -28
  39. package/src/process/fixArc.ts +23 -0
  40. package/src/process/getSVGMatrix.ts +70 -0
  41. package/src/process/lineToCubic.ts +17 -0
  42. package/src/process/{normalizePath.js → normalizePath.ts} +10 -10
  43. package/src/process/normalizeSegment.ts +47 -0
  44. package/src/process/{optimizePath.js → optimizePath.ts} +18 -21
  45. package/src/process/{projection2d.js → projection2d.ts} +16 -15
  46. package/src/process/quadToCubic.ts +31 -0
  47. package/src/process/reverseCurve.ts +21 -0
  48. package/src/process/reversePath.ts +101 -0
  49. package/src/process/roundPath.ts +29 -0
  50. package/src/process/segmentToCubic.ts +46 -0
  51. package/src/process/shortenSegment.ts +79 -0
  52. package/src/process/splitCubic.ts +28 -0
  53. package/src/process/{splitPath.js → splitPath.ts} +9 -8
  54. package/src/process/{transformPath.js → transformPath.ts} +55 -48
  55. package/src/types.ts +193 -0
  56. package/src/util/getClosestPoint.ts +15 -0
  57. package/src/util/{getDrawDirection.js → getDrawDirection.ts} +7 -4
  58. package/src/util/getPathArea.ts +70 -0
  59. package/src/util/{getPathBBox.js → getPathBBox.ts} +16 -5
  60. package/src/util/getPointAtLength.ts +14 -0
  61. package/src/util/{getPropertiesAtLength.js → getPropertiesAtLength.ts} +28 -19
  62. package/src/util/{getPropertiesAtPoint.js → getPropertiesAtPoint.ts} +16 -12
  63. package/src/util/getSegmentAtLength.ts +15 -0
  64. package/src/util/getSegmentOfPoint.ts +18 -0
  65. package/src/util/{getTotalLength.js → getTotalLength.ts} +6 -4
  66. package/src/util/isAbsoluteArray.ts +18 -0
  67. package/src/util/{isCurveArray.js → isCurveArray.ts} +6 -4
  68. package/src/util/{isNormalizedArray.js → isNormalizedArray.ts} +4 -2
  69. package/src/util/isPathArray.ts +19 -0
  70. package/src/util/isPointInStroke.ts +15 -0
  71. package/src/util/isRelativeArray.ts +18 -0
  72. package/src/util/{isValidPath.js → isValidPath.ts} +5 -4
  73. package/src/util/{pathLengthFactory.js → pathLengthFactory.ts} +38 -31
  74. package/src/util/{segmentArcFactory.js → segmentArcFactory.ts} +71 -55
  75. package/src/util/segmentCubicFactory.ts +114 -0
  76. package/src/util/{segmentLineFactory.js → segmentLineFactory.ts} +10 -8
  77. package/src/util/{segmentQuadFactory.js → segmentQuadFactory.ts} +47 -32
  78. package/src/util/shapeToPath.ts +214 -0
  79. package/dist/svg-path-commander.es5.js +0 -3875
  80. package/dist/svg-path-commander.es5.min.js +0 -2
  81. package/dist/svg-path-commander.esm.js +0 -3754
  82. package/dist/svg-path-commander.esm.min.js +0 -2
  83. package/dist/svg-path-commander.min.js +0 -2
  84. package/src/convert/pathToAbsolute.js +0 -86
  85. package/src/convert/pathToRelative.js +0 -84
  86. package/src/convert/pathToString.js +0 -14
  87. package/src/index.js +0 -10
  88. package/src/math/distanceSquareRoot.js +0 -14
  89. package/src/math/epsilon.js +0 -8
  90. package/src/math/midPoint.js +0 -13
  91. package/src/math/rotateVector.js +0 -14
  92. package/src/options/options.js +0 -10
  93. package/src/parser/error.js +0 -2
  94. package/src/parser/finalizeSegment.js +0 -28
  95. package/src/parser/isDigit.js +0 -9
  96. package/src/parser/isDigitStart.js +0 -13
  97. package/src/parser/isPathCommand.js +0 -25
  98. package/src/parser/isSpace.js +0 -16
  99. package/src/parser/paramsCount.js +0 -9
  100. package/src/parser/paramsParser.js +0 -8
  101. package/src/parser/pathParser.js +0 -24
  102. package/src/process/clonePath.js +0 -9
  103. package/src/process/fixArc.js +0 -21
  104. package/src/process/fixPath.js +0 -31
  105. package/src/process/getSVGMatrix.js +0 -61
  106. package/src/process/lineToCubic.js +0 -30
  107. package/src/process/normalizeSegment.js +0 -45
  108. package/src/process/quadToCubic.js +0 -22
  109. package/src/process/reverseCurve.js +0 -18
  110. package/src/process/reversePath.js +0 -89
  111. package/src/process/roundPath.js +0 -26
  112. package/src/process/segmentToCubic.js +0 -46
  113. package/src/process/shortenSegment.js +0 -58
  114. package/src/process/splitCubic.js +0 -26
  115. package/src/util/getClosestPoint.js +0 -12
  116. package/src/util/getPathArea.js +0 -47
  117. package/src/util/getPointAtLength.js +0 -12
  118. package/src/util/getSegmentAtLength.js +0 -11
  119. package/src/util/getSegmentOfPoint.js +0 -12
  120. package/src/util/isAbsoluteArray.js +0 -14
  121. package/src/util/isPathArray.js +0 -14
  122. package/src/util/isPointInStroke.js +0 -13
  123. package/src/util/isRelativeArray.js +0 -14
  124. package/src/util/segmentCubicFactory.js +0 -97
  125. package/src/util/shapeToPath.js +0 -204
  126. package/src/util/util.js +0 -82
  127. package/src/version.js +0 -8
  128. package/types/index.d.ts +0 -120
  129. package/types/more/modules.ts +0 -82
  130. package/types/more/svg.d.ts +0 -211
  131. package/types/svg-path-commander.d.ts +0 -1089
@@ -0,0 +1,14 @@
1
+ import type { PathArray } from '../types';
2
+ import pathLengthFactory from './pathLengthFactory';
3
+
4
+ /**
5
+ * Returns [x,y] coordinates of a point at a given length of a shape.
6
+ *
7
+ * @param pathInput the `pathArray` to look into
8
+ * @param distance the length of the shape to look at
9
+ * @returns the requested {x, y} point coordinates
10
+ */
11
+ const getPointAtLength = (pathInput: string | PathArray, distance: number): { x: number; y: number } => {
12
+ return pathLengthFactory(pathInput, distance).point;
13
+ };
14
+ export default getPointAtLength;
@@ -1,3 +1,5 @@
1
+ import type { PathArray, PathSegment } from '../types';
2
+ import type { SegmentProperties } from '../interface';
1
3
  import parsePathString from '../parser/parsePathString';
2
4
  import getTotalLength from './getTotalLength';
3
5
 
@@ -5,54 +7,61 @@ import getTotalLength from './getTotalLength';
5
7
  * Returns the segment, its index and length as well as
6
8
  * the length to that segment at a given length in a path.
7
9
  *
8
- * @param {string | SVGPath.pathArray} pathInput target `pathArray`
9
- * @param {number=} distance the given length
10
- * @returns {SVGPath.segmentProperties=} the requested properties
10
+ * @param pathInput target `pathArray`
11
+ * @param distance the given length
12
+ * @returns the requested properties
11
13
  */
12
- export default function getPropertiesAtLength(pathInput, distance) {
14
+ const getPropertiesAtLength = (pathInput: string | PathArray, distance?: number): SegmentProperties => {
13
15
  const pathArray = parsePathString(pathInput);
14
16
 
15
- if (typeof pathArray === 'string') {
16
- throw TypeError(pathArray);
17
- }
18
-
19
- let pathTemp = [...pathArray];
17
+ let pathTemp = [...pathArray] as PathArray;
20
18
  let pathLength = getTotalLength(pathTemp);
21
19
  let index = pathTemp.length - 1;
22
20
  let lengthAtSegment = 0;
23
21
  let length = 0;
24
- let segment = pathArray[0];
25
- const [x, y] = segment.slice(-2);
22
+ let segment = pathArray[0] as PathSegment;
23
+ const [x, y] = segment.slice(-2) as [number, number];
26
24
  const point = { x, y };
27
25
 
28
26
  // If the path is empty, return 0.
29
27
  if (index <= 0 || !distance || !Number.isFinite(distance)) {
30
28
  return {
31
- segment, index: 0, length, point, lengthAtSegment,
29
+ segment,
30
+ index: 0,
31
+ length,
32
+ point,
33
+ lengthAtSegment,
32
34
  };
33
35
  }
34
36
 
35
37
  if (distance >= pathLength) {
36
- pathTemp = pathArray.slice(0, -1);
38
+ pathTemp = pathArray.slice(0, -1) as PathArray;
37
39
  lengthAtSegment = getTotalLength(pathTemp);
38
40
  length = pathLength - lengthAtSegment;
39
41
  return {
40
- segment: pathArray[index], index, length, lengthAtSegment,
42
+ segment: pathArray[index],
43
+ index,
44
+ length,
45
+ lengthAtSegment,
41
46
  };
42
47
  }
43
48
 
44
- const segments = [];
49
+ const segments = [] as SegmentProperties[];
45
50
  while (index > 0) {
46
51
  segment = pathTemp[index];
47
- pathTemp = pathTemp.slice(0, -1);
52
+ pathTemp = pathTemp.slice(0, -1) as PathArray;
48
53
  lengthAtSegment = getTotalLength(pathTemp);
49
54
  length = pathLength - lengthAtSegment;
50
55
  pathLength = lengthAtSegment;
51
56
  segments.push({
52
- segment, index, length, lengthAtSegment,
57
+ segment,
58
+ index,
59
+ length,
60
+ lengthAtSegment,
53
61
  });
54
62
  index -= 1;
55
63
  }
56
64
 
57
- return segments.find(({ lengthAtSegment: l }) => l <= distance);
58
- }
65
+ return segments.find(({ lengthAtSegment: l }) => l <= distance) as SegmentProperties;
66
+ };
67
+ export default getPropertiesAtLength;
@@ -1,3 +1,5 @@
1
+ import type { PathArray } from '../types';
2
+ import type { PointProperties } from '../interface';
1
3
  import getPointAtLength from './getPointAtLength';
2
4
  import getPropertiesAtLength from './getPropertiesAtLength';
3
5
  import getTotalLength from './getTotalLength';
@@ -7,26 +9,26 @@ import normalizePath from '../process/normalizePath';
7
9
  /**
8
10
  * Returns the point and segment in path closest to a given point as well as
9
11
  * the distance to the path stroke.
12
+ *
10
13
  * @see https://bl.ocks.org/mbostock/8027637
11
14
  *
12
- * @param {string | SVGPath.pathArray} pathInput target `pathArray`
13
- * @param {{x: number, y: number}} point the given point
14
- * @returns {SVGPath.pointProperties} the requested properties
15
+ * @param pathInput target `pathArray`
16
+ * @param point the given point
17
+ * @returns the requested properties
15
18
  */
16
- export default function getPropertiesAtPoint(pathInput, point) {
17
- const path = (parsePathString(pathInput));
19
+ const getPropertiesAtPoint = (pathInput: string | PathArray, point: { x: number; y: number }): PointProperties => {
20
+ const path = parsePathString(pathInput);
18
21
  const normalPath = normalizePath(path);
19
22
  const pathLength = getTotalLength(path);
20
- /** @param {{x: number, y: number}} p */
21
- const distanceTo = (p) => {
23
+ const distanceTo = (p: { x: number; y: number }) => {
22
24
  const dx = p.x - point.x;
23
25
  const dy = p.y - point.y;
24
26
  return dx * dx + dy * dy;
25
27
  };
26
28
  let precision = 8;
27
- let scan;
29
+ let scan: { x: number; y: number };
30
+ let closest = { x: 0, y: 0 }; // make TS happy
28
31
  let scanDistance = 0;
29
- let closest;
30
32
  let bestLength = 0;
31
33
  let bestDistance = Infinity;
32
34
 
@@ -43,8 +45,8 @@ export default function getPropertiesAtPoint(pathInput, point) {
43
45
 
44
46
  // binary search for precise estimate
45
47
  precision /= 2;
46
- let before;
47
- let after;
48
+ let before: { x: number; y: number };
49
+ let after: { x: number; y: number };
48
50
  let beforeLength = 0;
49
51
  let afterLength = 0;
50
52
  let beforeDistance = 0;
@@ -74,4 +76,6 @@ export default function getPropertiesAtPoint(pathInput, point) {
74
76
  const distance = Math.sqrt(bestDistance);
75
77
 
76
78
  return { closest, distance, segment };
77
- }
79
+ };
80
+
81
+ export default getPropertiesAtPoint;
@@ -0,0 +1,15 @@
1
+ import type { PathArray, PathSegment } from '../types';
2
+ import getPropertiesAtLength from './getPropertiesAtLength';
3
+
4
+ /**
5
+ * Returns the segment at a given length.
6
+ *
7
+ * @param pathInput the target `pathArray`
8
+ * @param distance the distance in path to look at
9
+ * @returns the requested segment
10
+ */
11
+ const getSegmentAtLength = (pathInput: string | PathArray, distance?: number): PathSegment | undefined => {
12
+ return getPropertiesAtLength(pathInput, distance).segment;
13
+ };
14
+
15
+ export default getSegmentAtLength;
@@ -0,0 +1,18 @@
1
+ import type { SegmentProperties } from '../interface';
2
+ import type { PathArray } from '../types';
3
+ import getPropertiesAtPoint from './getPropertiesAtPoint';
4
+
5
+ /**
6
+ * Returns the path segment which contains a given point.
7
+ *
8
+ * @param path the `pathArray` to look into
9
+ * @param point the point of the shape to look for
10
+ * @returns the requested segment
11
+ */
12
+ const getSegmentOfPoint = (
13
+ path: string | PathArray,
14
+ point: { x: number; y: number },
15
+ ): SegmentProperties | undefined => {
16
+ return getPropertiesAtPoint(path, point).segment;
17
+ };
18
+ export default getSegmentOfPoint;
@@ -1,3 +1,4 @@
1
+ import type { PathArray } from '../types';
1
2
  import pathLengthFactory from './pathLengthFactory';
2
3
 
3
4
  /**
@@ -6,9 +7,10 @@ import pathLengthFactory from './pathLengthFactory';
6
7
  * The `normalizePath` version is lighter, faster, more efficient and more accurate
7
8
  * with paths that are not `curveArray`.
8
9
  *
9
- * @param {string | SVGPath.pathArray} pathInput the target `pathArray`
10
- * @returns {number} the shape total length
10
+ * @param pathInput the target `pathArray`
11
+ * @returns the shape total length
11
12
  */
12
- export default function getTotalLength(pathInput) {
13
+ const getTotalLength = (pathInput: string | PathArray): number => {
13
14
  return pathLengthFactory(pathInput).length;
14
- }
15
+ };
16
+ export default getTotalLength;
@@ -0,0 +1,18 @@
1
+ import type { AbsoluteArray } from '../types';
2
+ import isPathArray from './isPathArray';
3
+
4
+ /**
5
+ * Iterates an array to check if it's a `pathArray`
6
+ * with all absolute values.
7
+ *
8
+ * @param path the `pathArray` to be checked
9
+ * @returns iteration result
10
+ */
11
+ const isAbsoluteArray = (path: unknown): path is AbsoluteArray => {
12
+ return (
13
+ isPathArray(path) &&
14
+ // `isPathArray` also checks if it's `Array`
15
+ path.every(([x]) => x === x.toUpperCase())
16
+ );
17
+ };
18
+ export default isAbsoluteArray;
@@ -1,13 +1,15 @@
1
+ import { CurveArray } from '../types';
1
2
  import isNormalizedArray from './isNormalizedArray';
2
3
 
3
4
  /**
4
5
  * Iterates an array to check if it's a `pathArray`
5
6
  * with all C (cubic bezier) segments.
6
7
  *
7
- * @param {string | SVGPath.pathArray} path the `Array` to be checked
8
- * @returns {boolean} iteration result
8
+ * @param path the `Array` to be checked
9
+ * @returns iteration result
9
10
  */
10
- export default function isCurveArray(path) {
11
+ const isCurveArray = (path: unknown): path is CurveArray => {
11
12
  // `isPathArray` also checks if it's `Array`
12
13
  return isNormalizedArray(path) && path.every(([pc]) => 'MC'.includes(pc));
13
- }
14
+ };
15
+ export default isCurveArray;
@@ -1,3 +1,4 @@
1
+ import type { NormalArray } from '../types';
1
2
  import isAbsoluteArray from './isAbsoluteArray';
2
3
 
3
4
  /**
@@ -8,7 +9,8 @@ import isAbsoluteArray from './isAbsoluteArray';
8
9
  * @param {string | SVGPath.pathArray} path the `pathArray` to be checked
9
10
  * @returns {boolean} iteration result
10
11
  */
11
- export default function isNormalizedArray(path) {
12
+ const isNormalizedArray = (path: unknown): path is NormalArray => {
12
13
  // `isAbsoluteArray` also checks if it's `Array`
13
14
  return isAbsoluteArray(path) && path.every(([pc]) => 'ACLMQZ'.includes(pc));
14
- }
15
+ };
16
+ export default isNormalizedArray;
@@ -0,0 +1,19 @@
1
+ import type { PathArray, PathSegment, RelativeCommand } from '../types';
2
+ import paramsCount from '../parser/paramsCount';
3
+
4
+ /**
5
+ * Iterates an array to check if it's an actual `pathArray`.
6
+ *
7
+ * @param path the `pathArray` to be checked
8
+ * @returns iteration result
9
+ */
10
+ const isPathArray = (path: unknown): path is PathArray => {
11
+ return (
12
+ Array.isArray(path) &&
13
+ path.every((seg: PathSegment) => {
14
+ const lk = seg[0].toLowerCase() as RelativeCommand;
15
+ return paramsCount[lk] === seg.length - 1 && 'achlmqstvz'.includes(lk);
16
+ })
17
+ );
18
+ };
19
+ export default isPathArray;
@@ -0,0 +1,15 @@
1
+ import type { PathArray } from '../types';
2
+ import getPropertiesAtPoint from './getPropertiesAtPoint';
3
+
4
+ /**
5
+ * Checks if a given point is in the stroke of a path.
6
+ *
7
+ * @param pathInput target path
8
+ * @param point the given `{x,y}` point
9
+ * @returns the query result
10
+ */
11
+ const isPointInStroke = (pathInput: string | PathArray, point: { x: number; y: number }) => {
12
+ const { distance } = getPropertiesAtPoint(pathInput, point);
13
+ return Math.abs(distance) < 0.001; // 0.01 might be more permissive
14
+ };
15
+ export default isPointInStroke;
@@ -0,0 +1,18 @@
1
+ import type { RelativeArray } from '../types';
2
+ import isPathArray from './isPathArray';
3
+
4
+ /**
5
+ * Iterates an array to check if it's a `pathArray`
6
+ * with relative values.
7
+ *
8
+ * @param path the `pathArray` to be checked
9
+ * @returns iteration result
10
+ */
11
+ const isRelativeArray = (path: unknown): path is RelativeArray => {
12
+ return (
13
+ isPathArray(path) &&
14
+ // `isPathArray` checks if it's `Array`
15
+ path.slice(1).every(([pc]) => pc === pc.toLowerCase())
16
+ );
17
+ };
18
+ export default isRelativeArray;
@@ -6,10 +6,10 @@ import PathParser from '../parser/pathParser';
6
6
  * Parses a path string value to determine its validity
7
7
  * then returns true if it's valid or false otherwise.
8
8
  *
9
- * @param {string} pathString the path string to be parsed
10
- * @returns {boolean} the path string validity
9
+ * @param pathString the path string to be parsed
10
+ * @returns the path string validity
11
11
  */
12
- export default function isValidPath(pathString) {
12
+ const isValidPath = (pathString: string) => {
13
13
  if (typeof pathString !== 'string') {
14
14
  return false;
15
15
  }
@@ -23,4 +23,5 @@ export default function isValidPath(pathString) {
23
23
  }
24
24
 
25
25
  return !path.err.length && 'mM'.includes(path.segments[0][0]);
26
- }
26
+ };
27
+ export default isValidPath;
@@ -1,5 +1,6 @@
1
+ import type { MSegment, PathArray } from '../types';
2
+ import type { LengthFactory } from '../interface';
1
3
  import normalizePath from '../process/normalizePath';
2
-
3
4
  import segmentLineFactory from './segmentLineFactory';
4
5
  import segmentArcFactory from './segmentArcFactory';
5
6
  import segmentCubicFactory from './segmentCubicFactory';
@@ -10,23 +11,23 @@ import segmentQuadFactory from './segmentQuadFactory';
10
11
  * of a shape, the shape total length and
11
12
  * the shape minimum and maximum {x,y} coordinates.
12
13
  *
13
- * @param {string | SVGPath.pathArray} pathInput the `pathArray` to look into
14
- * @param {number=} distance the length of the shape to look at
15
- * @returns {SVGPath.lengthFactory} the path length, point, min & max
14
+ * @param pathInput the `pathArray` to look into
15
+ * @param distance the length of the shape to look at
16
+ * @returns the path length, point, min & max
16
17
  */
17
- export default function pathLengthFactory(pathInput, distance) {
18
+ const pathLengthFactory = (pathInput: string | PathArray, distance?: number): LengthFactory => {
18
19
  const path = normalizePath(pathInput);
19
20
  const distanceIsNumber = typeof distance === 'number';
20
21
  let isM;
21
- let data = [];
22
+ let data = [] as number[];
22
23
  let pathCommand;
23
24
  let x = 0;
24
25
  let y = 0;
25
26
  let mx = 0;
26
27
  let my = 0;
27
28
  let seg;
28
- let MIN = [];
29
- let MAX = [];
29
+ let MIN = [] as { x: number; y: number }[];
30
+ let MAX = [] as { x: number; y: number }[];
30
31
  let length = 0;
31
32
  let min = { x: 0, y: 0 };
32
33
  let max = min;
@@ -38,13 +39,13 @@ export default function pathLengthFactory(pathInput, distance) {
38
39
  seg = path[i];
39
40
  [pathCommand] = seg;
40
41
  isM = pathCommand === 'M';
41
- data = !isM ? [x, y, ...seg.slice(1)] : data;
42
+ data = !isM ? [x, y, ...(seg.slice(1) as number[])] : data;
42
43
 
43
44
  // this segment is always ZERO
44
45
  /* istanbul ignore else */
45
46
  if (isM) {
46
47
  // remember mx, my for Z
47
- [, mx, my] = seg;
48
+ [, mx, my] = seg as MSegment;
48
49
  min = { x: mx, y: my };
49
50
  max = min;
50
51
  length = 0;
@@ -53,26 +54,31 @@ export default function pathLengthFactory(pathInput, distance) {
53
54
  POINT = min;
54
55
  }
55
56
  } else if (pathCommand === 'L') {
56
- ({
57
- length, min, max, point,
58
- } = segmentLineFactory(...data, (distance || 0) - LENGTH));
57
+ ({ length, min, max, point } = segmentLineFactory(
58
+ ...(data as [number, number, number, number]),
59
+ (distance || 0) - LENGTH,
60
+ ));
59
61
  } else if (pathCommand === 'A') {
60
- ({
61
- length, min, max, point,
62
- } = segmentArcFactory(...data, (distance || 0) - LENGTH));
62
+ ({ length, min, max, point } = segmentArcFactory(
63
+ ...(data as [number, number, number, number, number, number, number, number, number]),
64
+ (distance || 0) - LENGTH,
65
+ ));
63
66
  } else if (pathCommand === 'C') {
64
- ({
65
- length, min, max, point,
66
- } = segmentCubicFactory(...data, (distance || 0) - LENGTH));
67
+ ({ length, min, max, point } = segmentCubicFactory(
68
+ ...(data as [number, number, number, number, number, number, number]),
69
+ (distance || 0) - LENGTH,
70
+ ));
67
71
  } else if (pathCommand === 'Q') {
68
- ({
69
- length, min, max, point,
70
- } = segmentQuadFactory(...data, (distance || 0) - LENGTH));
72
+ ({ length, min, max, point } = segmentQuadFactory(
73
+ ...(data as [number, number, number, number, number, number]),
74
+ (distance || 0) - LENGTH,
75
+ ));
71
76
  } else if (pathCommand === 'Z') {
72
77
  data = [x, y, mx, my];
73
- ({
74
- length, min, max, point,
75
- } = segmentLineFactory(...data, (distance || 0) - LENGTH));
78
+ ({ length, min, max, point } = segmentLineFactory(
79
+ ...(data as [number, number, number, number]),
80
+ (distance || 0) - LENGTH,
81
+ ));
76
82
  }
77
83
 
78
84
  if (distanceIsNumber && LENGTH < distance && LENGTH + length >= distance) {
@@ -83,7 +89,7 @@ export default function pathLengthFactory(pathInput, distance) {
83
89
  MIN = [...MIN, min];
84
90
  LENGTH += length;
85
91
 
86
- [x, y] = pathCommand !== 'Z' ? seg.slice(-2) : [mx, my];
92
+ [x, y] = pathCommand !== 'Z' ? (seg.slice(-2) as [number, number]) : [mx, my];
87
93
  }
88
94
 
89
95
  // native `getPointAtLength` behavior when the given distance
@@ -96,12 +102,13 @@ export default function pathLengthFactory(pathInput, distance) {
96
102
  length: LENGTH,
97
103
  point: POINT,
98
104
  min: {
99
- x: Math.min(...MIN.map((n) => n.x)),
100
- y: Math.min(...MIN.map((n) => n.y)),
105
+ x: Math.min(...MIN.map(n => n.x)),
106
+ y: Math.min(...MIN.map(n => n.y)),
101
107
  },
102
108
  max: {
103
- x: Math.max(...MAX.map((n) => n.x)),
104
- y: Math.max(...MAX.map((n) => n.y)),
109
+ x: Math.max(...MAX.map(n => n.x)),
110
+ y: Math.max(...MAX.map(n => n.y)),
105
111
  },
106
112
  };
107
- }
113
+ };
114
+ export default pathLengthFactory;