PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/share/spice/quandl/fundamentals/quandl_fundamentals.js

http://github.com/duckduckgo/zeroclickinfo-spice
JavaScript | 93 lines | 71 code | 14 blank | 8 comment | 16 complexity | 30f369b83b25993e48bcb2caa8753d20 MD5 | raw file
Possible License(s): Apache-2.0
  1. (function (env) {
  2. 'use strict';
  3. env.ddg_spice_quandl_fundamentals = function(api_result){
  4. if (!api_result) {
  5. return Spice.failed('quandl_fundamentals');
  6. }
  7. if (api_result.data == null) {
  8. return Spice.failed('quandl_fundamentals');
  9. }
  10. // we need two data points to get percent change
  11. if (api_result.data.length < 2) {
  12. return Spice.failed('quandl_fundamentals');
  13. }
  14. // get recent and previous data values
  15. var url = 'https://quandl.com/' + api_result.source_code + "/" + api_result.code,
  16. urlTitle = 'View more fundamentals data at Quandl',
  17. recentValue = DDG.getProperty(api_result.data,'0.1'),
  18. previousValue = DDG.getProperty(api_result.data,'1.1');
  19. // data title link
  20. api_result.url = url;
  21. // check we have both data values
  22. if (!recentValue || !previousValue) {
  23. return Spice.failed('quandl_fundamentals');
  24. }
  25. // splitting title into header and subheader
  26. var nameSplit = api_result.name.split('-');
  27. api_result.header = nameSplit.shift().trim();
  28. api_result.subheader = nameSplit.join('-').trim();
  29. // getting rounded percentage
  30. var percentChange = 10000 * ((recentValue - previousValue) / Math.abs(previousValue));
  31. percentChange = Math.round(percentChange);
  32. percentChange /= 100;
  33. api_result.change_percent = Math.abs(percentChange);
  34. // setting style based on change
  35. if (percentChange > 0 || percentChange === 0) api_result.changeDirection = 'up';
  36. if (percentChange < 0) api_result.changeDirection = 'down';
  37. // the most recent fundamental value
  38. var value = recentValue;
  39. if (Math.abs(value) >= Math.pow(10,12)) {
  40. value = Math.round(value / Math.pow(10,10)) / Math.pow(10,2);
  41. value = "$" + value + " trillion";
  42. } else if (Math.abs(value) >= Math.pow(10,9)) {
  43. value = Math.round(value / Math.pow(10,7)) / Math.pow(10,2);
  44. value = "$" + value + " billion";
  45. } else if (Math.abs(value) >= Math.pow(10,6)) {
  46. value = Math.round(value / Math.pow(10,4)) / Math.pow(10,2);
  47. value = "$" + value + " million";
  48. } else if (Math.abs(value) >= Math.pow(10,3)) {
  49. value = Math.round(value / Math.pow(10,1)) / Math.pow(10,2);
  50. value = "$" + value + " thousand";
  51. }
  52. api_result.value = value;
  53. DDG.require('moment.js', function() {
  54. Spice.add({
  55. id: 'quandl_fundamentals',
  56. name: 'Fundamentals',
  57. data: api_result,
  58. meta: {
  59. sourceName: 'Quandl',
  60. sourceUrl: api_result.url,
  61. sourceIconUrl: DDG.get_asset_path('quandl/fundamentals','quandl32x32.png')
  62. },
  63. normalize: function(item) {
  64. return {
  65. to_date: moment(item.to_date).format('MMM DD, YYYY'),
  66. from_date: moment(item.data[1][0]).format('MMM DD, YYYY')
  67. };
  68. },
  69. templates: {
  70. group: 'base',
  71. options: {
  72. content: Spice.quandl_fundamentals.content,
  73. moreAt: true
  74. }
  75. }
  76. });
  77. });
  78. };
  79. }(this));