File size: 773 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * @file Utility for replacing placeholders in eslint rule messages
 * @author Automattic
 * @copyright 2016 Automattic. All rights reserved.
 * See LICENSE.md file in root directory for full license.
 */

/**
 * Given a message containing data terms, format the string using the specified
 * terms object.
 * @see https://github.com/eslint/eslint/blob/v2.12.0/lib/eslint.js#L964-L971
 * @param  {string} message Message template
 * @param  {Object} terms   Terms
 * @returns {string}         Formatted message
 */
function formatMessage( message, terms ) {
	return message.replace( /\{\{\s*(.+?)\s*\}\}/g, function ( fullMatch, term ) {
		if ( terms.hasOwnProperty( term ) ) {
			return terms[ term ];
		}

		return fullMatch;
	} );
}

module.exports = formatMessage;