Skip to content

Commit b0f8d19

Browse files
authored
feat(lint): unify regex and .length check syntax
Modernize code using eslint advisory introduced in GH-2596. This PR focuses on unified regex and .length check usage, also removes useless escapes.
1 parent 504f80d commit b0f8d19

31 files changed

+157
-162
lines changed

.eslintrc.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,9 @@ module.exports = {
136136
'no-shadow-restricted-names': 'off',
137137
'no-unused-expressions': 'off',
138138
'no-use-before-define': 'off',
139-
'no-useless-concat': 'off',
140-
'no-useless-escape': 'off',
141-
radix: 'off',
142-
'unicorn/better-regex': 'off',
143139
'unicorn/consistent-function-scoping': 'off',
144140
'unicorn/empty-brace-spaces': 'off',
145141
'unicorn/escape-case': 'off',
146-
'unicorn/explicit-length-check': 'off',
147142
'unicorn/new-for-builtins': 'off',
148143
'unicorn/no-hex-escape': 'off',
149144
'unicorn/no-instanceof-array': 'off',

src/definitions/behaviors/api.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@
464464
: (el.value === 'false' ? false : el.value)),
465465
nameKeys = el.name.match(settings.regExp.key) || [],
466466
k,
467-
pushKey = el.name.replace(/\[\]$/, '')
467+
pushKey = el.name.replace(/\[]$/, '')
468468
;
469469
if (!(pushKey in pushes)) {
470470
pushes[pushKey] = 0;
@@ -1001,7 +1001,7 @@
10011001
passedArguments = passedArguments || queryArguments;
10021002
context = context || element;
10031003
if (typeof query === 'string' && object !== undefined) {
1004-
query = query.split(/[\. ]/);
1004+
query = query.split(/[ .]/);
10051005
maxDepth = query.length - 1;
10061006
$.each(query, function (depth, value) {
10071007
var camelCaseValue = (depth != maxDepth)
@@ -1186,13 +1186,13 @@
11861186
},
11871187

11881188
regExp: {
1189-
required: /\{\$*[a-z0-9]+\}/gi,
1190-
optional: /\{\/\$*[a-z0-9]+\}/gi,
1191-
validate: /^[a-z_][a-z0-9_-]*(?:\[[a-z0-9_-]*\])*$/i,
1192-
key: /[a-z0-9_-]+|(?=\[\])/gi,
1189+
required: /{\$*[\da-z]+}/gi,
1190+
optional: /{\/\$*[\da-z]+}/gi,
1191+
validate: /^[_a-z][\w-]*(?:\[[\w-]*])*$/i,
1192+
key: /[\w-]+|(?=\[])/gi,
11931193
push: /^$/,
11941194
fixed: /^\d+$/,
1195-
named: /^[a-z0-9_-]+$/i,
1195+
named: /^[\w-]+$/i,
11961196
},
11971197

11981198
className: {

src/definitions/behaviors/form.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -542,28 +542,28 @@
542542
if (!rule.prompt) {
543543
suffixPrompt = (
544544
parts[0] === ''
545-
? settings.prompt.maxValue.replace(/\{ruleValue\}/g, '{max}')
545+
? settings.prompt.maxValue.replace(/{ruleValue}/g, '{max}')
546546
: parts[1] === ''
547-
? settings.prompt.minValue.replace(/\{ruleValue\}/g, '{min}')
547+
? settings.prompt.minValue.replace(/{ruleValue}/g, '{min}')
548548
: settings.prompt.range
549549
);
550-
prompt += suffixPrompt.replace(/\{name\}/g, ' ' + settings.text.and);
550+
prompt += suffixPrompt.replace(/{name}/g, ' ' + settings.text.and);
551551
}
552-
prompt = prompt.replace(/\{min\}/g, parts[0]);
553-
prompt = prompt.replace(/\{max\}/g, parts[1]);
552+
prompt = prompt.replace(/{min}/g, parts[0]);
553+
prompt = prompt.replace(/{max}/g, parts[1]);
554554
}
555555
if (requiresValue) {
556-
prompt = prompt.replace(/\{value\}/g, $field.val());
556+
prompt = prompt.replace(/{value}/g, $field.val());
557557
}
558558
if (requiresName) {
559559
$label = $field.closest(selector.group).find('label').eq(0);
560560
name = ($label.length == 1)
561561
? $label.text()
562562
: $field.prop('placeholder') || settings.text.unspecifiedField;
563-
prompt = prompt.replace(/\{name\}/g, name);
563+
prompt = prompt.replace(/{name}/g, name);
564564
}
565-
prompt = prompt.replace(/\{identifier\}/g, field.identifier);
566-
prompt = prompt.replace(/\{ruleValue\}/g, ancillary);
565+
prompt = prompt.replace(/{identifier}/g, field.identifier);
566+
prompt = prompt.replace(/{ruleValue}/g, ancillary);
567567
if (!rule.prompt) {
568568
module.verbose('Using default validation prompt for type', prompt, ruleName);
569569
}
@@ -855,7 +855,7 @@
855855
$.each(newValidation.rules, function (_index, rule) {
856856
if ($.grep(validation[name].rules, function (item) {
857857
return item.type == rule.type;
858-
}).length == 0) {
858+
}).length === 0) {
859859
validation[name].rules.push(rule);
860860
}
861861
});
@@ -871,7 +871,7 @@
871871
$field = module.get.field(identifier),
872872
$fieldGroup = $field.closest($group),
873873
$prompt = $fieldGroup.children(selector.prompt),
874-
promptExists = ($prompt.length !== 0)
874+
promptExists = ($prompt.length > 0)
875875
;
876876
errors = (typeof errors === 'string')
877877
? [errors]
@@ -1247,7 +1247,7 @@
12471247
module.debug('Using field name as identifier', identifier);
12481248
field.identifier = identifier;
12491249
}
1250-
var isDisabled = !$field.filter(':not(:disabled)').length;
1250+
var isDisabled = $field.filter(':not(:disabled)').length === 0;
12511251
if (isDisabled) {
12521252
module.debug('Field is disabled. Skipping', identifier);
12531253
} else if (field.optional && module.is.blank($field)) {
@@ -1326,7 +1326,7 @@
13261326
});
13271327
}
13281328

1329-
return internal ? invalidFields : !(invalidFields.length > 0);
1329+
return internal ? invalidFields : invalidFields.length === 0;
13301330
},
13311331
},
13321332

@@ -1411,7 +1411,7 @@
14111411
title += ' \'' + moduleSelector + '\'';
14121412
}
14131413
if ($allModules.length > 1) {
1414-
title += ' ' + '(' + $allModules.length + ')';
1414+
title += ' (' + $allModules.length + ')';
14151415
}
14161416
if ((console.group !== undefined || console.table !== undefined) && performance.length > 0) {
14171417
console.groupCollapsed(title);
@@ -1437,7 +1437,7 @@
14371437
passedArguments = passedArguments || queryArguments;
14381438
context = context || element;
14391439
if (typeof query === 'string' && object !== undefined) {
1440-
query = query.split(/[\. ]/);
1440+
query = query.split(/[ .]/);
14411441
maxDepth = query.length - 1;
14421442
$.each(query, function (depth, value) {
14431443
var camelCaseValue = (depth != maxDepth)
@@ -1529,15 +1529,15 @@
15291529
},
15301530

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

15431543
text: {
@@ -1922,15 +1922,15 @@
19221922
length: [16],
19231923
},
19241924
discover: {
1925-
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)/,
1925+
pattern: /^(6011|622(12[6-9]|1[3-9]\d|[2-8]\d{2}|9[01]\d|92[0-5]|64[4-9])|65)/,
19261926
length: [16],
19271927
},
19281928
unionPay: {
19291929
pattern: /^(62|88)/,
19301930
length: [16, 17, 18, 19],
19311931
},
19321932
jcb: {
1933-
pattern: /^35(2[89]|[3-8][0-9])/,
1933+
pattern: /^35(2[89]|[3-8]\d)/,
19341934
length: [16],
19351935
},
19361936
maestro: {
@@ -1964,7 +1964,7 @@
19641964
}
19651965

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

19691969
// verify card types
19701970
if (requiredTypes) {
@@ -1976,7 +1976,7 @@
19761976
length: ($.inArray(cardNumber.length, validation.length) !== -1),
19771977
pattern: (cardNumber.search(validation.pattern) !== -1),
19781978
};
1979-
if (valid.length && valid.pattern) {
1979+
if (valid.length > 0 && valid.pattern) {
19801980
validCard = true;
19811981
}
19821982
}

src/definitions/behaviors/state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@
516516
passedArguments = passedArguments || queryArguments;
517517
context = context || element;
518518
if (typeof query === 'string' && object !== undefined) {
519-
query = query.split(/[\. ]/);
519+
query = query.split(/[ .]/);
520520
maxDepth = query.length - 1;
521521
$.each(query, function (depth, value) {
522522
var camelCaseValue = (depth != maxDepth)

src/definitions/behaviors/visibility.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@
11241124
passedArguments = passedArguments || queryArguments;
11251125
context = context || element;
11261126
if (typeof query === 'string' && object !== undefined) {
1127-
query = query.split(/[\. ]/);
1127+
query = query.split(/[ .]/);
11281128
maxDepth = query.length - 1;
11291129
$.each(query, function (depth, value) {
11301130
var camelCaseValue = (depth != maxDepth)

src/definitions/globals/site.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@
358358
passedArguments = passedArguments || queryArguments;
359359
context = context || element;
360360
if (typeof query === 'string' && object !== undefined) {
361-
query = query.split(/[\. ]/);
361+
query = query.split(/[ .]/);
362362
maxDepth = query.length - 1;
363363
$.each(query, function (depth, value) {
364364
var camelCaseValue = (depth != maxDepth)

src/definitions/modules/accordion.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@
479479
passedArguments = passedArguments || queryArguments;
480480
context = context || element;
481481
if (typeof query === 'string' && object !== undefined) {
482-
query = query.split(/[\. ]/);
482+
query = query.split(/[ .]/);
483483
maxDepth = query.length - 1;
484484
$.each(query, function (depth, value) {
485485
var camelCaseValue = (depth != maxDepth)

0 commit comments

Comments
 (0)