refactored type detection (seems a bit over complicated)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-07-17 16:00:31 +03:00
parent 6d17023cf7
commit f6d04cd54b

94
diff.js
View File

@ -416,30 +416,58 @@ var Types = Object.assign(
//*/ //*/
// Custom types... // Custom types...
['Text', ['Text',
function(diff, A, B, options){ {
return Types.handle(Array, this, A.split(/\n/), B.split(/\n/), options) }], check: function(A, B, options){
options = options || {}
min = options.min_text_length || 1000
return typeof(A) == 'string' && typeof(B) == 'string'
&& A.length > min && B.length > min
},
handle: function(diff, A, B, options){
return Types.handle(Array, this, A.split(/\n/), B.split(/\n/), options) },
}],
]), ]),
{ {
detect: function(A, B){ // NOTE: if A and B types mismatch we treat them as Object...
var type = Object // XXX this may have issues with type (key) ordering, for example
for(var t of Types.keys()){ // if Object is not last it will match any set of items...
// leave pure objects for last... // XXX add support for checker predicates...
if(t === Object // ...the main question here is how do we structure the predicate???
// skip non-conctructor stuff... // XXX should .handle(..)/.check(..) be coupled here or in _diff(..)???
|| !(t instanceof Function)){ detect: function(A, B, options){
continue var type
}
// full hit -- type match... // predicates have priority...
if(A instanceof t && B instanceof t){ for(var t of Types.keys()){
if(Types.get(t).check
&& Types.get(t).check(A, B, options)){
type = t type = t
break break
} }
// partial hit -- type mismatch... }
if(A instanceof t || B instanceof t){
type = 'Basic' // search instances...
break if(!type){
type = Object
for(var t of Types.keys()){
// leave pure objects for last...
if(t === Object
// skip non-conctructor stuff...
|| !(t instanceof Function)){
continue
}
// full hit -- type match...
if(A instanceof t && B instanceof t){
type = t
break
}
// partial hit -- type mismatch...
if(A instanceof t || B instanceof t){
type = 'Basic'
break
}
} }
} }
return type return type
@ -456,10 +484,12 @@ var Types = Object.assign(
if(handler == null){ if(handler == null){
throw new TypeError('Diff: can\'t handle: ' + type) throw new TypeError('Diff: can\'t handle: ' + type)
} }
} while(!(handler instanceof Function)) } while(!(handler instanceof Function) && !handler.handle)
// call the handler... // call the handler...
handler.call(obj, ...args) handler.handle ?
handle.handle.call(obj, ...args)
: handler.call(obj, ...args)
return obj return obj
} }
@ -515,30 +545,8 @@ function(A, B, options, cache){
// find the matching type... // find the matching type...
// NOTE: if A and B types mismatch we treat them as Object... var type = Types.detect(A, B, options)
// XXX this may have issues with type (key) ordering, for example
// if Object is not last it will match any set of items...
// XXX should type detection be here or in types?
var type = Object
for(var t of Types.keys()){
// leave pure objects for last...
if(t === Object
// skip non-conctructor stuff...
|| !(t instanceof Function)){
continue
}
// full hit -- type match...
if(A instanceof t && B instanceof t){
type = t
break
}
// partial hit -- type mismatch...
if(A instanceof t || B instanceof t){
type = 'Basic'
break
}
}
// handle type... // handle type...
var res = Types.handle(type, {}, diff, A, B, options) var res = Types.handle(type, {}, diff, A, B, options)
// handle things we treat as objects (skipping object itself)... // handle things we treat as objects (skipping object itself)...