retarget
v0.0.2
Published
Create selectors for working with redux store data
Downloads
4
Readme
This is experimental. I would only use it if you are comfortable with breaking changes.
retarget
selectors via targets
Install
npm i retarget -S
or
yarn add retarget
import retarget from 'retarget'
const STATE = {
profile: {
name: {
first: 'Waylon',
last: 'Jennings'
}
}
}
const lastNameSelector = retarget`profile.name.last`
console.log(lastNameSelector(STATE)) // logs "Jennings"
Composing
It is possible to compose multiple selectors together to create a new one.
import retarget from 'retarget'
const STATE = {
users: {
'1': {
profile: {
name: {
first: 'Waylon',
last: 'Jennings'
}
}
}
}
}
const lastNameSelector = retarget`profile.name.last`
const createUserSelector = (id) =>
retarget`users.${id}${lastNameSelector}`
const userSelector = createUserSelector(1)
console.log(userSelector(STATE)) // logs "Jennings"
API
retarget function
import retarget from 'retarget'
const selector = retarget`dot.seperated.path.to.value`
const state = {/* Huge object */}
selector(state) // returns the value at "dot.seperated.path.to.value"
Returns
retarget
returns a function that when passed an obj, attempts to get the value for the given path.