Can simple vanilla dependency injection, such as modules exporting a function that expects an object of dependencies fix the below problem of helper files trying to share their helper functions with each other? Thanks.
index.js
const { sumEvens } = require("./math");
const nums = [1, 2, 1, 1, 2];
console.log(sumEvens(nums));
math.js
const { removeOdds } = require("./filters");
const isEven = (n) => n % 2 === 0;
const sumEvens = (nums) => removeOdds(nums)
.reduce((sum, n) => sum + n, 0);
module.exports = {
isEven,
sumEvens
}
filters.js
const { isEven } = require("./math");
const removeOdds = (nums) => nums.filter((n) => isEven(n));
module.exports = {
removeOdds
}
TypeError: isEven is not a function
, I think because of some circular requiring?