patience-diff
Version:
Linear Diff algorithm for Arrays.
53 lines (44 loc) • 1.12 kB
JavaScript
var tape = require('tape')
var MyersDiff = require('./')
tape('INSERT at start', function (test) {
var differ = new MyersDiff()
compare(test, differ, {
current: [ 'a' ],
desired: [ 'b', 'a' ],
instructions: [ 2, 0, 0, 0 ]
})
test.end()
})
tape('INSERT at end', function (test) {
var differ = new MyersDiff()
compare(test, differ, {
current: [ 'a' ],
desired: [ 'a', 'b' ],
instructions: [ 2, 1, 1, 0 ]
})
test.end()
})
// Helper function to run a comparison.
function compare (t, differ, args) {
differ.diff(args.current, args.desired)
var instructions = args.instructions
var output = new Array(instructions.length)
output.fill('0x0000')
var i = 0
var el
while (true) {
el = differ.instructions[i]
output[i] = hex(el)
if (el === 0) break
i++
}
instructions.forEach(function (instruction, i) {
instructions[i] = hex(instruction || 0)
})
t.equal(output.toString(), instructions.toString(), 'result was same')
}
function hex (number) {
var str = (number).toString(16)
while (str.length < 4) str = '0' + str
return '0x' + str
}