Home > Software design >  Call a function from within the same .js file which is exported
Call a function from within the same .js file which is exported

Time:11-21

I have a .js file with several functions that are getting exported. In one case, I need to call one of the exported functions from within the other exported function but It wont let me since it says it can not find the function. What am I doing wrong? I also tried using "this.SMA" but makes no difference.

exports.SMA = async function (ohlcv, period) {
  const close = ohlcv.map((c) => c[OHLCV_INDEX.CLOSE]);
  const result = await tulind.indicators.sma.indicator([close], [period]);
  const sma = result[0][result[0].length - 1];
  return sma;
};

exports.STRONG_UPTREND = async function (ohlcv, period1, period2, period3) {
  const close = ohlcv.map((c) => c[OHLCV_INDEX.CLOSE]);
  const sma1 = await SMA(ohlcv, period1); //want to call this
  const sma2 = await SMA(ohlcv, period2); //want to call this
  const sma3 = await SMA(ohlcv, period3); //want to call this

  const last_close = close[close.length - 1];

  if (last_close >= sma1 && last_close > sma2 && last_close > sma3) return true;

  return false;
};

CodePudding user response:

How about setting the function before exporting it? So something like:

const sma = async function (ohlcv, period) {
  const close = ohlcv.map((c) => c[OHLCV_INDEX.CLOSE]);
  const result = await tulind.indicators.sma.indicator([close], [period]);
  const sma = result[0][result[0].length - 1];
  return sma;
};

exports.SMA = sma;

then you should be able to call it like this: sma()

CodePudding user response:

There are multiple ways of doing this. Two of which I will list below.

Way 1

Call exports.SMA instead of SMA

exports.STRONG_UPTREND = async function (ohlcv, period1, period2, period3) {
  const close = ohlcv.map((c) => c[OHLCV_INDEX.CLOSE]);
  const sma1 = await exports.SMA(ohlcv, period1); //want to call this
  ...

Way 2

Extract the function to a constant and then assign the exports at the end.

const SMA = async function (ohlcv, period) {
  const close = ohlcv.map((c) => c[OHLCV_INDEX.CLOSE]);
  const result = await tulind.indicators.sma.indicator([close], [period]);
  const sma = result[0][result[0].length - 1];
  return sma;
};

const STRONG_UPTREND = async function (ohlcv, period1, period2, period3) {
  const close = ohlcv.map((c) => c[OHLCV_INDEX.CLOSE]);
  const sma1 = await SMA(ohlcv, period1); //want to call this
  const sma2 = await SMA(ohlcv, period2); //want to call this
  const sma3 = await SMA(ohlcv, period3); //want to call this

  const last_close = close[close.length - 1];

  if (last_close >= sma1 && last_close > sma2 && last_close > sma3) return true;

  return false;
};

exports.SMA = SMA;
exports.STRONG_UPTREND = STRONG_UPTREND;

CodePudding user response:

have you tried adding module before exports maybe that'll work

module.exports.SMA = async function (ohlcv, period) {
  const close = ohlcv.map((c) => c[OHLCV_INDEX.CLOSE]);
  const result = await tulind.indicators.sma.indicator([close], [period]);
  const sma = result[0][result[0].length - 1];
  return sma;
};
  • Related