UNPKG

@tidyjs/tidy

Version:

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

41 lines (38 loc) 1.36 kB
import { autodetectByMap, makeByMap, isMatch } from './innerJoin.js'; function fullJoin(itemsToJoin, options) { const _fullJoin = (items) => { if (!itemsToJoin.length) return items; if (!items.length) return itemsToJoin; const byMap = (options == null ? void 0 : options.by) == null ? autodetectByMap(items, itemsToJoin) : makeByMap(options.by); const matchMap = new Map(); const joinObjectKeys = Object.keys(itemsToJoin[0]); const joined = items.flatMap((d) => { const matches = itemsToJoin.filter((j) => { const matched = isMatch(d, j, byMap); if (matched) { matchMap.set(j, true); } return matched; }); if (matches.length) { return matches.map((j) => ({...d, ...j})); } const undefinedFill = Object.fromEntries(joinObjectKeys.filter((key) => d[key] == null).map((key) => [key, void 0])); return {...d, ...undefinedFill}; }); if (matchMap.size < itemsToJoin.length) { const leftEmptyObject = Object.fromEntries(Object.keys(items[0]).map((key) => [key, void 0])); for (const item of itemsToJoin) { if (!matchMap.has(item)) { joined.push({...leftEmptyObject, ...item}); } } } return joined; }; return _fullJoin; } export { fullJoin }; //# sourceMappingURL=fullJoin.js.map