Is it possible to concatenate two lodash methods? In my case uniqBy and sortBy
Example:
const uniquePotentialClients = _.uniqBy(
[...potentialClients],
uniqueContact => uniqueContact.phoneNumbers[0].number,
);
const sortPotentialClients = _.sortBy(
[...potentialClients],
uniqueContact => uniqueContact.givenName,
);
Is it possible two apply these two methods to one collection?
You have two choices:
This wraps the value you'd be working on and then you can chain more operations on it without supplying a parameter. When finished .value()
extracts the final value.
const result = _.chain([...potentialClients])
.uniqBy(uniqueContact => uniqueContact.phoneNumbers[0].number)
.sortBy(uniqueContact => uniqueContact.givenName)
.value();
const uniqFn = _.uniqBy(uniqueContact => uniqueContact.phoneNumbers[0].number);
const sortFn = _.sortBy(uniqueContact => uniqueContact.givenName);
const composedFn = _.flow(uniqFn, sortFn);
const result = composedFn([...potentialClients]);
This implementation is possible if you're using the FP release of Lodash. If you aren't, then you'd have to apply modifiers to the functions to flip the arguments and make them curried which will make them functional programming friendly, similar to what lodash/fp does. Here is a sample of how this can be done:
//_.curry requires the arity to be declared, passing 2 satisfies both these functions
const makeFpFriendly = f => _.curry(_.flip(f), 2);
const uniqByFpFriendly = makeFpFriendly(_.uniqBy);
const sortByFpFriendly = makeFpFriendly(_.sortBy);