Build-Time MS Converter Macro

2.0.0 · active · verified Tue Apr 21

ms.macro is a Babel macro designed to convert human-readable time strings (e.g., "1 day", "2 hours") into their corresponding millisecond values directly at build time. This package, currently at version 2.0.0, aims to eliminate runtime dependencies and shift potential conversion errors from runtime to build time. It is a stable package that typically sees updates driven by changes in Babel's macro API or the underlying `ms` package's parsing logic. Its primary differentiator is its build-time execution, which means that the original `ms` package does not need to be shipped with your production code for these specific conversions, resulting in smaller bundle sizes and improved reliability by catching invalid time strings during compilation rather than during execution. Unlike the original `ms` package, `ms.macro` exclusively supports the string-to-number conversion; it does not offer the reverse conversion from milliseconds to a human-readable string.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates `ms.macro` converting various human-readable time strings into millisecond values at build time, using both function call and tagged template literal syntax.

import ms from 'ms.macro';

// Basic function call usage
const ONE_HOUR = ms('1 hour');
const TWO_DAYS = ms('2 days');
const THIRTY_MINUTES = ms('30m');

// Tagged template literal usage
const ONE_WEEK = ms`1 week`;
const HALF_DAY = ms`0.5d`;

console.log(`One hour in milliseconds: ${ONE_HOUR}`); // -> 3600000
console.log(`Two days in milliseconds: ${TWO_DAYS}`); // -> 172800000
console.log(`Thirty minutes in milliseconds: ${THIRTY_MINUTES}`); // -> 1800000
console.log(`One week in milliseconds: ${ONE_WEEK}`); // -> 604800000
console.log(`Half day in milliseconds: ${HALF_DAY}`); // -> 43200000

view raw JSON →