diff --git a/.babelrc b/.babelrc index 10266dd..6a13c6a 100644 --- a/.babelrc +++ b/.babelrc @@ -1,9 +1,13 @@ { "presets": [ - "env" + ["@babel/preset-env", { + "targets": { + "node": "8.3.0" + } + }] ], "plugins": [ - "transform-regenerator", - "transform-object-rest-spread" + "@babel/plugin-transform-regenerator", + "@babel/plugin-proposal-object-rest-spread" ] } diff --git a/.eslintrc b/.eslintrc deleted file mode 100644 index e3578aa..0000000 --- a/.eslintrc +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "standard" -} diff --git a/.eslintrc.json b/.eslintrc.json index 4ff11de..71c4cbc 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,26 +1,44 @@ { - "parser": "babel-eslint", - "extends": [ - "standard" + "root": true, + + "plugins": [ + "jsdoc", + "mocha" ], - "rules": { - "jsx-quotes": ["error", "prefer-single"], - "semi": [1, "never"], - "max-len": 0, - "import/no-named-as-default": 0, - "import/no-named-as-default-member": 0 + + "env": { + "es6": true, + "node": true, + "es2020": true }, - "settings": { - "import/resolver": { - "node": { - "moduleDirectory": [ - "node_modules", - "lib" - ] - } - } + + "extends": [ + "standard", + "eslint:recommended", + "plugin:promise/recommended", + "plugin:node/recommended", + "plugin:import/errors", + "plugin:import/warnings", + "plugin:lodash/recommended", + "plugin:mocha/recommended", + "plugin:jsdoc/recommended" + ], + + "parserOptions": { + "ecmaVersion": 2020 }, - "env": { - "jest": true + + "rules": { + "jsdoc/check-tag-names": 0, + "jsdoc/no-undefined-types": 0, + "no-multiple-empty-lines": ["error", { "max": 1, "maxEOF": 1 }], + "promise/no-callback-in-promise": 0, + "lodash/prefer-lodash-method": 0, + "lodash/prefer-noop": 0, + "lodash/prefer-constant": 0, + "node/no-unpublished-require": 0, + "mocha/no-mocha-arrows": 0, + "mocha/no-setup-in-describe": 0, + "mocha/no-skipped-tests": 0 } } diff --git a/.gitignore b/.gitignore index 68d4541..6d32c63 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ todo package-lock.json .env dist/ +tags +.nyc_output +coverage diff --git a/CHANGELOG b/CHANGELOG index 95ffc8d..9b6f6ce 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,8 @@ +# 1.0.10 +- meta: bump deps +- meta: normalize eslint config +- refactor: improve doclets + # 1.0.9 - meta: bump bignumber.js dep to 9.0.0 diff --git a/README.md b/README.md index 3374b9d..4c5b67c 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,8 @@ const n = nonce() ### Docs -[Refer to `docs/func_docs.md`](/docs/func_docs.md) for JSDoc-generated API documentation +API documentation can be found in [`docs/reference.md`](docs/reference.md), and +examples in the [`examples`](examples) folder. ### Example: Padding Candles diff --git a/docs/func_docs.md b/docs/reference.md similarity index 59% rename from docs/func_docs.md rename to docs/reference.md index 38b45c8..4ee84b5 100644 --- a/docs/func_docs.md +++ b/docs/reference.md @@ -1,14 +1,25 @@ +## Modules + +
+
bfx-api-node-util
+

This module contains minor utilities shared by the Bitfinex Node.JS API +libraries.

+
+
+ ## Functions
-
genAuthSig(apiSecret, payload)Object
+
genAuthSig(secret, [payload])AuthSignature

Generates an auth signature, payload, and nonce for passing to the WS & REST APIs

isClass(f)boolean
-
+

Query if the provided argument is a class

+
isSnapshot(msg)boolean
-
+

Query if the provided message is a websocket snapshot packet

+
nonce()number

Generates a new nonce for usage with the Bitfinex APIs

@@ -16,13 +27,13 @@ APIs

Fills in missing (0 volume) candles on the provided dataset. The result is a new array (does not mutate)

-
setSigFig(n, maxSigs)string
+
setSigFig(number, [maxSigs])string

Smartly set the precision (decimal) on a value based off of the significant digit maximum. For example, calling with 3.34 when the max sig figs allowed is 5 would return '3.3400', the representation number of decimals IF they weren't zeros.

-
setPrecision(number, decimals)string
+
setPrecision(number, [decimals])string

Casts the provided value to a number if required, and limits the number of decimals to the specified value. Returns a decimal string.

@@ -34,39 +45,58 @@ decimals to the specified value. Returns a decimal string.

+## Typedefs + +
+
AuthSignature : object
+

Authorization signature for WS & REST APIs.

+
+
+ + + +## bfx-api-node-util +This module contains minor utilities shared by the Bitfinex Node.JS API +libraries. + +**License**: MIT -## genAuthSig(apiSecret, payload) ⇒ Object +## genAuthSig(secret, [payload]) ⇒ [AuthSignature](#AuthSignature) Generates an auth signature, payload, and nonce for passing to the WS & REST APIs **Kind**: global function -**Returns**: Object - authSignature +**Returns**: [AuthSignature](#AuthSignature) - authSignature -| Param | Type | Description | -| --- | --- | --- | -| apiSecret | string | | -| payload | string | optional signature payload, generated by default | +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| secret | string | | API secret | +| [payload] | string | "''" | signature payload, generated by default | ## isClass(f) ⇒ boolean +Query if the provided argument is a class + **Kind**: global function **Returns**: boolean - isClass -| Param | Type | -| --- | --- | -| f | Class | +| Param | Type | Description | +| --- | --- | --- | +| f | function | function to check | ## isSnapshot(msg) ⇒ boolean +Query if the provided message is a websocket snapshot packet + **Kind**: global function **Returns**: boolean - isSnapshot -| Param | Type | -| --- | --- | -| msg | Array \| Array.<Array> | +| Param | Type | Description | +| --- | --- | --- | +| msg | Array \| Array.<Array> | message | @@ -91,7 +121,7 @@ new array (does not mutate) -## setSigFig(n, maxSigs) ⇒ string +## setSigFig(number, [maxSigs]) ⇒ string Smartly set the precision (decimal) on a value based off of the significant digit maximum. For example, calling with 3.34 when the max sig figs allowed is 5 would return '3.3400', the representation number of decimals IF they @@ -100,24 +130,24 @@ weren't zeros. **Kind**: global function **Returns**: string - str -| Param | Type | Description | -| --- | --- | --- | -| n | number | | -| maxSigs | number | default 5 | +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| number | number | 0 | number | +| [maxSigs] | number | 5 | max significant figures | -## setPrecision(number, decimals) ⇒ string +## setPrecision(number, [decimals]) ⇒ string Casts the provided value to a number if required, and limits the number of decimals to the specified value. Returns a decimal string. **Kind**: global function **Returns**: string - result -| Param | Type | Default | -| --- | --- | --- | -| number | number \| string | 0 | -| decimals | number | 0 | +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| number | number \| string | 0 | number | +| [decimals] | number | 0 | decimal precision | @@ -127,9 +157,9 @@ Prepares a amount for submitting via the API **Kind**: global function **Returns**: string - preparedAmount -| Param | Type | Default | -| --- | --- | --- | -| amount | number \| string | 0 | +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| amount | number \| string | 0 | amount | @@ -139,7 +169,21 @@ Prepares a price for submitting via the API **Kind**: global function **Returns**: string - preparedPrice -| Param | Type | Default | +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| price | number \| string | 0 | price | + + + +## AuthSignature : object +Authorization signature for WS & REST APIs. + +**Kind**: global typedef +**Properties** + +| Name | Type | Description | | --- | --- | --- | -| price | number \| string | 0 | +| payload | string | request payload, default 'AUTH${nonce}${nonce}'' | +| sig | string | signature in hexadecimal | +| nonce | number | used nonce | diff --git a/index.js b/index.js index db3fd22..692a391 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,14 @@ const isSnapshot = require('./lib/is_snapshot') const isClass = require('./lib/is_class') const padCandles = require('./lib/pad_candles') +/** + * This module contains minor utilities shared by the Bitfinex Node.JS API + * libraries. + * + * @license MIT + * @module bfx-api-node-util + */ + module.exports = { ...precision, nonce, diff --git a/lib/gen_auth_sig.js b/lib/gen_auth_sig.js index ad27df7..0beefe7 100644 --- a/lib/gen_auth_sig.js +++ b/lib/gen_auth_sig.js @@ -3,12 +3,23 @@ const crypto = require('crypto') const getNonce = require('./nonce') +/** + * Authorization signature for WS & REST APIs. + * + * @typedef {object} AuthSignature + * @property {string} payload - request payload, default + * 'AUTH${nonce}${nonce}'' + * @property {string} sig - signature in hexadecimal + * @property {number} nonce - used nonce + */ + /** * Generates an auth signature, payload, and nonce for passing to the WS & REST * APIs - * @param {string} apiSecret - * @param {string?} payload - optional signature payload, generated by default - * @return {Object} authSignature + * + * @param {string} secret - API secret + * @param {string} [payload=''] - signature payload, generated by default + * @returns {AuthSignature} authSignature */ const genAuthSig = (secret, payload = '') => { const nonce = getNonce() diff --git a/lib/is_class.js b/lib/is_class.js index 94e174c..976f53e 100644 --- a/lib/is_class.js +++ b/lib/is_class.js @@ -1,14 +1,16 @@ 'use strict' +const _isFunction = require('lodash/isFunction') + /** - * @param {Class} f - * @return {boolean} isClass + * Query if the provided argument is a class + * + * @param {Function} f - function to check + * @returns {boolean} isClass */ -const isClass = (f) => { - return ( - (typeof f === 'function') && - (/^class\s/.test(Function.prototype.toString.call(f))) - ) -} +const isClass = f => ( + _isFunction(f) && + (/^class\s/.test(Function.prototype.toString.call(f))) +) module.exports = isClass diff --git a/lib/is_snapshot.js b/lib/is_snapshot.js index 847b764..575d4e2 100644 --- a/lib/is_snapshot.js +++ b/lib/is_snapshot.js @@ -1,9 +1,13 @@ 'use strict' +const _isArray = require('lodash/isArray') + /** - * @param {Array|Array[]} msg - * @return {boolean} isSnapshot + * Query if the provided message is a websocket snapshot packet + * + * @param {Array|Array[]} msg - message + * @returns {boolean} isSnapshot */ -const isSnapshot = msg => msg[0] && Array.isArray(msg[0]) +const isSnapshot = msg => msg[0] && _isArray(msg[0]) module.exports = isSnapshot diff --git a/lib/nonce.js b/lib/nonce.js index 5f8adef..9ab0063 100644 --- a/lib/nonce.js +++ b/lib/nonce.js @@ -5,7 +5,7 @@ let lastNonce = Date.now() * 1000 /** * Generates a new nonce for usage with the Bitfinex APIs * - * @return {number} nonce + * @returns {number} nonce */ const nonce = () => { const now = Date.now() * 1000 diff --git a/lib/pad_candles.js b/lib/pad_candles.js index fc27a70..669031e 100644 --- a/lib/pad_candles.js +++ b/lib/pad_candles.js @@ -8,7 +8,7 @@ const _reverse = require('lodash/reverse') * * @param {Array[]} candles - array-format candles * @param {number} candleWidth - in ms - * @return {Array[]} paddedCandles + * @returns {Array[]} paddedCandles */ const padCandles = (candles, candleWidth) => { const paddedCandles = _reverse([...candles]) @@ -19,7 +19,7 @@ const padCandles = (candles, candleWidth) => { const candlesToFill = ((nextCandle[0] - candle[0]) / candleWidth) - 1 if (candlesToFill > 0) { - const fillerCandles = Array.apply(null, Array(candlesToFill)).map((c, i) => { + const fillerCandles = Array.apply(null, Array(candlesToFill)).map((_, i) => { return [ candle[0] + (candleWidth * (i + 1)), // mts candle[2], // open diff --git a/lib/precision.js b/lib/precision.js index 9a67b40..4feafa3 100644 --- a/lib/precision.js +++ b/lib/precision.js @@ -12,9 +12,9 @@ const AMOUNT_DECIMALS = 8 * is 5 would return '3.3400', the representation number of decimals IF they * weren't zeros. * - * @param {number} n - * @param {number} maxSigs - default 5 - * @return {string} str + * @param {number} number - number + * @param {number} [maxSigs=5] - max significant figures + * @returns {string} str */ const setSigFig = (number = 0, maxSigs = DEFAULT_SIG_FIGS) => { const n = +(number) @@ -32,9 +32,9 @@ const setSigFig = (number = 0, maxSigs = DEFAULT_SIG_FIGS) => { * Casts the provided value to a number if required, and limits the number of * decimals to the specified value. Returns a decimal string. * - * @param {number|string} number - * @param {number} decimals - * @return {string} result + * @param {number|string} number - number + * @param {number} [decimals=0] - decimal precision + * @returns {string} result */ const setPrecision = (number = 0, decimals = 0) => { const n = +(number) @@ -47,8 +47,8 @@ const setPrecision = (number = 0, decimals = 0) => { /** * Prepares a amount for submitting via the API * - * @param {number|string} amount - * @return {string} preparedAmount + * @param {number|string} amount - amount + * @returns {string} preparedAmount */ const prepareAmount = (amount = 0) => { return setPrecision(amount, AMOUNT_DECIMALS) @@ -57,8 +57,8 @@ const prepareAmount = (amount = 0) => { /** * Prepares a price for submitting via the API * - * @param {number|string} price - * @return {string} preparedPrice + * @param {number|string} price - price + * @returns {string} preparedPrice */ const preparePrice = (price = 0) => { return setSigFig(price, PRICE_SIG_FIGS) diff --git a/package.json b/package.json index c27165e..47e441e 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,22 @@ { "name": "bfx-api-node-util", - "version": "1.0.9", + "version": "1.0.10", "description": "Utilities for the Bitfinex node API", "engines": { - "node": ">=7" + "node": ">=8.3.0" }, "main": "./dist/index.js", + "husky": { + "hooks": { + "pre-commit": "npm test" + } + }, "scripts": { - "lint": "standard", + "lint": "./node_modules/.bin/eslint lib/ test/ index.js", "test": "npm run lint && npm run unit", - "unit": "NODE_ENV=test mocha -b --recursive", + "unit": "NODE_ENV=test nyc --reporter=lcov --reporter=html mocha -b --recursive", "build": "babel -q ./index.js -d ./dist && babel -q ./lib -d ./dist/lib && copy package.json dist", - "docs": "npm run func_docs", - "func_docs": "node_modules/jsdoc-to-markdown/bin/cli.js lib/*.js > docs/func_docs.md" + "docs": "rm -rf docs/*.md && ./node_modules/.bin/jsdoc2md -f index.js 'lib/**/*.js' > docs/reference.md" }, "repository": { "type": "git", @@ -39,18 +43,29 @@ }, "homepage": "http://bitfinexcom.github.io/bfx-api-node-util/", "devDependencies": { - "babel-eslint": "^10.0.3", + "bfx-api-node-models": "^1.1.0", "chai": "^4.2.0", - "jsdoc-to-markdown": "^5.0.1", - "mocha": "^6.2.0", - "standard": "^14.1.0", - "bfx-api-node-models": "^1.1.0" + "eslint": "^7.2.0", + "eslint-config-standard": "^14.1.0", + "eslint-plugin-import": "^2.20.1", + "eslint-plugin-jsdoc": "^27.0.4", + "eslint-plugin-lodash": "^7.1.0", + "eslint-plugin-mocha": "^7.0.1", + "eslint-plugin-node": "^11.0.0", + "eslint-plugin-promise": "^4.2.1", + "eslint-plugin-standard": "^4.0.1", + "husky": "^4.2.3", + "jsdoc-to-markdown": "^6.0.1", + "mocha": "^7.2.0", + "nyc": "^15.0.0" }, "dependencies": { - "babel-plugin-transform-object-rest-spread": "^6.26.0", - "babel-plugin-transform-regenerator": "^6.26.0", - "babel-preset-env": "^1.7.0", + "@babel/cli": "^7.10.1", + "@babel/plugin-proposal-object-rest-spread": "^7.10.1", + "@babel/plugin-transform-regenerator": "^7.10.1", + "@babel/preset-env": "^7.10.2", "bignumber.js": "^9.0.0", - "copy": "^0.3.2" + "copy": "^0.3.2", + "lodash": "^4.17.15" } }