mirror of
https://github.com/flynx/diff.js.git
synced 2025-10-28 18:40:09 +00:00
the walk variant is working...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
59a4b201ee
commit
bf849bb2d4
91
diff2.js
91
diff2.js
@ -396,18 +396,25 @@ module.WALK_HANDLERS = {
|
|||||||
if(obj === null){
|
if(obj === null){
|
||||||
throw module.STOP } } },
|
throw module.STOP } } },
|
||||||
|
|
||||||
|
set: {
|
||||||
|
walk: function(obj){
|
||||||
|
return obj instanceof Set
|
||||||
|
&& [...obj.values()].entries() } },
|
||||||
map: {
|
map: {
|
||||||
walk: function(obj){
|
walk: function(obj){
|
||||||
return obj instanceof Map
|
return obj instanceof Map
|
||||||
&& obj.entries() } },
|
&& obj.entries() } },
|
||||||
set: {
|
|
||||||
walk: function(obj){
|
attr: {
|
||||||
return obj instanceof Set
|
|
||||||
&& [...[...obj.values()].entries()] } },
|
|
||||||
attrs: {
|
|
||||||
walk: function(obj){
|
walk: function(obj){
|
||||||
return typeof(obj) == 'object'
|
return typeof(obj) == 'object'
|
||||||
&& [...Object.entries(obj)] } },
|
&& [...Object.entries(obj)] } },
|
||||||
|
proto: {
|
||||||
|
walk: function(obj){
|
||||||
|
return typeof(obj) == 'object'
|
||||||
|
&& obj.constructor.prototype !== obj.__proto__
|
||||||
|
&& [['__proto__', obj.__proto__]] }, },
|
||||||
|
|
||||||
text: {
|
text: {
|
||||||
walk: function(obj){
|
walk: function(obj){
|
||||||
return typeof(obj) == 'string'
|
return typeof(obj) == 'string'
|
||||||
@ -424,6 +431,10 @@ module.WALK_HANDLERS = {
|
|||||||
// -> <value>
|
// -> <value>
|
||||||
// !> STOP(<value>)
|
// !> STOP(<value>)
|
||||||
//
|
//
|
||||||
|
// <handler>(<obj>, <path>, <orig-path>, 'LINK')
|
||||||
|
// -> <value>
|
||||||
|
// !> STOP(<value>)
|
||||||
|
//
|
||||||
// <walker>(<obj>)
|
// <walker>(<obj>)
|
||||||
// <walker>(<obj>, <path>)
|
// <walker>(<obj>, <path>)
|
||||||
// -> <generator>
|
// -> <generator>
|
||||||
@ -451,6 +462,12 @@ function(handler, path=[], options={}){
|
|||||||
options)
|
options)
|
||||||
: options
|
: options
|
||||||
|
|
||||||
|
var _handler = function*(){
|
||||||
|
if(handler instanceof types.Generator){
|
||||||
|
yield* handler(...arguments)
|
||||||
|
} else {
|
||||||
|
yield handler(...arguments) } }
|
||||||
|
|
||||||
var _walk = function*(obj, path=p, type=undefined){
|
var _walk = function*(obj, path=p, type=undefined){
|
||||||
path = path instanceof Array ?
|
path = path instanceof Array ?
|
||||||
path
|
path
|
||||||
@ -459,6 +476,15 @@ function(handler, path=[], options={}){
|
|||||||
: []
|
: []
|
||||||
type = type || 'root'
|
type = type || 'root'
|
||||||
|
|
||||||
|
// handle loops...
|
||||||
|
var seen = options.seen =
|
||||||
|
options.seen || new Map()
|
||||||
|
if(seen.has(obj)){
|
||||||
|
yield* _handler(obj, path, seen.get(obj), 'LINK')
|
||||||
|
return }
|
||||||
|
typeof(obj) == 'object'
|
||||||
|
&& seen.set(obj, path)
|
||||||
|
|
||||||
var handlers = options.handlers || module.WALK_HANDLERS
|
var handlers = options.handlers || module.WALK_HANDLERS
|
||||||
// format:
|
// format:
|
||||||
// [
|
// [
|
||||||
@ -482,10 +508,7 @@ function(handler, path=[], options={}){
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// main object...
|
// main object...
|
||||||
if(handler instanceof types.Generator){
|
yield* _handler(obj, path, next, type)
|
||||||
yield* handler(obj, path, next, type)
|
|
||||||
} else {
|
|
||||||
yield handler(obj, path, next, type) }
|
|
||||||
// next/children...
|
// next/children...
|
||||||
yield* next
|
yield* next
|
||||||
.map(function*([type, items]){
|
.map(function*([type, items]){
|
||||||
@ -1066,47 +1089,21 @@ console.log(JSON.stringify(diff(
|
|||||||
|
|
||||||
console.log('---')
|
console.log('---')
|
||||||
|
|
||||||
var walker = walk(function(e, p){
|
var walker = walk(function(e, p, n, t){
|
||||||
return [
|
return t == 'LINK' ?
|
||||||
p,
|
[p, 'LINK', n]
|
||||||
e == null ?
|
: [
|
||||||
e
|
p,
|
||||||
: typeof(e) == 'object' ?
|
e == null ?
|
||||||
{type: e.constructor.name}
|
e
|
||||||
: e,
|
: typeof(e) == 'object' ?
|
||||||
] })
|
{type: e.constructor.name}
|
||||||
|
: e,
|
||||||
|
] })
|
||||||
|
|
||||||
|
// XXX test functions...
|
||||||
console.log([
|
console.log([
|
||||||
/*
|
|
||||||
...walker(o)
|
...walker(o)
|
||||||
/*/
|
|
||||||
...walker({
|
|
||||||
// literals...
|
|
||||||
null: null,
|
|
||||||
undefined: undefined,
|
|
||||||
NaN: NaN,
|
|
||||||
true: true,
|
|
||||||
false: false,
|
|
||||||
number: 1,
|
|
||||||
string: 'string',
|
|
||||||
|
|
||||||
// containers...
|
|
||||||
array: [1,2,3],
|
|
||||||
object: {a:1},
|
|
||||||
|
|
||||||
// encapsulated containers...
|
|
||||||
set: new Set([1, 2, 3]),
|
|
||||||
map: new Map([[1, 2], [3, 4]]),
|
|
||||||
|
|
||||||
// recursive...
|
|
||||||
// XXX
|
|
||||||
|
|
||||||
// mixed...
|
|
||||||
array_with_attrs: Object.assign(
|
|
||||||
[1,2,3],
|
|
||||||
{a: 333}),
|
|
||||||
})
|
|
||||||
//*/
|
|
||||||
.chain(serializePaths) ])
|
.chain(serializePaths) ])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user