forked from workshopper/javascripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompare-solution.js
More file actions
51 lines (41 loc) · 1.08 KB
/
Copy pathcompare-solution.js
File metadata and controls
51 lines (41 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* eslint-disable standard/no-callback-literal */
require('colors')
var path = require('path')
var diff = require('diff')
var run = require(path.join(__dirname, 'run-solution'))
module.exports = function (solution, attempt, i18n, cb) {
run(solution, i18n, function (err, solutionResult) {
if (err) {
console.error(err)
return cb(false)
}
run(attempt, i18n, function (err, attemptResult) {
if (err && err.code !== 8) {
console.error(err)
return cb(false)
}
if (solutionResult === attemptResult) {
return cb(true)
}
cb(false, {
solution: solutionResult,
attempt: err || attemptResult,
diff: generateDiff(solutionResult, attemptResult)
})
})
})
}
function generateDiff (solution, attempt) {
var parts = diff.diffChars(solution, attempt)
var result = ''
parts.forEach(function (part) {
if (part.added) {
result += part.value.bgRed
} else if (part.removed) {
result += part.value.bgGreen
} else {
result += part.value
}
})
return result
}