diff --git a/diff2.js b/diff2.js index 3343096..4f44b88 100644 --- a/diff2.js +++ b/diff2.js @@ -364,17 +364,20 @@ function(p){ .join('/') } -// XXX do we handle @key here??? var deserializePathElem = function(p){ - return /[^\\]:CONTENT$/.test(p) ? - [p.replace(/(? 2 ? + value + : root } // get value at path... - var content = false + var mode = 'normal' var base var target = path .reduce(function(cur, p){ @@ -447,11 +456,12 @@ function(root, path, value){ if(p === module.CONTENT){ // NOTE: indicate to the next iteration that we'll need // to use map/set API to get/set the value... - content = true + mode = 'content' base = cur return cur } // value in content... - if(content){ + if(mode == 'content'){ + mode = 'content-item' return cur instanceof Set ? [...cur][p] // map key... @@ -459,7 +469,7 @@ function(root, path, value){ [...cur][p.slice(0, -4)][0] // map value... : [...cur][p][1] } - content = false + mode = 'normal' base = cur return cur[p] }, root) @@ -468,13 +478,13 @@ function(root, path, value){ return target } // write attr... - if(!content){ + if(mode != 'content-item'){ value === module.EMPTY ? (delete base[path.last()]) : (base[path.last()] = value) // XXX should we return value or base??? // ...should we return target when writing EMPTY - return value} + return value } var index = path.last() // write set item... @@ -508,6 +518,42 @@ function(root, path, 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... -var o = { +var o = module.o = { number: 123, string: 'abc', @@ -579,20 +625,12 @@ var o = { block of text...`, } -// clone... -// NOTE: JSON does not support: -// - sparse arrays -// = sets/maps -// - loops -oo = JSON.parse(JSON.stringify(o)) - // 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 - console.log([ ...handle(o) .chain( @@ -601,7 +639,8 @@ console.log([ stripAttr('source'), )]) -//console.log([...handle(o)]) +//console.log('---\n', +// write(null, handle(o)))