PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/share/spice/economic_indicators/economic_indicators.js

http://github.com/duckduckgo/zeroclickinfo-spice
JavaScript | 93 lines | 69 code | 12 blank | 12 comment | 24 complexity | c2cbce0aa372cf4724daba5ab7cf952f MD5 | raw file
Possible License(s): Apache-2.0
  1. (function (env) {
  2. "use strict";
  3. function getDataFromAPIResponse(api_response){
  4. //validate if api response is null
  5. if(api_response === null || api_response === undefined){
  6. return null;
  7. }
  8. //the root object of api response must be an array of length 2
  9. if ( !$.isArray(api_response) || api_response.length != 2){
  10. return null;
  11. }
  12. //the object at position 1 in api_response contains an yearwise list of values
  13. var indicatorValuesByYear = api_response[1];
  14. //check if indicatorValuesByYear is an array
  15. if( !$.isArray(indicatorValuesByYear) ){
  16. return null;
  17. }
  18. //the year wise list is sorted by most recent year first
  19. //iterare and get the first result where indicator value is not null
  20. var indicatorData = null;
  21. $.each(indicatorValuesByYear,function(index,indicator){
  22. if(indicator.value != null && indicator.country !== null && indicator.date !== null && indicator.indicator !== null){
  23. indicatorData = indicator;
  24. return false; //break
  25. }
  26. });
  27. return indicatorData;
  28. }
  29. env.ddg_spice_economic_indicators = function(api_response){
  30. var indicatorData = getDataFromAPIResponse(api_response);
  31. if ( indicatorData === null || indicatorData === undefined ) {
  32. return Spice.failed('economic_indicators');
  33. }
  34. // Render the response
  35. Spice.add({
  36. id: "economic_indicators",
  37. // Customize these properties
  38. name: "Economic Indicator",
  39. data: indicatorData,
  40. meta: {
  41. sourceName: "WorldBank",
  42. sourceUrl: 'http://data.worldbank.org/country/' + indicatorData.country.value.replace(/[^\w\s]/gi, '').replace(/\W/g, '-')
  43. },
  44. templates: {
  45. group: 'text',
  46. options: {
  47. moreAt: true
  48. }
  49. },
  50. normalize: function(indicatorData){
  51. var indicatorValue = "";
  52. // if the indicator is per capita income
  53. if(indicatorData.indicator.value.match(/per capita/gi)){
  54. //for seperating number by commas every three digits,uses snippet from http://stackoverflow.com/questions/1990512/add-comma-to-numbers-every-three-digits-using-jquery
  55. indicatorValue = indicatorData.value.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") + " USD";
  56. }
  57. //else if indicator is growth rate
  58. else if(indicatorData.indicator.value.match(/growth/gi)){
  59. indicatorValue = parseFloat(indicatorData.value).toFixed(2) + " %";
  60. }
  61. // else if the indicator is GDP
  62. else if(indicatorData.indicator.value.match(/GDP/gi)){
  63. var indicatorValue = parseFloat(indicatorData.value);
  64. if(indicatorValue /1000000000000 > 1){
  65. indicatorValue = (indicatorValue/1000000000000).toFixed(2) + " Trillion USD";
  66. } else if(indicatorValue/1000000000 > 1){
  67. indicatorValue = (indicatorValue/1000000000).toFixed(2) + " Billion USD";
  68. } else if(indicatorValue/1000000 > 1){
  69. indicatorValue = (indicatorValue/1000000).toFixed(2) + " Million USD";
  70. } else {
  71. indicatorValue = indicatorValue.toFixed(2) + " USD";
  72. }
  73. }
  74. return {
  75. title : indicatorValue,
  76. subtitle : DDG.getProperty(indicatorData,'country.value') + " - " + DDG.getProperty(indicatorData,'indicator.value').replace(/ *\([^)]*\) */g, "") + " (" + DDG.getProperty(indicatorData,'date') + ")"
  77. }
  78. }
  79. });
  80. };
  81. }(this));