How it works…

How would we use this module, and how does it work? If we wanted to import some of its functions from other modules, we'd write something as follows; see how we use some of the operations we designed:

// Source file: src/doroundmath.js

/* @flow */
"use strict";

const RM = require("./roundmath.js");

console.log(RM.addR(12.348, 4.221)); // 16.57
console.log(RM.changeSign(0.07)); // error; RM.changeSign is not a function

The first two lines are the usual. Then, we require() whatever modules we need; in this case, a single one. Also, per convention, all such requirements are grouped together, at the start, to make it simpler to understand the needs of a module, without having to go all through the code. In our case, RM gets assigned the exports object, so you can refer to RM.addR(), RM.subR(), and so on, and this makes clear to the reader that you are using something from the RM module.

If you want to write a bit less, you can take advantage of the destructuring statement (which we met in the Destructuring arrays and objects section in the previous chapter) and directly assign the desired methods to individual variables:

/* @flow */
"use strict";

const { multR, divR } = require("./roundmath.js");

console.log(multR(22.9, 12.4)); // 283.96
console.log(divR(22, 7)); // 3.14

It is better if you get used to only importing modules that you'll need. In other cases (which we'll see in later chapters) we can use tools to just remove whatever modules you don't actually use, and if you require() everything, that wouldn't be possible.