Skip to content

Commit

Permalink
Remove isDecimal and quantity options (#25)
Browse files Browse the repository at this point in the history
* Remove `isDecimal` and `quantity` options

* Bump dependencies
  • Loading branch information
knutkirkhorn committed Mar 9, 2023
1 parent ef9d4d5 commit 681bd4b
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 192 deletions.
29 changes: 4 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ setApiKey('example-cmc-API-key');
console.log(`$${await btcValue()}`);
// => e.g. $11048

// Print the current value as a decimal number if `isDecimal` is `true`
console.log(`$${await btcValue({isDecimal: true})}`);
// => e.g. $11048.10

// Print the current value of Bitcoin in NOK (Norwegian krone)
console.log(`kr ${await btcValue({currencyCode: 'NOK'})}`);
console.log(`kr ${await btcValue('NOK')}`);
// => e.g. kr 86664

// Print the current value of 2.2 BTC in USD
Expand All @@ -63,28 +59,11 @@ console.log(await getSupportedCurrencies());

The Bitcoin value can be retrieved from [CoinMarketCap](https://coinmarketcap.com/) or [CoinGecko](https://www.coingecko.com). See the API used for CoinMarketCap [here](https://coinmarketcap.com/api/) and for CoinGecko [here](https://www.coingecko.com/en/api). If using the CoinMarketCap API to retrieve Bitcoin values, it is required to obtain and use an API key. This can be done [here](https://coinmarketcap.com/api/). Before using the functions for retrieving the Bitcoin value, one must then call `btcValue.setApiKey(<KEY_HERE>)` with your key. If using CoinGecko, this is not needed.

### btcValue([options])

Returns the current Bitcoin value in USD ($) as an `integer`.

#### options ***(optional)***

Type: `object`

##### isDecimal

Type: `boolean`<br>
Default: `false`

Returns the current Bitcoin value as a `decimal number` if `isDecimal` is `true`.

##### quantity

Type: `number`
### btcValue(currencyCode?)

Returns the current Bitcoin value of a specified `quantity`.
Returns the current Bitcoin value in USD ($).

##### currencyCode
#### currencyCode

Type: `string`<br>
Default: `USD`
Expand Down
26 changes: 3 additions & 23 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
export type Options = {
/**
To return the number as a decimal number.
@default false
*/
isDecimal?: boolean;

/**
To return the value of a specified quantity.
*/
quantity?: number;

/**
To return the value of a specified currency.
@default 'USD'
*/
currencyCode?: string;
};

export type CurrencyProvider = 'cmc' | 'coingecko';

export type CMCCurrency = {
Expand All @@ -30,7 +9,8 @@ export type CMCCurrency = {
/**
Get the current Bitcoin value.
@param options
@param currencyCode The currency to return the value in
@default 'USD'
@example
```
Expand All @@ -39,7 +19,7 @@ console.log(`$${await btcValue()}`);
// => e.g. $11048
```
*/
export default function btcValue(options?: Options): Promise<number>;
export default function btcValue(currencyCode?: string): Promise<number>;

/**
Set the selected provider to retrieve Bitcoin values from. Supported providers are: `cmc` (CoinMarketCap) and `coingecko`.
Expand Down
48 changes: 2 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,44 +105,9 @@ export function setApiKey(newApiKey) {
apiKey = newApiKey;
}

function convertToTwoDecimals(number) {
// Check if the number is not an integer. If it is not, convert it to two decimals.
if (number % 1 !== 0) {
return Number.parseFloat(number.toFixed(2));
}

return number;
}

function parseOptions(currencyValue, options) {
const {isDecimal, quantity} = options;

// Set the new currency value if quantity is provided
if (quantity) {
currencyValue *= quantity;
}

// If `isDecimal` is false => return an integer
if (!isDecimal) {
currencyValue = Number.parseInt(currencyValue, 10);
}

return convertToTwoDecimals(currencyValue);
}

export default async function btcValue(options) {
export default async function btcValue(currencyCode = 'USD') {
let urlParameters = '';

// Set default value of `currencyCode` and `isDecimal`
options = {
currencyCode: 'USD',
isDecimal: false,
...options
};

let {currencyCode} = options;
const {isDecimal, quantity} = options;

// Check that the type of `currencyCode` is `string`
if (typeof currencyCode !== 'string') {
throw new TypeError('`currencyCode` should be of type `string`');
Expand All @@ -151,14 +116,6 @@ export default async function btcValue(options) {
// Ensure the currency code is uppercase
currencyCode = currencyCode.toUpperCase();

if (typeof isDecimal !== 'boolean') {
throw new TypeError('`isDecimal` should be of type `boolean`');
}

if (quantity && typeof quantity !== 'number') {
throw new TypeError('`quantity` should be of type `number`');
}

let currencyValue;

if (selectedProvider === 'cmc') {
Expand Down Expand Up @@ -189,8 +146,7 @@ export default async function btcValue(options) {
}
}

currencyValue = Number(currencyValue);
return parseOptions(currencyValue, options);
return Number(currencyValue);
}

async function getPercentageChangeLastTime(type) {
Expand Down
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ expectType<void>(setProvider('cmc'));
expectType<void>(setApiKey('example-CMC-PRO-API-key'));
expectType<Promise<CMCCurrency[] | string[]>>(getSupportedCurrencies());
expectType<Promise<number>>(btcValue());
expectType<Promise<number>>(btcValue({isDecimal: true, quantity: 13.37, currencyCode: 'NOK'}));
expectType<Promise<number>>(btcValue('NOK'));
expectType<Promise<number>>(getPercentageChangeLastHour());
expectType<Promise<number>>(getPercentageChangeLastDay());
expectType<Promise<number>>(getPercentageChangeLastWeek());
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"license": "MIT",
"dependencies": {
"got": "^12.5.3",
"got": "^12.6.0",
"package-user-agent": "^1.0.1"
},
"devDependencies": {
Expand All @@ -39,9 +39,9 @@
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-ava": "^14.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-unicorn": "^45.0.2",
"eslint-plugin-unicorn": "^46.0.0",
"nock": "^13.3.0",
"nyc": "^15.1.0",
"tsd": "^0.25.0"
"tsd": "^0.26.1"
}
}
5 changes: 2 additions & 3 deletions test/coingecko.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ test('throws if missing percent change', async t => {
}
});

test('return integer when options is `currencyCode`: `USD`, `isDecimal`: `true` and `quantity`: `2.2`', async t => {
test('return number when `currencyCode` is `USD`', async t => {
nock('https://api.coingecko.com')
.get(/api\/v3\/simple\/price/)
.reply(200, {
Expand All @@ -182,10 +182,9 @@ test('return integer when options is `currencyCode`: `USD`, `isDecimal`: `true`
});

try {
const value = await btcValue({currencyCode: 'USD', isDecimal: false, quantity: 2.2});
const value = await btcValue('USD');

t.is(typeof value, 'number');
t.is(value % 1, 0);
} catch (error) {
t.is(error, '');
t.fail();
Expand Down
70 changes: 2 additions & 68 deletions test/coinmarketcap.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,82 +128,16 @@ test('percentage last week return a number', async t => {
}
});

test('return integer when options is `currencyCode`: `USD`', async t => {
test('return number when `currencyCode` is `USD`', async t => {
try {
const value = await btcValue({currencyCode: 'USD'});
const value = await btcValue('USD');

t.is(typeof value, 'number');
t.is(value % 1, 0);
} catch {
t.fail();
}
});

test('return integer when `currencyCode` is `NOK`', async t => {
try {
const value = await btcValue({currencyCode: 'NOK'});

t.is(typeof value, 'number');
t.is(value % 1, 0);
} catch {
t.fail();
}
});

test('return decimal when options is `currencyCode`: `USD` and `isDecimal`: `true`', async t => {
try {
const value = await btcValue({currencyCode: 'USD', isDecimal: true});

t.is(typeof value, 'number');
} catch {
t.fail();
}
});

test('return integer when options is `currencyCode`: `USD`, `isDecimal`: `true` and `quantity`: `2.2`', async t => {
try {
const value = await btcValue({currencyCode: 'USD', isDecimal: false, quantity: 2.2});

t.is(typeof value, 'number');
t.is(value % 1, 0);
} catch {
t.fail();
}
});

test('return integer when `isDecimal` is false', async t => {
try {
const value = await btcValue({isDecimal: false});

t.is(typeof value, 'number');
t.is(value % 1, 0);
} catch {
t.fail();
}
});

test('return decimal when `isDecimal` is true', async t => {
try {
const value = await btcValue({isDecimal: true});

t.is(typeof value, 'number');
} catch {
t.fail();
}
});

test('return integer when `isDecimal` is not in options', async t => {
try {
const value = await btcValue({isDecimalsss: true});

t.is(typeof value, 'number');
t.is(value % 1, 0);
} catch (error) {
t.is(error, '');
t.fail();
}
});

test('supported currencies returns array of `cmc` currency objects', async t => {
const expectedResult = [
{
Expand Down
24 changes: 1 addition & 23 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,11 @@ test('throws TypeError when `apiKey` is not a string', t => {
}
});

test('throws TypeError when `isDecimal` is not a boolean', async t => {
const expectedResult = new TypeError('`isDecimal` should be of type `boolean`');

try {
await btcValue({isDecimal: 'true'});
t.fail();
} catch (error) {
t.deepEqual(error, expectedResult);
}
});

test('throws TypeError when `quantity` is not a number', async t => {
const expectedResult = new TypeError('`quantity` should be of type `number`');

try {
await btcValue({quantity: '1337'});
t.fail();
} catch (error) {
t.deepEqual(error, expectedResult);
}
});

test('throws TypeError when `currencyCode` is not a string', async t => {
const expectedResult = new TypeError('`currencyCode` should be of type `string`');

try {
await btcValue({currencyCode: 1337});
await btcValue(1337);
t.fail();
} catch (error) {
t.deepEqual(error, expectedResult);
Expand Down

0 comments on commit 681bd4b

Please sign in to comment.