Skip to content

Commit 4b17e5d

Browse files
committed
Implement all features and tests
1 parent aa30aa3 commit 4b17e5d

File tree

9 files changed

+7243
-0
lines changed

9 files changed

+7243
-0
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: node_js
2+
node_js:
3+
- '6'
4+
- '7'
5+
- '8'
6+
- '9'
7+
- '10'

diff.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const diffNumbers = require('./src/diff-numbers.js')
2+
const diffLists = require('./src/diff-lists.js')
3+
4+
function diffStats(statsA, statsB) {
5+
// This assumes that the statsB is newer than statsA
6+
return Object.entries(statsB).reduce((diff, [key, value]) => {
7+
return diff.set(key, diffStat(statsA[key], value))
8+
}, new Map())
9+
}
10+
11+
function diffStat(statA, statB) {
12+
if (typeof statB === 'undefined') {
13+
statB = statA
14+
statA = undefined
15+
}
16+
17+
if (typeof statA === 'undefined') {
18+
statA = null
19+
}
20+
21+
// Abort if the types are not compatible
22+
// A must have the same type as B, or A must be null
23+
if (typeof statA !== typeof statB && statA !== null) {
24+
throw new Error(
25+
`Stat types do not match. ${typeof statA} is not compatible to compare with ${typeof statB}`
26+
)
27+
}
28+
29+
if (!Number.isNaN(statB) && Number.isFinite(statB)) {
30+
return diffNumbers(statA, statB)
31+
}
32+
33+
if (Array.isArray(statB)) {
34+
statA = statA === null ? [] : statA
35+
return diffLists(statA, statB)
36+
}
37+
38+
return null
39+
}
40+
41+
module.exports = diffStats

0 commit comments

Comments
 (0)