[DependsOn] Action's names, PR fixes, cleanup
/**
* @author Alexey Stsefanovich (ala'n), Yana Bernatskaya (YanaBr)
- * @version 2.2.4
+ * @version 2.3.0
*
* DependsOn Actions Registry
* Action defines steps to process query result
(function ($, ns) {
'use strict';
+ const NAME_REGEX = /[^a-z0-9-]+/g;
const actionRegistryMap = {};
+
class ActionRegistry {
/**
/**
* Function to remove symbols that don't match the pattern
* @param {string} target - target string
- * @param {RegExp} pattern - pattern for correct symbols
* */
- static _replaceForbiddenSymbols(target, pattern) {
- return target
- .split('')
- .map((symbol) => (symbol.search(pattern) !== -1 ? symbol : ''))
- .join('');
- }
-
- /**
- * Function to develop action's name (allowed symbols a-z, 0-9, -)
- * @param {string} name - action name
- * @returns {string} correct action name (according to nameRegex)
- * */
- static _getValidName(name) {
- const nameRegex = /^[a-z0-9-]+$/;
- let resultName = name;
- if (name.trim().search(nameRegex) === -1) {
- resultName = resultName.toLocaleLowerCase();
- resultName = this._replaceForbiddenSymbols(resultName, nameRegex);
- console.warn(`[DependsOn]: Action's name ${name} was overridden by ${resultName} (allowed symbols: a-z, 0-9, -)`);
- }
- return resultName;
- }
+ static sanitizeName(target) {
+ return target.toLowerCase().replace(NAME_REGEX, '');
+ }p
/**
* @param {string} name - is action name
* @returns {function} actual actionCb after register
* */
static register(name, actionFn) {
- name = this._getValidName(name);
+ name = name.trim();
+ const sanitizedName = ActionRegistry.sanitizeName(name);
+ if (name !== sanitizedName) {
+ console.warn(`[DependsOn]: Action's name ${name} was overridden by ${sanitizedName} (allowed symbols: a-z, 0-9, -)`);
+ }
if (typeof actionFn !== 'function') {
throw new Error(`[DependsOn]: Action ${actionFn} is not a valid action definition`);
}
- if (actionRegistryMap[name]) {
- console.warn(`[DependsOn]: Action ${name} was overridden by ${actionFn}`);
+ if (actionRegistryMap[sanitizedName]) {
+ console.warn(`[DependsOn]: Action ${sanitizedName} was overridden by ${actionFn}`);
}
- return actionRegistryMap[name] = actionFn;
+ return actionRegistryMap[sanitizedName] = actionFn;
}
}
