experimenting...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-06-25 15:46:51 +03:00
parent eebfc2e6f1
commit 59a4b201ee

View File

@ -390,6 +390,12 @@ function*(obj, path=[], options={}){
var WALK_HANDLERS = var WALK_HANDLERS =
module.WALK_HANDLERS = { module.WALK_HANDLERS = {
// prevent dissecting null...
null: {
walk: function(obj){
if(obj === null){
throw module.STOP } } },
map: { map: {
walk: function(obj){ walk: function(obj){
return obj instanceof Map return obj instanceof Map
@ -397,11 +403,11 @@ module.WALK_HANDLERS = {
set: { set: {
walk: function(obj){ walk: function(obj){
return obj instanceof Set return obj instanceof Set
&& obj.values() } }, && [...[...obj.values()].entries()] } },
attrs: { attrs: {
walk: function(obj){ walk: function(obj){
return typeof(obj) == 'object' return typeof(obj) == 'object'
&& Object.entries(obj) } }, && [...Object.entries(obj)] } },
text: { text: {
walk: function(obj){ walk: function(obj){
return typeof(obj) == 'string' return typeof(obj) == 'string'
@ -427,9 +433,11 @@ module.WALK_HANDLERS = {
// XXX the idea here is to try to decouple the walk from the format and // XXX the idea here is to try to decouple the walk from the format and
// move the formatters and other stuff out... // move the formatters and other stuff out...
// ...not sure if this is simpler yet... // ...not sure if this is simpler yet...
// XXX handle recursive structures...
var walk = var walk =
module.walk = module.walk =
function(handler, path=[], options={}){ function(handler, path=[], options={}){
var p = path
// parse args... // parse args...
options = options =
typeof(path) == 'object' && !(path instanceof Array) ? typeof(path) == 'object' && !(path instanceof Array) ?
@ -443,7 +451,7 @@ function(handler, path=[], options={}){
options) options)
: options : options
var _walk = function*(obj, path=path, type=undefined){ var _walk = function*(obj, path=p, type=undefined){
path = path instanceof Array ? path = path instanceof Array ?
path path
: typeof(path) == 'string' ? : typeof(path) == 'string' ?
@ -451,20 +459,22 @@ function(handler, path=[], options={}){
: [] : []
type = type || 'root' type = type || 'root'
var handlers = options.handlers || module.HANDLERS var handlers = options.handlers || module.WALK_HANDLERS
// format: // format:
// [ // [
// [<handler-name>, [ [<key>, <value>], .. ]], // [<handler-name>, [ [<key>, <value>], .. ]],
// .. // ..
// ] // ]
var next = Object.entries(handlers) var next = Object.entries(handlers)
// NOTE: we need this to support throwing STOP...
.iter()
.filter(function([n, h]){ .filter(function([n, h]){
return h.walk return h.walk
&& !options['no' + n.capitalize()] }) && !options['no' + n.capitalize()] })
.map(function([n, h]){ .map(function([n, h]){
// XXX should we call the handler(..) once per set of // XXX should we call the handler(..) once per set of
// next values (i.e. attrs, items, ...)??? // next values (i.e. attrs, items, ...)???
var res = h.walk.call(obj) var res = h.walk(obj)
return res return res
&& [n, res] }) && [n, res] })
.filter(function(e){ .filter(function(e){
@ -478,11 +488,10 @@ function(handler, path=[], options={}){
yield handler(obj, path, next, type) } yield handler(obj, path, next, type) }
// next/children... // next/children...
yield* next yield* next
.iter()
.map(function*([type, items]){ .map(function*([type, items]){
yield* items yield* items
.iter() .iter()
.map(function([key, value]){ .map(function*([key, value]){
yield* _walk(value, path.concat(key), type) }) }) yield* _walk(value, path.concat(key), type) }) })
// handle STOP... // handle STOP...
} catch(err){ } catch(err){
@ -536,7 +545,7 @@ function(p){
: res.pop()) : res.pop())
+ ':CONTENT' + ':CONTENT'
// special case: '' as key... // special case: '' as key...
: e == '' ? : e === '' ?
"''" "''"
: e : e
res.push(e) res.push(e)
@ -1055,6 +1064,52 @@ console.log(JSON.stringify(diff(
//*/ //*/
console.log('---')
var walker = walk(function(e, p){
return [
p,
e == null ?
e
: typeof(e) == 'object' ?
{type: e.constructor.name}
: e,
] })
console.log([
/*
...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) ])
/********************************************************************** /**********************************************************************
* vim:set ts=4 sw=4 : */ return module }) * vim:set ts=4 sw=4 : */ return module })