-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(isISBN): allow usage of options object #2157
Merged
profnandaa
merged 2 commits into
validatorjs:master
from
WikiRik:isISBN-options-refactor
Jan 30, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the backward compatibilty! 👍 |
||
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', | ||
], | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a bit off topic for the PR;
I would like to see that "sanitization" step replaced with as option, maybe something like a "strict" mode, checking/ignoring for spaces or hyphens only inside the ISBN, not also at the beginning or end of the ISBN string.
but that shouldn't be part of this PR, just noting it down for the future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I had that in mind as well when working on this but from personal experience people write the ISBN down differently. I haven't checked if there are rules on where the - should be, but I think that in this case sanitising whitespaces and - adds value to this specific validator.
About the beginning/end of the string; that's not something I thought of but if you want I can add that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, don't worry about it here, IMHO that would be out of scope for this particular PR - let's keep it simple and get this merged and then after the release, we can work on that via a new PR, I'd say