From 7bdde08f29311a7949da8f7220efc2a6aa32476e Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Tue, 24 Jul 2018 16:52:00 +0300 Subject: [PATCH] added ANY support + some experimenting... Signed-off-by: Alex A. Naanou --- diff.js | 79 +++++++++++++++++++++++++++++++++++----------------- package.json | 34 ++++++++++++++++++++++ 2 files changed, 87 insertions(+), 26 deletions(-) create mode 100644 package.json diff --git a/diff.js b/diff.js index 4ed5dea..33a2952 100644 --- a/diff.js +++ b/diff.js @@ -7,7 +7,7 @@ (function(require){ var module={} // make module AMD/node compatible... /*********************************************************************/ - +var object = require('ig-object') @@ -335,25 +335,6 @@ var proxy = function(path, func){ // like the type information which is not needed for patching but // may be useful for a more thorough compatibility check. // -// XXX should we change this to a different API? -// ...the only "bad" thing I can think of is the dependency on object.js -// Example: -// var d = new Diff(A, B) -// d.patch(X) -// d.undo(X) // same as: d.reverse().patch(X) -// d.json() -// ... -// -// API (class): -// .cmp(A, B) -// .diff(A, B) -// ... -// API (instance): -// .patch(X) -// .check(X) -// .json() -// .load(json) // same as: new Diff(json) -// ... var Types = { __cache: null, @@ -379,10 +360,12 @@ var Types = { // positions nor does it affect the the array lengths. // NOTE: these are compared by identity while diffing but are compared // by value when patching... + ANY: ANY, NONE: NONE, EMPTY: EMPTY, get DIFF_TYPES(){ return new Set([ + this.ANY, this.NONE, this.EMPTY, ]) }, @@ -601,17 +584,22 @@ var Types = { diff: function(A, B, options, cache){ var that = this options = options ? Object.create(options) : {} - options.cmp = options.cmp || function(a, b){ - return a === b - || a == b - // NOTE: diff(..) is in closure, see cache setup below... - || (diff(a, b) == null) } + options.cmp = options.cmp + || function(a, b){ + return a === that.ANY + || b === that.ANY + || a === b + || a == b + // NOTE: diff(..) is in closure, so we do not need to + // pass options and cache down. + // see cache setup below... + || (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 === B || A == B){ + if(A === that.ANY || B === that.ANY || A === B || A == B){ return null } @@ -697,6 +685,9 @@ var Types = { // XXX should we do this or reverse patch / undo-patch??? reverse: function(diff){ // XXX + //this.walk(diff, function(change){ + // // XXX + //}) }, // Check if diff is applicable to obj... @@ -1277,6 +1268,42 @@ function(diff, obj, options, types){ +//--------------------------------------------------------------------- + +var DiffClassPrototype = { + cmp: function(A, B){ + }, + fromJSON: function(json){ + }, +} + +var DiffPrototype = { + + __init__: function(A, B, options){ + }, + + reverse: function(obj){ + }, + + // XXX need to be able to check the other side... + check: function(obj){ + }, + patch: function(obj){ + }, + unpatch: function(obj){ + return this.reverse().patch(obj) }, + + json: function(){ + }, +} + +var Diff = +module.Diff = +object.makeConstructor('Diff', + DiffClassPrototype, + DiffPrototype) + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..b84ffcc --- /dev/null +++ b/package.json @@ -0,0 +1,34 @@ +{ + "name": "object-diff", + "version": "0.0.1", + "description": "", + "main": "diff.js", + "author": { + "name": "Alex A. Naanou", + "email": "alex.nanou@gmail.com", + "url": "https://github.com/flynx" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/flynx/object-diff.js.git" + }, + "keywords": [ + "diff", + "utility", + "object", + "array", + "json" + ], + "author": "Alex A. Naanou (https://github.com/flynx)", + "license": "BSD-3-Clause", + "bugs": { + "url": "https://github.com/flynx/object-diff.js/issues" + }, + "homepage": "https://github.com/flynx/object-diff.js#readme", + "dependencies": { + "ig-object": "^1.0.2" + } +}