A TypeScript library, that aggregates record changes, into a single stream.
npm install flutate
yarn add flutate
const newState = flutate(state)
.clone()
.update('prop.name', 'new value')
.update('prop.rows', (rows) => rows.add(item))
.update('prop.rows', (rows) => rows.replace({ id: '123' }, newItem))
.update('prop.rows', (rows) =>
rows.at({ id: '123' }, (row) => row.update('note', 'new note value'))
)
.done()
const record = flutate(source)
Update specified parameter.
The path can specify the hierarchy of the object by separating them with dots.
flutate({
value1: 'example_1',
value2: {
value21: 'example_2_1'
}
})
.update('value1', 'new value')
.update('value2.value21', 'new value')
For array manipulation.
flutate({
values: [
{ id: 1, value: 'item_1' },
{ id: 2, value: 'item_2' }
]
}).update('values', (items) => items.add({ id: 3, value: 'item_3' }))
Deep copy record value, and return new instance.
const newRecord = record.clone()
Get value from record, by dot's path.
const value = flutate({
path: {
to: 'example'
}
}).get('path.to')
// return "example"
Return mutated result value.
const result = record.done()
For use in record.update
method's mutation function.
record.update('array', (collection) => {
// manipulate array by collection methods
collection.add(...)
return collation
})
For finding item by index and partial match of properties.
// by index
collection.remove(1)
// by partial match
collection.remove({ id: 1 })
Add item in collection the last.
collection.add({
id: 1,
value: 'item_1'
})
Remove item in collection.
collection.remove({ id: 1 })
Replace item in collection.
collection.replace({ id: 1 }, { id: 1, value: 'new value' })
Move item position by direction.
If you specify "forward" as the direction, the element will move forward. If you specify "backward", the element will move backward.
collection.move({ id: 1 }, 'forward').move({ id: 1 }, 'backward')
Mutate element value by mutate function with FlutateRecord
.
collection.at({ id: 1 }, (record) => record.update('value', 'new value'))