Using Highland.js to hydrate one stream with data from another

146 Views Asked by At

Say I have two Highland streams:

import hl from 'highland'

const accounts = hl([
  {id: 1, name: "Bob"},
  {id: 2, name: "Chris"},
]);

const accountData = hl([
  {id: 1, age: 21},
  {id: 2, age: 43},
]);

I'd like to map over the accounts stream and merge in the extra data from accountData so the result looks something like this:

Highland.Stream<[
  {id: 1, name: "Bob", age: 21},
  {id: 2, name: "Chris", age: 43},
]>

This is something that's pretty simple with normal arrays but I was wondering if it were possible when using streams.

1

There are 1 best solutions below

0
On
import hl from 'highland';

const accounts = hl([
  { id: 1, name: 'Bob' },
  { id: 2, name: 'Chris' }
]);

const accountData = hl([
  { id: 1, age: 21 },
  { id: 2, age: 43 }
]);

hl([ accounts, accountData ])
  .merge()
  .reduce(new Map(), (accum, account) => {
    const { id }        = account;
    const mergedAccount = Object.assign({}, accum.get(id), account);
    return accum.set(id, mergedAccount);
  })
  .map(map => [ ...map.values() ])
  .doto(console.log)
  .done(() => {});

Output:

[ 
  { id: 1, name: 'Bob', age: 21 },
  { id: 2, name: 'Chris', age: 43 } 
]