Skip to content

Commit 9ee09a7

Browse files
tux-tnprofnandaa
authored andcommitted
chore: code coverage improvements (#1024)
* fix(isISO8601): add additionnals checks for leap years * fix(isDecimal): correct decimal point type for some locales * refactor(rtrim): implement the logic of ltrim * fix(ltrim): add chars escaping * test: add thrown errors handling * test: add missing test cases * fix(isIdentityCard): remove default value for locale * chore: add nyc config for code coverage * chore: add compiled bundles
1 parent d7dbdd5 commit 9ee09a7

24 files changed

+348
-129
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.DS_Store
22
node_modules
33
coverage
4+
.nyc_output
45
package-lock.json
56
yarn.lock

.nycrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"reporter": [
3+
"html",
4+
"text-summary"
5+
],
6+
"include": [
7+
"src/**/*.js"
8+
],
9+
"exclude": [
10+
"validator.js",
11+
"lib/**/*.js"
12+
]
13+
}

lib/alpha.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ for (var _locale, _i = 0; _i < arabicLocales.length; _i++) {
8787
} // Source: https://en.wikipedia.org/wiki/Decimal_mark
8888

8989

90-
var dotDecimal = [];
90+
var dotDecimal = ['ar-EG', 'ar-LB', 'ar-LY'];
9191
exports.dotDecimal = dotDecimal;
92-
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'];
92+
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'];
9393
exports.commaDecimal = commaDecimal;
9494

9595
for (var _i2 = 0; _i2 < dotDecimal.length; _i2++) {

lib/isISO8601.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var isValidDate = function isValidDate(str) {
2525
var oYear = Number(ordinalMatch[1]);
2626
var oDay = Number(ordinalMatch[2]); // if is leap year
2727

28-
if (oYear % 4 === 0 && oYear % 100 !== 0) return oDay <= 366;
28+
if (oYear % 4 === 0 && oYear % 100 !== 0 || oYear % 400 === 0) return oDay <= 366;
2929
return oDay <= 365;
3030
}
3131

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

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

4241
if (month && day) {
4342
return d.getUTCFullYear() === year && d.getUTCMonth() + 1 === month && d.getUTCDate() === day;

lib/isIdentityCard.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,15 @@ var validators = {
7878
}
7979
};
8080

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

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

lib/isIn.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function isIn(str, options) {
2121
var array = [];
2222

2323
for (i in options) {
24+
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
25+
// istanbul ignore else
2426
if ({}.hasOwnProperty.call(options, i)) {
2527
array[i] = (0, _toString.default)(options[i]);
2628
}

lib/isMobilePhone.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ function isMobilePhone(str, locale, options) {
101101

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

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

lib/isPostalCode.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ function _default(str, locale) {
7474
return patterns[locale].test(str);
7575
} else if (locale === 'any') {
7676
for (var key in patterns) {
77+
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
78+
// istanbul ignore else
7779
if (patterns.hasOwnProperty(key)) {
7880
var pattern = patterns[key];
7981

lib/ltrim.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ var _assertString = _interopRequireDefault(require("./util/assertString"));
1010
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1111

1212
function ltrim(str, chars) {
13-
(0, _assertString.default)(str);
14-
var pattern = chars ? new RegExp("^[".concat(chars, "]+"), 'g') : /^\s+/g;
13+
(0, _assertString.default)(str); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
14+
15+
var pattern = chars ? new RegExp("^[".concat(chars.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), "]+"), 'g') : /^\s+/g;
1516
return str.replace(pattern, '');
1617
}
1718

lib/rtrim.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,10 @@ var _assertString = _interopRequireDefault(require("./util/assertString"));
1010
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1111

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

17-
for (; idx >= 0 && pattern.test(str[idx]); idx--) {
18-
;
19-
}
20-
21-
return idx < str.length ? str.substr(0, idx + 1) : str;
15+
var pattern = chars ? new RegExp("[".concat(chars.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), "]+$"), 'g') : /\s+$/g;
16+
return str.replace(pattern, '');
2217
}
2318

2419
module.exports = exports.default;

0 commit comments

Comments
 (0)