diff --git a/time-series/join/index.js b/time-series/join/index.js index 3aa8482..4aff5df 100644 --- a/time-series/join/index.js +++ b/time-series/join/index.js @@ -1,5 +1,6 @@ /* @flow */ import type { TimeSeries, TimeSeriesPoint } from 'ikeru/time-series' +import { initialize, next } from '../tournament-tree' export default function( timeSeriesList: Array>, @@ -9,7 +10,23 @@ export default function( return [] } - return timeSeriesList[0].map(({ time }, i) => - combine(time.valueOf(), timeSeriesList.map(timeSeries => timeSeries[i])) - ) + const getTime = item => item.time + + let tournamentTree = initialize(timeSeriesList, getTime) + const groups = [] + let currentGroup = [] + + while (tournamentTree.value) { + if (currentGroup[0] && tournamentTree.value.time !== currentGroup[0].time) { + groups.push(currentGroup) + currentGroup = [] + } + + currentGroup.push(tournamentTree.value) + tournamentTree = next(tournamentTree, getTime) + } + + groups.push(currentGroup) + + return groups.map(group => combine(group[0].time.valueOf(), group)) } diff --git a/time-series/join/index.test.js b/time-series/join/index.test.js index e53bdda..5d2e51c 100644 --- a/time-series/join/index.test.js +++ b/time-series/join/index.test.js @@ -1,29 +1,31 @@ /* @flow */ import test from 'ava' +import { sumBy } from 'lodash' import { join } from 'ikeru/time-series' test('combining multiple time series by time', t => { const series1 = [ { time: Date.parse('2019-01-01'), value: 1 }, { time: Date.parse('2019-01-02'), value: 2 }, - { time: Date.parse('2019-01-03'), value: 3 } + { time: Date.parse('2019-01-03'), value: 3 }, + { time: Date.parse('2019-01-04'), value: 4 } ] const series2 = [ - { time: Date.parse('2019-01-01'), value: 4 }, - { time: Date.parse('2019-01-02'), value: 5 }, - { time: Date.parse('2019-01-03'), value: 6 } + { time: Date.parse('2019-01-02'), value: 2 }, + { time: Date.parse('2019-01-03'), value: 3 } ] t.deepEqual( - join([series1, series2], (time, [p1, p2]) => ({ + join([series1, series2], (time, points) => ({ time, - value: p1.value + p2.value + value: sumBy(points, 'value') })), [ - { time: Date.parse('2019-01-01'), value: 5 }, - { time: Date.parse('2019-01-02'), value: 7 }, - { time: Date.parse('2019-01-03'), value: 9 } + { time: Date.parse('2019-01-01'), value: 1 }, + { time: Date.parse('2019-01-02'), value: 4 }, + { time: Date.parse('2019-01-03'), value: 6 }, + { time: Date.parse('2019-01-04'), value: 4 } ] ) }) diff --git a/time-series/merge/index.js b/time-series/merge/index.js index 6014188..37f02df 100644 --- a/time-series/merge/index.js +++ b/time-series/merge/index.js @@ -1,6 +1,6 @@ /* @flow */ import type { TimeSeries } from 'ikeru/time-series' -import { initialize, next } from './tournament-tree' +import { initialize, next } from '../tournament-tree' export default function( ...timeSeriesList: Array> diff --git a/time-series/merge/tournament-tree/index.js b/time-series/tournament-tree/index.js similarity index 100% rename from time-series/merge/tournament-tree/index.js rename to time-series/tournament-tree/index.js diff --git a/time-series/merge/tournament-tree/initialize.js b/time-series/tournament-tree/initialize.js similarity index 100% rename from time-series/merge/tournament-tree/initialize.js rename to time-series/tournament-tree/initialize.js diff --git a/time-series/merge/tournament-tree/next.js b/time-series/tournament-tree/next.js similarity index 100% rename from time-series/merge/tournament-tree/next.js rename to time-series/tournament-tree/next.js diff --git a/time-series/merge/tournament-tree/tournament-tree.js b/time-series/tournament-tree/tournament-tree.js similarity index 100% rename from time-series/merge/tournament-tree/tournament-tree.js rename to time-series/tournament-tree/tournament-tree.js