The Convenience of _.chain Without Importing the World
I’ve been meaning to work out how to maintain the convenience of the Lodash’s _.chain
function whilst only including the parts of Lodash that I actually need.
Turns out you can cherry pick the fp
version of the functions you need and compose them together with _.flow
.
import sortBy from "lodash/fp/sortBy";
import flatMap from "lodash/fp/flatMap";
import uniq from "lodash/fp/uniq";
import reverse from "lodash/fp/reverse";
import flow from "lodash/fp/flow";
const exampleData = [
{
happenedAt: "2017-06-15T19:00:00+08:00",
projects: ["Project One"],
},
{
happenedAt: "2017-06-16T19:00:00+08:00",
projects: ["Project One", "Project Two"],
},
];
const listOfProjectsByTime = (entries) => {
return flow(
sortBy("happenedAt"),
reverse,
flatMap("projects"),
uniq
)(entries);
};
You can read more in Lodash’s FP Guide.