Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/lib/isISBN.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ export default function isISBN(isbn, options) {

// For backwards compatibility:
// isISBN(str [, version]), i.e. `options` could be used as argument for the legacy `version`
const version = String(options?.version || options);
let version = typeof options === 'object' ? options?.version : options;

if (!(options?.version || options)) {
if (!version) {
return isISBN(isbn, { version: 10 }) || isISBN(isbn, { version: 13 });
}

version = String(version);
const sanitizedIsbn = isbn.replace(/[\s-]+/g, '');

let checksum = 0;
Expand Down
24 changes: 24 additions & 0 deletions test/validators/isISBN.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ describe('isISBN', () => {
});
});

[
{ description: 'null options', options: null },
{ description: 'empty options', options: {} },
{ description: 'an undefined version', options: { version: undefined } },
{ description: 'a null version', options: { version: null } },
].forEach(({ description, options }) => {
it(`should validate both ISBN versions with ${description}`, () => {
test({
validator: 'isISBN',
args: [options],
valid: [
'340101319X',
'9784873113685',
],
invalid: [
'3423214121',
'9783836221190',
'foo',
'',
],
});
});
});

describe('(legacy syntax)', () => {
it('should validate ISBNs', () => {
test({
Expand Down
Loading