https://github.com/nichoth/model
Functions for modeling application state
https://github.com/nichoth/model
Last synced: about 1 month ago
JSON representation
Functions for modeling application state
- Host: GitHub
- URL: https://github.com/nichoth/model
- Owner: nichoth
- Created: 2017-01-23T22:34:10.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-23T22:34:18.000Z (over 9 years ago)
- Last Synced: 2025-03-10T06:37:34.217Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://github.com/tvwit/model#readme
- Size: 4.88 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# model
Functions for reducing a log of events into application state.
## Api
`index.js` consumes the folowing events
```js
[
'start',
'resolve',
'pushError',
'shiftError',
'get',
'edit',
'add',
'delete'
];
```
## example
```js
var test = require('tape');
var Model = require('../');
test('compose models', function (t) {
t.plan(1);
var TestModel = Model('id');
var evs = [
['start', { op: 'get' }],
['get', [{ id: 'a' }]],
['resolve', { op: 'get' }],
['pushError', { test: 'test' }]
];
var states = evs.reduce(function (acc, ev) {
var type = ev[0];
var fn = TestModel.update[type];
var state = fn(acc[acc.length - 1], ev[1]);
return acc.concat(state);
}, [TestModel()]);
t.deepEqual(states, [{
resolving: [],
errors: [],
data: {}
}, {
resolving: [{ op: 'get' }],
errors: [],
data: {}
}, {
resolving: [{ op: 'get' }],
errors: [],
data: { a: { id: 'a' } }
}, {
resolving: [],
errors: [],
data: { a: { id: 'a' } }
}, {
resolving: [],
errors: [{ test: 'test' }],
data: { a: { id: 'a' } }
}], 'should compose reduce functions');
});
```