diff --git a/package.json b/package.json index b491eb9..ba79b51 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,6 @@ }, "homepage": "https://github.com/flynx/serialize.js#readme", "dependencies": { - "ig-test": "^1.5.9" + "ig-test": "^1.6.*" } } diff --git a/serialize2.js b/serialize2.js index 7309cb3..ce21158 100644 --- a/serialize2.js +++ b/serialize2.js @@ -212,14 +212,41 @@ module.eJSON = { +` ${line}: ${ str.slice(pre, post) }\n` +` ${ ' '.repeat( i - pre + ((line + ': ').length) ) }^`) }, - getItem: function(obj, path){ - for(var k of path){ + // + // ._getItem(obj, path) + // -> [path, obj] + // + // NOTE: we are returning path here because we need to distinguish + // incomplete paths that we can infer the container from and + // complete paths, e.g: + // For + // m = [new Map([[1,2], [2,3]])] + // Both of the below will return the map and the correct path ([0]): + // eJSON._getItem(m, [0, 1]) + // eJSON._getItem(m, [0]) + // But we need to destinguish between the two cases when + // writing to a map (see: .setItem(..)) + _getItem: function(obj, path){ + for(var i=0; i obj + // + // NOTE: this is a POLS wrapper of ._getItem(..) + getItem: function(obj, path){ + return this._getItem(...arguments)[1] }, // NOTE: this behaves in a similar way to normal key value assignment, // i.e. replacing items if they exist, this is not a problem here // as in the general case we are assigning over a placeholder @@ -227,16 +254,32 @@ module.eJSON = { // NOTE: as a side-effect this maintains element oreder even in object // with no direct concept of element order, like objects, sets and // maps. + // NOTE: some operations envolve rewriting the container elements so + // are not as fast, namely writing set elements and map keys. setItem: function(obj, path, value){ - var parent = this.getItem(obj, path.slice(0, -1)) + var [p, parent] = this._getItem(obj, path.slice(0, -1)) var k = path.at(-1) - if(obj instanceof Set){ + if(parent instanceof Set){ var elems = [...parent] elems[k] = obj + // we cant to keep the order... + parent.clear() for(var e of elems){ parent.add(e) } - } else if(obj instanceof Map){ - parent.set(k, value) + } else if(parent instanceof Map){ + if(path.length-2 !== p.length){ + throw new Error('.setItem(..): incomplete path.') } + var i = path.at(-2) + // replace the index... + if(k == 0){ + var elems = [...parent] + elems[i][0] = value + parent.clear() + for(var e of elems){ + parent.set(...e) } + // set the value... + } else { + parent.set([...parent][i][0], value) } } else { parent[k] = value } return value }, diff --git a/test.js b/test.js index 1212faa..9342e3a 100755 --- a/test.js +++ b/test.js @@ -72,7 +72,7 @@ test.Tests({ var res assert( setup == (res = eJSON.serialize(eJSON.deserialize(setup))), - 'serialize(deserialize( setup )) == setup: expected:', setup, 'got:', res) }, + 'serialize(deserialize( setup )) == setup: expected:', setup, 'got:', res) }, })