Last active
February 3, 2019 19:57
-
-
Save Beraliv/2e219aefb60bb9063119340c77a10566 to your computer and use it in GitHub Desktop.
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
import { filterT, map, mapT, pipeT, takeT, transduce, where } from 'nanoutils'; | |
const isEven = value => value % 2 === 0; | |
const filterPredicates = { | |
name: ({ author }) => name => author === undefined || name === author, | |
timestamp: ({ from, to }) => timestamp => | |
from === undefined || | |
to === undefined || | |
(from <= timestamp && timestamp <= to) | |
}; | |
const pushReducer = (arr, value) => { | |
arr.push(value); | |
return arr; | |
}; | |
const transducers = (posts, filters) => { | |
// iterates over filters and specifies filter values | |
const specificFilters = map(filter => filter(filters), filterPredicates); | |
// creates validation using Object mapping with post | |
const withFilters = where(specificFilters); | |
const transducer = pipeT( | |
// applies filters | |
filterT(withFilters), | |
// takes first post | |
takeT(1) | |
); | |
// iterates over posts | |
// applies pusReducer to prevent recreation of new arrays | |
// starts from [] | |
// gets values from posts | |
return transduce(transducer, pushReducer, [], posts); | |
}; | |
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 | |
} | |
]; | |
transducers(posts, { author: "Joel Will" }) // [{ name: 'Joel Will', title: 'Ea consequatur eveniet ut qui et quae voluptatem.', timestamp: 1531221363376 }] | |
transducers(posts, { author: "Joel Will", from: 0, to: 1531221363370 }) // [] | |
transducers(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