Comparing version 0.1.6 to 0.1.7
@@ -48,8 +48,10 @@ 'use strict'; | ||
var nEndItem = next[nEndIdx]; | ||
var movedFromFront = 0; | ||
// Reversed | ||
while (pStartIdx <= pEndIdx && nStartIdx <= nEndIdx && equal(pStartItem, nEndItem)) { | ||
effect(MOVE, pStartItem, nEndItem, nEndIdx); | ||
effect(MOVE, pStartItem, nEndItem, pEndIdx - movedFromFront); | ||
pStartItem = prev[++pStartIdx]; | ||
nEndItem = next[--nEndIdx]; | ||
++movedFromFront; | ||
} | ||
@@ -62,2 +64,3 @@ | ||
nStartItem = next[++nStartIdx]; | ||
--movedFromFront; | ||
} | ||
@@ -91,21 +94,28 @@ | ||
var created = 0; | ||
var pivotDest = null; | ||
var pivotIdx = pStartIdx - movedFromFront; | ||
var keepBase = pStartIdx; | ||
var keep = (0, _bitVector.createBv)(pEndIdx - pStartIdx); | ||
if (nStartIdx <= nEndIdx) { | ||
var prevMap = keyMap(prev, pStartIdx, pEndIdx + 1, key); | ||
var prevMap = keyMap(prev, pStartIdx, pEndIdx + 1, key); | ||
for (; nStartIdx <= nEndIdx; nStartItem = next[++nStartIdx]) { | ||
var oldIdx = prevMap[key(nStartItem)]; | ||
for (; nStartIdx <= nEndIdx; nStartItem = next[++nStartIdx]) { | ||
var oldIdx = prevMap[key(nStartItem)]; | ||
if (isUndefined(oldIdx)) { | ||
effect(CREATE, null, nStartItem, nStartIdx); | ||
++created; | ||
} else { | ||
(0, _bitVector.setBit)(keep, oldIdx - keepBase); | ||
effect(oldIdx === nStartIdx ? UPDATE : MOVE, prev[oldIdx], nStartItem, nStartIdx); | ||
} | ||
if (isUndefined(oldIdx)) { | ||
effect(CREATE, null, nStartItem, pivotIdx++); | ||
++created; | ||
} else if (pStartIdx !== oldIdx) { | ||
(0, _bitVector.setBit)(keep, oldIdx - keepBase); | ||
effect(MOVE, prev[oldIdx], nStartItem, pivotIdx++); | ||
} else { | ||
pivotDest = nStartIdx; | ||
} | ||
} | ||
if (pivotDest !== null) { | ||
(0, _bitVector.setBit)(keep, 0); | ||
effect(MOVE, prev[pStartIdx], next[pivotDest], pivotDest); | ||
} | ||
// If there are no creations, then you have to | ||
@@ -112,0 +122,0 @@ // remove exactly max(prevLen - nextLen, 0) elements in this |
@@ -5,3 +5,3 @@ { | ||
"repository": "git://github.com/ashaffer/dift.git", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"license": "MIT", | ||
@@ -14,7 +14,11 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"array-permutation": "^0.2.0", | ||
"bit-vector": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
"babel-preset-es2015": "^6.1.2", | ||
"babel-tape-runner": "^1.3.0", | ||
"babelify": "^7.2.0", | ||
"permute": "^1.0.0", | ||
"powerset": "0.0.1", | ||
"standard": "^5.1.0", | ||
@@ -21,0 +25,0 @@ "tape": "^4.2.0" |
@@ -7,2 +7,4 @@ /** | ||
import diff, {CREATE, UPDATE, MOVE, REMOVE} from '../src' | ||
import powerset from 'powerset' | ||
import permutations from 'array-permutation' | ||
@@ -228,11 +230,102 @@ /** | ||
let patch = update(c) | ||
diff(a, b, update(c), key) | ||
diff(a, b, patch, key) | ||
t.deepEqual(c, b) | ||
t.end() | ||
}) | ||
test('insert (3), rearrange', t => { | ||
for (let i = 0; i < 1000; i++) { | ||
const a = range(0, 10) | ||
const b = randomize(range(0, 10).concat(range(11, 14))) | ||
const c = clone(a) | ||
diff(a, b, update(c), key) | ||
t.deepEqual(c, b) | ||
} | ||
t.end() | ||
}) | ||
test('remove (3), rearrange', t => { | ||
for (let i = 0; i < 1000; i++) { | ||
const a = range(0, 13) | ||
const b = randomize(range(0, 10)) | ||
const c = clone(a) | ||
diff(a, b, update(c), key) | ||
t.deepEqual(c, b) | ||
} | ||
t.end() | ||
}) | ||
test('remove (3), insert (3), rearrange', t => { | ||
for (let i = 0; i < 1000; i++) { | ||
const a = range(0, 13) | ||
const b = randomize(range(0, 10).concat(14, 17)) | ||
const c = clone(a) | ||
diff(a, b, update(c), key) | ||
t.deepEqual(c, b) | ||
} | ||
t.end() | ||
}) | ||
test('empty initial', t => { | ||
const a = [] | ||
const b = range(0, 10) | ||
const c = clone(a) | ||
diff(a, b, update(c), key) | ||
t.deepEqual(c, b) | ||
t.end() | ||
}) | ||
test('reversed sides, middle rearranged', t => { | ||
const a = range(0, 10) | ||
const b = [{key: 13}, {key: 3}, {key: 2}, {key: 9}, {key: 5}, {key: 8}, {key: 7}, {key: 12}, {key: 11}, {key: 6}, {key: 4}, {key: 1}, {key: 0}] | ||
const c = clone(a) | ||
diff(a, b, update(c), key) | ||
t.deepEqual(c, b) | ||
t.end() | ||
}) | ||
test('exhaustive - same items', t => { | ||
const a = range(0, 10) | ||
const ps = powerset(range(0, 8)) | ||
for (let i = 0; i < ps.length; i++) { | ||
const iter = permutations(ps[i]) | ||
for (let b of iter) { | ||
const c = clone(a) | ||
diff(a, b, update(c), key) | ||
t.deepEqual(c, b) | ||
} | ||
} | ||
t.end() | ||
}) | ||
test('exhaustive - mixed items', t => { | ||
const a = range(0, 10) | ||
const ps = powerset(range(7, 15)) | ||
for (let i = 0; i < ps.length; i++) { | ||
const iter = permutations(ps[i]) | ||
for (let b of iter) { | ||
const c = clone(a) | ||
diff(a, b, update(c), key) | ||
t.deepEqual(c, b) | ||
} | ||
} | ||
t.end() | ||
}) | ||
function key (a) { | ||
@@ -242,2 +335,24 @@ return a.key | ||
function randomize (list) { | ||
const newList = [] | ||
for (let i = 0, len = list.length; i < len; i++) { | ||
const j = Math.floor(Math.random() * 100000) % list.length | ||
newList.push(list[j]) | ||
list.splice(j, 1) | ||
} | ||
return newList | ||
} | ||
function range (begin, end) { | ||
const r = [] | ||
for (let i = begin; i < end; i++) { | ||
r.push({key: i}) | ||
} | ||
return r | ||
} | ||
function update (list) { | ||
@@ -244,0 +359,0 @@ return function(type, prev, next, pos) { |
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
16995
511
2
7
+ Addedarray-permutation@^0.2.0
+ Addedarray-permutation@0.2.0(transitive)