mirror of
https://github.com/flynx/diff.js.git
synced 2025-10-28 18:40:09 +00:00
notes, testing and preparing for a rethink...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
472ab7dc8a
commit
f54810aabc
79
diff2.js
79
diff2.js
@ -281,6 +281,12 @@ function(obj, handlers=module.HANDLERS){
|
|||||||
// 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 might be a good idea to generate "structural hashes" for objects...
|
// XXX might be a good idea to generate "structural hashes" for objects...
|
||||||
|
// XXX can we combine .handle(..) and .match(..) ???
|
||||||
|
// ...for example via .handle(obj, '?') protocol...
|
||||||
|
// .....or even simpler, just thread the object through all the
|
||||||
|
// handlers in one go -- unless there is a fast way to test and
|
||||||
|
// classify object predictably there is no point in a test stage...
|
||||||
|
// .....would also be nice to support a STOP(res) instead of .final
|
||||||
var handle =
|
var handle =
|
||||||
module.handle =
|
module.handle =
|
||||||
function*(obj, path=[], options={}){
|
function*(obj, path=[], options={}){
|
||||||
@ -337,12 +343,22 @@ function*(obj, path=[], options={}){
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// path2str(..)
|
||||||
|
//
|
||||||
// XXX need to figure out a way to avoid clashes with module.CONTENT in
|
// XXX need to figure out a way to avoid clashes with module.CONTENT in
|
||||||
// path with actual attribute keys...
|
// path with actual attribute keys...
|
||||||
// ways to do this:
|
// ways to do this:
|
||||||
// - serialize CONTENT in a cleaver way
|
// - serialize CONTENT in a cleaver way
|
||||||
// - add a different path separator to indicate content and quote
|
// - add a different path separator to indicate content and quote
|
||||||
// it in strings -- ':'???
|
// it in strings -- ':'???
|
||||||
|
// XXX Q: should there be a difference between:
|
||||||
|
// ['', module.CONTENT]
|
||||||
|
// and
|
||||||
|
// [module.CONTENT] ???
|
||||||
|
// ...currently they are the same...
|
||||||
|
// A: there should be a difference....
|
||||||
|
// [...d.handle({'':new Set([1,2,3]), x:123}).chain(d.serializePaths)]
|
||||||
|
// ...the problem is in path2str(..)
|
||||||
var serializePathElem = function(p, i, l){
|
var serializePathElem = function(p, i, l){
|
||||||
return typeof(p) == 'object' ?
|
return typeof(p) == 'object' ?
|
||||||
JSON.stringify(p)
|
JSON.stringify(p)
|
||||||
@ -357,20 +373,34 @@ function(p){
|
|||||||
.map(serializePathElem)
|
.map(serializePathElem)
|
||||||
.reduce(function(res, e){
|
.reduce(function(res, e){
|
||||||
e = e === module.CONTENT ?
|
e = e === module.CONTENT ?
|
||||||
res.pop() + ':CONTENT'
|
(res.length == 0 ?
|
||||||
|
''
|
||||||
|
: res.pop())
|
||||||
|
+ ':CONTENT'
|
||||||
|
// special case: '' as key...
|
||||||
|
: e == '' ?
|
||||||
|
"''"
|
||||||
: e
|
: e
|
||||||
res.push(e)
|
res.push(e)
|
||||||
return res }, [])
|
return res }, [])
|
||||||
.join('/') }
|
.join('/') }
|
||||||
|
|
||||||
|
|
||||||
|
// str2path(..)
|
||||||
|
//
|
||||||
|
var unquote = function(str){
|
||||||
|
return str
|
||||||
|
.replace(/^(['"])(.*)\1$/, '$2') }
|
||||||
var deserializePathElem = function(p){
|
var deserializePathElem = function(p){
|
||||||
return p == ':CONTENT'?
|
return p == ':CONTENT'?
|
||||||
[module.CONTENT]
|
[module.CONTENT]
|
||||||
: /[^\\]:CONTENT$/.test(p) ?
|
: /[^\\]:CONTENT$/.test(p) ?
|
||||||
[p.replace(/(?<!\\):CONTENT$/, ''), module.CONTENT]
|
[unquote(p.replace(/(?<!\\):CONTENT$/, '')), module.CONTENT]
|
||||||
: [p] }
|
: [unquote(p)] }
|
||||||
// XXX should we hanve relative paths????
|
// XXX should we hanve relative paths????
|
||||||
|
// XXX PROBLEM: need to be able to reference '' in {'': 123}, i.e, how do
|
||||||
|
// we stringify ['']???
|
||||||
|
// [''] => ???
|
||||||
var str2path =
|
var str2path =
|
||||||
module.str2path =
|
module.str2path =
|
||||||
function(str){
|
function(str){
|
||||||
@ -379,6 +409,7 @@ function(str){
|
|||||||
: str == '' || str == '/' ?
|
: str == '' || str == '/' ?
|
||||||
[]
|
[]
|
||||||
: (str
|
: (str
|
||||||
|
// remove leading '/'
|
||||||
.replace(/^\//, '')
|
.replace(/^\//, '')
|
||||||
.split(/\//g)
|
.split(/\//g)
|
||||||
.map(deserializePathElem)
|
.map(deserializePathElem)
|
||||||
@ -435,13 +466,13 @@ function(...attrs){
|
|||||||
//
|
//
|
||||||
// NOTE: to set this needs the full basepath to exist...
|
// NOTE: to set this needs the full basepath to exist...
|
||||||
//
|
//
|
||||||
// XXX how do we get :CONTENT of root???
|
|
||||||
// XXX need to write a map key to an item that does not exist...
|
// XXX need to write a map key to an item that does not exist...
|
||||||
// XXX str2path and passing in a list path produce different results...
|
// XXX str2path and passing in a list path produce different results...
|
||||||
var atPath =
|
var atPath =
|
||||||
module.atPath =
|
module.atPath =
|
||||||
function(root, path, value){
|
function(root, path, value){
|
||||||
path = str2path(path)
|
path = str2path(path)
|
||||||
|
//console.log(' ', path, value)
|
||||||
// special case: get/set root...
|
// special case: get/set root...
|
||||||
if(path.length == 0 || path[0] == '/'){
|
if(path.length == 0 || path[0] == '/'){
|
||||||
return arguments.length > 2 ?
|
return arguments.length > 2 ?
|
||||||
@ -462,13 +493,18 @@ function(root, path, value){
|
|||||||
// value in content...
|
// value in content...
|
||||||
if(mode == 'content'){
|
if(mode == 'content'){
|
||||||
mode = 'content-item'
|
mode = 'content-item'
|
||||||
return cur instanceof Set ?
|
return (
|
||||||
|
// set item...
|
||||||
|
cur instanceof Set ?
|
||||||
[...cur][p]
|
[...cur][p]
|
||||||
// map key...
|
// map key...
|
||||||
: p.slice(-4) == '@key' ?
|
: typeof(p) != 'number'
|
||||||
[...cur][p.slice(0, -4)][0]
|
&& p.slice(-4) == '@key' ?
|
||||||
|
// NOTE: we can write to a non-existant item...
|
||||||
|
([...cur][p.slice(0, -4)] || [])[0]
|
||||||
// map value...
|
// map value...
|
||||||
: [...cur][p][1] }
|
: [...cur][p][1] )}
|
||||||
|
// attr...
|
||||||
mode = 'normal'
|
mode = 'normal'
|
||||||
base = cur
|
base = cur
|
||||||
return cur[p] }, root)
|
return cur[p] }, root)
|
||||||
@ -499,7 +535,8 @@ function(root, path, value){
|
|||||||
return value }
|
return value }
|
||||||
|
|
||||||
// write map item/key...
|
// write map item/key...
|
||||||
var isKey = index.slice(-4) == '@key'
|
var isKey = typeof(index) != 'number'
|
||||||
|
&& index.slice(-4) == '@key'
|
||||||
index = isKey ?
|
index = isKey ?
|
||||||
index.slice(0, -4)
|
index.slice(0, -4)
|
||||||
: index
|
: index
|
||||||
@ -510,7 +547,9 @@ function(root, path, value){
|
|||||||
value === module.EMPTY ?
|
value === module.EMPTY ?
|
||||||
base.delete(key)
|
base.delete(key)
|
||||||
: isKey ?
|
: isKey ?
|
||||||
base.replaceKey(key, value)
|
(base.has(key) ?
|
||||||
|
base.replaceKey(key, value)
|
||||||
|
: base.set(value, undefined))
|
||||||
: base.set(key, value)
|
: base.set(key, value)
|
||||||
|
|
||||||
// XXX should we return value or base???
|
// XXX should we return value or base???
|
||||||
@ -524,7 +563,7 @@ module.write =
|
|||||||
function(root, spec){
|
function(root, spec){
|
||||||
return types.generator.iter(spec)
|
return types.generator.iter(spec)
|
||||||
.reduce(function(root, [path, value, ...rest]){
|
.reduce(function(root, [path, value, ...rest]){
|
||||||
console.log('>>>>', path2str(path), value)
|
//console.log('>>>>', path2str(path), value)
|
||||||
// generate/normalize value...
|
// generate/normalize value...
|
||||||
value =
|
value =
|
||||||
// XXX STUB...
|
// XXX STUB...
|
||||||
@ -559,6 +598,9 @@ function(root, spec){
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// XXX move to test...
|
// XXX move to test...
|
||||||
|
|
||||||
@ -599,13 +641,14 @@ var o = module.o = {
|
|||||||
z: 'shadowing',
|
z: 'shadowing',
|
||||||
}),
|
}),
|
||||||
|
|
||||||
// XXX
|
/*/ XXX
|
||||||
func: function(){},
|
func: function(){},
|
||||||
func_with_attrs: Object.assign(
|
func_with_attrs: Object.assign(
|
||||||
function(){},
|
function(){},
|
||||||
{
|
{
|
||||||
x: 333,
|
x: 333,
|
||||||
}),
|
}),
|
||||||
|
//*/
|
||||||
|
|
||||||
array_with_attrs: Object.assign(
|
array_with_attrs: Object.assign(
|
||||||
[1, 2, 3],
|
[1, 2, 3],
|
||||||
@ -618,11 +661,13 @@ var o = module.o = {
|
|||||||
|
|
||||||
'special/character\\in:key': [],
|
'special/character\\in:key': [],
|
||||||
|
|
||||||
|
/* XXX EXPERIMENTAL
|
||||||
text: `this
|
text: `this
|
||||||
is
|
is
|
||||||
a
|
a
|
||||||
multi-line
|
multi-line
|
||||||
block of text...`,
|
block of text...`,
|
||||||
|
//*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// loop...
|
// loop...
|
||||||
@ -639,8 +684,14 @@ console.log([
|
|||||||
stripAttr('source'),
|
stripAttr('source'),
|
||||||
)])
|
)])
|
||||||
|
|
||||||
//console.log('---\n',
|
|
||||||
// write(null, handle(o)))
|
console.log('\n\n---\n',
|
||||||
|
[...handle(write(null, handle(o)))
|
||||||
|
.chain(
|
||||||
|
serializePaths,
|
||||||
|
// make the output a bit more compact...
|
||||||
|
stripAttr('source'),
|
||||||
|
)])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user