From 74cd12cd47afb27662b352a5e00ff672b4f57601 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 30 Jun 2021 10:53:38 +0300 Subject: [PATCH] refactoring... Signed-off-by: Alex A. Naanou --- diff2.js | 158 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 88 insertions(+), 70 deletions(-) diff --git a/diff2.js b/diff2.js index 3e14bec..fd0aef6 100644 --- a/diff2.js +++ b/diff2.js @@ -104,8 +104,36 @@ module.CONTENT = var Walk = module.Walk = object.Constructor('Walk', { + + // + // .handler(obj, path, next, type) + // -> + // -> [path, ...] + // + // XXX should there be a default here??? handler: undefined, + + // + // Format: + // { + // : , + // + // : { + // list: , + // ... + // }, + // + // ... + // } + // + // XXX should there be a default here??? listers: undefined, + + // + // .normalizePath() + // -> + // + // (optional) normalizePath: undefined, // NOTE: handler argument always overwrites the value given in options... @@ -184,73 +212,21 @@ object.Constructor('Walk', { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// -// Format: -// { -// : , -// -// : { -// list: , -// ... -// }, -// -// ... -// } -// -var OBJECT_LISTERS = -module.OBJECT_LISTERS = { - // prevent dissecting null... - null: function(obj){ - if(obj === null){ - throw module.STOP } }, - - set: function(obj){ - return obj instanceof Set - && [...obj.values()] - .entries() - .map(function([k, v]){ - return [[module.CONTENT, k], v] }) }, - map: function(obj){ - return obj instanceof Map - && obj.entries() - .map(function*([k, v], i){ - yield* [ - [[module.CONTENT, i+'@key'], k], - [[module.CONTENT, i], v], - ] }) }, - - /* XXX should we handle array elements differently??? - // ...these to simply mark attr type for the handler(..), not - // sure if the added complexity is worth it... (???) - array: function(obj){ - return obj instanceof Array - && [...Object.entries(obj)] - .filter(function(e){ - return !isNaN(parseInt(e)) }) }, - attr: function(obj){ - return obj instanceof Array ? - [...Object.entries(obj)] - .filter(function(e){ - return isNaN(parseInt(e)) }) - : typeof(obj) == 'object' - && [...Object.entries(obj)] }, - /*/ - attr: function(obj){ - return typeof(obj) == 'object' - && Object.entries(obj) }, - //*/ - proto: function(obj){ - return typeof(obj) == 'object' - && obj.constructor.prototype !== obj.__proto__ - && [['__proto__', obj.__proto__]] }, -} - - +// XXX we can treat the output as a stack language... +// ...need to clear out unneeded stuff... // XXX add function support... // XXX this needs .name set correctly... var objectWalker = module.objectWalker = Walk({ + // support string paths... + normalizePath: function(path){ + return path instanceof Array ? + path + : typeof(path) == 'string' ? + str2path(path) + : [] }, + handler: function(obj, path, next, type){ return type == 'LINK' ? [path, 'LINK', next] @@ -265,17 +241,59 @@ Walk({ // primitives... : obj, ] }, - listers: module.OBJECT_LISTERS, - // support string paths... - normalizePath: function(path){ - return path instanceof Array ? - path - : typeof(path) == 'string' ? - str2path(path) - : [] }, + + listers: { + // prevent dissecting null... + null: function(obj){ + if(obj === null){ + throw module.STOP } }, + + set: function(obj){ + return obj instanceof Set + && [...obj.values()] + .entries() + .map(function([k, v]){ + return [[module.CONTENT, k], v] }) }, + map: function(obj){ + return obj instanceof Map + && obj.entries() + .map(function*([k, v], i){ + yield* [ + [[module.CONTENT, i+'@key'], k], + [[module.CONTENT, i], v], + ] }) }, + + /* XXX should we handle array elements differently??? + // ...these to simply mark attr type for the handler(..), not + // sure if the added complexity is worth it... (???) + array: function(obj){ + return obj instanceof Array + && [...Object.entries(obj)] + .filter(function(e){ + return !isNaN(parseInt(e)) }) }, + attr: function(obj){ + return obj instanceof Array ? + [...Object.entries(obj)] + .filter(function(e){ + return isNaN(parseInt(e)) }) + : typeof(obj) == 'object' + && [...Object.entries(obj)] }, + /*/ + attr: function(obj){ + return typeof(obj) == 'object' + && Object.entries(obj) }, + //*/ + proto: function(obj){ + return typeof(obj) == 'object' + && obj.constructor.prototype !== obj.__proto__ + && [['__proto__', obj.__proto__]] }, + }, }) + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + // like objectWalker(..) but with 'text' support... // var objectWalkerWithText =