keyed callbag states
npm install callbag-state-keyedš Key-track callbag-states:
``ts
import state from 'callbag-state';
import keyed from 'callbag-state-keyed';
import subscribe from 'callbag-subscribe';
const s = state([
{ id: 1, name: 'John' },
{ id: 2, name: 'Jack' },
{ id: 3, name: 'Jill' },
]);
const k = keyed(s, p => p.id);
subscribe(console.log)(k.key(2)); // > { id: 2, name: 'Jack' }
s.set([
{ id: 2, name: 'Jack' },
{ id: 3, name: 'Jill' }
]); // --> no changes to id 2, no logs
s.set([
{ id: 2, name: 'Joe' },
{ id: 3, name: 'Jill' }
]); // > { id: 2, name: 'Joe' }
`
āŗ TRY IT!
š Modify key-tracked sub-states:
`ts`
const jill = k.key(3);
subscribe(console.log)(jill); // > { id: 3, name: 'Jill' }
jill.sub('name').set('Jillian'); // > { id: 3, name: 'Jillian' }
āŗ TRY IT!
š Track index of a particular key:
`tsid: 2
subscribe(console.log)(k.index(2)); // --> index of element with `
// > 0
s.set([
{ id: 3, name: 'Jillian' },
{ id: 2, name: 'Joe' }
]); // > 1
āŗ TRY IT!
š Get detailed list changes for a keyed list:
`ts
subscribe(console.log)(k.changes());
s.set([
{ id: 4, name: 'Joseph' },
{ id: 3, name: 'Jillian' },
{ id: 1, name: 'John' },
]);
// > {
// > additions: [
// > { index: 0, item: { id: 4, name: 'Joseph' } },
// > { index: 2, item: { id: 1, name: 'John' } },
// > ],
// > deletions: [
// > { index: 1, item: { id: 2, name: 'Joe' } }
// > ],
// > moves: [
// > { item: { id: 3, name: 'Jillian' }, oldIndex: 0, newIndex: 1 }
// > ]
// > }
`
āŗ TRY IT!
Same as callbag-state.
Be nice and respectful, more importantly super open and welcoming to all.
š Useful commands for working on this repo:
`bash`
git clone https://github.com/loreanvictor/callbag-state-keyed.git`bash`
npm i # --> install dependencies`bashsamples/index.ts
npm start # --> run ``bash`
npm test # --> run all tests`bash``
npm run cov:view # --> view code coverage