omniscientjs / immstruct

Immutable data structures with history for top-to-bottom properties in component based libraries like React. Based on Immutable.js
374 stars 21 forks source link

Support string keys to `cursor()` #20

Closed torgeir closed 9 years ago

torgeir commented 9 years ago

This would be handy

var keypath = immstruct({ some: { long: { keypath: 42 } } }).cursor('some.long.keypath');
dashed commented 9 years ago

See: https://github.com/facebook/immutable-js/issues/237

torgeir commented 9 years ago

Well if you know a keypath and what data you're getting at in advance I don't see why something like this would not work. For routing/dynamic paths, that's another story.

I was thinking more of a shorthand if (keypath.indexOf('.') !== -1) { return keypath.split('.') }. You are already doing ['some', 'long', 'keypath'] anyways

dashed commented 9 years ago

Well, map keys don't need to be just strings.

From your example, if you set keyPath conventions for your project, you can have a simple utility parsing function:

// utils.js in your project
function pathParsing(path) {
    return path.split('/');
}

var keypath = immstruct({ some: { long: { keypath: 42 } } })
                .cursor(pathParsing('some/long/keypath'));
torgeir commented 9 years ago

Aye :) Seems tedious, tho

dashed commented 9 years ago

You'll probably also need to wrap the immstruct library to get the API flavour you want (i.e. less tedious).

If this gets baked into immstruct, there needs to be a nota bene in the README.

mikaelbr commented 9 years ago

This seems like a really nice-to-have feature, but I don't really think this something that should be a part of immstruct. There would also be alot of confusion, as immstruct only exposes the "first level cursor".

Where as if you do:

structure.cursor('foo').cursor('bar');

The first .cursor method will be immstruct, but the second will be Immutable.js cursor. So we would either have to live with the difference, or patching each cursor level. Neither sound like a feasible solution in my mind, really.

torgeir commented 9 years ago

Oh, you're right, I didn't think of that. That makes it pretty much a no-go then..