Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

alphavantage

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alphavantage - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

.env.example

10

index.js
'use strict';
require('dotenv').config();
const apiKey = 'AV_KEY';
/**

@@ -7,7 +10,5 @@ * The Alpha Vantage core module.

module.exports = config => {
// Check for any config errors.
if (!config) {
throw new Error(`Missing config for alphavantage module`);
}
config = Object.assign({}, { key: process.env[apiKey] }, config);
// Check for config errors.
let errors = [];

@@ -28,2 +29,3 @@ ['key'].forEach(prop => {

return {
util: require('./lib/util')(config),
data: require('./lib/data')(config),

@@ -30,0 +32,0 @@ performance: require('./lib/performance')(config)

89

lib/util.js
'use strict';
module.exports = config => ({
/**
* Time stamp regex that AlphaVantage uses.
*/
const timestamp = /[0-9]{4}-[0-9]{2}-[0-9]{2}( [0-9]{2}:[0-9]{2}:[0-9]{2})?/g;
/**
* The data keys to replace from the AlphaVantage API.
*/
const keys = {
'Meta Data': 'meta',
'1. Information': 'information',
Information: 'information',
'2. Symbol': 'symbol',
'3. Last Refreshed': 'updated',
'Last Refreshed': 'updated',
'4. Interval': 'interval',
'4. Output Size': 'size',
'5. Output Size': 'size',
'4. Time Zone': 'zone',
'5. Time Zone': 'zone',
'6. Time Zone': 'zone',
'Time Series (1min)': 'data',
'Time Series (Daily)': 'data',
'Weekly Time Series': 'data',
'Monthly Time Series': 'data',
'1. open': 'open',
'2. high': 'high',
'3. low': 'low',
'4. close': 'close',
'5. volume': 'volume',
'6. volume': 'volume',
'5. adjusted close': 'adjusted',
'7. dividend amount': 'dividend',
'8. split coefficient': 'split',
'Rank A: Real-Time Performance': 'real',
'Rank B: 1 Day Performance': '1day',
'Rank C: 5 Day Performance': '5day',
'Rank D: 1 Month Performance': '1month',
'Rank E: 3 Month Performance': '3month',
'Rank F: Year-to-Date (YTD) Performance': 'ytd',
'Rank G: 1 Year Performance': '1year',
'Rank H: 3 Year Performance': '3year',
'Rank I: 5 Year Performance': '5year',
'Rank J: 10 Year Performance': '10year'
};
module.exports = config => {
/**
* Recursively walk the data tree and replace weird keys with a normalized set.
*
* @param {Object|String|Number} data
* The data to normalize.
*
* @returns {Object|String|Number}
* Normalized data.
*/
const polish = data => {
// If this is not an object, dont recurse.
if (typeof data !== 'object') {
return data;
}
// If the data is a complex object, walk all subtrees to normalize all branches.
let clean = {};
Object.keys(data).forEach(key => {
// If the key is a date time string, convert it to an iso timestamp.
if (timestamp.test(key.toString())) {
clean[new Date(key.toString()).toISOString()] = polish(data[key]);
return;
}
clean[keys[key] || key] = polish(data[key]);
});
return clean;
};
/**
* Util function to build the proper API url.

@@ -21,3 +97,3 @@ *

*/
url: (fn, symbol, size, type, interval) => {
const url = (fn, symbol, size, type, interval) => {
let params = [];

@@ -42,3 +118,8 @@

return `${config.base}${params.join('&')}`;
}
});
};
return {
url,
polish
};
};
{
"name": "alphavantage",
"version": "0.0.4",
"version": "0.0.5",
"description": "A simple interface to the Alpha Vantage API.",

@@ -37,2 +37,3 @@ "main": "index.js",

"devDependencies": {
"dotenv": "^4.0.0",
"jest": "^20.0.4",

@@ -39,0 +40,0 @@ "prettier": "^1.5.3"

@@ -22,3 +22,3 @@ # AlphaVantage

- [ ] Add Technical indicators data
- [ ] Add utils to clean up response data
- [x] Add utils to clean up response data

@@ -97,2 +97,10 @@ #### Installation

});
/**
* Data polishing
*
* Rewrite weird data keys to be consistent across all api calls. This is an optional
* utility you can use with the result of any api call.
*/
const polished = alpha.util.polish(data);
```

@@ -99,0 +107,0 @@

'use strict';
const alpha = require('../')({
key: 'demo'
});
const alpha = require('../')();

@@ -14,1 +12,33 @@ test(`intraday data works`, () => {

});
test(`daily data works`, () => {
expect.assertions(2);
return alpha.data.daily(`msft`).then(data => {
expect(data['Meta Data']).toBeDefined();
expect(data['Time Series (Daily)']).toBeDefined();
});
});
test(`adjusted data works`, () => {
expect.assertions(2);
return alpha.data.adjusted(`msft`).then(data => {
expect(data['Meta Data']).toBeDefined();
expect(data['Time Series (Daily)']).toBeDefined();
});
});
test(`weekly data works`, () => {
expect.assertions(2);
return alpha.data.weekly(`msft`).then(data => {
expect(data['Meta Data']).toBeDefined();
expect(data['Weekly Time Series']).toBeDefined();
});
});
test(`monthly data works`, () => {
expect.assertions(2);
return alpha.data.monthly(`msft`).then(data => {
expect(data['Meta Data']).toBeDefined();
expect(data['Monthly Time Series']).toBeDefined();
});
});
'use strict';
const env = process.env;
const Alpha = require('../');
// Clear the current environment variables for testing.
process.env = {};
test(`initialization without a config throws an error`, () => {
expect.assertions(1);
try {
const alpha = require('../')();
const alpha = Alpha();
} catch (e) {
expect(e.message).toEqual(`Missing config for alphavantage module`);
expect(e.message).toEqual(`Missing Alpha Vantage config settings: key`);
}

@@ -15,3 +21,3 @@ });

try {
const alpha = require('../')({});
const alpha = Alpha({});
} catch (e) {

@@ -21,1 +27,12 @@ expect(e.message).toEqual(`Missing Alpha Vantage config settings: key`);

});
test(`initialization without an api key, but with env key works`, () => {
expect.assertions(1);
try {
process.env = env;
const alpha = Alpha();
expect(alpha).toBeDefined();
} catch (e) {
expect(e).toBeUndefined();
}
});
'use strict';
const alpha = require('../')({
key: 'demo'
});
const alpha = require('../')();

@@ -7,0 +5,0 @@ test(`sector performance data works`, () => {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc