Yomguithereal / baobab

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

Allow to use "xpath" selectors like '/palette/colors/2' #452

Closed ebuster closed 7 years ago

ebuster commented 8 years ago

Instead of proposed selectors format with multiple arguments or array:

var thirdColorCursor = tree.select('palette', 'colors', 2);
var thirdColorCursor = tree.select(['palette', 'colors', 2]);

I would like to use string path selectors: var thirdColorCursor = tree.select('/palette/colors/2);

Implementation promises to be a simple (I didn't read sources yet, sorry):

select() {
  // get first passed argument
  // ...
  if (_.isString(argument) && _.includes(argument, '/')) {
    path = _.split('/', argument)
  }
  // use path var like it was 1st argument
}

Additionally it could be allows to use relative selectors, from already taken cursor:

var paletteCursor = tree.select('/palette'); // absolute
var thirdColorCursor = paletteCursor.select('./colors/2'); // relative from pallete
// or the same but without leading dotslash
var thirdColorCursor = paletteCursor.select('colors/2');
Yomguithereal commented 8 years ago

Hello @ebuster. This would be a nice idea indeed to enable XPath like selection or else but I won't add it to the library's core. However, a distinct library to enhance a tree so it can use this kind of utility by adding, say, a selectXpath or xpath to the tree & cursors coule be a nice addition.