Last active
October 19, 2015 02:34
-
-
Save antonycourtney/1724be25e086a4f1d42a to your computer and use it in GitHub Desktop.
Purely functional data structure for Todo list application state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* todoAppState.js -- Application State for TodoMVC as | |
* an immutable record | |
*/ | |
export default class TodoAppState extends Immutable.Record({ | |
todoItems: Immutable.Map() // map from id to TodoItem | |
}) { | |
/** | |
* functional item update -- returns a new state with the given item included in the | |
* set of todo items. If there is an existing entry for item.id, the result state | |
* will map id to item (functional update). | |
*/ | |
addItem(item) { | |
const nextTodoItems = this.todoItems.set(item.id,item); | |
return this.set('todoItems',nextTodoItems); | |
} | |
/** | |
* functional delete -- returns a new state with the item for the given id removed | |
*/ | |
removeItem(id) { | |
const nextTodoItems = this.todoItems.delete(id); | |
return this.set('todoItems',nextTodoItems); | |
} | |
/** An Immutable.Seq of all todo items */ | |
getAll() { | |
return this.todoItems.toSetSeq(); | |
} | |
/* returns true iff all items are complete */ | |
areAllComplete() { | |
return this.todoItems.every((item) => item.complete); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment