brisky-state-get
v1.0.3
Published
Get state object, using Brisky subscription notation
Downloads
3
Readme
brisky-state-get
Get state object, using Brisky subscription notation.
var stateObj = stateGet(state, path)
Get state at path
- state (object) - state startpoint
- path (array) - array of keys to follow
- {*} [set] - value to set if property is undefined (optional)
- returns (object) stateObj - state at endpoint
const get = require('brisky-state-get')
const s = require('vigour-state/s')
const state = s({
pages: {
discover: {
carousel: {}
},
shows: {
episodes: [
{ title: 1 },
{ title: 2 },
{ title: 3 }
]
}
}
})
get(state, ['pages', 'discover', 'carousel']) // → state.pages.discover.carousel
get(state.pages.discover, ['$parent', 'shows']) // → state.pages.shows
get(state.pages.discover, ['$root', 'pages', 'shows']) // → state.pages.shows
get(state.pages.shows, ['episodes', '$any']) // → state.pages.shows.episodes
get(state, ['pages', '$switch']) // → state.pages
get(state, ['pages', '$condition']) // → state.pages
get(state, ['pages', 'channels'], { foo: 'bar' }).foo.compute() // → 'bar'