Skip to content

Commit

Permalink
Prefer template strings
Browse files Browse the repository at this point in the history
  • Loading branch information
forabi committed Feb 20, 2016
1 parent e44ca88 commit f919358
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 19 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Expand Up @@ -11,7 +11,6 @@
},
"rules": {
"camelcase": [0],
"prefer-template": 0,
"no-param-reassign": [0],
"one-var": 0,
"func-names": 0,
Expand Down
2 changes: 2 additions & 0 deletions lib/isCurrency.js
Expand Up @@ -57,10 +57,12 @@ function currencyRegex(options) {
}
}

/* eslint-disable prefer-template */
return new RegExp('^' +
// ensure there's a dollar and/or decimal amount, and that
// it doesn't start with a space or a negative sign followed by a space
'(?!-? )(?=.*\\d)' + pattern + '$');
/* eslint-enable prefer-template */
}

var default_currency_options = {
Expand Down
18 changes: 10 additions & 8 deletions src/lib/isCurrency.js
Expand Up @@ -2,15 +2,15 @@ import merge from './util/merge';
import assertString from './util/assertString';

function currencyRegex(options) {
const symbol = '(\\' + options.symbol.replace(/\./g, '\\.') + ')' +
(options.require_symbol ? '' : '?'),
const symbol =
`(\\${options.symbol.replace(/\./g, '\\.')})${(options.require_symbol ? '' : '?')}`,
negative = '-?',
whole_dollar_amount_without_sep = '[1-9]\\d*',
whole_dollar_amount_with_sep = '[1-9]\\d{0,2}(\\' + options.thousands_separator + '\\d{3})*',
whole_dollar_amount_with_sep = `[1-9]\\d{0,2}(\\${options.thousands_separator}\\d{3})*`,
valid_whole_dollar_amounts = [
'0', whole_dollar_amount_without_sep, whole_dollar_amount_with_sep],
whole_dollar_amount = '(' + valid_whole_dollar_amounts.join('|') + ')?',
decimal_amount = '(\\' + options.decimal_separator + '\\d{2})?';
whole_dollar_amount = `(${valid_whole_dollar_amounts.join('|')})?`,
decimal_amount = `(\\${options.decimal_separator}\\d{2})?`;
let pattern = whole_dollar_amount + decimal_amount;

// default is negative sign before symbol, but there are two other options (besides parens)
Expand All @@ -24,9 +24,9 @@ function currencyRegex(options) {

// South African Rand, for example, uses R 123 (space) and R-123 (no space)
if (options.allow_negative_sign_placeholder) {
pattern = '( (?!\\-))?' + pattern;
pattern = `( (?!\\-))?${pattern}`;
} else if (options.allow_space_after_symbol) {
pattern = ' ?' + pattern;
pattern = ` ?${pattern}`;
} else if (options.allow_space_after_digits) {
pattern += '( (?!$))?';
}
Expand All @@ -39,12 +39,13 @@ function currencyRegex(options) {

if (options.allow_negatives) {
if (options.parens_for_negatives) {
pattern = '(\\(' + pattern + '\\)|' + pattern + ')';
pattern = `(\\(${pattern}\\)|${pattern})`;
} else if (!(options.negative_sign_before_digits || options.negative_sign_after_digits)) {
pattern = negative + pattern;
}
}

/* eslint-disable prefer-template */
return new RegExp(
'^' +
// ensure there's a dollar and/or decimal amount, and that
Expand All @@ -53,6 +54,7 @@ function currencyRegex(options) {
pattern +
'$'
);
/* eslint-enable prefer-template */
}


Expand Down
2 changes: 1 addition & 1 deletion src/lib/isDate.js
Expand Up @@ -13,7 +13,7 @@ function getTimezoneOffset(str) {
sign = timezone[1];
let offset = timezone[2];
if (offset.length === 3) {
offset = '0' + offset;
offset = `0${offset}`;
}
if (offset.length <= 2) {
hours = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ltrim.js
Expand Up @@ -2,6 +2,6 @@ import assertString from './util/assertString';

export default function ltrim(str, chars) {
assertString(str);
const pattern = chars ? new RegExp('^[' + chars + ']+', 'g') : /^\s+/g;
const pattern = chars ? new RegExp(`^[${chars}]+`, 'g') : /^\s+/g;
return str.replace(pattern, '');
}
2 changes: 1 addition & 1 deletion src/lib/rtrim.js
Expand Up @@ -2,6 +2,6 @@ import assertString from './util/assertString';

export default function rtrim(str, chars) {
assertString(str);
const pattern = chars ? new RegExp('[' + chars + ']+$', 'g') : /\s+$/g;
const pattern = chars ? new RegExp(`[${chars}]+$`, 'g') : /\s+$/g;
return str.replace(pattern, '');
}
2 changes: 1 addition & 1 deletion src/lib/trim.js
Expand Up @@ -2,6 +2,6 @@ import assertString from './util/assertString';

export default function trim(str, chars) {
assertString(str);
const pattern = chars ? new RegExp('^[' + chars + ']+|[' + chars + ']+$', 'g') : /^\s+|\s+$/g;
const pattern = chars ? new RegExp(`^[${chars}]+|[${chars}]+$`, 'g') : /^\s+|\s+$/g;
return str.replace(pattern, '');
}
2 changes: 1 addition & 1 deletion src/lib/whitelist.js
Expand Up @@ -2,5 +2,5 @@ import assertString from './util/assertString';

export default function whitelist(str, chars) {
assertString(str);
return str.replace(new RegExp('[^' + chars + ']+', 'g'), '');
return str.replace(new RegExp(`[^${chars}]+`, 'g'), '');
}
2 changes: 1 addition & 1 deletion test/client-side.js
Expand Up @@ -7,7 +7,7 @@ describe('Minified version', function () {
for (var key in validator) {
if ({}.hasOwnProperty.call(validator, key)) {
assert.equal(typeof validator[key],
typeof min[key], 'Minified version did not export ' + key);
typeof min[key], `Minified version did not export ${key}`);
}
}
});
Expand Down
8 changes: 4 additions & 4 deletions test/validators.js
Expand Up @@ -58,7 +58,7 @@ describe('Validators', function () {
'"foobar"@example.com',
'" foo m端ller "@example.com',
'"foo\\@bar"@example.com',
repeat('a', 64) + '@' + repeat('a', 252) + '.com',
`${repeat('a', 64)}@${repeat('a', 252)}.com`,
],
invalid: [
'invalidemail@',
Expand All @@ -69,8 +69,8 @@ describe('Validators', function () {
'foo@bar.co.uk.',
'z@co.c',
'gmailgmailgmailgmailgmail@gmail.com',
repeat('a', 64) + '@' + repeat('a', 253) + '.com',
repeat('a', 65) + '@' + repeat('a', 252) + '.com',
`${repeat('a', 64)}@${repeat('a', 253)}.com`,
`${repeat('a', 65)}@${repeat('a', 252)}.com`,
],
});
});
Expand Down Expand Up @@ -214,7 +214,7 @@ describe('Validators', function () {
'http://www.foobar.com/\t',
'http://\n@www.foobar.com/',
'',
'http://foobar.com/' + new Array(2083).join('f'),
`http://foobar.com/${new Array(2083).join('f')}`,
'http://*.foo.com',
'*.foo.com',
'!.foo.com',
Expand Down
2 changes: 2 additions & 0 deletions validator.js
Expand Up @@ -913,10 +913,12 @@
}
}

/* eslint-disable prefer-template */
return new RegExp('^' +
// ensure there's a dollar and/or decimal amount, and that
// it doesn't start with a space or a negative sign followed by a space
'(?!-? )(?=.*\\d)' + pattern + '$');
/* eslint-enable prefer-template */
}

var default_currency_options = {
Expand Down

0 comments on commit f919358

Please sign in to comment.