sjekksum 0.0.4 → 0.0.5
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.
- checksums.yaml +4 -4
- data/README.md +25 -21
- data/lib/sjekksum/isbn10.rb +6 -4
- data/lib/sjekksum/isbn13.rb +68 -0
- data/lib/sjekksum/luhn.rb +1 -0
- data/lib/sjekksum/upc.rb +1 -0
- data/lib/sjekksum/verhoeff.rb +1 -0
- data/lib/sjekksum/version.rb +1 -1
- data/lib/sjekksum.rb +59 -10
- data/spec/sjekksum/isbn10_spec.rb +2 -2
- data/spec/sjekksum/isbn13_spec.rb +27 -0
- data/spec/sjekksum_spec.rb +1 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e68d799e7f1e30802bc9f64ea41f0274328be23
|
4
|
+
data.tar.gz: 4b873a7bea6e7ab8a0f12f0d883c6aa344e675fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba1ada821381e10cf13b37a15fdf985a04ec1326ca62919391e98fd9382085f230c448a39cceffac61207043423566f884aceb7ea4fc99ef5eaf4ae64c3e87a5
|
7
|
+
data.tar.gz: 76b890f5e049a5c892a252a77c6480c101029fb37b4c9c9dc54d5ba1ec0b667dd8aacb80535260dc02f04e0a7fa80030cfeed7fcc99dce83ebfbee57cef3c126
|
data/README.md
CHANGED
@@ -50,33 +50,37 @@ gem install sjekksum
|
|
50
50
|
```ruby
|
51
51
|
require "sjekksum"
|
52
52
|
|
53
|
-
Sjekksum.damm(572)
|
54
|
-
Sjekksum.damm!(572)
|
55
|
-
Sjekksum.damm?(5724)
|
53
|
+
Sjekksum.damm(572) #=> 4
|
54
|
+
Sjekksum.damm!(572) #=> 5724
|
55
|
+
Sjekksum.damm?(5724) #=> true
|
56
56
|
|
57
|
-
Sjekksum.isbn10("147743025")
|
58
|
-
Sjekksum.isbn10!("147743025")
|
59
|
-
Sjekksum.isbn10?("1477430253")
|
57
|
+
Sjekksum.isbn10("147743025") #=> 3
|
58
|
+
Sjekksum.isbn10!("147743025") #=> "1477430253"
|
59
|
+
Sjekksum.isbn10?("1477430253") #=> true
|
60
60
|
|
61
|
-
Sjekksum.
|
62
|
-
Sjekksum.
|
63
|
-
Sjekksum.
|
61
|
+
Sjekksum.isbn13("978-0-306-40615-") #=> 7
|
62
|
+
Sjekksum.isbn13!("978-0-306-40615-") #=> "978-0-306-40615-7"
|
63
|
+
Sjekksum.isbn13?("978-0-306-40615-7") #=> true
|
64
64
|
|
65
|
-
Sjekksum.
|
66
|
-
Sjekksum.
|
67
|
-
Sjekksum.
|
65
|
+
Sjekksum.luhn(7992739871) #=> 3
|
66
|
+
Sjekksum.luhn!(7992739871) #=> 79927398713
|
67
|
+
Sjekksum.luhn?(79927398713) #=> true
|
68
68
|
|
69
|
-
Sjekksum.
|
70
|
-
Sjekksum.
|
71
|
-
Sjekksum.
|
69
|
+
Sjekksum.upc("03600024145") #=> 7
|
70
|
+
Sjekksum.upc!("03600024145") #=> "036000241457"
|
71
|
+
Sjekksum.upc?("036000241457") #=> true
|
72
72
|
|
73
|
-
Sjekksum.
|
74
|
-
Sjekksum.
|
75
|
-
Sjekksum.
|
73
|
+
Sjekksum.verhoeff(142857) #=> 0
|
74
|
+
Sjekksum.verhoeff!(142857) #=> 1428570
|
75
|
+
Sjekksum.verhoeff?(1428570) #=> true
|
76
76
|
|
77
|
-
Sjekksum.
|
78
|
-
Sjekksum.
|
79
|
-
Sjekksum.
|
77
|
+
Sjekksum.primitive(232323) #=> 6
|
78
|
+
Sjekksum.primitive!(232323) #=> 2323236
|
79
|
+
Sjekksum.primitive?(2323236) #=> true
|
80
|
+
|
81
|
+
Sjekksum.primitive97(23569) #=> 5
|
82
|
+
Sjekksum.primitive97!(23569) #=> 235695
|
83
|
+
Sjekksum.primitive97?(235695) #=> true
|
80
84
|
```
|
81
85
|
|
82
86
|
|
data/lib/sjekksum/isbn10.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Sjekksum
|
2
2
|
#
|
3
|
-
# Module for calculation and validation of ISBN
|
3
|
+
# Module for calculation and validation of ISBN-10 (International Standard Book Number) checksums
|
4
4
|
#
|
5
5
|
# @see http://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-10_check_digit_calculation ISBN-10 check digit calculation
|
6
6
|
#
|
@@ -9,7 +9,7 @@ module Sjekksum
|
|
9
9
|
extend Shared
|
10
10
|
|
11
11
|
#
|
12
|
-
# Calculates ISBN
|
12
|
+
# Calculates ISBN-10 checksum
|
13
13
|
#
|
14
14
|
# @example
|
15
15
|
# Sjekksum::ISBN10.of("147743025") #=> 3
|
@@ -21,16 +21,18 @@ module Sjekksum
|
|
21
21
|
def of number
|
22
22
|
raise_on_type_mismatch number
|
23
23
|
digits = convert_number_to_digits(number)[0..9]
|
24
|
+
|
24
25
|
sum = digits.reverse_each.with_index.reduce(0) do |check, (digit, idx)|
|
25
26
|
check += digit * (idx+2)
|
26
27
|
end
|
28
|
+
|
27
29
|
check = (11 - sum % 11) % 11
|
28
30
|
check == 10 ? "X" : check
|
29
31
|
end
|
30
32
|
alias_method :checksum, :of
|
31
33
|
|
32
34
|
#
|
33
|
-
# ISBN
|
35
|
+
# ISBN-10 validation of provided number
|
34
36
|
#
|
35
37
|
# @example
|
36
38
|
# Sjekksum::ISBN10.valid?("1477430253") #=> true
|
@@ -47,7 +49,7 @@ module Sjekksum
|
|
47
49
|
alias_method :is_valid?, :valid?
|
48
50
|
|
49
51
|
#
|
50
|
-
# Transforms a number by appending the ISBN
|
52
|
+
# Transforms a number by appending the ISBN-10 checksum digit
|
51
53
|
#
|
52
54
|
# @example
|
53
55
|
# Sjekksum::ISBN10.convert("147743025") #=> "1477430253"
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Sjekksum
|
2
|
+
#
|
3
|
+
# Module for calculation and validation of ISBN-10 (International Standard Book Number) checksums
|
4
|
+
#
|
5
|
+
# @see http://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-13_check_digit_calculation ISBN-13 check digit calculation
|
6
|
+
#
|
7
|
+
# Its check digit is generated the same way as the {Sjekksum::UPC} except that the even digits are multiplied by 3 instead of the odd digits.
|
8
|
+
#
|
9
|
+
module ISBN13
|
10
|
+
extend self
|
11
|
+
extend Shared
|
12
|
+
|
13
|
+
#
|
14
|
+
# Calculates ISBN13 checksum
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
# Sjekksum::ISBN13.of("978-0-306-40615-") #=> 7
|
18
|
+
#
|
19
|
+
# @param number [Integer, String] number for which the checksum should be calculated
|
20
|
+
#
|
21
|
+
# @return [Integer] calculated checksum
|
22
|
+
def of number
|
23
|
+
raise_on_type_mismatch number
|
24
|
+
digits = convert_number_to_digits(number)
|
25
|
+
|
26
|
+
sum = digits.map.with_index do |digit, idx|
|
27
|
+
idx.odd? ? (digit * 3) : digit
|
28
|
+
end.reduce(&:+)
|
29
|
+
|
30
|
+
(10 - sum % 10) % 10
|
31
|
+
end
|
32
|
+
alias_method :checksum, :of
|
33
|
+
|
34
|
+
#
|
35
|
+
# ISBN13 validation of provided number
|
36
|
+
#
|
37
|
+
# @example
|
38
|
+
# Sjekksum::ISBN13.valid?("978-0-306-40615-7") #=> true
|
39
|
+
#
|
40
|
+
# @param number [Integer, String] number with included checksum
|
41
|
+
#
|
42
|
+
# @return [Boolean]
|
43
|
+
def valid? number
|
44
|
+
raise_on_type_mismatch number
|
45
|
+
num, check = split_number(number)
|
46
|
+
self.of(num) == check
|
47
|
+
end
|
48
|
+
alias_method :is_valid?, :valid?
|
49
|
+
|
50
|
+
#
|
51
|
+
# Transforms a number by appending the ISBN13 checksum digit
|
52
|
+
#
|
53
|
+
# It will preserve type (Integer/String) and therefore leading zeros.
|
54
|
+
#
|
55
|
+
# @example
|
56
|
+
# Sjekksum::ISBN13.convert("978-0-306-40615-") #=> "978-0-306-40615-7"
|
57
|
+
#
|
58
|
+
# @param number [Integer, String] number without a checksum
|
59
|
+
#
|
60
|
+
# @return [Integer, String] final number including the checksum
|
61
|
+
def convert number
|
62
|
+
raise_on_type_mismatch number
|
63
|
+
typed_conversion number
|
64
|
+
end
|
65
|
+
alias_method :transform, :convert
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
data/lib/sjekksum/luhn.rb
CHANGED
data/lib/sjekksum/upc.rb
CHANGED
data/lib/sjekksum/verhoeff.rb
CHANGED
@@ -49,6 +49,7 @@ module Sjekksum
|
|
49
49
|
def of number
|
50
50
|
raise_on_type_mismatch number
|
51
51
|
digits = convert_number_to_digits(number)
|
52
|
+
|
52
53
|
INVERSE[digits.reverse_each.with_index.reduce(0) { |check, (digit, idx)|
|
53
54
|
d_row = DIHEDRAL_GROUP_D5[check]
|
54
55
|
d_row[ PERMUTATION[idx.next % 8][digit] ]
|
data/lib/sjekksum/version.rb
CHANGED
data/lib/sjekksum.rb
CHANGED
@@ -2,6 +2,7 @@ require "sjekksum/version"
|
|
2
2
|
require "sjekksum/shared"
|
3
3
|
require "sjekksum/damm"
|
4
4
|
require "sjekksum/isbn10"
|
5
|
+
require "sjekksum/isbn13"
|
5
6
|
require "sjekksum/luhn"
|
6
7
|
require "sjekksum/upc"
|
7
8
|
require "sjekksum/verhoeff"
|
@@ -31,13 +32,13 @@ module Sjekksum
|
|
31
32
|
alias_method :damm, :damm_of
|
32
33
|
|
33
34
|
#
|
34
|
-
# Calculates ISBN
|
35
|
+
# Calculates ISBN-10 checksum
|
35
36
|
#
|
36
37
|
# @example
|
37
|
-
# Sjekksum
|
38
|
-
# Sjekksum
|
38
|
+
# Sjekksum.isbn10("147743025") #=> 3
|
39
|
+
# Sjekksum.isbn10("193435600") #=> "X"
|
39
40
|
#
|
40
|
-
# @see Sjekksum::
|
41
|
+
# @see Sjekksum::ISBN10#of
|
41
42
|
#
|
42
43
|
# @param number [Integer, String] number for which the checksum should be calculated
|
43
44
|
#
|
@@ -47,6 +48,22 @@ module Sjekksum
|
|
47
48
|
end
|
48
49
|
alias_method :isbn10, :isbn10_of
|
49
50
|
|
51
|
+
#
|
52
|
+
# Calculates ISBN-13 checksum
|
53
|
+
#
|
54
|
+
# @example
|
55
|
+
# Sjekksum.isbn13("978-0-306-40615-") #=> 7
|
56
|
+
#
|
57
|
+
# @see Sjekksum::ISBN13#of
|
58
|
+
#
|
59
|
+
# @param number [Integer, String] number for which the checksum should be calculated
|
60
|
+
#
|
61
|
+
# @return [Integer, String] calculated checksum
|
62
|
+
def isbn13_of number
|
63
|
+
ISBN13.of number
|
64
|
+
end
|
65
|
+
alias_method :isbn13, :isbn13_of
|
66
|
+
|
50
67
|
#
|
51
68
|
# Calculates Luhn checksum
|
52
69
|
#
|
@@ -144,11 +161,11 @@ module Sjekksum
|
|
144
161
|
alias_method :damm?, :valid_damm?
|
145
162
|
|
146
163
|
#
|
147
|
-
# ISBN
|
164
|
+
# ISBN-10 validation of provided number
|
148
165
|
#
|
149
166
|
# @example
|
150
|
-
# Sjekksum
|
151
|
-
# Sjekksum
|
167
|
+
# Sjekksum.isbn10?("1477430253") #=> true
|
168
|
+
# Sjekksum.isbn10?("193435600X") #=> true
|
152
169
|
#
|
153
170
|
# @see Sjekksum::ISBN10#valid?
|
154
171
|
#
|
@@ -160,6 +177,22 @@ module Sjekksum
|
|
160
177
|
end
|
161
178
|
alias_method :isbn10?, :valid_isbn10?
|
162
179
|
|
180
|
+
#
|
181
|
+
# ISBN-13 validation of provided number
|
182
|
+
#
|
183
|
+
# @example
|
184
|
+
# Sjekksum.isbn13?("978-0-306-40615-7") #=> true
|
185
|
+
#
|
186
|
+
# @see Sjekksum::ISBN13#valid?
|
187
|
+
#
|
188
|
+
# @param number [Integer, String] number with included checksum
|
189
|
+
#
|
190
|
+
# @return [Boolean]
|
191
|
+
def valid_isbn13? number
|
192
|
+
ISBN13.valid? number
|
193
|
+
end
|
194
|
+
alias_method :isbn13?, :valid_isbn13?
|
195
|
+
|
163
196
|
#
|
164
197
|
# Luhn validation of provided number
|
165
198
|
#
|
@@ -257,11 +290,11 @@ module Sjekksum
|
|
257
290
|
alias_method :damm!, :make_damm
|
258
291
|
|
259
292
|
#
|
260
|
-
# Transforms a number by appending the ISBN
|
293
|
+
# Transforms a number by appending the ISBN-10 checksum digit
|
261
294
|
#
|
262
295
|
# @example
|
263
|
-
# Sjekksum
|
264
|
-
# Sjekksum
|
296
|
+
# Sjekksum.isbn10!("147743025") #=> "1477430253"
|
297
|
+
# Sjekksum.isbn10!("193435600") #=> "193435600X"
|
265
298
|
#
|
266
299
|
# @see Sjekksum::ISBN10#convert
|
267
300
|
#
|
@@ -273,6 +306,22 @@ module Sjekksum
|
|
273
306
|
end
|
274
307
|
alias_method :isbn10!, :make_isbn10
|
275
308
|
|
309
|
+
#
|
310
|
+
# Transforms a number by appending the ISBN-13 checksum digit
|
311
|
+
#
|
312
|
+
# @example
|
313
|
+
# Sjekksum.isbn13("978-0-306-40615-") #=> "978-0-306-40615-7"
|
314
|
+
#
|
315
|
+
# @see Sjekksum::ISBN13#convert
|
316
|
+
#
|
317
|
+
# @param number [Integer, String] number without a checksum
|
318
|
+
#
|
319
|
+
# @return [Integer, String] final number including the checksum
|
320
|
+
def make_isbn13 number
|
321
|
+
ISBN13.convert number
|
322
|
+
end
|
323
|
+
alias_method :isbn13!, :make_isbn13
|
324
|
+
|
276
325
|
#
|
277
326
|
# Transforms a number by appending the Luhn checksum digit
|
278
327
|
#
|
@@ -12,14 +12,14 @@ describe Sjekksum::ISBN10 do
|
|
12
12
|
[ 388221591, 7 ],
|
13
13
|
[ "359617218", 7 ],
|
14
14
|
[ "147743025", 3 ],
|
15
|
-
[ "0-306-40615-", 2 ]
|
15
|
+
[ "0-306-40615-", 2 ]
|
16
16
|
]
|
17
17
|
|
18
18
|
fail_spec_matrix = [
|
19
19
|
[ 123, 9 ],
|
20
20
|
[ "123", 9 ],
|
21
21
|
[ "147743025", 9 ],
|
22
|
-
[ "0-306-40615-", 9 ]
|
22
|
+
[ "0-306-40615-", 9 ]
|
23
23
|
]
|
24
24
|
|
25
25
|
it_behaves_like "a checksum implementation:", success_spec_matrix, fail_spec_matrix
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "shared_implementation"
|
3
|
+
|
4
|
+
describe Sjekksum::ISBN13 do
|
5
|
+
|
6
|
+
success_spec_matrix = [
|
7
|
+
[ "978-363941830", 9 ],
|
8
|
+
[ "978-193435600", 5 ],
|
9
|
+
[ 978364222974, 9 ],
|
10
|
+
[ "978345330684", 4 ],
|
11
|
+
[ "978-388221068", 2 ],
|
12
|
+
[ 978388221591, 5 ],
|
13
|
+
[ "978-359617218", 4 ],
|
14
|
+
[ "978-147743025", 5 ],
|
15
|
+
[ "978-030640615", 7 ]
|
16
|
+
]
|
17
|
+
|
18
|
+
fail_spec_matrix = [
|
19
|
+
[ 123, 9 ],
|
20
|
+
[ "123", 9 ],
|
21
|
+
[ "978-147743025", 9 ],
|
22
|
+
[ "978-030640615", 9 ]
|
23
|
+
]
|
24
|
+
|
25
|
+
it_behaves_like "a checksum implementation:", success_spec_matrix, fail_spec_matrix
|
26
|
+
|
27
|
+
end
|
data/spec/sjekksum_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sjekksum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christoph Grabo
|
@@ -111,6 +111,7 @@ files:
|
|
111
111
|
- lib/sjekksum.rb
|
112
112
|
- lib/sjekksum/damm.rb
|
113
113
|
- lib/sjekksum/isbn10.rb
|
114
|
+
- lib/sjekksum/isbn13.rb
|
114
115
|
- lib/sjekksum/luhn.rb
|
115
116
|
- lib/sjekksum/primitive.rb
|
116
117
|
- lib/sjekksum/primitive97.rb
|
@@ -122,6 +123,7 @@ files:
|
|
122
123
|
- spec/shared_implementation.rb
|
123
124
|
- spec/sjekksum/damm_spec.rb
|
124
125
|
- spec/sjekksum/isbn10_spec.rb
|
126
|
+
- spec/sjekksum/isbn13_spec.rb
|
125
127
|
- spec/sjekksum/luhn_spec.rb
|
126
128
|
- spec/sjekksum/primitive97_spec.rb
|
127
129
|
- spec/sjekksum/primitive_spec.rb
|
@@ -157,6 +159,7 @@ test_files:
|
|
157
159
|
- spec/shared_implementation.rb
|
158
160
|
- spec/sjekksum/damm_spec.rb
|
159
161
|
- spec/sjekksum/isbn10_spec.rb
|
162
|
+
- spec/sjekksum/isbn13_spec.rb
|
160
163
|
- spec/sjekksum/luhn_spec.rb
|
161
164
|
- spec/sjekksum/primitive97_spec.rb
|
162
165
|
- spec/sjekksum/primitive_spec.rb
|