From da905844a37066b70ec231824dd55113a3d8fcd8 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 24 Sep 2017 09:12:38 -0400 Subject: [PATCH] Replace a map method with reduce in jest-diff (#4528) --- .../__tests__/__snapshots__/diff.test.js.snap | 14 ++++++------ packages/jest-diff/src/diff_strings.js | 22 ++++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap b/packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap index 7f1b56e8b384..9d8d7abf845d 100644 --- a/packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap +++ b/packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap @@ -71,7 +71,7 @@ exports[`collapses big diffs to patch format 1`] = ` + Received @@ -6,9 +6,9 @@ - 4, + 4, 5, 6, 7, @@ -104,7 +104,7 @@ exports[`context number of lines: -1 (5 default) 1`] = ` + Received @@ -6,9 +6,9 @@ - 4, + 4, 5, 6, 7, @@ -121,9 +121,9 @@ exports[`context number of lines: 0 1`] = ` + Received @@ -11,0 +11,1 @@ -+ 10, ++ 10, @@ -12,1 +13,0 @@ -- 10," +- 10," `; exports[`context number of lines: 1 1`] = ` @@ -131,7 +131,7 @@ exports[`context number of lines: 1 1`] = ` + Received @@ -10,4 +10,4 @@ - 8, + 8, + 10, 9, - 10, @@ -143,7 +143,7 @@ exports[`context number of lines: 2 1`] = ` + Received @@ -9,6 +9,6 @@ - 7, + 7, 8, + 10, 9, @@ -157,7 +157,7 @@ exports[`context number of lines: null (5 default) 1`] = ` + Received @@ -6,9 +6,9 @@ - 4, + 4, 5, 6, 7, diff --git a/packages/jest-diff/src/diff_strings.js b/packages/jest-diff/src/diff_strings.js index eb83731b6b2d..b32df0b5b69b 100644 --- a/packages/jest-diff/src/diff_strings.js +++ b/packages/jest-diff/src/diff_strings.js @@ -189,7 +189,7 @@ const shouldShowPatchMarks = (hunk: Hunk, oldLinesCount: number): boolean => const createPatchMark = (hunk: Hunk): string => { const markOld = `-${hunk.oldStart},${hunk.oldLines}`; const markNew = `+${hunk.newStart},${hunk.newLines}`; - return chalk.yellow(`@@ ${markOld} ${markNew} @@\n`); + return chalk.yellow(`@@ ${markOld} ${markNew} @@`); }; // Given original lines, return callback function which given indexes for hunk, @@ -232,18 +232,20 @@ const formatHunks = ( const getter = original && getterForHunks(original); const oldLinesCount = (a.match(/\n/g) || []).length; return hunks - .map((hunk: Hunk) => { + .reduce((lines, hunk: Hunk) => { + if (shouldShowPatchMarks(hunk, oldLinesCount)) { + lines.push(createPatchMark(hunk)); + } + // Hunk properties are one-based but index args are zero-based. const getOriginal = getter && getter(hunk.oldStart - 1, hunk.newStart - 1); - const lines = hunk.lines - .map(line => formatLine(line[0], line.slice(1), getOriginal)) - .join('\n'); - - return shouldShowPatchMarks(hunk, oldLinesCount) - ? createPatchMark(hunk) + lines - : lines; - }) + hunk.lines.forEach(line => { + lines.push(formatLine(line[0], line.slice(1), getOriginal)); + }); + + return lines; + }, []) .join('\n'); };