Merge the latest version of Speedometer 2.0 to browserbench.org against at r221119.
[WebKit-https.git] / Websites / browserbench.org / Speedometer2.0 / resources / todomvc / architecture-examples / inferno / src / model.js
1 import { assign, isEqual } from './share';
2
3 const STOR = {};
4 const STOR_ID = 'todos-inferno';
5
6 export default class Model {
7     get = () => (this.data = JSON.parse(STOR[STOR_ID] || '[]'))
8
9     set = arr => {
10         this.data = arr || this.data || [];
11         STOR[STOR_ID] = JSON.stringify(this.data);
12         return this.data;
13     }
14
15     add = str => this.set(
16         this.data.concat({title: str, completed: false})
17     )
18
19     put = (todo, obj) => this.set(
20         this.data.map(t => isEqual(t, todo) ? assign(todo, obj) : t)
21     )
22
23     del = todo => this.set(
24         this.data.filter(t => !isEqual(t, todo))
25     )
26
27     toggle = todo => this.put(todo, {completed: !todo.completed})
28
29     toggleAll = completed => this.set(
30         this.data.map(t => ({...t, completed}))
31     )
32
33     clearCompleted = () => this.set(
34         this.data.filter(t => !t.completed)
35     )
36 }