Skip to content

Commit

Permalink
Replace a map method with reduce in jest-diff (#4528)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrottimark authored and cpojer committed Sep 24, 2017
1 parent f92c3be commit da90584
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
14 changes: 7 additions & 7 deletions packages/jest-diff/src/__tests__/__snapshots__/diff.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ exports[`collapses big diffs to patch format 1`] = `
<red>+ Received</>
@@ -6,9 +6,9 @@</>
</><dim> 4,
<dim> 4,
<dim> 5,
<dim> 6,
<dim> 7,
Expand Down Expand Up @@ -104,7 +104,7 @@ exports[`context number of lines: -1 (5 default) 1`] = `
<red>+ Received</>
@@ -6,9 +6,9 @@</>
</><dim> 4,
<dim> 4,
<dim> 5,
<dim> 6,
<dim> 7,
Expand All @@ -121,17 +121,17 @@ exports[`context number of lines: 0 1`] = `
<red>+ Received</>
@@ -11,0 +11,1 @@</>
</><red>+ 10,</>
<red>+ 10,</>
@@ -12,1 +13,0 @@</>
</><green>- 10,</>"
<green>- 10,</>"
`;
exports[`context number of lines: 1 1`] = `
"<green>- Expected</>
<red>+ Received</>
@@ -10,4 +10,4 @@</>
</><dim> 8,
<dim> 8,
<red>+ 10,</>
<dim> 9,
<green>- 10,</>
Expand All @@ -143,7 +143,7 @@ exports[`context number of lines: 2 1`] = `
<red>+ Received</>
@@ -9,6 +9,6 @@</>
</><dim> 7,
<dim> 7,
<dim> 8,
<red>+ 10,</>
<dim> 9,
Expand All @@ -157,7 +157,7 @@ exports[`context number of lines: null (5 default) 1`] = `
<red>+ Received</>
@@ -6,9 +6,9 @@</>
</><dim> 4,
<dim> 4,
<dim> 5,
<dim> 6,
<dim> 7,
Expand Down
22 changes: 12 additions & 10 deletions packages/jest-diff/src/diff_strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
};

Expand Down

0 comments on commit da90584

Please sign in to comment.