0.1.1 • Published 7 years ago
recd v0.1.1
recd
Simple data transformations
Install
npm install --save recd
Usage
import createData from 'recd'
const profile = createData({})
const setName = profile.transform((data, name) => data({name}))
profile.subscribe(data => console.log(data))
setName('unicorn')
profile.pull() // {name: 'unicorn'}
API
createData(initial)
Create and return data
instance with initial
.
initial
Type: object
data.pull(next)
Pull current data.
next
Type: object
Method merge current data snapshot with next
if specified and return.
data.subscribe(listener)
Add listener invoke after data is changed.
listener(next)
Type: function
next
Type: object
Updated data snapshot.
data.transform(fn)
Create data transform function.
fn(pull, ...args)
Type: function
pull
Type: function
Link for data.pull
method.
Sync transform:
const setLoading = profile.transform((data, loading) => data({loading}))
Async transform:
const getProfile = profile.transform(async (data, id) => {
if (data().loading) {
return
}
setLoading(true)
const info = await request(id)
return data({
...info,
loading: false
})
})
data(next)
Alias for data.pull([next])
.
data(fn)
Alias for data.transform(fn)
.
License
MIT