Skip to content

Commit

Permalink
Converting path to List type (fixes intelie#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
skrat committed Jan 17, 2015
1 parent 61bae3b commit 772b2c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 51 deletions.
33 changes: 15 additions & 18 deletions src/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,42 @@
var Immutable = require('immutable');
var utils = require('./utils');
var lcs = require('./lcs');
var path = require('./path');
var concatPath = path.concat,
escape = path.escape,
op = utils.op,
isMap = utils.isMap,
isIndexed = utils.isIndexed;
var op = utils.op,
isMap = utils.isMap,
isIndexed = utils.isIndexed;

var mapDiff = function(a, b, p){
var ops = [];
var path = p || '';
var path = p || [];

if(Immutable.is(a, b) || (a == b == null)){ return ops; }

if(a.forEach){
a.forEach(function(aValue, aKey){
if(b.has(aKey)){
if(isMap(aValue) && isMap(b.get(aKey))){
ops = ops.concat(mapDiff(aValue, b.get(aKey), concatPath(path, escape(aKey))));
ops = ops.concat(mapDiff(aValue, b.get(aKey), path.concat(aKey)));
}
else if(isIndexed(b.get(aKey)) && isIndexed(aValue)){
ops = ops.concat(sequenceDiff(aValue, b.get(aKey), concatPath(path, escape(aKey))));
ops = ops.concat(sequenceDiff(aValue, b.get(aKey), path.concat(aKey)));
}
else {
var bValue = b.get ? b.get(aKey) : b;
var areDifferentValues = (aValue !== bValue);
if (areDifferentValues) {
ops.push(op('replace', concatPath(path, escape(aKey)), bValue));
ops.push(op('replace', path.concat(aKey), bValue));
}
}
}
else {
ops.push( op('remove', concatPath(path, escape(aKey))) );
ops.push( op('remove', path.concat(aKey)) );
}
});
}

b.forEach(function(bValue, bKey){
if(a.has && !a.has(bKey)){
ops.push( op('add', concatPath(path, escape(bKey)), bValue) );
ops.push( op('add', path.concat(bKey), bValue) );
}
});

Expand All @@ -62,19 +59,19 @@ var sequenceDiff = function (a, b, p) {
if(diff.op === '='){ pathIndex++; }
else if(diff.op === '!='){
if(isMap(diff.val) && isMap(diff.newVal)){
var mapDiffs = mapDiff(diff.val, diff.newVal, concatPath(path, pathIndex));
var mapDiffs = mapDiff(diff.val, diff.newVal, path.concat(pathIndex));
ops = ops.concat(mapDiffs);
}
else{
ops.push(op('replace', concatPath(path, pathIndex), diff.newVal));
ops.push(op('replace', path.concat(pathIndex), diff.newVal));
}
pathIndex++;
}
else if(diff.op === '+'){
ops.push(op('add', concatPath(path, pathIndex), diff.val));
ops.push(op('add', path.concat(pathIndex), diff.val));
pathIndex++;
}
else if(diff.op === '-'){ ops.push(op('remove', concatPath(path, pathIndex))); }
else if(diff.op === '-'){ ops.push(op('remove', path.concat(pathIndex))); }
});

return ops;
Expand All @@ -84,7 +81,7 @@ var primitiveTypeDiff = function (a, b, p) {
var path = p || '';
if(a === b){ return []; }
else{
return [ op('replace', concatPath(path, ''), b) ];
return [ op('replace', path.concat(''), b) ];
}
};

Expand All @@ -101,4 +98,4 @@ var diff = function(a, b, p){
}
};

module.exports = diff;
module.exports = diff;
33 changes: 0 additions & 33 deletions src/path.js

This file was deleted.

0 comments on commit 772b2c8

Please sign in to comment.