From f919358815821890c72f32396561f9307bd46412 Mon Sep 17 00:00:00 2001 From: Muhammad Fawwaz Orabi Date: Sat, 20 Feb 2016 11:24:50 +0200 Subject: [PATCH] Prefer template strings --- .eslintrc | 1 - lib/isCurrency.js | 2 ++ src/lib/isCurrency.js | 18 ++++++++++-------- src/lib/isDate.js | 2 +- src/lib/ltrim.js | 2 +- src/lib/rtrim.js | 2 +- src/lib/trim.js | 2 +- src/lib/whitelist.js | 2 +- test/client-side.js | 2 +- test/validators.js | 8 ++++---- validator.js | 2 ++ 11 files changed, 24 insertions(+), 19 deletions(-) diff --git a/.eslintrc b/.eslintrc index 066d48970..05e4a15f9 100644 --- a/.eslintrc +++ b/.eslintrc @@ -11,7 +11,6 @@ }, "rules": { "camelcase": [0], - "prefer-template": 0, "no-param-reassign": [0], "one-var": 0, "func-names": 0, diff --git a/lib/isCurrency.js b/lib/isCurrency.js index 6cbe86bd1..8d95c7354 100644 --- a/lib/isCurrency.js +++ b/lib/isCurrency.js @@ -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 = { diff --git a/src/lib/isCurrency.js b/src/lib/isCurrency.js index d88255d81..fa5eac68c 100644 --- a/src/lib/isCurrency.js +++ b/src/lib/isCurrency.js @@ -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) @@ -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 += '( (?!$))?'; } @@ -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 @@ -53,6 +54,7 @@ function currencyRegex(options) { pattern + '$' ); + /* eslint-enable prefer-template */ } diff --git a/src/lib/isDate.js b/src/lib/isDate.js index 1df336022..60b2aca43 100644 --- a/src/lib/isDate.js +++ b/src/lib/isDate.js @@ -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; diff --git a/src/lib/ltrim.js b/src/lib/ltrim.js index 3f383e6cc..7f1ee11cd 100644 --- a/src/lib/ltrim.js +++ b/src/lib/ltrim.js @@ -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, ''); } diff --git a/src/lib/rtrim.js b/src/lib/rtrim.js index 5155056ad..4938566bc 100644 --- a/src/lib/rtrim.js +++ b/src/lib/rtrim.js @@ -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, ''); } diff --git a/src/lib/trim.js b/src/lib/trim.js index ce3530f48..f4f8fe004 100644 --- a/src/lib/trim.js +++ b/src/lib/trim.js @@ -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, ''); } diff --git a/src/lib/whitelist.js b/src/lib/whitelist.js index ed9233331..3df56e71b 100644 --- a/src/lib/whitelist.js +++ b/src/lib/whitelist.js @@ -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'), ''); } diff --git a/test/client-side.js b/test/client-side.js index 18a8f17c9..503ca2b2f 100644 --- a/test/client-side.js +++ b/test/client-side.js @@ -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}`); } } }); diff --git a/test/validators.js b/test/validators.js index cf33ec023..a7f2b8c03 100644 --- a/test/validators.js +++ b/test/validators.js @@ -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@', @@ -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`, ], }); }); @@ -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', diff --git a/validator.js b/validator.js index a1d384bea..601e86ee1 100644 --- a/validator.js +++ b/validator.js @@ -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 = {