mirror of
https://github.com/flynx/diff.js.git
synced 2025-10-29 02:50:10 +00:00
tweaking and experimenting...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
1ad987190d
commit
472ab7dc8a
87
diff2.js
87
diff2.js
@ -364,17 +364,20 @@ function(p){
|
|||||||
.join('/') }
|
.join('/') }
|
||||||
|
|
||||||
|
|
||||||
// XXX do we handle @key here???
|
|
||||||
var deserializePathElem = function(p){
|
var deserializePathElem = function(p){
|
||||||
return /[^\\]:CONTENT$/.test(p) ?
|
return p == ':CONTENT'?
|
||||||
[p.replace(/(?<!\\):CONTENT$/, ''), module.CONTENT]
|
[module.CONTENT]
|
||||||
|
: /[^\\]:CONTENT$/.test(p) ?
|
||||||
|
[p.replace(/(?<!\\):CONTENT$/, ''), module.CONTENT]
|
||||||
: [p] }
|
: [p] }
|
||||||
// XXX should we hanve relative paths????
|
// XXX should we hanve relative paths????
|
||||||
var str2path =
|
var str2path =
|
||||||
module.str2path =
|
module.str2path =
|
||||||
function(str){
|
function(str){
|
||||||
return str instanceof Array ?
|
return str instanceof Array ?
|
||||||
str
|
str
|
||||||
|
: str == '' || str == '/' ?
|
||||||
|
[]
|
||||||
: (str
|
: (str
|
||||||
.replace(/^\//, '')
|
.replace(/^\//, '')
|
||||||
.split(/\//g)
|
.split(/\//g)
|
||||||
@ -407,7 +410,7 @@ var stripAttr =
|
|||||||
module.stripAttr =
|
module.stripAttr =
|
||||||
function(...attrs){
|
function(...attrs){
|
||||||
return types.generator.iter
|
return types.generator.iter
|
||||||
.map(function([p, v]){
|
.map(function([p, v, ...rest]){
|
||||||
if(v && typeof(v) == 'object'){
|
if(v && typeof(v) == 'object'){
|
||||||
// keep things non-destructive...
|
// keep things non-destructive...
|
||||||
v = Object.assign({}, v)
|
v = Object.assign({}, v)
|
||||||
@ -415,7 +418,7 @@ function(...attrs){
|
|||||||
.forEach(function(attr){
|
.forEach(function(attr){
|
||||||
attr in v
|
attr in v
|
||||||
&& (delete v[attr]) }) }
|
&& (delete v[attr]) }) }
|
||||||
return [p, v] }) }
|
return [p, v, ...rest] }) }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -432,14 +435,20 @@ function(...attrs){
|
|||||||
//
|
//
|
||||||
// NOTE: to set this needs the full basepath to exist...
|
// NOTE: to set this needs the full basepath to exist...
|
||||||
//
|
//
|
||||||
// XXX overcomplicated...
|
// XXX how do we get :CONTENT of root???
|
||||||
// XXX add support for deleting / writeing EMPTY
|
// 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...
|
||||||
var atPath =
|
var atPath =
|
||||||
module.atPath =
|
module.atPath =
|
||||||
function(root, path, value){
|
function(root, path, value){
|
||||||
path = str2path(path)
|
path = str2path(path)
|
||||||
|
// special case: get/set root...
|
||||||
|
if(path.length == 0 || path[0] == '/'){
|
||||||
|
return arguments.length > 2 ?
|
||||||
|
value
|
||||||
|
: root }
|
||||||
// get value at path...
|
// get value at path...
|
||||||
var content = false
|
var mode = 'normal'
|
||||||
var base
|
var base
|
||||||
var target = path
|
var target = path
|
||||||
.reduce(function(cur, p){
|
.reduce(function(cur, p){
|
||||||
@ -447,11 +456,12 @@ function(root, path, value){
|
|||||||
if(p === module.CONTENT){
|
if(p === module.CONTENT){
|
||||||
// NOTE: indicate to the next iteration that we'll need
|
// NOTE: indicate to the next iteration that we'll need
|
||||||
// to use map/set API to get/set the value...
|
// to use map/set API to get/set the value...
|
||||||
content = true
|
mode = 'content'
|
||||||
base = cur
|
base = cur
|
||||||
return cur }
|
return cur }
|
||||||
// value in content...
|
// value in content...
|
||||||
if(content){
|
if(mode == 'content'){
|
||||||
|
mode = 'content-item'
|
||||||
return cur instanceof Set ?
|
return cur instanceof Set ?
|
||||||
[...cur][p]
|
[...cur][p]
|
||||||
// map key...
|
// map key...
|
||||||
@ -459,7 +469,7 @@ function(root, path, value){
|
|||||||
[...cur][p.slice(0, -4)][0]
|
[...cur][p.slice(0, -4)][0]
|
||||||
// map value...
|
// map value...
|
||||||
: [...cur][p][1] }
|
: [...cur][p][1] }
|
||||||
content = false
|
mode = 'normal'
|
||||||
base = cur
|
base = cur
|
||||||
return cur[p] }, root)
|
return cur[p] }, root)
|
||||||
|
|
||||||
@ -468,13 +478,13 @@ function(root, path, value){
|
|||||||
return target }
|
return target }
|
||||||
|
|
||||||
// write attr...
|
// write attr...
|
||||||
if(!content){
|
if(mode != 'content-item'){
|
||||||
value === module.EMPTY ?
|
value === module.EMPTY ?
|
||||||
(delete base[path.last()])
|
(delete base[path.last()])
|
||||||
: (base[path.last()] = value)
|
: (base[path.last()] = value)
|
||||||
// XXX should we return value or base???
|
// XXX should we return value or base???
|
||||||
// ...should we return target when writing EMPTY
|
// ...should we return target when writing EMPTY
|
||||||
return value}
|
return value }
|
||||||
|
|
||||||
var index = path.last()
|
var index = path.last()
|
||||||
// write set item...
|
// write set item...
|
||||||
@ -508,6 +518,42 @@ function(root, path, value){
|
|||||||
return value }
|
return value }
|
||||||
|
|
||||||
|
|
||||||
|
// XXX rename to patch(..)
|
||||||
|
var write =
|
||||||
|
module.write =
|
||||||
|
function(root, spec){
|
||||||
|
return types.generator.iter(spec)
|
||||||
|
.reduce(function(root, [path, value, ...rest]){
|
||||||
|
console.log('>>>>', path2str(path), value)
|
||||||
|
// generate/normalize value...
|
||||||
|
value =
|
||||||
|
// XXX STUB...
|
||||||
|
typeof(value) == 'function' ?
|
||||||
|
value.source
|
||||||
|
: value == null || typeof(value) != 'object' ?
|
||||||
|
value
|
||||||
|
// XXX move this out / use HANDLERS...
|
||||||
|
: value.type == 'Object' ?
|
||||||
|
{}
|
||||||
|
: value.type == 'Array' ?
|
||||||
|
[]
|
||||||
|
: value.type == 'Set' ?
|
||||||
|
new Set()
|
||||||
|
: value.type == 'Map'?
|
||||||
|
new Map()
|
||||||
|
: undefined
|
||||||
|
// root value...
|
||||||
|
if(path.length == 0
|
||||||
|
|| (path.length == 1
|
||||||
|
&& (path[0] == '/' || path[0] == ''))){
|
||||||
|
return value }
|
||||||
|
// link...
|
||||||
|
if(rest.length > 0 && value == 'LINK'){
|
||||||
|
atPath(root, path, atPath(root, rest[0]))
|
||||||
|
return root }
|
||||||
|
// set value...
|
||||||
|
atPath(root, path, value)
|
||||||
|
return root }, root) }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -516,7 +562,7 @@ function(root, path, value){
|
|||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// XXX move to test...
|
// XXX move to test...
|
||||||
|
|
||||||
var o = {
|
var o = module.o = {
|
||||||
number: 123,
|
number: 123,
|
||||||
string: 'abc',
|
string: 'abc',
|
||||||
|
|
||||||
@ -579,20 +625,12 @@ var o = {
|
|||||||
block of text...`,
|
block of text...`,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
// NOTE: we are creating the loop before we pass it to JSON because JSON
|
||||||
// does not support loops in objects...
|
// does not support loops in objects...
|
||||||
o.object.y = o.object
|
o.object.y = o.object
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
console.log([
|
console.log([
|
||||||
...handle(o)
|
...handle(o)
|
||||||
.chain(
|
.chain(
|
||||||
@ -601,7 +639,8 @@ console.log([
|
|||||||
stripAttr('source'),
|
stripAttr('source'),
|
||||||
)])
|
)])
|
||||||
|
|
||||||
//console.log([...handle(o)])
|
//console.log('---\n',
|
||||||
|
// write(null, handle(o)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user