mirror of
https://github.com/flynx/diff.js.git
synced 2025-10-29 11:00:12 +00:00
cleanup, tooling and tweaking...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
5e47d4d3a5
commit
6f812057ea
79
diff2.js
79
diff2.js
@ -23,6 +23,13 @@ var types = require('ig-types')
|
|||||||
// - subtract specs (diff)
|
// - subtract specs (diff)
|
||||||
// - full
|
// - full
|
||||||
// - relaxed -- ignore item order
|
// - relaxed -- ignore item order
|
||||||
|
// - modes:
|
||||||
|
// - JSON
|
||||||
|
// - reconstructable
|
||||||
|
// - full
|
||||||
|
// - not reconstructable -- if some types are used (functions, ...)
|
||||||
|
// - compare protocol
|
||||||
|
// - reconstruct protocol
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
/*********************************************************************/
|
/*********************************************************************/
|
||||||
@ -35,6 +42,27 @@ module.CONTENT =
|
|||||||
Symbol('CONTENT')
|
Symbol('CONTENT')
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// Format:
|
||||||
|
// {
|
||||||
|
// <name>: {
|
||||||
|
// // optional
|
||||||
|
// final: <bool>,
|
||||||
|
//
|
||||||
|
// match: <name> | <func>,
|
||||||
|
//
|
||||||
|
// handle: <name> | <func>,
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// NOTE: this is more of a grammar than a set of object handlers, nother
|
||||||
|
// way to think of this is as a set of handlrs of aspects of objects
|
||||||
|
// and not full objects...
|
||||||
|
// XXX not sure if this is how this is going to continue though as
|
||||||
|
// we'll need to organize constructors preferably within this
|
||||||
|
// structure and keep it extensible...
|
||||||
|
//
|
||||||
// XXX need to deal with functions...
|
// XXX need to deal with functions...
|
||||||
var HANDLERS =
|
var HANDLERS =
|
||||||
module.HANDLERS = {
|
module.HANDLERS = {
|
||||||
@ -62,8 +90,7 @@ module.HANDLERS = {
|
|||||||
final: true,
|
final: true,
|
||||||
match: function(obj){
|
match: function(obj){
|
||||||
return obj === null },
|
return obj === null },
|
||||||
handle: function(obj){
|
handle: 'value', },
|
||||||
return [[], obj] }, },
|
|
||||||
|
|
||||||
// Functions...
|
// Functions...
|
||||||
//
|
//
|
||||||
@ -71,13 +98,7 @@ module.HANDLERS = {
|
|||||||
func: {
|
func: {
|
||||||
match: function(obj){
|
match: function(obj){
|
||||||
return typeof(obj) == 'function' },
|
return typeof(obj) == 'function' },
|
||||||
handle: function(obj){
|
handle: 'object', },
|
||||||
return [[], {
|
|
||||||
type: 'function',
|
|
||||||
gen: obj.constructor.prototype === obj.__proto__ ? 1 : 2,
|
|
||||||
// XXX
|
|
||||||
source: obj,
|
|
||||||
}] }, },
|
|
||||||
|
|
||||||
// Non-Objects...
|
// Non-Objects...
|
||||||
//
|
//
|
||||||
@ -175,6 +196,14 @@ module.HANDLERS = {
|
|||||||
//*/
|
//*/
|
||||||
|
|
||||||
|
|
||||||
|
/* XXX not sure about this...
|
||||||
|
// Service stuff...
|
||||||
|
//
|
||||||
|
error: {
|
||||||
|
match: function(obj){},
|
||||||
|
},
|
||||||
|
//*/
|
||||||
|
|
||||||
|
|
||||||
// Testing...
|
// Testing...
|
||||||
//
|
//
|
||||||
@ -253,7 +282,8 @@ function*(obj, path=[], options={}){
|
|||||||
// get compatible handler list...
|
// get compatible handler list...
|
||||||
var cache = options.cache =
|
var cache = options.cache =
|
||||||
options.cache || new Map()
|
options.cache || new Map()
|
||||||
var handlers = module.getHandlers(obj, options.handlers || module.HANDLERS)
|
var HANDLERS = options.handlers || module.HANDLERS
|
||||||
|
var handlers = module.getHandlers(obj, HANDLERS)
|
||||||
|
|
||||||
// XXX might be a good idea to move this up (or into options) so as
|
// XXX might be a good idea to move this up (or into options) so as
|
||||||
// not to define this on each call...
|
// not to define this on each call...
|
||||||
@ -281,10 +311,15 @@ function*(obj, path=[], options={}){
|
|||||||
.filter(function(handler){
|
.filter(function(handler){
|
||||||
return !!handler.handle })
|
return !!handler.handle })
|
||||||
.map(function*(handler){
|
.map(function*(handler){
|
||||||
yield* handler.handle instanceof types.Generator ?
|
var h = handler
|
||||||
handler.handle(obj, path, options)
|
// expand aliases...
|
||||||
|
while(h && typeof(h.handle) == 'string'){
|
||||||
|
h = HANDLERS[h.handle] }
|
||||||
|
yield* h.handle instanceof types.Generator ?
|
||||||
|
// XXX should .handle(..) be called in the context of h or handler???
|
||||||
|
h.handle.call(handler, obj, path, options)
|
||||||
.map(subtree)
|
.map(subtree)
|
||||||
: subtree(handler.handle(obj, path, options)) }) }
|
: subtree(h.handle.call(handler, obj, path, options)) }) }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -341,6 +376,20 @@ types.generator.iter
|
|||||||
: [serializePath(p), v] })
|
: [serializePath(p), v] })
|
||||||
|
|
||||||
|
|
||||||
|
var stripAttr =
|
||||||
|
module.stripAttr =
|
||||||
|
function(...attrs){
|
||||||
|
return types.generator.iter
|
||||||
|
.map(function([p, v]){
|
||||||
|
if(v && typeof(v) == 'object'){
|
||||||
|
// keep things non-destructive...
|
||||||
|
v = Object.assign({}, v)
|
||||||
|
attrs
|
||||||
|
.forEach(function(attr){
|
||||||
|
attr in v
|
||||||
|
&& (delete v[attr]) }) }
|
||||||
|
return [p, v] }) }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -420,7 +469,9 @@ o.object.y = o.object
|
|||||||
|
|
||||||
console.log([
|
console.log([
|
||||||
...handle(o)
|
...handle(o)
|
||||||
.chain(serializePaths)])
|
.chain(
|
||||||
|
serializePaths,
|
||||||
|
stripAttr('source'), )])
|
||||||
|
|
||||||
//console.log([...handle(o)])
|
//console.log([...handle(o)])
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user