🎥 Make videos programmatically with React
same internal structure + more tests
Jonny Burger502935e2ab74
diff --git a/packages/paths/src/reverse-path.ts b/packages/paths/src/reverse-path.tsindex 4c7e3b4c7e..e63e98a4af 100644--- a/packages/paths/src/reverse-path.ts+++ b/packages/paths/src/reverse-path.ts@@ -10,26 +10,50 @@ import type {Instruction} from './helpers/types';import {normalizeInstructions} from './normalize-path';import {parsePath} from './parse-path';import {reduceInstructions} from './reduce-instructions';+import {serializeInstructions} from './serialize-instructions';function reverseNormalizedPath(instructions: Instruction[]) {- const reversed: unknown[] = [];+ const reversed: Instruction[] = [];++ let nextX = 0;+ let nextY = 0;for (const term of instructions) {if (term.type === 'A') {- reversed.unshift(term.sweepFlag ? '0' : '1');- reversed.unshift(term.largeArcFlag ? '1' : '0');- reversed.unshift(term.xAxisRotation);- reversed.unshift(term.ry);- reversed.unshift(term.rx);+ reversed.unshift({+ type: 'A',+ largeArcFlag: term.largeArcFlag,+ rx: term.rx,+ ry: term.ry,+ xAxisRotation: term.xAxisRotation,+ sweepFlag: !term.sweepFlag,+ x: nextX,+ y: nextY,+ });} else if (term.type === 'C') {- reversed.unshift(term.cp1y);- reversed.unshift(term.cp1x);- reversed.unshift(term.cp2y);- reversed.unshift(term.cp2x);+ reversed.unshift({+ type: 'C',+ cp1x: term.cp2x,+ cp1y: term.cp2y,+ cp2x: term.cp1x,+ cp2y: term.cp1y,+ x: nextX,+ y: nextY,+ });} else if (term.type === 'Q') {- reversed.unshift(term.cpy);- reversed.unshift(term.cpx);+ reversed.unshift({+ type: 'Q',+ cpx: term.cpx,+ cpy: term.cpy,+ x: nextX,+ y: nextY,+ });} else if (term.type === 'L') {+ reversed.unshift({+ type: 'L',+ x: nextX,+ y: nextY,+ });// Do nothing} else if (term.type === 'M') {// Do nothing@@ -37,19 +61,17 @@ function reverseNormalizedPath(instructions: Instruction[]) {throw new Error('unnormalized instruction ' + term.type);}- reversed.unshift(term.type);- reversed.unshift(term.y);- reversed.unshift(term.x);+ nextX = term.x;+ nextY = term.y;}- reversed.unshift('M');+ reversed.unshift({+ type: 'M',+ x: nextX,+ y: nextY,+ });- // generating the reversed path string involves- // running through our transformed terms in reverse.- let revstring = '';- for (let r = 0; r < reversed.length - 1; r++) {- revstring += reversed[r] + ' ';- }+ let revstring = serializeInstructions(reversed);if (instructions[instructions.length - 1].type === 'Z') revstring += 'Z';revstring = revstring.replace(/M M/g, 'Z M');diff --git a/packages/paths/src/test/reverse-path.test.ts b/packages/paths/src/test/reverse-path.test.tsindex 5d01d3e330..594fece34f 100644--- a/packages/paths/src/test/reverse-path.test.ts+++ b/packages/paths/src/test/reverse-path.test.ts@@ -17,3 +17,12 @@ test('Should not crash out', () => {'M 1047.91 508 L 1047.91 406.455 C 1047.91 379.142 1025.77 357 998.455 357 L 998.455 357 C 971.142 357 949 379.142 949 406.455 L 949 508');});++test('Should be able to reverse a path with 2 M statements', () => {+ const reversedPath = reversePath(`+ M 0 0 L 100 0+ M 0 0 L 200 100++ `);+ expect(reversedPath).toEqual('M 100 0 L 0 0 M 200 100 L 0 0');+});