Skip to content

Instantly share code, notes, and snippets.

@agoldis
Created April 27, 2018 06:03
Show Gist options
  • Save agoldis/1d95268e8de82e65f5458badfb7fcd5f to your computer and use it in GitHub Desktop.
Save agoldis/1d95268e8de82e65f5458badfb7fcd5f to your computer and use it in GitHub Desktop.
redux - full example
import { compose } from "redux";
const usersSummary = function() {
return this.users.map(user => ({
...user,
booksCount: user.books.length,
commentsCount: this.comments.filter(c => c.user === user.id).length
}));
};
const booksSummary = function() {
return this.books.map(book => ({
...book,
readersCount: this.users.filter(u => u.books.indexOf(book.isbn) > -1)
.length,
commentsCount: this.comments.filter(c => c.book === book.isbn).length
}));
};
const addUsersSummary = state =>
Object.defineProperty(state, "usersSummary", {
get: usersSummary
});
const addBooksSummary = state =>
Object.defineProperty(state, "booksSummary", {
get: booksSummary
});
export const deriveState = compose(addBooksSummary, addUsersSummary);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment