0

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.

Craig Otis
  • 31,257
  • 32
  • 136
  • 234
  • Read up on ES6 module syntax. A good reference is http://www.2ality.com/2014/09/es6-modules-final.html. –  Oct 03 '15 at 19:55

1 Answers1

3

are you perhaps looking for the wildcard?

import * as nums from '../utils/numbers';

A similiar question: TypeScript 1.5: ES6 Module default import of CommonJS 'export =' (.d.ts only issue?)

A more detailed answer into this: New es6 syntax for importing commonjs / amd modules i.e. `import foo = require('foo')`

Community
  • 1
  • 1
Robin Radic
  • 144
  • 6