yahoo-exchange
Advanced tools
Comparing version 1.0.0-alpha.2 to 1.0.0
45
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const request = require("request"); | ||
const parseHTML = html => parseFloat(html.toString().split('react-text: 36 -->')[1].split('<')[0].replace(',', '')); | ||
const parseHTML2 = h => { | ||
const html = h.toString(); | ||
return [parseFloat(html.split('react-text: 36 -->')[1].split('<')[0].replace(',', '')), | ||
parseFloat(html.split('react-text: 38 -->')[2].split('<')[0] + html.split('react-text: 39 -->')[1].split(' ')[0]), | ||
parseFloat(html.split('react-text: 39 -->')[1].split('(')[1].split("\%")[0]) | ||
]; | ||
}; | ||
/** | ||
* @deprecated Since version 1.0. Will be deleted in version 2.0. Use getExchangeDataArray instead. | ||
*/ | ||
function getData(pair, callback, errorHandler = err => console.log(err)) { | ||
@@ -12,8 +23,11 @@ request({ | ||
else | ||
callback(parseFloat(html.toString().split('react-text: 36 -->')[1].split('<')[0].replace(',', '')), pair); | ||
callback(parseHTML(html), pair); | ||
}); | ||
} | ||
exports.getData = getData; | ||
/** | ||
* @deprecated Since version 1.0. Will be deleted in version 2.0. Use getExchangeDataArray instead. | ||
*/ | ||
function getDataArray(pair, callback, errorHandler = err => console.log(err)) { | ||
pair.map(v => request({ | ||
pair.forEach(v => request({ | ||
url: `https://finance.yahoo.com/quote/${v}=X?p=${v}=X`, | ||
@@ -25,6 +39,31 @@ encoding: null, | ||
else | ||
callback(parseFloat(html.toString().split('react-text: 36 -->')[1].split('<')[0].replace(',', '')), v); | ||
callback(parseHTML(html), v); | ||
})); | ||
} | ||
exports.getDataArray = getDataArray; | ||
function getExchangeDataArray(pair, callback, errorHandler = err => console.log(err)) { | ||
if (typeof pair === 'string') { | ||
request({ | ||
url: `https://finance.yahoo.com/quote/${pair}=X?p=${pair}=X`, | ||
encoding: null, | ||
}, (err, response, html) => { | ||
if (err) | ||
errorHandler(err, pair); | ||
else | ||
callback(parseHTML2(html), pair); | ||
}); | ||
} | ||
else if (Array.isArray(pair)) { | ||
pair.forEach(v => request({ | ||
url: `https://finance.yahoo.com/quote/${v}=X?p=${v}=X`, | ||
encoding: null, | ||
}, (err, response, html) => { | ||
if (err) | ||
errorHandler(err, v); | ||
else | ||
callback(parseHTML2(html), v); | ||
})); | ||
} | ||
} | ||
exports.getExchangeDataArray = getExchangeDataArray; | ||
function getPairArray(currency, base) { | ||
@@ -31,0 +70,0 @@ let list = []; |
48
index.ts
import request = require("request"); | ||
export function getData(pair: string, callback: (data: number, pair?: string) => any, errorHandler: (error: Error, pair?: String) => any = err => console.log(err)): void { | ||
const parseHTML = html => parseFloat(html.toString().split('react-text: 36 -->')[1].split('<')[0].replace(',', '')); | ||
const parseHTML2 = h => { | ||
const html = h.toString(); | ||
return [parseFloat(html.split('react-text: 36 -->')[1].split('<')[0].replace(',', '')), | ||
parseFloat(html.split('react-text: 38 -->')[2].split('<')[0] + html.split('react-text: 39 -->')[1].split(' ')[0]), | ||
parseFloat(html.split('react-text: 39 -->')[1].split('(')[1].split("\%")[0]) | ||
]; | ||
}; | ||
/** | ||
* @deprecated Since version 1.0. Will be deleted in version 2.0. Use getExchangeDataArray instead. | ||
*/ | ||
export function getData(pair: string, callback: (data: any, pair?: string) => any, errorHandler: (error: Error, pair?: String) => any = err => console.log(err)): void { | ||
request({ | ||
@@ -9,8 +22,12 @@ url: `https://finance.yahoo.com/quote/${pair}=X?p=${pair}=X`, | ||
if (err) errorHandler(err, pair); | ||
else callback(parseFloat(html.toString().split('react-text: 36 -->')[1].split('<')[0].replace(',', '')), pair); | ||
else callback(parseHTML(html), pair); | ||
}); | ||
} | ||
export function getDataArray(pair: Array<string>, callback: (data: number, pair?: string) => any, errorHandler: (error: Error, pair?: String) => any = err => console.log(err)): void { | ||
pair.map(v => request({ | ||
/** | ||
* @deprecated Since version 1.0. Will be deleted in version 2.0. Use getExchangeDataArray instead. | ||
*/ | ||
export function getDataArray(pair: Array<string>, callback: (data: any, pair?: string) => any, errorHandler: (error: Error, pair?: String) => any = err => console.log(err)): void { | ||
pair.forEach(v => request({ | ||
url: `https://finance.yahoo.com/quote/${v}=X?p=${v}=X`, | ||
@@ -20,6 +37,27 @@ encoding: null, | ||
if (err) errorHandler(err, v); | ||
else callback(parseFloat(html.toString().split('react-text: 36 -->')[1].split('<')[0].replace(',', '')), v); | ||
else callback(parseHTML(html), v); | ||
})); | ||
} | ||
export function getExchangeDataArray(pair: any, callback: (data: Array<number>, pair?: string) => any, errorHandler: (error: Error, pair?: String) => any = err => console.log(err)): void { | ||
if (typeof pair === 'string') { | ||
request({ | ||
url: `https://finance.yahoo.com/quote/${pair}=X?p=${pair}=X`, | ||
encoding: null, | ||
}, (err, response, html) => { | ||
if (err) errorHandler(err, pair); | ||
else callback(parseHTML2(html), pair); | ||
}); | ||
} else if (Array.isArray(pair)) { | ||
pair.forEach(v => request({ | ||
url: `https://finance.yahoo.com/quote/${v}=X?p=${v}=X`, | ||
encoding: null, | ||
}, (err, response, html) => { | ||
if (err) errorHandler(err, v); | ||
else callback(parseHTML2(html), v); | ||
})); | ||
} | ||
} | ||
export function getPairArray(currency: Array<string>, base: Array<string>): Array<string> { | ||
@@ -26,0 +64,0 @@ let list: Array<string> = []; |
@@ -14,3 +14,3 @@ { | ||
"main": "index.js", | ||
"version": "1.0.0-alpha.2", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
@@ -17,0 +17,0 @@ "dependencies": { |
# yahoo-exchange | ||
npm : https://www.npmjs.com/package/yahoo-exchange | ||
## getData(pair, callback, errorHandler): void | ||
## getExchangeDataArray(pair, callback, errorHandler): void @deprecated | ||
* pair : string or Array\<string\> | ||
* callback : (Array\<number\>, pair) => any | ||
Array\<number\> : [Now, Changes, Changes percent] | ||
### Example | ||
* ``` javascript | ||
getData('USDKRW', data => console.log(data)); // 1070 | ||
getExchangeDataArray('USDKRW', data => console.log(data)); // [1070, 1.5, 0.7] | ||
``` | ||
* ``` javascript | ||
getExchangeDataArray('USDKRW', data => console.log(data), error => console.log('[Error]' + error)); | ||
// [1070, 1.5, 0.7] or [Error] error message | ||
``` | ||
* ``` javascript | ||
getExchangeDataArray('USDKRW', (data,pair) => console.log(data,pair), error => console.log('[Error]' + error)); | ||
// [1070, 1.5, 0.7] USDRKW or [Error] error message | ||
``` | ||
* ``` javascript | ||
getExchangeDataArray('USDKRW', (data,pair) => console.log(data,pair), (error,pair) => console.log(`[Error:${pair}]${error}`)); | ||
// [1070, 1.5, 0.7] USDKRW or [Error:USDKRW] error message | ||
``` | ||
* ``` javascript | ||
getExchangeDataArray(['USDKRW', 'JPYKRW'], data => console.log(data)); | ||
// [1070, 1.5, 0.7] | ||
// [970, 1.5, 0.7] | ||
``` | ||
* ``` javascript | ||
getExchangeDataArray(['USDKRW', 'JPYKRW'], data => console.log(data), error => console.log('[Error]' + error)); | ||
// [1070, 1.5, 0.7] or [Error] error message | ||
// [970, 1.5, 0.7] or [Error] error message | ||
``` | ||
* ``` javascript | ||
getExchangeDataArray(['USDKRW', 'JPYKRW'], (data,pair) => console.log(data, pair)), error => console.log('[Error]' + error)); | ||
// [1070, 1.5, 0.7] USDKRW or [Error] error message | ||
// [970, 1.5, 0.7] JPYKRW or [Error] error message | ||
``` | ||
* ``` javascript | ||
getExchangeDataArray(['USDKRW', 'JPYKRW'], (data,pair) => console.log(data, pair)), (error,pair) => console.log(`[Error:${pair}]${error}`)); | ||
// [1070, 1.5, 0.7] USDKRW or [Error:USDKRW] error message | ||
// [970, 1.5, 0.7] JPYKRW or [Error:JPYKRW] error message | ||
``` | ||
### Warning | ||
getExchangeDataArray does not return Arrays to Callback. | ||
## getData(pair, callback, errorHandler): void @deprecated | ||
> ### deprecated Since version 1.0. Will be deleted in version 2.0. Use getExchangeDataArray instead. | ||
### Example | ||
* ``` javascript | ||
getData('USDKRW', data => console.log(data)); // [ | ||
``` | ||
* ``` javascript | ||
getData('USDKRW', data => console.log(data), error => console.log('[Error]' + error)); | ||
@@ -20,6 +64,7 @@ // 1070 or [Error] error message | ||
``` | ||
## getDataArray(pair, callback, errorHandler): void | ||
## getDataArray(pair, callback, errorHandler): void @deprecated | ||
> ### deprecated Since version 1.0. Will be deleted in version 2.0. Use getExchangeDataArray instead. | ||
### Example | ||
* ``` javascript | ||
getDataArray(['USDKRW', 'JPYKRW'], dat a=> console.log(data)); | ||
getDataArray(['USDKRW', 'JPYKRW'], data => console.log(data)); | ||
// 1070 | ||
@@ -34,3 +79,3 @@ // 970 | ||
* ``` javascript | ||
getDataArray(['USDKRW', 'JPYKRW'], (data,pair)=>console.log(data, pair)), error => console.log('[Error]' + error)); | ||
getDataArray(['USDKRW', 'JPYKRW'], (data,pair) => console.log(data, pair)), error => console.log('[Error]' + error)); | ||
// 1070 USDKRW or [Error] error message | ||
@@ -40,3 +85,3 @@ // 970 JPYKRW or [Error] error message | ||
* ``` javascript | ||
getDataArray(['USDKRW', 'JPYKRW'], (data,pair)=>console.log(data, pair)), (error,pair) => console.log(`[Error:${pair}]${error}`)); | ||
getDataArray(['USDKRW', 'JPYKRW'], (data,pair) => console.log(data, pair)), (error,pair) => console.log(`[Error:${pair}]${error}`)); | ||
// 1070 USDKRW or [Error:USDKRW] error message | ||
@@ -59,7 +104,17 @@ // 970 JPYKRW or [Error:JPYKRW] error message | ||
const yahooExchange = require('yahoo-exchange'); | ||
yahooExchange.getData('USDKRW', data => console.log(data)); | ||
yahooExchange.getDataArray(['USDKRW', 'JPYKRW'], data =>console.log(data)); | ||
yahooExchange.getData('USDKRW', (data, pair) => console.log(data, pair)); | ||
yahooExchange.getDataArray(['USDKRW', 'JPYKRW'], (data, pair) => console.log(data, pair)); | ||
/** | ||
* yahooExchange.getData('USDKRW', data => console.log(data)); | ||
* yahooExchange.getDataArray(['USDKRW', 'JPYKRW'], data => console.log(data)); | ||
* yahooExchange.getData('USDKRW', (data, pair) => console.log(data, pair)); | ||
* yahooExchange.getDataArray(['USDKRW', 'JPYKRW'], (data, pair) => console.log(data, pair)); | ||
* yahooExchange.getData('USDKRW', (data, pair) => console.log(data, pair), (error,pair) => console.log(`[Error] ${pair}\n${error}`)); | ||
* yahooExchange.getDataArray(['USDKRW', 'JPYKRW'], (data, pair) => console.log(data, pair), (error,pair) => console.log(`[Error] ${pair}\n${error}`)); | ||
*/ | ||
yahooExchange.getExchangeDataArray('USDKRW', data => console.log(data)); | ||
yahooExchange.getExchangeDataArray(['USDKRW', 'JPYKRW'], data => console.log(data)); | ||
yahooExchange.getExchangeDataArray('USDKRW', (data, pair) => console.log(data, pair)); | ||
yahooExchange.getExchangeDataArray(['USDKRW', 'JPYKRW'], (data, pair) => console.log(data, pair)); | ||
yahooExchange.getExchangeDataArray('USDKRW', (data, pair) => console.log(data, pair), (error, pair) => console.log(`[Error] ${pair}\n${error}`)); | ||
yahooExchange.getExchangeDataArray(['USDKRW', 'JPYKRW'], (data, pair) => console.log(data, pair), (error, pair) => console.log(`[Error] ${pair}\n${error}`)); | ||
``` |
const index = require('../index'); | ||
index.getData('USDKRW', data => console.log(data)); | ||
index.getDataArray(['USDKRW', 'JPYKRW'], data => console.log(data)); | ||
index.getData('USDKRW', (data, pair) => console.log(data, pair)); | ||
index.getDataArray(['USDKRW', 'JPYKRW'], (data, pair) => console.log(data, pair)); | ||
index.getData('USDKRW', (data, pair) => console.log(data, pair), (error,pair) => console.log(`[Error] ${pair}\n${error}`)); | ||
index.getDataArray(['USDKRW', 'JPYKRW'], (data, pair) => console.log(data, pair), (error,pair) => console.log(`[Error] ${pair}\n${error}`)); | ||
/** | ||
* index.getData('USDKRW', data => console.log(data)); | ||
* index.getDataArray(['USDKRW', 'JPYKRW'], data => console.log(data)); | ||
* index.getData('USDKRW', (data, pair) => console.log(data, pair)); | ||
* index.getDataArray(['USDKRW', 'JPYKRW'], (data, pair) => console.log(data, pair)); | ||
* index.getData('USDKRW', (data, pair) => console.log(data, pair), (error,pair) => console.log(`[Error] ${pair}\n${error}`)); | ||
* index.getDataArray(['USDKRW', 'JPYKRW'], (data, pair) => console.log(data, pair), (error,pair) => console.log(`[Error] ${pair}\n${error}`)); | ||
*/ | ||
index.getExchangeDataArray('USDKRW', data => console.log(data)); | ||
index.getExchangeDataArray(['USDKRW', 'JPYKRW'], data => console.log(data)); | ||
index.getExchangeDataArray('USDKRW', (data, pair) => console.log(data, pair)); | ||
index.getExchangeDataArray(['USDKRW', 'JPYKRW'], (data, pair) => console.log(data, pair)); | ||
index.getExchangeDataArray('USDKRW', (data, pair) => console.log(data, pair), (error, pair) => console.log(`[Error] ${pair}\n${error}`)); | ||
index.getExchangeDataArray(['USDKRW', 'JPYKRW'], (data, pair) => console.log(data, pair), (error, pair) => console.log(`[Error] ${pair}\n${error}`)); |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
26276
156
1
117