experimenting with relative indexes (offsets)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-07-13 02:05:17 +03:00
parent a2d6e30a7e
commit 85bbacc1c2

View File

@ -14,8 +14,14 @@ var types = require('ig-types')
/*********************************************************************/ /*********************************************************************/
// //
//
// XXX try using offset from prev element as an index for arrays...
// ...this should make it possible to use LCS on the linear diff
// directly...
//
//
// XXX thinks this needs to do: // XXX thinks this needs to do:
// - path sepcification - DONE // - path specification - DONE
// - walk object tree - DONE // - walk object tree - DONE
// - generate a spec - DONE // - generate a spec - DONE
// - serializable // - serializable
@ -86,6 +92,17 @@ module.CONTENT =
Symbol('CONTENT') Symbol('CONTENT')
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// XXX OFFSET: should we also store index???
var Offset =
module.Offset =
object.Constructor('Offset', {
__init__: function(value, index){
this.value = value
this.index = index }, })
//--------------------------------------------------------------------- //---------------------------------------------------------------------
@ -155,8 +172,8 @@ object.Constructor('Walk', {
: Object.assign( : Object.assign(
function*(){ yield handler.call(this, ...arguments) }, function*(){ yield handler.call(this, ...arguments) },
{toString: function(){ return handler.toString() }}) }, {toString: function(){ return handler.toString() }}) },
// XXX might be a good idea to form the output of this like a stack // XXX STACK: might be a good idea to form the output of this like
// language program... // a stack language program...
// This would require actions to: // This would require actions to:
// - block type (BLOCK) // - block type (BLOCK)
// - push sub-path (PUSH) // - push sub-path (PUSH)
@ -200,7 +217,7 @@ object.Constructor('Walk', {
try { try {
// XXX should this be a yield* or a simple yield??? // XXX should this be a yield* or a simple yield???
yield* this.handler(obj, path, next, type) yield* this.handler(obj, path, next, type)
// XXX BLOCK... /* XXX STACK: BLOCK...
.map(function([p, v]){ .map(function([p, v]){
v && typeof(v) == 'object' v && typeof(v) == 'object'
&& console.log(' BLOCK', v.type) && console.log(' BLOCK', v.type)
@ -213,8 +230,8 @@ object.Constructor('Walk', {
yield* items yield* items
.iter() .iter()
.map(function*([key, value]){ .map(function*([key, value]){
// XXX PUSH key // XXX STACK: PUSH key
console.log(' PUSH', key) //console.log(' PUSH', key)
// XXX add relative path support... // XXX add relative path support...
// ...maybe [path, key] instead of path.concat(key) ??? // ...maybe [path, key] instead of path.concat(key) ???
yield* that(value, path.concat(key), type, seen) }) }) yield* that(value, path.concat(key), type, seen) }) })
@ -227,8 +244,8 @@ object.Constructor('Walk', {
return } return }
throw err } throw err }
// XXX POP // XXX STACK: POP
console.log(' POP') //console.log(' POP')
}, },
}) })
@ -275,6 +292,7 @@ Walk({
if(obj === null){ if(obj === null){
throw module.STOP } }, throw module.STOP } },
// XXX do we use offsets here or indexes...
set: function(obj){ set: function(obj){
return obj instanceof Set return obj instanceof Set
&& [...obj.values()] && [...obj.values()]
@ -290,19 +308,26 @@ Walk({
[[module.CONTENT, i], v], [[module.CONTENT, i], v],
] }) }, ] }) },
/* XXX should we handle array elements differently??? //* XXX should we handle array elements differently???
// ...these to simply mark attr type for the handler(..), not // ...these to simply mark attr type for the handler(..), not
// sure if the added complexity is worth it... (???) // sure if the added complexity is worth it... (???)
array: function(obj){ array: function(obj){
var prev = 0
return obj instanceof Array return obj instanceof Array
&& [...Object.entries(obj)] && [...Object.entries(obj)]
.filter(function(e){ .filter(function([k, v]){
return !isNaN(parseInt(e)) }) }, return !isNaN(parseInt(k)) })
// NOTE: we are outputting index offsets instead of
// actual indexes...
// XXX need to make this a syntax...
.map(function([k, v]){
[prev, k] = [k, k-prev]
return [module.Offset(k, prev), v] }) },
attr: function(obj){ attr: function(obj){
return obj instanceof Array ? return obj instanceof Array ?
[...Object.entries(obj)] [...Object.entries(obj)]
.filter(function(e){ .filter(function([k, v]){
return isNaN(parseInt(e)) }) return isNaN(parseInt(k)) })
: typeof(obj) == 'object' : typeof(obj) == 'object'
&& [...Object.entries(obj)] }, && [...Object.entries(obj)] },
/*/ /*/
@ -368,13 +393,23 @@ Object.assign(
// A: there should be a difference.... // A: there should be a difference....
// [...d.handle({'':new Set([1,2,3]), x:123}).chain(d.serializePaths)] // [...d.handle({'':new Set([1,2,3]), x:123}).chain(d.serializePaths)]
// ...the problem is in path2str(..) // ...the problem is in path2str(..)
// XXX OFFSET: add option to either encode offset or index...
var serializePathElem = function(p, i, l){ var serializePathElem = function(p, i, l){
return typeof(p) == 'object' ? return (
p instanceof Offset ?
// XXX OFFSET: add option to either encode offset or index...
// XXX OFFSET: should we use + or : ???
// XXX OFFSET: should we use .toString() or something like .toKey()???
':'+ p.value
: typeof(p) == 'object' ?
JSON.stringify(p) JSON.stringify(p)
// quote special chars... // quote special chars...
: typeof(p) == 'string' ? : typeof(p) == 'string' ?
// XXX OFFSET: should we use + or : ???
//p.replace(/([\/:+])/g, '\\$1')
p.replace(/([\/:])/g, '\\$1') p.replace(/([\/:])/g, '\\$1')
: p } : p )}
// XXX OFFSET: add option to either encode offset or index...
var path2str = var path2str =
module.path2str = module.path2str =
function(p){ function(p){
@ -384,10 +419,10 @@ function(p){
.map(serializePathElem) .map(serializePathElem)
.reduce(function(res, e){ .reduce(function(res, e){
e = e === module.CONTENT ? e = e === module.CONTENT ?
(res.length == 0 ? (res.length == 0 ?
'' ''
: res.pop()) : res.pop())
+ ':CONTENT' + ':CONTENT'
// special case: '' as key... // special case: '' as key...
: e === '' ? : e === '' ?
"''" "''"
@ -496,6 +531,8 @@ function(...attrs){
// ??? // ???
// ...revise... // ...revise...
// XXX str2path and passing in a list path produce different results... // XXX str2path and passing in a list path produce different results...
// XXX OFFSET: add offset support...
// ...get prev in context and add offset...
var atPath = var atPath =
module.atPath = module.atPath =
function(root, path, value){ function(root, path, value){
@ -953,13 +990,28 @@ console.log(valueDiff(
)) ))
//*/ //*/
console.log('---')
console.log([
...objectWalkerWithText(
[,,,1])
.chain(
serializePaths,
stripAttr('source'),
) ])
console.log('---')
// XXX BUG: no change is detected here... // XXX BUG: no change is detected here...
console.log(valueDiff( console.dir(
[,,,1], keyValueDiff(
[1], [,,,1,2,3],
)) [1,2,3],
), {depth: null})
console.log('---')