@tidyjs/tidy
Version:
Tidy up your data with JavaScript, inspired by dplyr and the tidyverse
45 lines (40 loc) • 1.45 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
var innerJoin = require('./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 ? innerJoin.autodetectByMap(items, itemsToJoin) : innerJoin.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 = innerJoin.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;
}
exports.fullJoin = fullJoin;
//# sourceMappingURL=fullJoin.js.map
;