Background: Building on this question about how to expose a library for unit testing with Jest. I would now like to create a class of functions that can be called with dot notation inside of dot notation (this may not even be possible). First a bit of methodology that I'm currently using:
Here's an example of how I've been modifying the JavaScript Math functions:
Math.mean = function(numericArray){
if(Math.isNumericArray(numericArray)){
return math.mean(numericArray);
}
return false;
}
FYI, The lowercase math.mean() call is to the math library: https://mathjs.org/, and isNumericArray is just a validator to make sure what's passed in is a numeric array.
And then I export it like this:
module.exports.mean = exports = Math.mean;
So Jest can see it for my unit tests.
My actual question: What I want to do is create an upper level "class" called Math.acs, so you'd make the call into it with Math.acs. Then it would have sub-functions (EG: foo() & bar()) so you'd call them like this: Math.acs.foo(data); or Math.acs.bar(data);
I've tried to encapsulate them into an IIFE:
Math.acs = (function(data) {
function foo(data){
console.log('hi mom');
};
function bar(data){
console.log("hi dad");
}
return bar;
})();
Which didn't work (the CLI can't see anything below Math.acs), I've also tried straight functions inside of functions which also didn't work.
I'm not going to die if this isn't possible, but it would make the half dozen or so functions required in the acs module centralized and easier to maintain. If it's not possible, I can write the individual math modules the same way I've shown above.