Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

message-tag

mkrause486MIT0.10.0TypeScript support: included

ES6 template literal tag to format message strings

message, error, log, format, template, tag, tagged, es6-tag

readme

message-tag

npm GitHub Actions Types

A template literal tag to format arbitrary values in a string template. Useful for error messages, logs, etc.

import msg from 'message-tag';

const time = new Date();
const user = { name: 'John' };

const message = msg`[${time}] Found user: ${user}`;
// Result: '[2018-04-22T22:19:39Z] Found user: `{"name": "John"}`'

Motivation

When constructing (plain text) messages, we often want to include other JS objects, dates, etc. These need to be converted to string first, in order to interpolate them into the message template.

const log = `Current user: '${formatUserAsString(user)}'`;

Converting a JS value to a string is in general not straightforward. The toString() method only exists for objects, and even then is often useless as it just prints the type of the value (e.g. the well-known [object Object]). JSON.stringify often does the job, but includes tons of edge cases. Because JSON.stringify only prints valid JSON, it won't work as expected for values like undefined, NaN, or Infinity. For custom types, this approach also won't work unless they've implemented toJSON.

If you always know the type of the input, then these can be dealt with by using the correct formatting procedure, but getting it right is often cumbersome and error-prone.

In addition, sometimes we do not have any knowledge of the type of the input at all. This is common in the case of error handling, where we may want to construct error messages such as:

if (!(input instanceof Foo)) {
    throw new TypeError(`Expected instance of Foo, but given '${JSON.stringify(input)}' instead`);
}

Since we do not know the type of input, using JSON.stringify is optimistic here. It will produce suboptimal or cryptic results for values like NaN, functions, or custom types that don't implement toJSON().

Usage

First, import or require the msg tag:

import msg from 'message-tag';
//const msg = require('message-tag'); // CommonJS version

Values of any known, built-in type can be formatted:

msg`Message: ${undefined}`; // 'Message: `undefined`'
msg`Message: ${null}`; // 'Message: `null`'
msg`Message: ${42}`; // 'Message: 42'
msg`Message: ${[1, 2, 3]}`; // 'Message: `[1,2,3]`'
msg`Message: ${{ name: 'John' }}`; // 'Message: `{"name": "John"}`'

// Dates are formatted as ISO strings:
msg`Message: ${new Date()}`; // 'Message: 2018-04-22T22:19:39Z'

// Also works for functions:
msg`Message: ${x => x + 1}`; // 'Message: `x => x + 1`'

Instances of custom types will be printed with the name of the type, and with the contents serialized through toJSON(). If toJSON is not implemented, all enumerable properties of the instance are used instead.

class MyType {
    value = 42;

    toJSON() {
        return { value: this.value };
    }
}

msg`Custom type: ${new MyType()}`; // 'Custom type: [MyType] `{"value": 42}`'

If you want to disable formatting for a specific input, use msg.raw:

msg`Will not be formatted: ${msg.raw('foo')}`; // 'Will not be formatted: foo'

Custom formatting

You can customize the formatting using msg.custom. For example:

msg`The date is: ${msg.custom({ dateFormat: 'mmmm dS, yyyy' }, new Date('2018-01-01'))}`;
// Result: 'The date is: January 1st, 2018'

You can also change the defaults by creating your own customized tag:

import { msgTag } from 'message-tag';

const msg = msgTag({ dateFormat: 'mmmm dS, yyyy' });
msg`The date is: ${new Date('2018-01-01')}`;
// Result: 'The date is: January 1st, 2018'

Supported options:

  • dateFormat: Custom format for Date objects. See the dateformat package for a complete overview of the format options.
  • format: Formatting for JS objects. Can use any option supported by pretty-format.

Types

This package includes definitions for TypeScript.

changelog

Change Log

  • v0.10

    • Upgrade to package-lock v2 format.
  • v0.9

    • Upgrade message-tag to use modern Node.js 14+ features.
    • Drop support for Node 12.
    • Use exports in package.json rather than main.
    • Upgrade all dependencies to latest versions.
    • Move dist files to new dist directory containing ESM, CommonJS, and TypeScript build files.
  • v0.8

    • Invalid Date instances are now formatted as [invalid Date], rather than throwing a TypeError as before.
  • v0.7

    • Update import for pretty-format.
  • v0.6

    • Drop support for Node v10, and IE 11.
  • v0.5

    • Drop support for Node v8, add tests for Node v14.
  • v0.4

    • Remove core-js polyfills to improve file size.
    • Add tsd to test TypeScript declaration.
  • v0.3

    • Add TypeScript support.
  • v0.0

    • Initial version.