ms.macro
v2.0.0
Published
Convert various time formats to milliseconds at build time in Babel.
Downloads
71,850
Readme
ms.macro
Convert various time formats to milliseconds at build time in Babel.
Usage
Simply install and configure babel-plugin-macros
and then use ms.macro
the same way you use ms
.
Example
Given the following input:
import ms from 'ms.macro';
const ONE_DAY = ms('1 day');
const TWO_DAYS = ms('2 days');
Babel will produce the following output:
const ONE_DAY = 86400000;
const TWO_DAYS = 172800000;
It also works as a tagged template literal:
const ONE_DAY = ms`1 day`;
const TWO_DAYS = ms`2 days`;
That will produce the same output as the function version.
FAQ
What are the advantages of running ms
as a macro?
The two main advantages of running ms
as a macro are that there are no
runtime dependencies and any errors (such as mistakenly callingms('1 da')
instead of ms('1 day')
) become build-time errors rather than run-time errors.
Are there any disadvantages of running ms
as a macro?
This macro only supports the single string-argument signature of ms
, i.e.,
passing a single string and getting back a number. This is because when you are
converting a number of milliseconds to an equivalent string representation you
are typically using calculated values not available at build-time. If you want
to convert a number of milliseconds to an equivalent string representation you
should use the ms
package directly. If you want to use both packages
together, you can give the imported values different names:
import ms from 'ms';
import msm from './ms.macro';
const ONE_DAY = msm('1 day');
const str = ms(172800000);
That will result in the following output:
import ms from 'ms';
const ONE_DAY = 86400000;
const str = ms(172800000);
License
MIT