Skip to content

Instantly share code, notes, and snippets.

@Beraliv
Last active February 3, 2019 19:57
Show Gist options
  • Save Beraliv/2e219aefb60bb9063119340c77a10566 to your computer and use it in GitHub Desktop.
Save Beraliv/2e219aefb60bb9063119340c77a10566 to your computer and use it in GitHub Desktop.
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