-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1449 from UziTech/use-htmldiffer
Update tests to node 4 syntax
- Loading branch information
Showing
5 changed files
with
151 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const HtmlDiffer = require('@markedjs/html-differ').HtmlDiffer; | ||
const htmlDiffer = new HtmlDiffer({ignoreSelfClosingSlash: true}); | ||
|
||
module.exports = { | ||
isEqual: htmlDiffer.isEqual.bind(htmlDiffer), | ||
firstDiff: (actual, expected, padding) => { | ||
padding = padding || 30; | ||
const result = htmlDiffer | ||
.diffHtml(actual, expected) | ||
.reduce((obj, diff) => { | ||
if (diff.added) { | ||
if (obj.firstIndex === null) { | ||
obj.firstIndex = obj.expected.length; | ||
} | ||
obj.expected += diff.value; | ||
} else if (diff.removed) { | ||
if (obj.firstIndex === null) { | ||
obj.firstIndex = obj.actual.length; | ||
} | ||
obj.actual += diff.value; | ||
} else { | ||
obj.actual += diff.value; | ||
obj.expected += diff.value; | ||
} | ||
|
||
return obj; | ||
}, { | ||
firstIndex: null, | ||
actual: '', | ||
expected: '' | ||
}); | ||
|
||
return { | ||
actual: result.actual.substring(result.firstIndex - padding, result.firstIndex + padding), | ||
expected: result.expected.substring(result.firstIndex - padding, result.firstIndex + padding) | ||
}; | ||
} | ||
}; |
Oops, something went wrong.