stuck on format spec -- not sure how to identify non attr data (map/set items)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-05-30 11:46:44 +03:00
parent 2a25dc00e0
commit a9d8ab7b89
2 changed files with 96 additions and 26 deletions

120
diff2.js
View File

@ -13,6 +13,7 @@ var types = require('ig-types')
/*********************************************************************/ /*********************************************************************/
// XXX need to deal with functions...
var HANDLERS = var HANDLERS =
module.HANDLERS = { module.HANDLERS = {
/*/ XXX /*/ XXX
@ -69,28 +70,37 @@ module.HANDLERS = {
}, },
}, },
// XXX do we need to also traverse/index the keys??? // XXX these still intersect with attrs...
// ...if yes then we'll need to somehow indicate a path to a key... // ...need a destinct way to encapsulate these to destinguish
// one way to do this is to add virtual paths and link to them... // the data from attrs...
// ...i.e. a virtual path is any path starting from a virtual root // this is simple when nesting, i.e. just add the entries to
// that can be linked from within the tree... // .entries, attributes to .attrs and done, but in a flat format
containerEntries: { // this is not obvious -- i.e. how do we destinguish attr 'x'
containers: [ // from map key 'x'???
Set, setEntries: {
Map, match: function(obj){
], return obj instanceof Set },
// NOTE: we are indexing sets...
handle: function(obj){
return [ obj.values()
.map(function(v, i){
return [[i], v] })
.toArray() ] },
},
mapEntries: {
// XXX should this be more generic and just check for .entries(..) ??? // XXX should this be more generic and just check for .entries(..) ???
match: function(obj){ match: function(obj){
for(var type of this.containers){ return obj instanceof Map },
if(obj instanceof type){
return true } } },
handle: function(obj){ handle: function(obj){
// XXX for some reason the .entries() iterator does not have return [ obj.entries()
// the generaator methods -- we can't call obj.entries().map(..)... .map(function([k, v], i){
// XXX for sets k is the same a v... not sure how to handle this... return [
return [ [...obj.entries()] // XXX not sure how to format these...
.map(function([k, v]){ [[i +':key'], k],
return [[k], v] }), ] }, [[i], v],
] })
.flat()
.toArray() ] },
}, },
// XXX do we need to treat array keys as a special case??? // XXX do we need to treat array keys as a special case???
@ -135,9 +145,20 @@ function(obj, handlers=module.HANDLERS){
// Format:
// [
// [<path>, {type: <name>}],
//
// [<path>, ['LINK', <path>]],
//
// [<path>, <value>],
// ]
//
// XXX need a way to index the path... // XXX need a way to index the path...
// ...and to filter paths by pattern... // ...and to filter paths by pattern...
// XXX need to generate object UIDs for use in paths etc... // XXX need to generate object UIDs for use in paths etc...
// XXX might be a good idea to include obj in the output to negate the
// need to get it via the path in client code...
var handle = var handle =
module.handle = module.handle =
function*(obj, path=[], options={}){ function*(obj, path=[], options={}){
@ -199,6 +220,7 @@ var serializePathElem = function(p){
JSON.stringify(p) JSON.stringify(p)
: p } : p }
var serializePath = function(p){ var serializePath = function(p){
//return '/'+ p.map(JSON.stringify).join('/') }
return '/'+ p.map(serializePathElem).join('/') } return '/'+ p.map(serializePathElem).join('/') }
var serializePaths = var serializePaths =
module.serializePaths = module.serializePaths =
@ -213,6 +235,36 @@ types.generator.iter
// XXX make this more generic...
// ...or move these to the HANDLERS as .build(..)...
var construct = function(spec){
return typeof(spec) != 'object' ?
spec
: spec.type == 'Object' ?
{}
: spec.type == 'Array' ?
[]
: spec.type == 'Set' ?
new Set()
: spec.type == 'Map' ?
new Map()
: undefined }
var has = function(root, path){
}
var get = function(root, path){
}
var set = function(root, path, value){
}
var build =
types.generator.iter
.reduce(function(root, [path, spec]){
return path.length == 0 ?
construct(spec)
: set(root, path, value)
}, undefined)
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// XXX move to test... // XXX move to test...
@ -227,26 +279,43 @@ var o = {
empty_array: [], empty_array: [],
array: [1, 2, 3], array: [1, 2, 3,,,,'N'],
// XXX set key is the object itself, is this what we want??? // XXX set key is the object itself, is this what we want???
set: new Set([1, [], {a:1}]), set: new Set([
map: new Map([[[], 123], [321, {}]]), 1,
[],
{a:1},
]),
map: new Map([
[[9,8,7], 123],
[321, {x: 123}],
]),
object: { object: {
x: {}, x: {},
}, },
array_with_attrs: Object.assign( array_with_attrs: Object.assign(
// XXX should empty slots be marked in arrays??? [1, 2, 3],
[1, 2, 3, , 5, 6],
{ {
a: "some value", a: 'some value',
b: 'some other value',
// will overwrite 2... // will overwrite 2...
1: 333, 1: 333,
}) })
} }
// clone...
// NOTE: JSON does not support:
// - sparse arrays
// = sets/maps
// - loops
oo = JSON.parse(JSON.stringify(o))
// loop... // loop...
// NOTE: we are creating the loop before we pass it to JSON because JSON
// does not support loops in objects...
o.object.y = o.object o.object.y = o.object
@ -255,6 +324,7 @@ console.log([
...handle(o) ...handle(o)
.chain(serializePaths)]) .chain(serializePaths)])
//console.log([...handle(o)])

View File

@ -26,7 +26,7 @@
"dependencies": { "dependencies": {
"generic-walk": "^1.4.0", "generic-walk": "^1.4.0",
"ig-object": "^2.2.0", "ig-object": "^2.2.0",
"ig-types": "^6.1.0" "ig-types": "*"
}, },
"devDependencies": { "devDependencies": {
"ig-test": "*" "ig-test": "*"