mirror of
https://github.com/flynx/diff.js.git
synced 2025-10-29 11:00:12 +00:00
edge case handling notes and cleanup...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
9df2108cb8
commit
f515995db3
48
diff.js
48
diff.js
@ -217,21 +217,30 @@ var getDiffSections = function(A, B, cmp, min_chunk){
|
|||||||
var partHandlers = {
|
var partHandlers = {
|
||||||
// XXX might be good to consider item ordering
|
// XXX might be good to consider item ordering
|
||||||
// ...i.e. how an item's indes changed
|
// ...i.e. how an item's indes changed
|
||||||
// XXX handle sparse arrays correctly...
|
// XXX might be good to support section splicing...
|
||||||
// ...now empty slots get filled with undefined...
|
|
||||||
items: function(diff, A, B, options){
|
items: function(diff, A, B, options){
|
||||||
return getDiffSections(A, B, options.cmp)
|
var sections = getDiffSections(A, B, options.cmp)
|
||||||
|
|
||||||
|
// special case: last section set consists of sparse/empty arrays...
|
||||||
|
var last = sections[sections.length-1]
|
||||||
|
last
|
||||||
|
&& last[0][1]
|
||||||
|
.concat(last[1][1])
|
||||||
|
.filter(function(e){ return e }).length == 0
|
||||||
|
&& sections.pop()
|
||||||
|
|
||||||
|
return sections
|
||||||
.map(function(gap){
|
.map(function(gap){
|
||||||
var i = gap[0][0]
|
var i = gap[0][0]
|
||||||
var j = gap[1][0]
|
var j = gap[1][0]
|
||||||
var a = gap[0][1]
|
var a = gap[0][1]
|
||||||
var b = gap[1][1]
|
var b = gap[1][1]
|
||||||
|
|
||||||
console.log('!!!!', a, b)
|
|
||||||
|
|
||||||
return zip(
|
return zip(
|
||||||
function(n, elems){
|
function(n, elems){
|
||||||
return [
|
return [
|
||||||
|
// if a slot exists it gets an index,
|
||||||
|
// otherwise null...
|
||||||
(0 in elems || n < a.length) ?
|
(0 in elems || n < a.length) ?
|
||||||
i+n
|
i+n
|
||||||
: null,
|
: null,
|
||||||
@ -239,6 +248,7 @@ var partHandlers = {
|
|||||||
j+n
|
j+n
|
||||||
: null,
|
: null,
|
||||||
diff(
|
diff(
|
||||||
|
// use value, EMPTY or NONE...
|
||||||
0 in elems ?
|
0 in elems ?
|
||||||
elems[0]
|
elems[0]
|
||||||
: n < a.length ?
|
: n < a.length ?
|
||||||
@ -250,10 +260,8 @@ var partHandlers = {
|
|||||||
EMPTY
|
EMPTY
|
||||||
: NONE,
|
: NONE,
|
||||||
options),
|
options),
|
||||||
]
|
] },
|
||||||
},
|
a, b)
|
||||||
a,
|
|
||||||
b)
|
|
||||||
.filter(function(e){
|
.filter(function(e){
|
||||||
return e[2] != null})
|
return e[2] != null})
|
||||||
})
|
})
|
||||||
@ -363,7 +371,18 @@ var partHandlers = {
|
|||||||
// item_order: <array-diff>,
|
// item_order: <array-diff>,
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
|
// - A and B are long strings...
|
||||||
|
// {
|
||||||
|
// type: 'Text',
|
||||||
//
|
//
|
||||||
|
// // same structure as for 'Array'...
|
||||||
|
// ...
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// XXX might be a good idea to add sub-section splicing, i.e. sub-arrays
|
||||||
|
// and not just rely on item-level...
|
||||||
var Types = new Map([
|
var Types = new Map([
|
||||||
['Basic',
|
['Basic',
|
||||||
function(diff, A, B, options){
|
function(diff, A, B, options){
|
||||||
@ -394,12 +413,17 @@ var Types = new Map([
|
|||||||
}],
|
}],
|
||||||
[Set, Map],
|
[Set, Map],
|
||||||
//*/
|
//*/
|
||||||
|
|
||||||
|
// XXX not used yet...
|
||||||
|
['Text',
|
||||||
|
function(diff, A, B, options){
|
||||||
|
return Types.handle(Array, this, A.split(/\n/), B.split(/\n/), options) }],
|
||||||
])
|
])
|
||||||
Types.handle = function(type, obj, ...args){
|
Types.handle = function(type, obj, ...args){
|
||||||
// set .type
|
// set .type
|
||||||
obj.type = type.name ? type.name : type
|
obj.type = obj.type || (type.name ? type.name : type)
|
||||||
|
|
||||||
// get the handler while handling aliases...
|
// get the handler + resolve aliases...
|
||||||
var handler = type
|
var handler = type
|
||||||
do {
|
do {
|
||||||
var handler = this.get(handler)
|
var handler = this.get(handler)
|
||||||
@ -420,6 +444,8 @@ Types.handle = function(type, obj, ...args){
|
|||||||
// NOTE: this will include direct links to items.
|
// NOTE: this will include direct links to items.
|
||||||
// NOTE: for format info see doc for Types...
|
// NOTE: for format info see doc for Types...
|
||||||
//
|
//
|
||||||
|
// XXX special case: empty sections do not need to be inserted...
|
||||||
|
//
|
||||||
// XXX do we need to differentiate things like: new Number(123) vs. 123???
|
// XXX do we need to differentiate things like: new Number(123) vs. 123???
|
||||||
// XXX check seen -- avoid recursion...
|
// XXX check seen -- avoid recursion...
|
||||||
// XXX support Map(..) and other new-style types...
|
// XXX support Map(..) and other new-style types...
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user