JSFiddle - React, Tailwind, and code Playground

by kuyabiye

HTML

<link rel="stylesheet" href="https://s3.amazonaws.com/kiddsoftware-jsfiddle/mocha-1.9.0.css">
<script src="https://s3.amazonaws.com/kiddsoftware-jsfiddle/chai-jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/1.9.0/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/1.5.0/chai.min.js"></script>
<!-- No need to change this -->
<!-- Mocha test output goes here. -->
<div id="mocha"></div>

JavaScript

/* ACTUAL IMPLEMENTATION */
const responseToTimeseries = (response) => {
	let result = {}
	
  const pointCount = response.length
	response.forEach((point, index) => {
  	point.data.forEach(([x, point]) => {
			let { points = Array(pointCount).fill(0), total = 0 } = result[x + ""] || {}
      points[index] = point
      
    	result[x + ""] = {
      	x,
        points,
        total: total + parseInt(point, 10)
      }
    })
  })
  
  return result;
};


/* TESTS */
// Configure Mocha, telling both it and chai to use BDD-style tests.
mocha.setup("bdd");
chai.should();

describe('responseToTimeseries()', () => {
	describe('when receiving data for one point in time', () => { 
  	it('returns the expected output', () => {
      const input = [
        { data: [[1242362340000, 10], [1242362340001, 4]] },
      ];
      const output = {
        1242362340000: {
          x: 1242362340000,
          points: [10],
          total: 10,
        },
        1242362340001: {
        	x: 1242362340001,
          points: [4],
          total: 4,
        }
      };
			responseToTimeseries(input).should.deep.equal(output);
    });
  });
  
  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, 10], [1242362340001, 4],...