From 28041d00ba369c514af28fe40b658f3c5853c90b Mon Sep 17 00:00:00 2001
From: Cris Mihalache This module contains minor utilities shared by the Bitfinex Node.JS API
+libraries. Generates an auth signature, payload, and nonce for passing to the WS & REST
APIs Query if the provided argument is a class Query if the provided message is a websocket snapshot packet Generates a new nonce for usage with the Bitfinex APIs
+
+
## Functions
-
Object
AuthSignature
boolean
boolean
number
Fills in missing (0 volume) candles on the provided dataset. The result is a new array (does not mutate)
string
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.
string
string
Casts the provided value to a number if required, and limits the number of decimals to the specified value. Returns a decimal string.
object
Authorization signature for WS & REST APIs.
+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..75dcb25 100644
--- a/package.json
+++ b/package.json
@@ -3,16 +3,20 @@
"version": "1.0.9",
"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"
}
}
From 92f850cf472209f2b86adad645b48ed415063ecc Mon Sep 17 00:00:00 2001
From: Cris Mihalache