library_stdnums 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +1 -1
- data/README.markdown +33 -31
- data/VERSION +1 -1
- data/lib/library_stdnums.rb +21 -5
- metadata +1 -1
data/LICENSE
CHANGED
data/README.markdown
CHANGED
@@ -9,40 +9,41 @@ See the actual functions for more information than what's below.
|
|
9
9
|
## ISBN
|
10
10
|
|
11
11
|
````ruby
|
12
|
-
|
13
|
-
|
12
|
+
|
13
|
+
isbn = StdNum::ISBN.normalize(goodISBN)
|
14
|
+
# => a 13-digit ISBN with no dashes/spaces
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
isbn = StdNum::ISBN.normalize(badISBN)
|
17
|
+
# => nil (if it's not an ISBN or the checkdigit is bad)
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
tenDigit = StdNum::ISBN.convert_to_10(isbn13)
|
20
|
+
thirteenDigit = StdNum::ISBN.convert_to_13(isbn10)
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
thirteenDigit,tenDigit = StdNum::ISBN.allNormalizedValues(issn)
|
23
|
+
# => array of the ten and thirteen digit isbns if valid;
|
24
|
+
# an empty array if not
|
24
25
|
|
25
|
-
|
26
|
-
|
26
|
+
digit = StdNum::ISBN.checkdigit(isbn)
|
27
|
+
# => 0..9 (for isbn13) or 0..9,X (for isbn10)
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
if StdNum::ISBN.valid?(isbn)
|
30
|
+
puts "#{isbn} has a valid checkdigit"
|
31
|
+
end
|
31
32
|
|
32
33
|
````
|
33
34
|
|
34
35
|
# ISSN
|
35
36
|
|
36
37
|
````ruby
|
37
|
-
|
38
|
-
|
38
|
+
issn = StdNum::ISSN.normalize(issn)
|
39
|
+
# => the cleaned-up issn if valid; nil if not
|
39
40
|
|
40
|
-
|
41
|
-
|
41
|
+
digit = StdNum::ISSN.checkdigit(issn)
|
42
|
+
# => 0..9 or X
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
44
|
+
if StdNum::ISSN.valid?(issn)
|
45
|
+
puts "#{issn} has a valid checkdigit"
|
46
|
+
end
|
46
47
|
````
|
47
48
|
|
48
49
|
# LCCN
|
@@ -51,15 +52,16 @@ LCCNs are normalized according to the algorithm at http://www.loc.gov/marc/lccn-
|
|
51
52
|
|
52
53
|
````ruby
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
lccn = StdNum::LCCN.normalize(rawlccn)
|
56
|
+
# => either the normalized lccn, or nil if it has bad syntax
|
57
|
+
|
58
|
+
if StdNum::LCCN.valid?(rawlccn) {
|
59
|
+
puts "#{rawlccn} is valid"
|
60
|
+
}
|
61
|
+
|
60
62
|
````
|
61
63
|
|
62
|
-
|
64
|
+
## CHANGES
|
63
65
|
|
64
66
|
* 1.0.0
|
65
67
|
* Added normalization all around.
|
@@ -77,7 +79,7 @@ LCCNs are normalized according to the algorithm at http://www.loc.gov/marc/lccn-
|
|
77
79
|
* 0.1.0
|
78
80
|
* Initial release
|
79
81
|
|
80
|
-
|
82
|
+
## Note on Patches/Pull Requests
|
81
83
|
|
82
84
|
* Fork the project.
|
83
85
|
* Make your feature addition or bug fix.
|
@@ -87,6 +89,6 @@ LCCNs are normalized according to the algorithm at http://www.loc.gov/marc/lccn-
|
|
87
89
|
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
88
90
|
* Send me a pull request. Bonus points for topic branches.
|
89
91
|
|
90
|
-
|
92
|
+
## Copyright
|
91
93
|
|
92
|
-
Copyright (c) 2010 Bill Dueber. See LICENSE for details.
|
94
|
+
Copyright (c) 2010, 2011 Bill Dueber. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
data/lib/library_stdnums.rb
CHANGED
@@ -1,17 +1,29 @@
|
|
1
|
+
# Static Module functions to work with library "standard numbers" ISSN, ISBN, and LCCN
|
1
2
|
module StdNum
|
2
|
-
|
3
|
+
|
4
|
+
# Helper methods common to ISBN/ISSN
|
3
5
|
module Helpers
|
4
6
|
|
7
|
+
# The pattern we use to try and find an ISBN/ISSN. Ditch everthing before the first
|
8
|
+
# digit, then take all the digits/hyphens, optionally followed by an 'X'
|
5
9
|
STDNUMPAT = /^.*?(\d[\d\-]+[xX]?)/
|
6
10
|
|
7
11
|
# Extract the most likely looking number from the string. This will be the first
|
8
12
|
# string of digits-and-hyphens-and-maybe-a-trailing-X, with the hypens removed
|
13
|
+
# @param [String] str The string from which to extract an ISBN/ISSN
|
14
|
+
# @return [String] The extracted identifier
|
9
15
|
def extractNumber str
|
10
16
|
match = STDNUMPAT.match str
|
11
17
|
return nil unless match
|
12
18
|
return match[1].gsub(/\-/, '').upcase
|
13
19
|
end
|
14
20
|
|
21
|
+
# Given any string, extract what looks like the most likely ISBN/ISSN
|
22
|
+
# of the given size(s), or nil if nothing matches at the correct size.
|
23
|
+
# @param [String] rawnum The raw string containing (hopefully) an ISSN/ISBN
|
24
|
+
# @param [Integer, Array<Integer>, nil] An integer or array of integers of valid sizes
|
25
|
+
# for this type (e.g., 10 or 13 for ISBN, 8 for ISSN)
|
26
|
+
# @return [String,nil] the reduced and verified number, or nil if there's no match at the right size
|
15
27
|
def reduce_to_basics rawnum, valid_sizes = nil
|
16
28
|
return nil if rawnum.nil?
|
17
29
|
|
@@ -33,7 +45,7 @@ module StdNum
|
|
33
45
|
end
|
34
46
|
end
|
35
47
|
|
36
|
-
|
48
|
+
# Validate, convert, and normalize ISBNs (10-digit or 13-digit)
|
37
49
|
module ISBN
|
38
50
|
extend Helpers
|
39
51
|
|
@@ -84,7 +96,6 @@ module StdNum
|
|
84
96
|
# For an ISBN normalizing it is the same as converting to ISBN 13
|
85
97
|
# and making sure it's valid
|
86
98
|
# @param [String] isbn The ISBN to normalize
|
87
|
-
# @param [Boolean] passthrough On failure, return the original passed-in value instead of nil
|
88
99
|
# @return [String, nil] the normalized (to 13 digit) ISBN, or nil on failure
|
89
100
|
def self.normalize rawisbn
|
90
101
|
isbn = convert_to_13 rawisbn
|
@@ -137,8 +148,6 @@ module StdNum
|
|
137
148
|
# @example Get the normalized values and index them (if valid) or original value (if not)
|
138
149
|
# norms = StdNum::ISBN.allNormalizedValues(rawisbn)
|
139
150
|
# doc['isbn'] = norms ? norms : [rawisbn]
|
140
|
-
|
141
|
-
|
142
151
|
def self.allNormalizedValues isbn
|
143
152
|
isbn = reduce_to_basics isbn, [10,13]
|
144
153
|
return [] unless isbn
|
@@ -153,6 +162,7 @@ module StdNum
|
|
153
162
|
|
154
163
|
end
|
155
164
|
|
165
|
+
# Validate and and normalize ISSNs
|
156
166
|
module ISSN
|
157
167
|
extend Helpers
|
158
168
|
|
@@ -188,6 +198,11 @@ module StdNum
|
|
188
198
|
return issn[-1..-1] == self.checkdigit(issn, true)
|
189
199
|
end
|
190
200
|
|
201
|
+
|
202
|
+
|
203
|
+
# Make sure it's valid, remove the dashes, uppercase the X, and return
|
204
|
+
# @param [String] isbn The ISBN to normalize
|
205
|
+
# @return [String, nil] the normalized (to 13 digit) ISBN, or nil on failure
|
191
206
|
def self.normalize rawissn
|
192
207
|
issn = reduce_to_basics rawissn, 8
|
193
208
|
if issn and valid?(issn, true)
|
@@ -201,6 +216,7 @@ module StdNum
|
|
201
216
|
|
202
217
|
end
|
203
218
|
|
219
|
+
# Validate and and normalize LCCNs
|
204
220
|
module LCCN
|
205
221
|
|
206
222
|
# The rules for validity according to http://www.loc.gov/marc/lccn-namespace.html#syntax:
|