From a4a525b84a20dcd64d71ab6a746ab11bd97c0194 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 5 Sep 2018 02:58:15 +0300 Subject: [PATCH] lots of fixes and tweaks... Signed-off-by: Alex A. Naanou --- diff.js | 43 ++++++++++++++++++++++++++++++++++++++++--- format.js | 18 +++++++++--------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/diff.js b/diff.js index 70a13df..01ba54f 100644 --- a/diff.js +++ b/diff.js @@ -297,20 +297,37 @@ var LogicTypePrototype = { __cmp__: function(obj, cmp, context){ return false }, + // + // Deep compare this to obj (use Diff.cmp(..))... + // .cmp(obj) + // -> bool + // + // Deep compare this to obj in context (use Diff.cmp(..))... + // .cmp(obj, context) + // -> bool + // + // Compare this to obj using comparator cmp and an optional context context... + // .cmp(obj, cmp) + // .cmp(obj, cmp, context) + // -> bool + // // XXX need to track loops... + // XXX HACK???: this uses Diff.cmp(..) in simple cases... cmp: function(obj, cmp, context){ // XXX HACK??? - if(arguments.length < 3){ + if(arguments.length < 3 || !(cmp instanceof Function)){ return Diff.cmp( cmp instanceof Function ? this : this.context(cmp), obj) } + /* cmp = cmp || function(a, b){ return a === b //|| a == b || (a.__cmp__ && a.__cmp__(b, cmp, context)) || (b.__cmp__ && b.__cmp__(a, cmp, context)) } + //*/ context = context || this.context().__context__ // cache... @@ -365,6 +382,22 @@ module.ANY = function(){ return true })() +// Null type pattern... +// +// NULL +// -> pattern +// +// This matches null and undefined. +// +// NOTE: this will not match NaN (XXX revise) +var NULL = +module.NULL = + makeCIPattern('NULL', + function(obj){ + return obj === null + || obj === undefined })() + + // Bool pattern... // // BOOL @@ -547,6 +580,7 @@ object.makeConstructor('AND', Object.assign(new LogicType(), { }, })) + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // XXX BUG: // CONTEXT([ANY, ANY, ANY]).cmp([1, 2, 3]) @@ -602,6 +636,7 @@ object.makeConstructor('LIKE', Object.assign(new VAR(), { obj) }, })) + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // TEST(func) == L iff func(L) is true. var TEST = @@ -614,6 +649,7 @@ object.makeConstructor('TEST', Object.assign(new VAR(), { } })) + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // IN(A) == L iff A in L // @@ -1266,8 +1302,9 @@ module.Types = { // basic compare... // XXX do we need to differentiate things like: new Number(123) vs. 123??? var bcmp = function(a, b, cmp){ + // NOTE: we can't use a == b directly because of things like + // [2] == 2 -> true... return a === b - //|| a == b // basic patters... || a === that.ANY || b === that.ANY @@ -1677,7 +1714,7 @@ Types.set('Basic', { no_attributes: true, compatible: function(obj, options){ - return typeof(obj) != 'object' }, + return obj === null || typeof(obj) != 'object' }, handle: function(obj, diff, A, B, options){ ;(!options.keep_none && A === NONE) || (obj.A = A) diff --git a/format.js b/format.js index d7c3811..fb7456d 100644 --- a/format.js +++ b/format.js @@ -13,7 +13,7 @@ var diff = require('./diff') var { ANY, - BOOL, NUMBER, STRING, ARRAY, FUNCTION, + NULL, BOOL, NUMBER, STRING, ARRAY, FUNCTION, OR, AND, NOT, AT, OF, IN, VAR, LIKE, TEST, @@ -144,14 +144,14 @@ module.DIFF_OBJECT = AND( // instance metadata... AT('options', AND( - AT('tree_diff', OR(BOOL, null)), - AT('keep_none', OR(BOOL, null)), - AT('min_text_length', OR(NUMBER, null)), - AT('no_attributes', OR(BOOL, null)), - AT('NONE', OR(ANY, null)), - AT('EMPTY', OR(ANY, null)), - AT('no_length', OR(BOOL, null)), - AT('cmp', OR(FUNCTION, null)) )), + AT('tree_diff', OR(BOOL, NULL)), + AT('keep_none', OR(BOOL, NULL)), + AT('min_text_length', OR(NUMBER, NULL)), + AT('no_attributes', OR(BOOL, NULL)), + AT('NONE', OR(ANY, NULL)), + AT('EMPTY', OR(ANY, NULL)), + AT('no_length', OR(BOOL, NULL)), + AT('cmp', OR(FUNCTION, NULL)) )), AT('placeholders', AND( // XXX would be nice to store these and to use them to test // deeper stuff (i.e. VALUE)...