UNPKG

@tidyjs/tidy

Version:

Tidy up your data with JavaScript, inspired by dplyr and the tidyverse

49 lines (47 loc) 1.24 kB
function autodetectByMap(itemsA, itemsB) { if (itemsA.length === 0 || itemsB.length === 0) return {}; const keysA = Object.keys(itemsA[0]); const keysB = Object.keys(itemsB[0]); const byMap = {}; for (const key of keysA) { if (keysB.includes(key)) { byMap[key] = key; } } return byMap; } function makeByMap(by) { if (Array.isArray(by)) { const byMap = {}; for (const key of by) { byMap[key] = key; } return byMap; } else if (typeof by === "object") { return by; } return {[by]: by}; } function isMatch(d, j, byMap) { for (const jKey in byMap) { const dKey = byMap[jKey]; if (d[dKey] !== j[jKey]) { return false; } } return true; } function innerJoin(itemsToJoin, options) { const _innerJoin = (items) => { const byMap = (options == null ? void 0 : options.by) == null ? autodetectByMap(items, itemsToJoin) : makeByMap(options.by); const joined = items.flatMap((d) => { const matches = itemsToJoin.filter((j) => isMatch(d, j, byMap)); return matches.map((j) => ({...d, ...j})); }); return joined; }; return _innerJoin; } export { autodetectByMap, innerJoin, isMatch, makeByMap }; //# sourceMappingURL=innerJoin.js.map