Created
January 6, 2021 18:35
-
-
Save dshorthouse/8482dcfe0ec949c40f0d123a41b857b7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# encoding: utf-8 | |
class String | |
def is_orcid? | |
/(\d{4}-){3}\d{3}[0-9X]{1}$/.match?(self) | |
end | |
def valid_orcid? | |
parts = self.scan(/[0-9X]/) | |
mod = parts[0..14].map(&:to_i) | |
.inject { |sum, n| (sum + n)*2 } | |
.modulo(11) | |
result = (12 - mod) % 11 | |
parts.last == ((result == 10) ? "X" : result.to_s) | |
end | |
end | |
puts "https://orcid.org/0000-0002-4519-8167".is_orcid? | |
puts "https://orcid.org/0000-0002-4519-8165".valid_orcid? | |
puts "https://orcid.org/0000-0002-6662-847X".valid_orcid? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment