Skip to content

Commit

Permalink
Merge pull request #3 from jdunkerley/task2a
Browse files Browse the repository at this point in the history
Task2a
  • Loading branch information
jdunkerley committed Nov 10, 2015
2 parents 8179bc4 + 0290b4f commit 15bee09
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.git
1 change: 1 addition & 0 deletions AAPL.json

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions returnsCalc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
(function() {
'use strict';

var fs = require('fs');

function getTimeSeries(jsonObj, fieldName) {
var columnId = jsonObj.dataset.column_names.indexOf(fieldName);
return jsonObj.dataset.data.map(function(d) {
return {
date: new Date(d[0]),
value: d[columnId]
};
}).reverse();
}

function getStartDate(date) {
var result = new Date(date);
result.setDate(date.getDate() - 7);
return result;
}

function addStartAndReturn(d, i, data) {
d.start = getStartDate(d.date);

while (i >= 0 && data[i].date > d.start) {
i--;
}

if (i >= 0) {
d.startIndex = i;
d.startValue = data[i].value;
d.return = d.value / d.startValue - 1;
}

return d;
}

function getMinAndMax(returnSeries) {
return returnSeries.reduce(function(p, v) {
if (p == null) {
return {min: v, max: v};
}
return {
min: v.return < p.min.return ? v : p.min,
max: v.return > p.max.return ? v : p.max
};
}, null);
}

// Public API
module.exports = function(jsonObj, fieldName) {
var timeSeries = getTimeSeries(jsonObj, fieldName);

var returnSeries = timeSeries
.map(addStartAndReturn)
.filter(function(d) { return d.return; });

return getMinAndMax(returnSeries);
};

module.exports.getTimeSeries = getTimeSeries;
module.exports.getStartDate = getStartDate;
module.exports.addStartAndReturn = addStartAndReturn;
module.exports.getMinAndMax = getMinAndMax;
}());
12 changes: 12 additions & 0 deletions returnsCmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var returnsCalc = require('./returnsCalc');
var fs = require('fs');

fs.readFile('./AAPL.json', 'utf8', function(err, data) {
if (err) {
return console.log(err);
}

var jsonObj = JSON.parse(data);
var minMax = returnsCalc(jsonObj, 'Adj. Close');
console.log(minMax);
});
38 changes: 38 additions & 0 deletions spec/getMinAndMaxSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* global describe */
describe('getMinAndMax', function() {
var returnsCalc = require('../returnsCalc');

it('is a function', function() {
var typeName = typeof(returnsCalc.getMinAndMax);
expect(typeName).toEqual('function');
});

it('Min and max should be both 2', function() {
var arr = [{return: 2}];
var result = returnsCalc.getMinAndMax(arr);
expect(result.min === 2);
expect(result.max === 2);
});

it('Min and max should be both 2 - multiple same', function() {
var arr = [{return: 2}, {return: 2}, {return: 2}];
var result = returnsCalc.getMinAndMax(arr);
expect(result.min === 2);
expect(result.max === 2);
});

it('Min and max should work with extreme values', function() {
var arr = [{return: Number.POSITIVE_INFINITY}, {return: 8}, {return: Number.NEGATIVE_INFINITY}];
var result = returnsCalc.getMinAndMax(arr);
expect(result.min === Number.NEGATIVE_INFINITY);
expect(result.max === Number.POSITIVE_INFINITY);
});

it('Min and max should work with fractions', function() {
var arr = [{return: 0.002}, {return: 0.0000002}, {return: 0.2}];
var result = returnsCalc.getMinAndMax(arr);
expect(result.min === 0.0000002);
expect(result.max === 0.2);
});

});
21 changes: 21 additions & 0 deletions spec/getStartDateSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* global describe */
describe('getStartDate', function() {
var returnsCalc = require('../returnsCalc');

it('is a function', function() {
var typeName = typeof(returnsCalc.getStartDate);
expect(typeName).toEqual('function');
});

it('should return a day 7 days earlier', function() {
var date = new Date('2015-10-20');
var result = returnsCalc.getStartDate(date);
expect(result.toISOString().substr(0, 10)).toEqual('2015-10-13');
})

it('should return a day 7 days earlier in previous year', function() {
var date = new Date('2015-01-01');
var result = returnsCalc.getStartDate(date);
expect(result.toISOString().substr(0, 10)).toEqual('2014-12-25');
})
});
9 changes: 9 additions & 0 deletions spec/getTimeSeriesSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* global describe */
describe('getTimeSeries', function() {
var returnsCalc = require('../returnsCalc');

it('is a function', function() {
var typeName = typeof(returnsCalc.getTimeSeries);
expect(typeName).toEqual('function');
});
});

0 comments on commit 15bee09

Please sign in to comment.