3
3
var Immutable = require ( 'immutable' ) ;
4
4
var utils = require ( './utils' ) ;
5
5
var lcs = require ( './lcs' ) ;
6
- var path = require ( './path' ) ;
7
- var concatPath = path . concat ,
8
- escape = path . escape ,
9
- op = utils . op ,
10
- isMap = utils . isMap ,
11
- isIndexed = utils . isIndexed ;
6
+ var op = utils . op ,
7
+ isMap = utils . isMap ,
8
+ isIndexed = utils . isIndexed ;
12
9
13
10
var mapDiff = function ( a , b , p ) {
14
11
var ops = [ ] ;
15
- var path = p || '' ;
12
+ var path = p || [ ] ;
16
13
17
14
if ( Immutable . is ( a , b ) || ( a == b == null ) ) { return ops ; }
18
15
19
16
if ( a . forEach ) {
20
17
a . forEach ( function ( aValue , aKey ) {
21
18
if ( b . has ( aKey ) ) {
22
19
if ( isMap ( aValue ) && isMap ( b . get ( aKey ) ) ) {
23
- ops = ops . concat ( mapDiff ( aValue , b . get ( aKey ) , concatPath ( path , escape ( aKey ) ) ) ) ;
20
+ ops = ops . concat ( mapDiff ( aValue , b . get ( aKey ) , path . concat ( aKey ) ) ) ;
24
21
}
25
22
else if ( isIndexed ( b . get ( aKey ) ) && isIndexed ( aValue ) ) {
26
- ops = ops . concat ( sequenceDiff ( aValue , b . get ( aKey ) , concatPath ( path , escape ( aKey ) ) ) ) ;
23
+ ops = ops . concat ( sequenceDiff ( aValue , b . get ( aKey ) , path . concat ( aKey ) ) ) ;
27
24
}
28
25
else {
29
26
var bValue = b . get ? b . get ( aKey ) : b ;
30
27
var areDifferentValues = ( aValue !== bValue ) ;
31
28
if ( areDifferentValues ) {
32
- ops . push ( op ( 'replace' , concatPath ( path , escape ( aKey ) ) , bValue ) ) ;
29
+ ops . push ( op ( 'replace' , path . concat ( aKey ) , bValue ) ) ;
33
30
}
34
31
}
35
32
}
36
33
else {
37
- ops . push ( op ( 'remove' , concatPath ( path , escape ( aKey ) ) ) ) ;
34
+ ops . push ( op ( 'remove' , path . concat ( aKey ) ) ) ;
38
35
}
39
36
} ) ;
40
37
}
41
38
42
39
b . forEach ( function ( bValue , bKey ) {
43
40
if ( a . has && ! a . has ( bKey ) ) {
44
- ops . push ( op ( 'add' , concatPath ( path , escape ( bKey ) ) , bValue ) ) ;
41
+ ops . push ( op ( 'add' , path . concat ( bKey ) , bValue ) ) ;
45
42
}
46
43
} ) ;
47
44
@@ -62,19 +59,19 @@ var sequenceDiff = function (a, b, p) {
62
59
if ( diff . op === '=' ) { pathIndex ++ ; }
63
60
else if ( diff . op === '!=' ) {
64
61
if ( isMap ( diff . val ) && isMap ( diff . newVal ) ) {
65
- var mapDiffs = mapDiff ( diff . val , diff . newVal , concatPath ( path , pathIndex ) ) ;
62
+ var mapDiffs = mapDiff ( diff . val , diff . newVal , path . concat ( pathIndex ) ) ;
66
63
ops = ops . concat ( mapDiffs ) ;
67
64
}
68
65
else {
69
- ops . push ( op ( 'replace' , concatPath ( path , pathIndex ) , diff . newVal ) ) ;
66
+ ops . push ( op ( 'replace' , path . concat ( pathIndex ) , diff . newVal ) ) ;
70
67
}
71
68
pathIndex ++ ;
72
69
}
73
70
else if ( diff . op === '+' ) {
74
- ops . push ( op ( 'add' , concatPath ( path , pathIndex ) , diff . val ) ) ;
71
+ ops . push ( op ( 'add' , path . concat ( pathIndex ) , diff . val ) ) ;
75
72
pathIndex ++ ;
76
73
}
77
- else if ( diff . op === '-' ) { ops . push ( op ( 'remove' , concatPath ( path , pathIndex ) ) ) ; }
74
+ else if ( diff . op === '-' ) { ops . push ( op ( 'remove' , path . concat ( pathIndex ) ) ) ; }
78
75
} ) ;
79
76
80
77
return ops ;
@@ -84,7 +81,7 @@ var primitiveTypeDiff = function (a, b, p) {
84
81
var path = p || '' ;
85
82
if ( a === b ) { return [ ] ; }
86
83
else {
87
- return [ op ( 'replace' , concatPath ( path , '' ) , b ) ] ;
84
+ return [ op ( 'replace' , path . concat ( '' ) , b ) ] ;
88
85
}
89
86
} ;
90
87
@@ -101,4 +98,4 @@ var diff = function(a, b, p){
101
98
}
102
99
} ;
103
100
104
- module . exports = diff ;
101
+ module . exports = diff ;
0 commit comments