-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from jdunkerley/task2a
Task2a
- Loading branch information
Showing
7 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |