Skip to content

Commit

Permalink
Merge branch 'hkwu-feature/isISSN'
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Oct 15, 2016
2 parents b1b6c1e + 1205355 commit 8ffef17
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Passing anything other than a string is an error.
- **isHexadecimal(str)** - check if the string is a hexadecimal number.
- **isIP(str [, version])** - check if the string is an IP (version 4 or 6).
- **isISBN(str [, version])** - check if the string is an ISBN (version 10 or 13).
- **isISSN(str [, options])** - check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number). `options` is an object which defaults to `{ case_sensitive: false }`.
- **isISSN(str [, options])** - check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number). `options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected.
- **isISIN(str)** - check if the string is an [ISIN][ISIN] (stock/security identifier).
- **isISO8601(str)** - check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
- **isIn(str, values)** - check if the string is in a array of allowed values.
Expand Down
6 changes: 4 additions & 2 deletions lib/isISSN.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ var _assertString2 = _interopRequireDefault(_assertString);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var issn = /^\d{4}-\d{3}[\dX]$/;
var issn = '^\\d{4}-?\\d{3}[\\dX]$';

function isISSN(str) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

(0, _assertString2.default)(str);
var testIssn = options.case_sensitive ? issn : new RegExp(issn, 'i');
var testIssn = issn;
testIssn = options.require_hyphen ? testIssn.replace('?', '') : testIssn;
testIssn = options.case_sensitive ? new RegExp(testIssn) : new RegExp(testIssn, 'i');
if (!testIssn.test(str)) {
return false;
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/isISSN.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import assertString from './util/assertString';

const issn = /^\d{4}-\d{3}[\dX]$/;
const issn = '^\\d{4}-?\\d{3}[\\dX]$';

export default function isISSN(str, options = {}) {
assertString(str);
const testIssn = options.case_sensitive ? issn : new RegExp(issn, 'i');
let testIssn = issn;
testIssn = options.require_hyphen ? testIssn.replace('?', '') : testIssn;
testIssn = options.case_sensitive ? new RegExp(testIssn) : new RegExp(testIssn, 'i');
if (!testIssn.test(str)) {
return false;
}
Expand Down
38 changes: 38 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1859,23 +1859,61 @@ describe('Validators', function () {
'0000-0000',
'2434-561X',
'2434-561x',
'01896016',
'20905076',
],
invalid: [
'0378-5954',
'0000-0001',
'0378-123',
'037-1234',
'0',
'2434-561c',
'1684-5370',
'19960791',
'',
],
});
test({
validator: 'isISSN',
args: [{ case_sensitive: true }],
valid: [
'2434-561X',
'2434561X',
'0378-5955',
'03785955',
],
invalid: [
'2434-561x',
'2434561x',
],
});
test({
validator: 'isISSN',
args: [{ require_hyphen: true }],
valid: [
'2434-561X',
'2434-561x',
'0378-5955',
],
invalid: [
'2434561X',
'2434561x',
'03785955',
],
});
test({
validator: 'isISSN',
args: [{ case_sensitive: true, require_hyphen: true }],
valid: [
'2434-561X',
'0378-5955',
],
invalid: [
'2434-561x',
'2434561X',
'2434561x',
'03785955',
],
});
});
Expand Down
6 changes: 4 additions & 2 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,13 +1027,15 @@
return false;
}

var issn = /^\d{4}-\d{3}[\dX]$/;
var issn = '^\\d{4}-?\\d{3}[\\dX]$';

function isISSN(str) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

assertString(str);
var testIssn = options.case_sensitive ? issn : new RegExp(issn, 'i');
var testIssn = issn;
testIssn = options.require_hyphen ? testIssn.replace('?', '') : testIssn;
testIssn = options.case_sensitive ? new RegExp(testIssn) : new RegExp(testIssn, 'i');
if (!testIssn.test(str)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 8ffef17

Please sign in to comment.