1 | ;
|
2 |
|
3 | /**
|
4 | * Given an object of violation messages, return another
|
5 | * that provides the same messages postfixed with the rule
|
6 | * that has been violated.
|
7 | *
|
8 | * @param {string} ruleName
|
9 | * @param {{[k: string]: string|Function}} messages - Object whose keys are message identifiers
|
10 | * and values are either message strings or functions that return message strings
|
11 | * @return {{[k: string]: string|Function}} New message object, whose messages will be marked with the rule name
|
12 | */
|
13 | module.exports = function (ruleName, messages) {
|
14 | return Object.keys(messages).reduce(
|
15 | /**
|
16 | * @param {{[k: string]: string|Function}} newMessages
|
17 | * @param {string} messageId
|
18 | * @return {{[k: string]: string|Function}}
|
19 | */
|
20 | (newMessages, messageId) => {
|
21 | const messageText = messages[messageId];
|
22 |
|
23 | if (typeof messageText === 'string') {
|
24 | newMessages[messageId] = `${messageText} (${ruleName})`;
|
25 | } else {
|
26 | newMessages[messageId] = (/** @type {any[]} */ ...args) => {
|
27 | return `${messageText(...args)} (${ruleName})`;
|
28 | };
|
29 | }
|
30 |
|
31 | return newMessages;
|
32 | },
|
33 | {},
|
34 | );
|
35 | };
|