diff --git a/src/diff.js b/src/diff.js index 4b412bc..99e4684 100644 --- a/src/diff.js +++ b/src/diff.js @@ -3,16 +3,13 @@ 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; } @@ -20,28 +17,29 @@ var mapDiff = function(a, b, p){ 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))) ); + var aValue = a.get ? a.get(aKey) : a; + ops.push( op('remove', path.concat(aKey), aValue) ); } }); } 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) ); } }); @@ -62,19 +60,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), diff.val)); } }); return ops; @@ -84,7 +82,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) ]; } }; @@ -101,4 +99,4 @@ var diff = function(a, b, p){ } }; -module.exports = diff; \ No newline at end of file +module.exports = diff; diff --git a/src/path.js b/src/path.js deleted file mode 100644 index 0771078..0000000 --- a/src/path.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -var slashRe = new RegExp('/', 'g'); -var escapedSlashRe = new RegExp('~1', 'g'); -var tildeRe = /~/g; -var escapedTildeRe = /~0/g; - -var Path = { - escape: function (str) { - if(typeof(str) === 'number'){ - return str.toString(); - } - if(typeof(str) !== 'string'){ - throw 'param str (' + str + ') is not a string'; - } - - return str.replace(tildeRe, '~0').replace(slashRe, '~1'); - }, - - unescape: function (str) { - if(typeof(str) == 'string') { - return str.replace(escapedSlashRe, '/').replace(escapedTildeRe, '~'); - } - else { - return str; - } - }, - concat: function(path, key){ - return path + '/' + key; - } -}; - -module.exports = Path; \ No newline at end of file diff --git a/src/utils.js b/src/utils.js index 3011ebe..0d0dbce 100644 --- a/src/utils.js +++ b/src/utils.js @@ -6,8 +6,6 @@ var isMap = function(obj){ return Immutable.Iterable.isKeyed(obj); }; var isIndexed = function(obj) { return Immutable.Iterable.isIndexed(obj); }; var op = function(operation, path, value){ - if(operation === 'remove') { return { op: operation, path: path }; } - return { op: operation, path: path, value: value }; }; @@ -15,4 +13,4 @@ module.exports = { isMap: isMap, isIndexed: isIndexed, op: op -}; \ No newline at end of file +};