• Stars
    star
    197
  • Rank 197,722 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convert various time formats to milliseconds at build time in Babel.

ms.macro

Dependency Status devDependency Status Greenkeeper badge Build Status Npm Version License Badges

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

More Repositories

1

gulp-cheerio

Manipulate HTML and XML files with Cheerio in Gulp.
JavaScript
98
star
2

gulp-spawn-mocha

Runs Mocha as a child process in Gulp.
JavaScript
87
star
3

vue-node

Load vue components in node.
JavaScript
62
star
4

redux-ts-utils

Everything you need to create type-safe applications with Redux!
TypeScript
55
star
5

dotfiles

Muh dotfiles.
Shell
40
star
6

babel-plugin-remove-webpack

Removes webpack-specific functions from JavaScript code.
JavaScript
32
star
7

webpack-to-memory

Run a webpack build and get the resulting modules from memory.
JavaScript
25
star
8

nws

A simple tool designed to launch a simple static web server in your current working directory.
JavaScript
25
star
9

knpw.rs

With Ken Powers comes Ken Responsibility
MDX
23
star
10

css-modules-electron

A require hook to compile and use CSS modules in Electron.
JavaScript
22
star
11

connect-static-transform

A connect middleware which allows transformation of static files before serving them.
JavaScript
16
star
12

docker-swarm-terraform

Set up a Docker Swarm Cluster on Digital Ocean using Terraform
HCL
15
star
13

instant.bible

The source code for https://instant.bible, an as-you-type Bible search engine!
TypeScript
13
star
14

react-compose-components

A utility to flatten component pyramids in React.
TypeScript
13
star
15

testing-in-browsers-and-node

Code demonstrating how to make tests which run in web browsers and Node which automatically rerun when needed.
JavaScript
13
star
16

Base.js-Module

A conversion of Dean Edwards' Base.js to run as an AMD Module, a CommonJS module, or a standard script all in one file. Also updated to pass JSHint. Original code available here: http://dean.edwards.name/weblog/2006/03/base/
JavaScript
12
star
17

Multi-Threaded-In-Place-QuickSort

Java
10
star
18

babel-plugin-remove-bugs

A Babel plugin which removes all bugs from your code.
JavaScript
9
star
19

electron-handlebars

Use Handlebars instead of plain HTML for your Electron apps!
JavaScript
8
star
20

falafelify

A browserify transform which lets you run your JavaScript files through falafel.
JavaScript
4
star
21

pco-graphql

A GraphQL query server that sits in front of Planning Center Online
JavaScript
3
star
22

IntelliJ-Stack-Overflow-Plugin

Java
3
star
23

react-fp-starter

An opinionated starter project for React.
JavaScript
2
star
24

ws

Ruby
2
star
25

vite-plugin-svg-sprite-components

Create svg sprite sheets from imported svg files and get usable components for many popular frontend frameworks.
TypeScript
2
star
26

babel-plugin-ng-inject

Easy Angular dependency injection with Babel.
JavaScript
2
star
27

redux-saga-selector-channel

Create a redux-saga channel which updates with changes to a selector function.
TypeScript
1
star
28

pg

Ruby
1
star
29

brewgolia

Search Homebrew with Algolia
JavaScript
1
star
30

neon-examples

Rust
1
star
31

dr-promise

A promise which resolves when the DOM is ready.
JavaScript
1
star
32

ramda-lens-map

A lens for ramda which lets you focus on a key in a native Map.
TypeScript
1
star
33

redux-ts-actions

Type-safe FSA utilies for redux
TypeScript
1
star
34

learning-rust

I'm learning Rust
Rust
1
star
35

Karma-Decay-Chrome-Extension

This is a very simple extension which allows users to search for an image on Karma Decay by right clicking said image and selecting "Search for this image on Karma Decay."
JavaScript
1
star