join.js 822 B

12345678910111213141516171819202122
  1. import { rollup } from 'd3-array';
  2. function field(key) {
  3. return typeof key === 'string' ? (d) => d[key] : key;
  4. }
  5. /**
  6. * Join data with another dataset by SQL style.
  7. */
  8. export const Join = (options) => {
  9. // const { fromKey, from, key, unknown = NaN, ...rest } = options;
  10. const { join, on, select = [], as = select, unknown = NaN } = options;
  11. const [key, fromKey] = on;
  12. const fk = field(fromKey);
  13. const k = field(key);
  14. const keyData = rollup(join, ([d]) => d, // Get the first matched.
  15. (d) => fk(d));
  16. return (data) => data.map((d) => {
  17. const source = keyData.get(k(d));
  18. return Object.assign(Object.assign({}, d), select.reduce((prev, key, idx) => ((prev[as[idx]] = source ? source[key] : unknown), prev), {}));
  19. });
  20. };
  21. Join.props = {};
  22. //# sourceMappingURL=join.js.map