Skip to content

Instantly share code, notes, and snippets.

@dinocarl
Created September 7, 2022 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinocarl/8c363a9de09101985ed8aa429b04aa0b to your computer and use it in GitHub Desktop.
Save dinocarl/8c363a9de09101985ed8aa429b04aa0b to your computer and use it in GitHub Desktop.
const juxtMerge = (fnList) => compose(
mergeAll,
juxt(fnList)
);
// given an object, a foreignKey (name), and a lookup object
// containing data keyed to the foreignKey,
// return the data of the lookup, or a fallback value
const propOfPropOr = (fallback, foreignKey, lookup) => (item) => propOr(
fallback,
prop(foreignKey, item),
lookup
);
// return either a select subset of keys from a lookup,
// or an empty object in case of failure
const safeSelectDetail = (selectKeys, foreignKey, lookup) => (item) => pick(
selectKeys,
propOfPropOr({}, foreignKey, lookup)(item)
);
// returns a new list enhanced by the selected keys of a lookup object
// (de-normalize a data set with its foreign key data)
const joinSelect = ({ selectKeys, foreignKey, lookup }) => map(
juxtMerge([
identity,
safeSelectDetail(selectKeys, foreignKey, lookup)
])
); // ([1,2,3])
const orderData = [
{id: '123', recipient: 'ryan', order: 'pizza'},
{id: '124', recipient: 'alex', order: 'pizza'},
{id: '125', recipient: 'alex', order: 'hotDog'},
{id: '126', recipient: 'dave', order: 'hamburger'},
{id: '127', recipient: 'anne', order: 'soup'},
];
const foodData = {
pizza: { name: 'pizza', price: 15 },
hotDog: { name: 'hotDog', price: 6 },
hamburger: { name: 'hamburger', price: 9 },
};
const billingData = {
ryan: { address: '123 Mockingbird Ln', cc: 9876 },
alex: { address: '125 Mockingbird Ln', cc: 9876 },
dave: { address: '127 Mockingbird Ln', cc: 9876 },
};
const multiJoinData = [
{
selectKeys: ['address'],
foreignKey: 'recipient',
lookup: billingData,
},
{
selectKeys: ['price', 'ingredients'],
foreignKey: 'order',
lookup: foodData,
}
];
// joinSelect(
// multiJoinData[0]
// )(
joinSelect(
multiJoinData[1]
)(orderData)
// );
// apply(
// compose,
// map(joinSelect, multiJoinData)
// )(orderData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment