From 04320742ad9421ca0057b57afb0532f6c891190f Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 2 Sep 2018 03:32:27 +0300 Subject: [PATCH] added experimental diff format pattern + minor fixes... Signed-off-by: Alex A. Naanou --- diff.js | 34 ++++++++++++++++++--- format.js | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 format.js diff --git a/diff.js b/diff.js index 8334dcb..4cf5d62 100644 --- a/diff.js +++ b/diff.js @@ -275,7 +275,8 @@ var LogicTypePrototype = { } var res = this.__cmp__(obj, cmp, cache) - || (obj.__cmp__ + || (obj != null + && obj.__cmp__ && obj.__cmp__(this, cmp, cache)) c.set(obj, res) @@ -307,12 +308,38 @@ var makeCIPattern = function(name, check, init){ // ANY // -> pattern // +// XXX AT('moo', ANY) matches L even if 'moo' in L is false... var ANY = module.ANY = makeCIPattern('ANY', function(){ return true })() +// Bool pattern... +// +// BOOL +// -> pattern +// +var BOOL = +module.BOOL = + makeCIPattern('BOOL', + function(obj){ + return obj === true || obj === false })() + + +// Function pattern... +// +// FUNCTION +// -> pattern +// +// XXX add signature checking... +var FUNCTION = +module.FUNCTION = + makeCIPattern('FUNCTION', + function(obj){ + return obj instanceof Function })() + + // String pattern... // // STRING @@ -408,7 +435,7 @@ module.ARRAY = || (obj instanceof Array // XXX make this fail on first fail -- currently // this runs every test on every elem... - && this.value.filter(function(value){ + && (this.value || []).filter(function(value){ return (typeof(value) == typeof(123) ? obj.length == value // function... @@ -418,7 +445,7 @@ module.ARRAY = : obj.filter(function(e){ return cmp(value, e) }).length == obj.length) - }).length == this.value.length) }, + }).length == (this.value || []).length) }, function(...value){ this.value = value }) @@ -873,7 +900,6 @@ module.Types = { var typeB = this.detect(B, undefined, options) // type match... - //if(type === typeB){ if(type === typeB && this.has(type)){ return type diff --git a/format.js b/format.js new file mode 100644 index 0000000..4943426 --- /dev/null +++ b/format.js @@ -0,0 +1,89 @@ +/********************************************************************** +* +* +* +**********************************************************************/ +((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) +(function(require){ var module={} // make module AMD/node compatible... +/*********************************************************************/ + +var diff = require('./diff') +var { + ANY, + BOOL, NUMBER, STRING, ARRAY, FUNCTION, + OR, AND, NOT, + AT, OF, IN, + + EMPTY, NONE, +} = diff + + + +/*********************************************************************/ +// XXX need better mismatch checking -- ideally stating the exact spot +// where we did not match... +// + +var VALUE = +module.VALUE = OR( + EMPTY, + NONE, + ANY) + + +var CHANGE = +module.CHANGE = AND( + AT('path', ARRAY), + // XXX optional... + // ...see DIFF_OBJECT's options for description... + AT('type', OR(STRING, undefined)), + OR( + // A ans B... + AND( + AT('A', VALUE), + AT('B', VALUE)), + // only A... + AT('A', VALUE), + // only B... + AT('B', VALUE))) + + +var DIFF_FORMAT_FLAT = +module.DIFF_FORMAT_FLAT = ARRAY(CHANGE) + + +// XXX +var DIFF_FORMAT_TREE = +module.DIFF_FORMAT_TREE = ANY + + +var DIFF_OBJECT = +module.DIFF_OBJECT = AND( + AT('format', diff.FORMAT_NAME), + //AT('version', STRING(/\d+\.\d+\.\d+/)), + AT('placeholders', AND( + // XXX must be unique ANY... + AT('NONE', ANY), + AT('EMPTY', ANY))), + 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('timestamp', NUMBER), + OR( + AND( + AT('structure', 'flat'), + AT('diff', DIFF_FORMAT_FLAT)), + AND( + AT('structure', 'tree'), + AT('diff', DIFF_FORMAT_TREE))) ) + + + +/********************************************************************** +* vim:set ts=4 sw=4 : */ return module })