formatted-messages
v1.0.2
Published
A package for formatting messages in node packages
Downloads
14
Maintainers
Readme
Formatted Messages
formatted-messages
is just a smaller (non intl) copy of the react-intl
API made for Node packages. Reference chalk to see what colours formatted-messages
supports.
API
formatMessage
Formats and colourises a message
.
Arguments
unformattedMessage
: an object with the following propertiesmessage
: a string message to be formatted with tags to be replaceddefaultOptions
: an object with properties to be replaced in the message if options are not passed. These will be overridden by properties inoption
if passed.
options
: an object with variables to be replaced?separator
: an optional property that will define how array type variables will be separated
Example
let message;
// message = 'Hello, world!'
message = formatMessage({
message: 'Hello, world!',
});
// message = 'Hello, Nick!'
message = formatMessage({
message: 'Hello, {name}!',
defaultOptions: {
name: 'Nick',
},
});
// message = 'Hello, Alex!'
message = formatMessage({
message: 'Hello, {name}!',
defaultOptions: {
name: 'Nick',
},
}, {
name: 'Alex',
});
// message = 'Hello, Alex!' <= 'Alex' will be coloured cyan :)
message = formatMessage({
message: 'Hello, <cyan>{name}<cyan>!',
defaultOptions: {
name: 'Nick',
},
}, {
name: 'Alex',
});
// message = 'Hello, Alex!' <= 'Alex' will be coloured red :)
message = formatMessage({
message: 'Hello, <error>{name}<error>!',
defaultOptions: {
name: 'Nick',
},
}, {
name: 'Alex',
});
// message = 'Hello, Nick, Alex, Ash!' <= 'Nick, Alex, Ash' will be coloured green :)
message = formatMessage({
message: 'Hello, <green>[names]<green>!',
}, {
names: ['Nick', 'Alex', 'Ash'],
});
// message = 'Hello, Nick and Alex and Ash!' <= 'Nick and Alex and Ash' will be coloured green :)
message = formatMessage({
message: 'Hello, <green>[names]<green>!',
}, {
names: ['Nick', 'Alex', 'Ash'],
separator: ' and '
});
printMessage
Prints a message
to the console.
Arguments
message
: a string message to print to the console.
Example
const message = formatMessage({
message: 'Hello world',
});
// prints message to console
printMessage(message);