Sha256: 6ce2995515e334ee2677ec8cda6e876066fe326cb91bfb2a9ce1e65cd857f3cd

Contents?: true

Size: 1.77 KB

Versions: 261

Compression:

Stored size: 1.77 KB

Contents

/* global helpers */
const dataRegex = /\$\{\s?data\s?\}/g;

/**
 * Replace a placeholder with the value of a data string or object
 * @param {String} str
 * @param {String|Object} data
 */
function substitute(str, data) {
	// replace all instances of ${ data } with the value of the string
	if (typeof data === 'string') {
		return str.replace(dataRegex, data);
	}

	// replace all instances of ${ data[prop] } with the value of the property
	for (const prop in data) {
		if (data.hasOwnProperty(prop)) {
			const regex = new RegExp('\\${\\s?data\\.' + prop + '\\s?}', 'g');
			str = str.replace(regex, data[prop]);
		}
	}

	return str;
}

/**
 * Process a metadata message.
 * @param {String|Object} message
 * @param {Object} data
 * @return {String}
 */
axe.utils.processMessage = function processMessage(message, data) {
	if (!message) {
		return;
	}

	// data as array
	if (Array.isArray(data)) {
		data.values = data.join(', ');

		if (
			typeof message.singular === 'string' &&
			typeof message.plural === 'string'
		) {
			const str = data.length === 1 ? message.singular : message.plural;
			return substitute(str, data);
		}

		// no singular/plural message so just pass data
		return substitute(message, data);
	}

	// message is a string that uses data as a string or object properties
	if (typeof message === 'string') {
		return substitute(message, data);
	}

	// message is an object that uses value of data to determine message
	if (typeof data === 'string') {
		const str = message[data];
		return substitute(str, data);
	}

	// message uses value of data property to determine message
	let str = message.default || helpers.incompleteFallbackMessage();

	if (data && data.messageKey && message[data.messageKey]) {
		str = message[data.messageKey];
	}

	return processMessage(str, data);
};

Version data entries

261 entries across 261 versions & 1 rubygems

Version Path
govuk_publishing_components-30.4.1 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-30.4.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-30.3.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-30.2.1 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-30.2.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-30.1.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-30.0.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.15.3 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.15.2 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.15.1 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.15.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.14.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.13.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.12.1 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.12.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.11.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.10.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.9.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.8.0 node_modules/axe-core/lib/core/utils/process-message.js
govuk_publishing_components-29.7.0 node_modules/axe-core/lib/core/utils/process-message.js