now eJSON.getItem(..) / eJSON.setItem(..) fully work...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2025-12-30 03:11:24 +03:00
parent 0322edaed1
commit 439a68eeb7
3 changed files with 53 additions and 10 deletions

View File

@ -18,6 +18,6 @@
}, },
"homepage": "https://github.com/flynx/serialize.js#readme", "homepage": "https://github.com/flynx/serialize.js#readme",
"dependencies": { "dependencies": {
"ig-test": "^1.5.9" "ig-test": "^1.6.*"
} }
} }

View File

@ -212,14 +212,41 @@ module.eJSON = {
+` ${line}: ${ str.slice(pre, post) }\n` +` ${line}: ${ str.slice(pre, post) }\n`
+` ${ ' '.repeat( i - pre + ((line + ': ').length) ) }^`) }, +` ${ ' '.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<path.length; i++){
var k = path[i]
// speacial case: incomplete map index...
if( obj instanceof Map
&& i == path.length-1){
return [path.slice(0, -1), obj] }
obj = obj instanceof Set ? obj = obj instanceof Set ?
[...obj][k] [...obj][k]
: obj instanceof Map ? : obj instanceof Map ?
obj.get(k) [...obj][k][path[++i]]
: obj[k] } : obj[k] }
return obj }, return [path, obj] },
//
// .getItem(obj, path)
// -> 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, // 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 // i.e. replacing items if they exist, this is not a problem here
// as in the general case we are assigning over a placeholder // 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 // NOTE: as a side-effect this maintains element oreder even in object
// with no direct concept of element order, like objects, sets and // with no direct concept of element order, like objects, sets and
// maps. // 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){ 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) var k = path.at(-1)
if(obj instanceof Set){ if(parent instanceof Set){
var elems = [...parent] var elems = [...parent]
elems[k] = obj elems[k] = obj
// we cant to keep the order...
parent.clear()
for(var e of elems){ for(var e of elems){
parent.add(e) } parent.add(e) }
} else if(obj instanceof Map){ } else if(parent instanceof Map){
parent.set(k, value) 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 { } else {
parent[k] = value } parent[k] = value }
return value }, return value },

View File

@ -72,7 +72,7 @@ test.Tests({
var res var res
assert( assert(
setup == (res = eJSON.serialize(eJSON.deserialize(setup))), setup == (res = eJSON.serialize(eJSON.deserialize(setup))),
'serialize(deserialize( setup )) == setup: expected:', setup, 'got:', res) }, 'serialize(deserialize( setup )) == setup: expected:', setup, 'got:', res) },
}) })