The detailed code is here: https://jsfiddle.net/prettyboy811/v0L3kg5d/2/
This is the first time that I have seen this data transformation. Please help me with this. Thanks!
The question provides series of test. We need to add the code in order to pass the tests:
describe('when receiving data for multiple points in time', () => {
it('returns the expected output', () => {
const input = [
{ data: [[1242362340000, 10], [1242362340001, 4]] },
{ data: [[1242362340000, 6], [1242362340001, 9]] },
{ data: [[1242362340000, 11], [1242362340001, 13]] },
];
const output = {
1242362340000: {
x: 1242362340000,
points: [10, 6, 11],
total: 27,
},
1242362340001: {
x: 1242362340001,
points: [4, 9, 13],
total: 26,
}
};
responseToTimeseries(input).should.deep.equal(output);
});
});
describe('when some point is missing', () => {
it('returns the expected output', () => {
const input = [
{ data: [[1242362340000, 20]] },
{ data: [[1242362340000, 10], [1242362340001, 4], [1242362340002, 1]] },
{ data: [[1242362340000, 6], [1242362340001, 9]] },
{ data: [[1242362340000, 11], [1242362340001, 13], [1242362340002, 2]] },
];
const output = {
1242362340000: {
x: 1242362340000,
points: [20, 10, 6, 11],
total: 47,
},
1242362340001: {
x: 1242362340001,
points: [0, 4, 9, 13],
total: 26,
},
1242362340002: {
x: 1242362340002,
points: [0, 1, 0, 2],
total: 3,
}
};
responseToTimeseries(input).should.deep.equal(output);
});
});
});