I'm coming from Java, and trying to turn the idea of a "utility class" into something that works in ES6.
In my file numbers.js
, I can export a single function:
export default function padDigits(number, digits) {
return new Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
}
Which can then be imported in another file like:
import padDigits from '../utils/numbers';
...
var paddedNum = padDigits(myNum, 2);
However: I would like for two things to be possible: I want to (1) export multiple functions from this single file, and (2) I would like for them to be accessible via a single import statement, and called via a namespace/classname prefix, like:
import Numbers from '../utils/numbers';
...
var paddedNum = Numbers.padDigits(myNum, 2);
var truncatedNum = Numbers.truncate(myNum, 3);
But I'm having a hard time finding the right syntax to accomplish this.