Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code coverage improvements #1024

Merged
merged 11 commits into from
Jun 18, 2019
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
node_modules
coverage
.nyc_output
package-lock.json
yarn.lock
13 changes: 13 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"reporter": [
"html",
"text-summary"
],
"include": [
"src/**/*.js"
],
"exclude": [
"validator.js",
"lib/**/*.js"
]
}
4 changes: 2 additions & 2 deletions lib/alpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ for (var _locale, _i = 0; _i < arabicLocales.length; _i++) {
} // Source: https://en.wikipedia.org/wiki/Decimal_mark


var dotDecimal = [];
var dotDecimal = ['ar-EG', 'ar-LB', 'ar-LY'];
exports.dotDecimal = dotDecimal;
var commaDecimal = ['bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'es-ES', 'fr-FR', 'it-IT', 'ku-IQ', 'hu-HU', 'nb-NO', 'nn-NO', 'nl-NL', 'pl-PL', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA'];
var commaDecimal = ['bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-ZM', 'es-ES', 'fr-FR', 'it-IT', 'ku-IQ', 'hu-HU', 'nb-NO', 'nn-NO', 'nl-NL', 'pl-PL', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA'];
exports.commaDecimal = commaDecimal;

for (var _i2 = 0; _i2 < dotDecimal.length; _i2++) {
Expand Down
3 changes: 1 addition & 2 deletions lib/isISO8601.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var isValidDate = function isValidDate(str) {
var oYear = Number(ordinalMatch[1]);
var oDay = Number(ordinalMatch[2]); // if is leap year

if (oYear % 4 === 0 && oYear % 100 !== 0) return oDay <= 366;
if (oYear % 4 === 0 && oYear % 100 !== 0 || oYear % 400 === 0) return oDay <= 366;
return oDay <= 365;
}

Expand All @@ -37,7 +37,6 @@ var isValidDate = function isValidDate(str) {
var dayString = day ? "0".concat(day).slice(-2) : day; // create a date object and compare

var d = new Date("".concat(year, "-").concat(monthString || '01', "-").concat(dayString || '01'));
if (isNaN(d.getUTCFullYear())) return false;

if (month && day) {
return d.getUTCFullYear() === year && d.getUTCMonth() + 1 === month && d.getUTCDate() === day;
Expand Down
5 changes: 3 additions & 2 deletions lib/isIdentityCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ var validators = {
}
};

function isIdentityCard(str) {
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'any';
function isIdentityCard(str, locale) {
(0, _assertString.default)(str);

if (locale in validators) {
return validators[locale](str);
} else if (locale === 'any') {
for (var key in validators) {
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
// istanbul ignore else
if (validators.hasOwnProperty(key)) {
var validator = validators[key];

Expand Down
2 changes: 2 additions & 0 deletions lib/isIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ function isIn(str, options) {
var array = [];

for (i in options) {
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
// istanbul ignore else
if ({}.hasOwnProperty.call(options, i)) {
array[i] = (0, _toString.default)(options[i]);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ function isMobilePhone(str, locale, options) {

if (Array.isArray(locale)) {
return locale.some(function (key) {
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
// istanbul ignore else
if (phones.hasOwnProperty(key)) {
var phone = phones[key];

Expand All @@ -115,6 +117,7 @@ function isMobilePhone(str, locale, options) {
return phones[locale].test(str); // alias falsey locale as 'any'
} else if (!locale || locale === 'any') {
for (var key in phones) {
// istanbul ignore else
if (phones.hasOwnProperty(key)) {
var phone = phones[key];

Expand Down
2 changes: 2 additions & 0 deletions lib/isPostalCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ function _default(str, locale) {
return patterns[locale].test(str);
} else if (locale === 'any') {
for (var key in patterns) {
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
// istanbul ignore else
if (patterns.hasOwnProperty(key)) {
var pattern = patterns[key];

Expand Down
5 changes: 3 additions & 2 deletions lib/ltrim.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ var _assertString = _interopRequireDefault(require("./util/assertString"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ltrim(str, chars) {
(0, _assertString.default)(str);
var pattern = chars ? new RegExp("^[".concat(chars, "]+"), 'g') : /^\s+/g;
(0, _assertString.default)(str); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping

var pattern = chars ? new RegExp("^[".concat(chars.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), "]+"), 'g') : /^\s+/g;
return str.replace(pattern, '');
}

Expand Down
11 changes: 3 additions & 8 deletions lib/rtrim.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ var _assertString = _interopRequireDefault(require("./util/assertString"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function rtrim(str, chars) {
(0, _assertString.default)(str);
var pattern = chars ? new RegExp("[".concat(chars, "]")) : /\s/;
var idx = str.length - 1;
(0, _assertString.default)(str); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping

for (; idx >= 0 && pattern.test(str[idx]); idx--) {
;
}

return idx < str.length ? str.substr(0, idx + 1) : str;
var pattern = chars ? new RegExp("[".concat(chars.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), "]+$"), 'g') : /\s+$/g;
return str.replace(pattern, '');
}

module.exports = exports.default;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.11.0",
"mocha": "^5.1.1",
"nyc": "^14.1.0",
"rollup": "^0.43.0",
"rollup-plugin-babel": "^4.0.1",
"uglify-js": "^3.0.19"
Expand All @@ -62,7 +63,7 @@
"build:node": "babel src -d .",
"build": "npm run build:browser && npm run build:node",
"pretest": "npm run lint && npm run build",
"test": "mocha --require @babel/register --reporter dot"
"test": "nyc mocha --require @babel/register --reporter dot"
},
"engines": {
"node": ">= 0.10"
Expand Down
4 changes: 2 additions & 2 deletions src/lib/alpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ for (let locale, i = 0; i < arabicLocales.length; i++) {
}

// Source: https://en.wikipedia.org/wiki/Decimal_mark
export const dotDecimal = [];
export const dotDecimal = ['ar-EG', 'ar-LB', 'ar-LY'];
export const commaDecimal = [
'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'es-ES', 'fr-FR', 'it-IT', 'ku-IQ', 'hu-HU', 'nb-NO',
'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-ZM', 'es-ES', 'fr-FR', 'it-IT', 'ku-IQ', 'hu-HU', 'nb-NO',
'nn-NO', 'nl-NL', 'pl-PL', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS@latin',
'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA',
];
Expand Down
4 changes: 1 addition & 3 deletions src/lib/isISO8601.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const isValidDate = (str) => {
const oYear = Number(ordinalMatch[1]);
const oDay = Number(ordinalMatch[2]);
// if is leap year
if (oYear % 4 === 0
&& oYear % 100 !== 0) return oDay <= 366;
if ((oYear % 4 === 0 && oYear % 100 !== 0) || oYear % 400 === 0) return oDay <= 366;
return oDay <= 365;
}
const match = str.match(/(\d{4})-?(\d{0,2})-?(\d*)/).map(Number);
Expand All @@ -27,7 +26,6 @@ const isValidDate = (str) => {

// create a date object and compare
const d = new Date(`${year}-${monthString || '01'}-${dayString || '01'}`);
if (isNaN(d.getUTCFullYear())) return false;
if (month && day) {
return d.getUTCFullYear() === year
&& (d.getUTCMonth() + 1) === month
Expand Down
4 changes: 3 additions & 1 deletion src/lib/isIdentityCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ const validators = {
},
};

export default function isIdentityCard(str, locale = 'any') {
export default function isIdentityCard(str, locale) {
assertString(str);
if (locale in validators) {
return validators[locale](str);
} else if (locale === 'any') {
for (const key in validators) {
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
// istanbul ignore else
if (validators.hasOwnProperty(key)) {
const validator = validators[key];
if (validator(str)) {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/isIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export default function isIn(str, options) {
if (Object.prototype.toString.call(options) === '[object Array]') {
const array = [];
for (i in options) {
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
// istanbul ignore else
if ({}.hasOwnProperty.call(options, i)) {
array[i] = toString(options[i]);
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export default function isMobilePhone(str, locale, options) {
}
if (Array.isArray(locale)) {
return locale.some((key) => {
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
// istanbul ignore else
if (phones.hasOwnProperty(key)) {
const phone = phones[key];
if (phone.test(str)) {
Expand All @@ -102,6 +104,7 @@ export default function isMobilePhone(str, locale, options) {
// alias falsey locale as 'any'
} else if (!locale || locale === 'any') {
for (const key in phones) {
// istanbul ignore else
if (phones.hasOwnProperty(key)) {
const phone = phones[key];
if (phone.test(str)) {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/isPostalCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export default function (str, locale) {
return patterns[locale].test(str);
} else if (locale === 'any') {
for (const key in patterns) {
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
// istanbul ignore else
if (patterns.hasOwnProperty(key)) {
const pattern = patterns[key];
if (pattern.test(str)) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/ltrim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assertString from './util/assertString';

export default function ltrim(str, chars) {
assertString(str);
const pattern = chars ? new RegExp(`^[${chars}]+`, 'g') : /^\s+/g;
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
const pattern = chars ? new RegExp(`^[${chars.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}]+`, 'g') : /^\s+/g;
return str.replace(pattern, '');
}
10 changes: 3 additions & 7 deletions src/lib/rtrim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import assertString from './util/assertString';

export default function rtrim(str, chars) {
assertString(str);
const pattern = chars ? new RegExp(`[${chars}]`) : /\s/;

let idx = str.length - 1;
for (; idx >= 0 && pattern.test(str[idx]); idx--)
;

return idx < str.length ? str.substr(0, idx + 1) : str;
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
const pattern = chars ? new RegExp(`[${chars.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}]+$`, 'g') : /\s+$/g;
return str.replace(pattern, '');
}
10 changes: 5 additions & 5 deletions test/exports.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import assert from 'assert';
import validator from '../index';
import { locales as isPostalCodeLocales } from '../lib/isPostalCode';
import { locales as isAlphaLocales } from '../lib/isAlpha';
import { locales as isAlphanumericLocales } from '../lib/isAlphanumeric';
import { locales as isMobilePhoneLocales } from '../lib/isMobilePhone';
import { locales as isFloatLocales } from '../lib/isFloat';
import { locales as isPostalCodeLocales } from '../src/lib/isPostalCode';
import { locales as isAlphaLocales } from '../src/lib/isAlpha';
import { locales as isAlphanumericLocales } from '../src/lib/isAlphanumeric';
import { locales as isMobilePhoneLocales } from '../src/lib/isMobilePhone';
import { locales as isFloatLocales } from '../src/lib/isFloat';

describe('Exports', () => {
it('should export validators', () => {
Expand Down
22 changes: 21 additions & 1 deletion test/sanitizers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { format } from 'util';
import validator from '../index';
import validator from '../src/index';

function test(options) {
let args = options.args || [];
Expand Down Expand Up @@ -91,11 +91,24 @@ describe('Sanitizers', () => {
expect: { '010100201000': '201000' },
});

test({
sanitizer: 'ltrim',
args: ['\\S'],
expect: { '\\S01010020100001': '01010020100001' },
});


test({
sanitizer: 'rtrim',
args: ['01'],
expect: { '010100201000': '0101002' },
});

test({
sanitizer: 'rtrim',
args: ['\\S'],
expect: { '01010020100001\\S': '01010020100001' },
});
});

it('should convert strings to integers', () => {
Expand Down Expand Up @@ -246,6 +259,10 @@ describe('Sanitizers', () => {
'test@yandex.ua': 'test@yandex.ru',
'test@yandex.com': 'test@yandex.ru',
'test@yandex.by': 'test@yandex.ru',
'@gmail.com': false,
'@icloud.com': false,
'@outlook.com': false,
'@yahoo.com': false,
},
});

Expand All @@ -268,6 +285,7 @@ describe('Sanitizers', () => {
'SOME.name@yahoo.ca': 'some.name@yahoo.ca',
'SOME.name@outlook.ie': 'some.name@outlook.ie',
'SOME.name@me.com': 'some.name@me.com',
'SOME.name@yandex.ru': 'some.name@yandex.ru',
},
});

Expand All @@ -280,6 +298,7 @@ describe('Sanitizers', () => {
icloud_lowercase: false,
outlookdotcom_lowercase: false,
yahoo_lowercase: false,
yandex_lowercase: false,
}],
expect: {
'TEST@FOO.COM': 'TEST@foo.com', // all_lowercase
Expand All @@ -289,6 +308,7 @@ describe('Sanitizers', () => {
'ME@outlook.COM': 'ME@outlook.com', // outlookdotcom_lowercase
'JOHN@live.CA': 'JOHN@live.ca', // outlookdotcom_lowercase
'ME@ymail.COM': 'ME@ymail.com', // yahoo_lowercase
'ME@yandex.RU': 'ME@yandex.ru', // yandex_lowercase
},
});

Expand Down