mirror of
https://github.com/flynx/diff.js.git
synced 2025-10-29 19:10:11 +00:00
now the logic types mostly work, still need more testing...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
31c96dd325
commit
ef4048497a
81
diff.js
81
diff.js
@ -228,17 +228,41 @@ var EMPTY = {type: 'EMPTY_PLACEHOLDER'}
|
|||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// Logic patterns...
|
// Logic patterns...
|
||||||
// XXX we should handle LogicType-LogicType comparisons in a graceful
|
|
||||||
// way...
|
|
||||||
// XXX should this also include the above placeholders, especially ANY???
|
// XXX should this also include the above placeholders, especially ANY???
|
||||||
|
// XXX need to avoid recursion...
|
||||||
|
|
||||||
var LogicTypeClassPrototype = {
|
var LogicTypeClassPrototype = {
|
||||||
}
|
}
|
||||||
|
|
||||||
var LogicTypePrototype = {
|
var LogicTypePrototype = {
|
||||||
cmp: function(obj, cmp){
|
__cmp__: function(obj, cmp){
|
||||||
return false
|
return false
|
||||||
},
|
},
|
||||||
|
// XXX need to track loops...
|
||||||
|
cmp: function(obj, cmp, cache){
|
||||||
|
cmp = cmp || function(a, b){
|
||||||
|
return a === b
|
||||||
|
|| a == b
|
||||||
|
|| (a.__cmp__ && a.__cmp__(b, cmp, cache))
|
||||||
|
|| (b.__cmp__ && b.__cmp__(a, cmp, cache)) }
|
||||||
|
|
||||||
|
// cache...
|
||||||
|
cache = cache || new Map()
|
||||||
|
var c = cache.get(this) || new Map()
|
||||||
|
cache.has(c)
|
||||||
|
|| cache.set(this, c)
|
||||||
|
if(c.has(obj)){
|
||||||
|
return c.get(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
var res = this.__cmp__(obj, cmp, cache)
|
||||||
|
|| (obj.__cmp__ ?
|
||||||
|
obj.__cmp__(this, cmp, cache)
|
||||||
|
: false)
|
||||||
|
c.set(obj, res)
|
||||||
|
|
||||||
|
return res
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var LogicType =
|
var LogicType =
|
||||||
@ -251,7 +275,7 @@ object.makeConstructor('LogicType',
|
|||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
// ANY...
|
// ANY...
|
||||||
var ANYPrototype = {
|
var ANYPrototype = {
|
||||||
cmp: function(obj, cmp){
|
__cmp__: function(obj, cmp){
|
||||||
return true },
|
return true },
|
||||||
}
|
}
|
||||||
ANYPrototype.__proto__ = LogicTypePrototype
|
ANYPrototype.__proto__ = LogicTypePrototype
|
||||||
@ -262,11 +286,8 @@ module.ANY = new (object.makeConstructor('ANY', ANYPrototype))()
|
|||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
// OR...
|
// OR...
|
||||||
// XXX does not work with diff at the moment...
|
|
||||||
var ORPrototype = {
|
var ORPrototype = {
|
||||||
cmp: function(obj, cmp){
|
__cmp__: function(obj, cmp){
|
||||||
cmp = cmp || function(a, b){
|
|
||||||
return a === b || a == b }
|
|
||||||
for(var m of this.members){
|
for(var m of this.members){
|
||||||
if(cmp(m, obj)){
|
if(cmp(m, obj)){
|
||||||
return true
|
return true
|
||||||
@ -288,11 +309,8 @@ object.makeConstructor('OR',
|
|||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
// AND...
|
// AND...
|
||||||
// XXX does not work with diff at the moment...
|
|
||||||
var ANDPrototype = {
|
var ANDPrototype = {
|
||||||
cmp: function(obj, cmp){
|
__cmp__: function(obj, cmp){
|
||||||
cmp = cmp || function(a, b){
|
|
||||||
return a === b || a == b }
|
|
||||||
for(var m of this.members){
|
for(var m of this.members){
|
||||||
if(!cmp(m, obj)){
|
if(!cmp(m, obj)){
|
||||||
return false
|
return false
|
||||||
@ -438,7 +456,8 @@ object.makeConstructor('AND',
|
|||||||
// may be useful for a more thorough compatibility check.
|
// may be useful for a more thorough compatibility check.
|
||||||
//
|
//
|
||||||
// XXX Q: do we need to support both the flat and tree diff formats???
|
// XXX Q: do we need to support both the flat and tree diff formats???
|
||||||
var Types = {
|
var Types =
|
||||||
|
module.Types = {
|
||||||
__cache: null,
|
__cache: null,
|
||||||
|
|
||||||
// Object-level utilities...
|
// Object-level utilities...
|
||||||
@ -714,8 +733,11 @@ var Types = {
|
|||||||
diff: function(A, B, options, cache){
|
diff: function(A, B, options, cache){
|
||||||
var that = this
|
var that = this
|
||||||
options = options ? Object.create(options) : {}
|
options = options ? Object.create(options) : {}
|
||||||
var cmp = options.cmp = options.cmp
|
options.as_object = options.as_object || []
|
||||||
|| function(a, b){
|
|
||||||
|
// basic compare...
|
||||||
|
// XXX do we need to differentiate things like: new Number(123) vs. 123???
|
||||||
|
var bcmp = function(a, b, cmp){
|
||||||
return a === b
|
return a === b
|
||||||
|| a == b
|
|| a == b
|
||||||
// basic patters...
|
// basic patters...
|
||||||
@ -727,25 +749,16 @@ var Types = {
|
|||||||
&& a.cmp(b, cmp))
|
&& a.cmp(b, cmp))
|
||||||
|| (b instanceof LogicType
|
|| (b instanceof LogicType
|
||||||
&& b.cmp(a, cmp))
|
&& b.cmp(a, cmp))
|
||||||
|
}
|
||||||
|
// deep compare...
|
||||||
|
var cmp = options.cmp = options.cmp
|
||||||
|
|| function(a, b){
|
||||||
|
return bcmp(a, b, cmp)
|
||||||
// diff...
|
// diff...
|
||||||
// NOTE: diff(..) is in closure, so we do not need to
|
// NOTE: diff(..) is in closure, so we do not need to
|
||||||
// pass options and cache down.
|
// pass options and cache down.
|
||||||
// see cache setup below...
|
// see cache setup below...
|
||||||
|| (diff(a, b) == null) }
|
|| (diff(a, b) == null) }
|
||||||
options.as_object = options.as_object || []
|
|
||||||
|
|
||||||
|
|
||||||
// same object...
|
|
||||||
// XXX do we need to differentiate things like: new Number(123) vs. 123???
|
|
||||||
if(A === that.ANY || B === that.ANY || A === B || A == B){
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
// builtin types...
|
|
||||||
if(this.DIFF_TYPES.has(A) || this.DIFF_TYPES.has(B)){
|
|
||||||
return this.handle('Basic', {}, diff, A, B, options)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// cache...
|
// cache...
|
||||||
cache = this.__cache = cache || this.__cache || new Map()
|
cache = this.__cache = cache || this.__cache || new Map()
|
||||||
@ -757,6 +770,16 @@ var Types = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// same object...
|
||||||
|
if(bcmp(A, B)){
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
// builtin types...
|
||||||
|
if(this.DIFF_TYPES.has(A) || this.DIFF_TYPES.has(B)){
|
||||||
|
return this.handle('Basic', {}, diff, A, B, options)
|
||||||
|
}
|
||||||
|
|
||||||
// find the matching type...
|
// find the matching type...
|
||||||
var type = this.detect(A, B, options)
|
var type = this.detect(A, B, options)
|
||||||
// handle type...
|
// handle type...
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user