Comparing version 0.1.5 to 0.1.6
@@ -1,10 +0,7 @@ | ||
/** | ||
* Imports | ||
*/ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.REMOVE = exports.MOVE = exports.UPDATE = exports.CREATE = undefined; | ||
@@ -17,3 +14,6 @@ var _bitVector = require('bit-vector'); | ||
var CREATE = 0; | ||
var CREATE = 0; /** | ||
* Imports | ||
*/ | ||
var UPDATE = 1; | ||
@@ -27,37 +27,12 @@ var MOVE = 2; | ||
function dift(prev, next, effect) { | ||
var key = arguments.length <= 3 || arguments[3] === undefined ? defaultKey : arguments[3]; | ||
var prevLen = prev.length; | ||
var nextLen = next.length; | ||
if (prevLen === 0) { | ||
if (nextLen === 0) return;else { | ||
// All creates | ||
for (var i = 0; i < nextLen; i++) { | ||
effect(CREATE, null, next[i], i); | ||
} | ||
return; | ||
} | ||
} else if (nextLen === 0) { | ||
// All removes | ||
for (var i = 0; i < prevLen; i++) { | ||
effect(REMOVE, prev[i], null, i); | ||
} | ||
return; | ||
} | ||
function dift(prev, next, effect, key) { | ||
var pStartIdx = 0; | ||
var pEndIdx = prevLen - 1; | ||
var nStartIdx = 0; | ||
var nEndIdx = nextLen - 1; | ||
var pEndIdx = prev.length - 1; | ||
var nEndIdx = next.length - 1; | ||
var pStartItem = prev[pStartIdx]; | ||
var pEndItem = prev[pEndIdx]; | ||
var nStartItem = next[nStartIdx]; | ||
var nEndItem = next[nEndIdx]; | ||
var created = 0; | ||
// List head is the same | ||
while (pStartIdx < prevLen && nStartIdx < nextLen && equal(pStartItem, nStartItem)) { | ||
while (pStartIdx <= pEndIdx && nStartIdx <= nEndIdx && equal(pStartItem, nStartItem)) { | ||
effect(UPDATE, pStartItem, nStartItem, nStartIdx); | ||
@@ -68,2 +43,10 @@ pStartItem = prev[++pStartIdx]; | ||
// The above case is orders of magnitude more common than the others, so fast-path it | ||
if (nStartIdx > nEndIdx && pStartIdx > pEndIdx) { | ||
return; | ||
} | ||
var pEndItem = prev[pEndIdx]; | ||
var nEndItem = next[nEndIdx]; | ||
// Reversed | ||
@@ -76,8 +59,2 @@ while (pStartIdx <= pEndIdx && nStartIdx <= nEndIdx && equal(pStartItem, nEndItem)) { | ||
// The above two cases each could have processed the entire list (whereas the next two | ||
// cannot if these didn't). So bail out early here if we can. | ||
if (nStartIdx === nextLen && pStartIdx === prevLen) { | ||
return; | ||
} | ||
// Reversed the other way (in case of e.g. reverse and append) | ||
@@ -115,15 +92,19 @@ while (pEndIdx >= pStartIdx && nStartIdx <= nEndIdx && equal(nStartItem, pEndItem)) { | ||
var prevMap = keyMap(prev, pStartIdx, pEndIdx + 1, key); | ||
var created = 0; | ||
var keepBase = pStartIdx; | ||
var keep = (0, _bitVector.createBv)(pEndIdx - pStartIdx); | ||
for (; nStartIdx <= nEndIdx; nStartItem = next[++nStartIdx]) { | ||
var oldIdx = prevMap[key(nStartItem)]; | ||
if (nStartIdx <= nEndIdx) { | ||
var prevMap = keyMap(prev, pStartIdx, pEndIdx + 1, key); | ||
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); | ||
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); | ||
} | ||
} | ||
@@ -137,3 +118,3 @@ } | ||
// removed that many, we can stop. | ||
var necessaryRemovals = prevLen - nextLen + created; | ||
var necessaryRemovals = prev.length - next.length + created; | ||
for (var removals = 0; removals < necessaryRemovals; pStartItem = prev[++pStartIdx]) { | ||
@@ -151,6 +132,2 @@ if (!(0, _bitVector.getBit)(keep, pStartIdx - keepBase)) { | ||
function defaultKey(a) { | ||
return a.key; | ||
} | ||
function isUndefined(val) { | ||
@@ -174,3 +151,3 @@ return typeof val === 'undefined'; | ||
exports['default'] = dift; | ||
exports.default = dift; | ||
exports.CREATE = CREATE; | ||
@@ -177,0 +154,0 @@ exports.UPDATE = UPDATE; |
@@ -5,3 +5,3 @@ { | ||
"repository": "git://github.com/ashaffer/dift.git", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"license": "MIT", | ||
@@ -17,4 +17,4 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"babel": "^5.8.21", | ||
"babel-tape-runner": "^1.2.0", | ||
"babel-tape-runner": "^1.3.0", | ||
"babelify": "^7.2.0", | ||
"standard": "^5.1.0", | ||
@@ -21,0 +21,0 @@ "tape": "^4.2.0" |
@@ -31,2 +31,6 @@ /** | ||
function key (a) { | ||
return a.key | ||
} | ||
function trial (a, b) { | ||
@@ -37,3 +41,3 @@ const deltas = [] | ||
const t = +new Date() | ||
diff(a, b, noop) | ||
diff(a, b, noop, key) | ||
deltas.push((+new Date) - t) | ||
@@ -40,0 +44,0 @@ } |
@@ -20,3 +20,3 @@ /** | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -34,3 +34,3 @@ t.deepEqual(c, b) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -52,3 +52,3 @@ t.deepEqual(c, b) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -70,3 +70,3 @@ t.deepEqual(c, b) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -84,3 +84,3 @@ t.deepEqual(c, b) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -98,3 +98,3 @@ t.deepEqual(c, b) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -112,3 +112,3 @@ t.deepEqual(c, b) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -126,3 +126,3 @@ t.deepEqual(c, b) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -141,3 +141,3 @@ t.deepEqual(c, b) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -159,3 +159,3 @@ t.deepEqual(c, b) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -177,3 +177,3 @@ t.deepEqual(c, b) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -194,3 +194,3 @@ t.deepEqual(c, b) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -208,3 +208,3 @@ t.deepEqual(c, b) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -227,3 +227,3 @@ t.deepEqual(c, b) | ||
patch(...args) | ||
}) | ||
}, key) | ||
@@ -243,3 +243,3 @@ t.deepEqual(log, [MOVE, MOVE, MOVE, MOVE]) | ||
diff(a, b, patch) | ||
diff(a, b, patch, key) | ||
@@ -251,3 +251,7 @@ t.deepEqual(c, b) | ||
function update(list) { | ||
function key (a) { | ||
return a.key | ||
} | ||
function update (list) { | ||
return function(type, prev, next, pos) { | ||
@@ -254,0 +258,0 @@ switch(type) { |
Sorry, the diff of this file is not supported yet
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
14352
413