mirror of
https://github.com/flynx/diff.js.git
synced 2025-10-29 02:50:10 +00:00
a generalization...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
1fb6c3bdd2
commit
28648fff06
30
diff2.js
30
diff2.js
@ -88,6 +88,17 @@ module.CONTENT =
|
|||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
//
|
||||||
|
// Walk(<handler>[, <options>])
|
||||||
|
// -> <walker>
|
||||||
|
//
|
||||||
|
// Walk(<options>)
|
||||||
|
// -> <walker>
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// NOTE: in the Walk(<options>) case, <options> must contain a .handler
|
||||||
|
// function.
|
||||||
|
//
|
||||||
// XXX need to think about extension use-cases...
|
// XXX need to think about extension use-cases...
|
||||||
// XXX should we move this to a separate lib???
|
// XXX should we move this to a separate lib???
|
||||||
var Walk =
|
var Walk =
|
||||||
@ -99,6 +110,10 @@ object.Constructor('Walk', {
|
|||||||
|
|
||||||
// NOTE: handler argument always overwrites the value given in options...
|
// NOTE: handler argument always overwrites the value given in options...
|
||||||
__init__: function(handler, options){
|
__init__: function(handler, options){
|
||||||
|
// handle Walk(<options>) case...
|
||||||
|
if(arguments.length == 1 && typeof(handler) != 'function'){
|
||||||
|
options = handler
|
||||||
|
handler = options.handler }
|
||||||
// sanity check...
|
// sanity check...
|
||||||
if(typeof(handler) != 'function'){
|
if(typeof(handler) != 'function'){
|
||||||
throw new Error('Walk(..): a callable handler us required.') }
|
throw new Error('Walk(..): a callable handler us required.') }
|
||||||
@ -190,6 +205,7 @@ module.OBJECT_LISTERS = {
|
|||||||
throw module.STOP } },
|
throw module.STOP } },
|
||||||
|
|
||||||
/* XXX not sure if this should be here...
|
/* XXX not sure if this should be here...
|
||||||
|
// XXX TEXT...
|
||||||
// XXX also this is diff-specific...
|
// XXX also this is diff-specific...
|
||||||
text: function(obj){
|
text: function(obj){
|
||||||
return typeof(obj) == 'string'
|
return typeof(obj) == 'string'
|
||||||
@ -242,13 +258,14 @@ module.OBJECT_LISTERS = {
|
|||||||
|
|
||||||
// XXX add function support...
|
// XXX add function support...
|
||||||
// XXX would be nice to be able to extend this...
|
// XXX would be nice to be able to extend this...
|
||||||
// e.g. make this a generic walker and for a diff-specific walker add
|
// e.g. make this a generic walker and for a diff-specific walker
|
||||||
// text support...
|
// add text support...
|
||||||
var objectWalker =
|
var objectWalker =
|
||||||
module.objectWalker =
|
module.objectWalker =
|
||||||
Walk(
|
Walk({
|
||||||
function(obj, path, next, type){
|
handler: function(obj, path, next, type){
|
||||||
// text...
|
// text...
|
||||||
|
// XXX TEXT...
|
||||||
// XXX should this be here or in OBJECT_LISTERS???
|
// XXX should this be here or in OBJECT_LISTERS???
|
||||||
// XXX also this is diff-specific...
|
// XXX also this is diff-specific...
|
||||||
if(typeof(obj) == 'string' && obj.includes('\n')){
|
if(typeof(obj) == 'string' && obj.includes('\n')){
|
||||||
@ -266,10 +283,13 @@ Walk(
|
|||||||
obj
|
obj
|
||||||
: typeof(obj) == 'object' ?
|
: typeof(obj) == 'object' ?
|
||||||
{type: obj.constructor.name}
|
{type: obj.constructor.name}
|
||||||
|
// XXX TEXT...
|
||||||
|
//: typeof(obj) == 'string' && obj.includes('\n') ?
|
||||||
|
// {type: 'text'}
|
||||||
: obj,
|
: obj,
|
||||||
] },
|
] },
|
||||||
{
|
|
||||||
listers: module.OBJECT_LISTERS,
|
listers: module.OBJECT_LISTERS,
|
||||||
|
// support string paths...
|
||||||
normalizePath: function(path){
|
normalizePath: function(path){
|
||||||
return path instanceof Array ?
|
return path instanceof Array ?
|
||||||
path
|
path
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user