Comparing version 0.2.5 to 0.2.6
54
index.js
@@ -76,3 +76,4 @@ function head (a) { | ||
var args = getArgs(arguments) | ||
var a = args[0], b = args[1] | ||
function key (a,b){ | ||
@@ -82,2 +83,31 @@ return a.length + ':' + b.length | ||
//find length that matches at the head | ||
if(args.length > 2) { | ||
//if called with multiple sequences | ||
//recurse, since lcs(a, b, c, d) == lcs(lcs(a,b), lcs(c,d)) | ||
args.push(lcs(args.shift(), args.shift())) | ||
return lcs(args) | ||
} | ||
//this would be improved by truncating input first | ||
//and not returning an lcs as an intermediate step. | ||
//untill that is a performance problem. | ||
var start = 0, end = 0 | ||
for(var i = 0; i < a.length && i < b.length | ||
&& equal(a[i], b[i]) | ||
; i ++ | ||
) | ||
start = i + 1 | ||
if(a.length === start) | ||
return a.slice() | ||
for(var i = 0; i < a.length && i < b.length | ||
&& equal(a[a.length - 1 - i], b[b.length - 1 - i]) | ||
; i ++ | ||
) | ||
end = i | ||
function recurse (a, b) { | ||
@@ -96,10 +126,11 @@ if(!a.length || !b.length) return [] | ||
} | ||
var middleA = a.slice(start, a.length - end) | ||
var middleB = b.slice(start, b.length - end) | ||
if(args.length > 2) { | ||
//if called with multiple sequences | ||
//recurse, since lcs(a, b, c, d) == lcs(lcs(a,b), lcs(c,d)) | ||
args.push(lcs(args.shift(), args.shift())) | ||
return lcs(args) | ||
} | ||
return recurse(args[0], args[1]) | ||
return ( | ||
a.slice(0, start).concat( | ||
recurse(middleA, middleB) | ||
).concat(a.slice(a.length - end)) | ||
) | ||
} | ||
@@ -121,2 +152,9 @@ | ||
function reverseEach(a, it) { | ||
var l = a.length | ||
while(l--) { | ||
it(a[l], l, a) | ||
} | ||
} | ||
while(any(q, hasLength)) { | ||
@@ -123,0 +161,0 @@ //if each element is at the lcs then this chunk is stable. |
@@ -5,3 +5,3 @@ { | ||
"description": "diff and patch arrays.", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"homepage": "https://github.com/dominictarr/adiff", | ||
@@ -14,10 +14,8 @@ "repository": { | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "set -e; for t in test/*.js; do node $t; done" | ||
}, | ||
"engines": { | ||
"node": "*" | ||
"devDependencies": { | ||
"tape": "~0.1.0" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"optionalDependencies": {} | ||
} |
@@ -0,6 +1,4 @@ | ||
var tape = require('tape') | ||
var d = require('../') | ||
if(!module.parent) { | ||
var assert = require('assert') | ||
function split(a) { | ||
@@ -12,33 +10,11 @@ if('string' === typeof a) | ||
function test (a, b, lcs) { | ||
a = split(a) | ||
b = split(b) | ||
lcs = split(lcs) | ||
var _lcs = d.lcs(a, b) | ||
d.chunk([a, b], console.log) | ||
assert.deepEqual(_lcs, lcs) | ||
var changes = d.diff(a,b) | ||
var newA = d.patch(a, changes) | ||
assert.deepEqual(newA, b) | ||
} | ||
test('AA', 'AA', 'AA') | ||
test('AB', 'BA', 'A') | ||
test('ABA', 'BAA', 'AA') | ||
test('TANYANA', 'BANANA', 'ANANA') | ||
// the naive model takes 2.5 seconds to find this: | ||
// time to optimise... | ||
test('aoenuthooao', 'eukmcybkraoaeuo', 'aoeuo') | ||
test('aoenuthooaeuoao', 'eukipoimcybkraoaeuo', 'euooaeuo') | ||
// added caching... now it's way faster. | ||
function test3way(args, expected) { | ||
args = args.map(split) | ||
tape('diff3 '+args.join(' ')+' === '+expected, function (assert) { | ||
args = args.map(split) | ||
console.log('----- TEST', args) | ||
console.log('***********') | ||
var p = d.diff3.apply(null, args) | ||
var r = d.patch(args[0], p) | ||
assert.deepEqual(r, split(expected)) | ||
assert.end() | ||
}) | ||
} | ||
@@ -70,2 +46,3 @@ | ||
function assertDiffPatch (a, b) { | ||
tape('assertDiffPatch', function (assert) { | ||
a = split(a) | ||
@@ -77,2 +54,5 @@ b = split(b) | ||
assert.deepEqual(b, patched) | ||
assert.end() | ||
}) | ||
} | ||
@@ -90,10 +70,1 @@ | ||
assertDiffPatch([ | ||
{a: true}, | ||
{b: false} | ||
], [ | ||
{c: 6} | ||
]) | ||
} |
@@ -0,4 +1,3 @@ | ||
require('tape')('tree-test', function (assert) { | ||
var assert = require('assert') | ||
function listify(tree){ | ||
@@ -62,1 +61,4 @@ var a = [] | ||
assert.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
43873
13
407
0
1