From 772b2c8ffb4e0a9741ede9f7fdba2921ad676bb2 Mon Sep 17 00:00:00 2001 From: Dusan Maliarik Date: Sat, 17 Jan 2015 15:42:04 +0100 Subject: [PATCH 1/2] Converting path to List type (fixes #3) --- src/diff.js | 33 +++++++++++++++------------------ src/path.js | 33 --------------------------------- 2 files changed, 15 insertions(+), 51 deletions(-) delete mode 100644 src/path.js diff --git a/src/diff.js b/src/diff.js index 4b412bc..e654e6d 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,28 @@ 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))) ); + 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) ); } }); @@ -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; @@ -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) ]; } }; @@ -101,4 +98,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 From 624cbedf8cc194939ae2f152829044fdf16c99a6 Mon Sep 17 00:00:00 2001 From: Dusan Maliarik Date: Wed, 21 Jan 2015 13:33:29 +0100 Subject: [PATCH 2/2] Values in remove op --- src/diff.js | 5 +++-- src/utils.js | 4 +--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/diff.js b/src/diff.js index e654e6d..99e4684 100644 --- a/src/diff.js +++ b/src/diff.js @@ -31,7 +31,8 @@ var mapDiff = function(a, b, p){ } } else { - ops.push( op('remove', path.concat(aKey)) ); + var aValue = a.get ? a.get(aKey) : a; + ops.push( op('remove', path.concat(aKey), aValue) ); } }); } @@ -71,7 +72,7 @@ var sequenceDiff = function (a, b, p) { ops.push(op('add', path.concat(pathIndex), diff.val)); pathIndex++; } - else if(diff.op === '-'){ ops.push(op('remove', path.concat(pathIndex))); } + else if(diff.op === '-'){ ops.push(op('remove', path.concat(pathIndex), diff.val)); } }); return ops; 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 +};