Last active
February 3, 2019 19:15
-
-
Save Beraliv/fbdfa45384d86d7a8bab6a387e296922 to your computer and use it in GitHub Desktop.
This file contains 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
const isEven = value => value % 2 === 0; | |
const filterPredicates = { | |
// if available, checks if names equal | |
author: ({ name }, { author }) => author === undefined || name === author, | |
// if available, checks if date starts from a specified value | |
from: ({ timestamp }, { from }) => from === undefined || from <= timestamp, | |
// if available, checks if date ends with a specified value | |
to: ({ timestamp }, { to }) => to === undefined || timestamp <= to | |
}; | |
const filterEverySlice = (posts, filters) => | |
// iterates over posts | |
posts | |
// applies filters | |
.filter(post => | |
// checks all filters are applied to a specified post | |
Object.keys(filters).every(key => filterPredicates[key](post, filters)) | |
) | |
// gets first filtered posts | |
.slice(0, 1); | |
const posts = [ | |
{ | |
name: "Lorna Stiedemann", | |
title: "Fugiat qui maiores illo.", | |
timestamp: 1534534191881 | |
}, | |
{ | |
name: "Joel Will", | |
title: "Ea consequatur eveniet ut qui et quae voluptatem.", | |
timestamp: 1531221363376 | |
}, | |
{ | |
name: "Quincy Vandervort", | |
title: "Rerum odit nemo in.", | |
timestamp: 1535462116340 | |
} | |
]; | |
filterEverySlice(posts, { author: "Joel Will" }) // [{ name: 'Joel Will', title: 'Ea consequatur eveniet ut qui et quae voluptatem.', timestamp: 1531221363376 }] | |
filterEverySlice(posts, { author: "Joel Will", from: 0, to: 1531221363370 }) // [] | |
filterEverySlice(posts, { from: 0, to: 1535462116340 }) // [{ name: 'Lorna Stiedemann', title: 'Fugiat qui maiores illo.', timestamp: 1534534191881 }] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment