Yomguithereal / baobab

JavaScript & TypeScript persistent and optionally immutable data tree with cursors.
MIT License
3.15k stars 115 forks source link

Listening to elements in Array not working with strings #485

Closed psypersky closed 7 years ago

psypersky commented 7 years ago

Hi there, when listening to elements in arrays everything works OK when selecting the element with ['foo', 0] and it does not works when selecting with foo.0, is this how it should work?

I'm having problems with Cerebral.js since cerebral parses everything into a string, by the polymorphism of the selectors it looks to me that it should work that way but I'm not totally sure.

var Baobab = require("baobab");
var tree = new Baobab({
    foo: [
        { id: 0, val: 'zero' },
        { id: 1, val: 'one' },
    ]
});
tree.select(['foo', 0]).on('update', () => console.log('zero updated 1'));
tree.select('foo.0').on('update', () => console.log('zero updated 2'));
tree.merge(['foo', { id: 0 }], { val: 'zzeerroo' });

Here is the test in Runkit: https://runkit.com/58c9b468b035460013583d34/58e2fe1ee08ac000145eff6f

Thanks for the cool library :1st_place_medal:

Yomguithereal commented 7 years ago

Hello @psypersky. Baobab does not support string sugar for selection like your example foo.0 to avoid having to parse strings and deal with escaping etc. (I know weird people who use dots in their properties' names...). So, your result is to be expected.

psypersky commented 7 years ago

I see, yeah that makes sense, then this is just a cerebral problem.

Thank you very much for the help! 😄