Skip to content

Commit

Permalink
feat(lint): unify regex and .length check syntax
Browse files Browse the repository at this point in the history
Modernize code using eslint advisory introduced in GH-2596. This PR focuses on unified regex and .length check usage, also removes useless escapes.
  • Loading branch information
mvorisek authored Dec 12, 2022
1 parent 504f80d commit b0f8d19
Show file tree
Hide file tree
Showing 31 changed files with 157 additions and 162 deletions.
5 changes: 0 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,9 @@ module.exports = {
'no-shadow-restricted-names': 'off',
'no-unused-expressions': 'off',
'no-use-before-define': 'off',
'no-useless-concat': 'off',
'no-useless-escape': 'off',
radix: 'off',
'unicorn/better-regex': 'off',
'unicorn/consistent-function-scoping': 'off',
'unicorn/empty-brace-spaces': 'off',
'unicorn/escape-case': 'off',
'unicorn/explicit-length-check': 'off',
'unicorn/new-for-builtins': 'off',
'unicorn/no-hex-escape': 'off',
'unicorn/no-instanceof-array': 'off',
Expand Down
14 changes: 7 additions & 7 deletions src/definitions/behaviors/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@
: (el.value === 'false' ? false : el.value)),
nameKeys = el.name.match(settings.regExp.key) || [],
k,
pushKey = el.name.replace(/\[\]$/, '')
pushKey = el.name.replace(/\[]$/, '')
;
if (!(pushKey in pushes)) {
pushes[pushKey] = 0;
Expand Down Expand Up @@ -1001,7 +1001,7 @@
passedArguments = passedArguments || queryArguments;
context = context || element;
if (typeof query === 'string' && object !== undefined) {
query = query.split(/[\. ]/);
query = query.split(/[ .]/);
maxDepth = query.length - 1;
$.each(query, function (depth, value) {
var camelCaseValue = (depth != maxDepth)
Expand Down Expand Up @@ -1186,13 +1186,13 @@
},

regExp: {
required: /\{\$*[a-z0-9]+\}/gi,
optional: /\{\/\$*[a-z0-9]+\}/gi,
validate: /^[a-z_][a-z0-9_-]*(?:\[[a-z0-9_-]*\])*$/i,
key: /[a-z0-9_-]+|(?=\[\])/gi,
required: /{\$*[\da-z]+}/gi,
optional: /{\/\$*[\da-z]+}/gi,
validate: /^[_a-z][\w-]*(?:\[[\w-]*])*$/i,
key: /[\w-]+|(?=\[])/gi,
push: /^$/,
fixed: /^\d+$/,
named: /^[a-z0-9_-]+$/i,
named: /^[\w-]+$/i,
},

className: {
Expand Down
52 changes: 26 additions & 26 deletions src/definitions/behaviors/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,28 +542,28 @@
if (!rule.prompt) {
suffixPrompt = (
parts[0] === ''
? settings.prompt.maxValue.replace(/\{ruleValue\}/g, '{max}')
? settings.prompt.maxValue.replace(/{ruleValue}/g, '{max}')
: parts[1] === ''
? settings.prompt.minValue.replace(/\{ruleValue\}/g, '{min}')
? settings.prompt.minValue.replace(/{ruleValue}/g, '{min}')
: settings.prompt.range
);
prompt += suffixPrompt.replace(/\{name\}/g, ' ' + settings.text.and);
prompt += suffixPrompt.replace(/{name}/g, ' ' + settings.text.and);
}
prompt = prompt.replace(/\{min\}/g, parts[0]);
prompt = prompt.replace(/\{max\}/g, parts[1]);
prompt = prompt.replace(/{min}/g, parts[0]);
prompt = prompt.replace(/{max}/g, parts[1]);
}
if (requiresValue) {
prompt = prompt.replace(/\{value\}/g, $field.val());
prompt = prompt.replace(/{value}/g, $field.val());
}
if (requiresName) {
$label = $field.closest(selector.group).find('label').eq(0);
name = ($label.length == 1)
? $label.text()
: $field.prop('placeholder') || settings.text.unspecifiedField;
prompt = prompt.replace(/\{name\}/g, name);
prompt = prompt.replace(/{name}/g, name);
}
prompt = prompt.replace(/\{identifier\}/g, field.identifier);
prompt = prompt.replace(/\{ruleValue\}/g, ancillary);
prompt = prompt.replace(/{identifier}/g, field.identifier);
prompt = prompt.replace(/{ruleValue}/g, ancillary);
if (!rule.prompt) {
module.verbose('Using default validation prompt for type', prompt, ruleName);
}
Expand Down Expand Up @@ -855,7 +855,7 @@
$.each(newValidation.rules, function (_index, rule) {
if ($.grep(validation[name].rules, function (item) {
return item.type == rule.type;
}).length == 0) {
}).length === 0) {
validation[name].rules.push(rule);
}
});
Expand All @@ -871,7 +871,7 @@
$field = module.get.field(identifier),
$fieldGroup = $field.closest($group),
$prompt = $fieldGroup.children(selector.prompt),
promptExists = ($prompt.length !== 0)
promptExists = ($prompt.length > 0)
;
errors = (typeof errors === 'string')
? [errors]
Expand Down Expand Up @@ -1247,7 +1247,7 @@
module.debug('Using field name as identifier', identifier);
field.identifier = identifier;
}
var isDisabled = !$field.filter(':not(:disabled)').length;
var isDisabled = $field.filter(':not(:disabled)').length === 0;
if (isDisabled) {
module.debug('Field is disabled. Skipping', identifier);
} else if (field.optional && module.is.blank($field)) {
Expand Down Expand Up @@ -1326,7 +1326,7 @@
});
}

return internal ? invalidFields : !(invalidFields.length > 0);
return internal ? invalidFields : invalidFields.length === 0;
},
},

Expand Down Expand Up @@ -1411,7 +1411,7 @@
title += ' \'' + moduleSelector + '\'';
}
if ($allModules.length > 1) {
title += ' ' + '(' + $allModules.length + ')';
title += ' (' + $allModules.length + ')';
}
if ((console.group !== undefined || console.table !== undefined) && performance.length > 0) {
console.groupCollapsed(title);
Expand All @@ -1437,7 +1437,7 @@
passedArguments = passedArguments || queryArguments;
context = context || element;
if (typeof query === 'string' && object !== undefined) {
query = query.split(/[\. ]/);
query = query.split(/[ .]/);
maxDepth = query.length - 1;
$.each(query, function (depth, value) {
var camelCaseValue = (depth != maxDepth)
Expand Down Expand Up @@ -1529,15 +1529,15 @@
},

regExp: {
htmlID: /^[a-zA-Z][\w:.-]*$/g,
bracket: /\[(.*)\]/i,
htmlID: /^[A-Za-z][\w.:-]*$/g,
bracket: /\[(.*)]/i,
decimal: /^\d+\.?\d*$/,
email: /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i,
escape: /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|:,=@]/g,
email: /^[\w!#$%&'*+./=?^`{|}~-]+@[\da-z]([\da-z-]*[\da-z])?(\.[\da-z]([\da-z-]*[\da-z])?)*$/i,
escape: /[$()*+,./:=?@[\\\]^{|}-]/g,
flags: /^\/(.*)\/(.*)?/,
integer: /^\-?\d+$/,
number: /^\-?\d*(\.\d+)?$/,
url: /(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/i,
integer: /^-?\d+$/,
number: /^-?\d*(\.\d+)?$/,
url: /(https?:\/\/(?:www\.|(?!www))[^\s.]+\.\S{2,}|www\.\S+\.\S{2,})/i,
},

text: {
Expand Down Expand Up @@ -1922,15 +1922,15 @@
length: [16],
},
discover: {
pattern: /^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)/,
pattern: /^(6011|622(12[6-9]|1[3-9]\d|[2-8]\d{2}|9[01]\d|92[0-5]|64[4-9])|65)/,
length: [16],
},
unionPay: {
pattern: /^(62|88)/,
length: [16, 17, 18, 19],
},
jcb: {
pattern: /^35(2[89]|[3-8][0-9])/,
pattern: /^35(2[89]|[3-8]\d)/,
length: [16],
},
maestro: {
Expand Down Expand Up @@ -1964,7 +1964,7 @@
}

// allow dashes and spaces in card
cardNumber = cardNumber.replace(/[\s\-]/g, '');
cardNumber = cardNumber.replace(/[\s-]/g, '');

// verify card types
if (requiredTypes) {
Expand All @@ -1976,7 +1976,7 @@
length: ($.inArray(cardNumber.length, validation.length) !== -1),
pattern: (cardNumber.search(validation.pattern) !== -1),
};
if (valid.length && valid.pattern) {
if (valid.length > 0 && valid.pattern) {
validCard = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/behaviors/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@
passedArguments = passedArguments || queryArguments;
context = context || element;
if (typeof query === 'string' && object !== undefined) {
query = query.split(/[\. ]/);
query = query.split(/[ .]/);
maxDepth = query.length - 1;
$.each(query, function (depth, value) {
var camelCaseValue = (depth != maxDepth)
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/behaviors/visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@
passedArguments = passedArguments || queryArguments;
context = context || element;
if (typeof query === 'string' && object !== undefined) {
query = query.split(/[\. ]/);
query = query.split(/[ .]/);
maxDepth = query.length - 1;
$.each(query, function (depth, value) {
var camelCaseValue = (depth != maxDepth)
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/globals/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@
passedArguments = passedArguments || queryArguments;
context = context || element;
if (typeof query === 'string' && object !== undefined) {
query = query.split(/[\. ]/);
query = query.split(/[ .]/);
maxDepth = query.length - 1;
$.each(query, function (depth, value) {
var camelCaseValue = (depth != maxDepth)
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/modules/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@
passedArguments = passedArguments || queryArguments;
context = context || element;
if (typeof query === 'string' && object !== undefined) {
query = query.split(/[\. ]/);
query = query.split(/[ .]/);
maxDepth = query.length - 1;
$.each(query, function (depth, value) {
var camelCaseValue = (depth != maxDepth)
Expand Down
Loading

0 comments on commit b0f8d19

Please sign in to comment.