diff --git a/src/lib/isISBN.js b/src/lib/isISBN.js index 4499c59a0..882d86261 100644 --- a/src/lib/isISBN.js +++ b/src/lib/isISBN.js @@ -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; diff --git a/test/validators/isISBN.test.js b/test/validators/isISBN.test.js index 99fb2e014..f4005ff6a 100644 --- a/test/validators/isISBN.test.js +++ b/test/validators/isISBN.test.js @@ -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({