-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isISBN): allow usage of options object (#2157)
- Loading branch information
Showing
4 changed files
with
142 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,55 @@ | ||
import assertString from './util/assertString'; | ||
|
||
const isbn10Maybe = /^(?:[0-9]{9}X|[0-9]{10})$/; | ||
const isbn13Maybe = /^(?:[0-9]{13})$/; | ||
const possibleIsbn10 = /^(?:[0-9]{9}X|[0-9]{10})$/; | ||
const possibleIsbn13 = /^(?:[0-9]{13})$/; | ||
const factor = [1, 3]; | ||
|
||
export default function isISBN(str, version = '') { | ||
assertString(str); | ||
version = String(version); | ||
if (!version) { | ||
return isISBN(str, 10) || isISBN(str, 13); | ||
export default function isISBN(isbn, options) { | ||
assertString(isbn); | ||
|
||
// For backwards compatibility: | ||
// isISBN(str [, version]), i.e. `options` could be used as argument for the legacy `version` | ||
const version = String(options?.version || options); | ||
|
||
if (!(options?.version || options)) { | ||
return isISBN(isbn, { version: 10 }) || isISBN(isbn, { version: 13 }); | ||
} | ||
const sanitized = str.replace(/[\s-]+/g, ''); | ||
|
||
const sanitizedIsbn = isbn.replace(/[\s-]+/g, ''); | ||
|
||
let checksum = 0; | ||
let i; | ||
|
||
if (version === '10') { | ||
if (!isbn10Maybe.test(sanitized)) { | ||
if (!possibleIsbn10.test(sanitizedIsbn)) { | ||
return false; | ||
} | ||
for (i = 0; i < 9; i++) { | ||
checksum += (i + 1) * sanitized.charAt(i); | ||
|
||
for (let i = 0; i < version - 1; i++) { | ||
checksum += (i + 1) * sanitizedIsbn.charAt(i); | ||
} | ||
if (sanitized.charAt(9) === 'X') { | ||
|
||
if (sanitizedIsbn.charAt(9) === 'X') { | ||
checksum += 10 * 10; | ||
} else { | ||
checksum += 10 * sanitized.charAt(9); | ||
checksum += 10 * sanitizedIsbn.charAt(9); | ||
} | ||
|
||
if ((checksum % 11) === 0) { | ||
return !!sanitized; | ||
return true; | ||
} | ||
} else if (version === '13') { | ||
if (!isbn13Maybe.test(sanitized)) { | ||
if (!possibleIsbn13.test(sanitizedIsbn)) { | ||
return false; | ||
} | ||
for (i = 0; i < 12; i++) { | ||
checksum += factor[i % 2] * sanitized.charAt(i); | ||
|
||
for (let i = 0; i < 12; i++) { | ||
checksum += factor[i % 2] * sanitizedIsbn.charAt(i); | ||
} | ||
if (sanitized.charAt(12) - ((10 - (checksum % 10)) % 10) === 0) { | ||
return !!sanitized; | ||
|
||
if (sanitizedIsbn.charAt(12) - ((10 - (checksum % 10)) % 10) === 0) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import test from '../testFunctions'; | ||
|
||
describe('isISBN', () => { | ||
it('should validate ISBNs', () => { | ||
test({ | ||
validator: 'isISBN', | ||
args: [{ version: 10 }], | ||
valid: [ | ||
'3836221195', '3-8362-2119-5', '3 8362 2119 5', | ||
'1617290858', '1-61729-085-8', '1 61729 085-8', | ||
'0007269706', '0-00-726970-6', '0 00 726970 6', | ||
'3423214120', '3-423-21412-0', '3 423 21412 0', | ||
'340101319X', '3-401-01319-X', '3 401 01319 X', | ||
], | ||
invalid: [ | ||
'3423214121', '3-423-21412-1', '3 423 21412 1', | ||
'978-3836221191', '9783836221191', | ||
'123456789a', 'foo', '', | ||
], | ||
}); | ||
test({ | ||
validator: 'isISBN', | ||
args: [{ version: 13 }], | ||
valid: [ | ||
'9783836221191', '978-3-8362-2119-1', '978 3 8362 2119 1', | ||
'9783401013190', '978-3401013190', '978 3401013190', | ||
'9784873113685', '978-4-87311-368-5', '978 4 87311 368 5', | ||
], | ||
invalid: [ | ||
'9783836221190', '978-3-8362-2119-0', '978 3 8362 2119 0', | ||
'3836221195', '3-8362-2119-5', '3 8362 2119 5', | ||
'01234567890ab', 'foo', '', | ||
], | ||
}); | ||
test({ | ||
validator: 'isISBN', | ||
valid: [ | ||
'340101319X', | ||
'9784873113685', | ||
], | ||
invalid: [ | ||
'3423214121', | ||
'9783836221190', | ||
], | ||
}); | ||
test({ | ||
validator: 'isISBN', | ||
args: [{ version: 'foo' }], | ||
invalid: [ | ||
'340101319X', | ||
'9784873113685', | ||
], | ||
}); | ||
}); | ||
|
||
describe('(legacy syntax)', () => { | ||
it('should validate ISBNs', () => { | ||
test({ | ||
validator: 'isISBN', | ||
args: [10], | ||
valid: [ | ||
'3836221195', '3-8362-2119-5', '3 8362 2119 5', | ||
'1617290858', '1-61729-085-8', '1 61729 085-8', | ||
'0007269706', '0-00-726970-6', '0 00 726970 6', | ||
'3423214120', '3-423-21412-0', '3 423 21412 0', | ||
'340101319X', '3-401-01319-X', '3 401 01319 X', | ||
], | ||
invalid: [ | ||
'3423214121', '3-423-21412-1', '3 423 21412 1', | ||
'978-3836221191', '9783836221191', | ||
'123456789a', 'foo', '', | ||
], | ||
}); | ||
test({ | ||
validator: 'isISBN', | ||
args: [13], | ||
valid: [ | ||
'9783836221191', '978-3-8362-2119-1', '978 3 8362 2119 1', | ||
'9783401013190', '978-3401013190', '978 3401013190', | ||
'9784873113685', '978-4-87311-368-5', '978 4 87311 368 5', | ||
], | ||
invalid: [ | ||
'9783836221190', '978-3-8362-2119-0', '978 3 8362 2119 0', | ||
'3836221195', '3-8362-2119-5', '3 8362 2119 5', | ||
'01234567890ab', 'foo', '', | ||
], | ||
}); | ||
test({ | ||
validator: 'isISBN', | ||
valid: [ | ||
'340101319X', | ||
'9784873113685', | ||
], | ||
invalid: [ | ||
'3423214121', | ||
'9783836221190', | ||
], | ||
}); | ||
test({ | ||
validator: 'isISBN', | ||
args: ['foo'], | ||
invalid: [ | ||
'340101319X', | ||
'9784873113685', | ||
], | ||
}); | ||
}); | ||
}); | ||
}); |