sjekksum 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ebf2decf9fa08a77d79569fc64a315cafdaf33b0
4
- data.tar.gz: 32770f2b17ad7053f425b38a24a82ff28780ebc3
3
+ metadata.gz: 5766c405550125325430eb38e4ce6f8600721311
4
+ data.tar.gz: a8e2cc7a1cd0f8f89eb9214335fc3a7e35a93108
5
5
  SHA512:
6
- metadata.gz: 7f4b7601494c7e8631b2203c81d9e4333212cfcd4f0d7861da62e78a8715bddef3ee255b904d7556de1c3acce5de87c9b64f851bfec5dd3917e61a6ebbe0a900
7
- data.tar.gz: 41346fdd102f54ac079cc5e53bdff8bb8aed90feaa90be508eb9e8581e904e1111b53252a47381f0bcdd86d89bf2b6cb3176cbab7255da187e62d7cf73d484bd
6
+ metadata.gz: 8db7a683ca0a17d7ea7962e4210cab4dc0bad173f492b7fe44797f72f110882fdcd5295c0eb21d37612955507dd4d3eb588b491a00ed72774916211961a40f39
7
+ data.tar.gz: adc09f1148201ca7755cea7b81a42085181d9fe476481523fe5b2c9126bd9044d9c1902eed36287e9be0e10a8e398c3ff8d0acdeadae9b60a303453385a68060
data/README.md CHANGED
@@ -1,17 +1,26 @@
1
1
  # Sjekksum [![Gem Version](https://badge.fury.io/rb/sjekksum.png)](http://badge.fury.io/rb/sjekksum) [![Build Status](https://travis-ci.org/asaaki/sjekksum.png?branch=master)](https://travis-ci.org/asaaki/sjekksum)
2
2
 
3
- A gem to provide some checksum algorithms like Luhn, Damm and Verhoeff.
3
+ A gem to provide some checksum algorithms like Damm, Luhn, UPC and Verhoeff.
4
4
 
5
5
  ----
6
6
 
7
- All used algorithms for generating and validating checksums must calculate a single check digit only.
7
+ All used algorithms for generating and validating checksums **must calculate a single check digit only.**
8
8
 
9
9
  More extensive checksum algorithms like Adler-32, CRC, BSD checksum, SYSV checksum, all hash algorithms and friends are not part of this project.
10
- Also no advanced algorithms like IBAN and credit card number calculation can be found here due to their nature in these cases more transformations and steps have to be taken.
10
+
11
11
  Furthermore this gem does not allow any other input type than integers or strings of integer digits to simplify transformations here (so all extended algorithms allowing characters are not included as well).
12
- For the string inputs all non-integer bytes are thrown away (e. g. `"01-234.567 89" => "0123456789" => [0,1,2,3,4,5,6,7,8,9]`).
13
12
 
14
- Shortly: This gem tries to follow the [UNIX philosophy](http://en.wikipedia.org/wiki/Unix_philosophy) in parts: »Write programs that do one thing and do it well.«
13
+ For the string inputs all non-integer bytes are thrown away:
14
+
15
+ ```ruby
16
+ "01-234.567 89"
17
+ #=>
18
+ "0123456789" # cleaned string
19
+ #=>
20
+ [0,1,2,3,4,5,6,7,8,9] # internal digits representation
21
+ ```
22
+
23
+ Shortly: This gem tries to follow the [UNIX philosophy](http://en.wikipedia.org/wiki/Unix_philosophy) in parts: _»Write programs that do one thing and do it well.«_
15
24
 
16
25
 
17
26
 
@@ -19,16 +28,21 @@ Shortly: This gem tries to follow the [UNIX philosophy](http://en.wikipedia.org/
19
28
 
20
29
  Add this line to your application's Gemfile:
21
30
 
22
- gem "sjekksum"
31
+ ```ruby
32
+ gem "sjekksum"
33
+ ```
23
34
 
24
35
  And then execute:
25
36
 
26
- $ bundle
37
+ ```shell
38
+ bundle
39
+ ```
27
40
 
28
41
  Or install it yourself as:
29
42
 
30
- $ gem install damm
31
-
43
+ ```shell
44
+ gem install sjekksum
45
+ ```
32
46
 
33
47
 
34
48
  ## Usage
@@ -36,29 +50,33 @@ Or install it yourself as:
36
50
  ```ruby
37
51
  require "sjekksum"
38
52
 
39
- Sjekksum.damm(572) #=> 4
40
- Sjekksum.damm!(572) #=> 5724
41
- Sjekksum.damm?(5724) #=> true
53
+ Sjekksum.damm(572) #=> 4
54
+ Sjekksum.damm!(572) #=> 5724
55
+ Sjekksum.damm?(5724) #=> true
56
+
57
+ Sjekksum.isbn10("147743025") #=> 3
58
+ Sjekksum.isbn10!("147743025") #=> "1477430253"
59
+ Sjekksum.isbn10?("1477430253") #=> true
42
60
 
43
- Sjekksum.luhn(7992739871) #=> 3
44
- Sjekksum.luhn!(7992739871) #=> 79927398713
45
- Sjekksum.luhn?(79927398713) #=> true
61
+ Sjekksum.luhn(7992739871) #=> 3
62
+ Sjekksum.luhn!(7992739871) #=> 79927398713
63
+ Sjekksum.luhn?(79927398713) #=> true
46
64
 
47
- Sjekksum.upc("03600024145") #=> 7
48
- Sjekksum.upc!("03600024145") #=> "036000241457"
49
- Sjekksum.upc?("03600024145") #=> true
65
+ Sjekksum.upc("03600024145") #=> 7
66
+ Sjekksum.upc!("03600024145") #=> "036000241457"
67
+ Sjekksum.upc?("036000241457") #=> true
50
68
 
51
- Sjekksum.verhoeff(142857) #=> 0
52
- Sjekksum.verhoeff?(1428570) #=> true
53
- Sjekksum.verhoeff!(142857) #=> 1428570
69
+ Sjekksum.verhoeff(142857) #=> 0
70
+ Sjekksum.verhoeff!(142857) #=> 1428570
71
+ Sjekksum.verhoeff?(1428570) #=> true
54
72
 
55
- Sjekksum.primitive(232323) #=> 6
56
- Sjekksum.primitive?(2323236) #=> true
57
- Sjekksum.primitive!(232323) #=> 2323236
73
+ Sjekksum.primitive(232323) #=> 6
74
+ Sjekksum.primitive!(232323) #=> 2323236
75
+ Sjekksum.primitive?(2323236) #=> true
58
76
 
59
- Sjekksum.primitive97(23569) #=> 0
60
- Sjekksum.primitive97?(235695) #=> true
61
- Sjekksum.primitive97!(23569) #=> 235695
77
+ Sjekksum.primitive97(23569) #=> 5
78
+ Sjekksum.primitive97!(23569) #=> 235695
79
+ Sjekksum.primitive97?(235695) #=> true
62
80
  ```
63
81
 
64
82
 
@@ -0,0 +1,86 @@
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-10_check_digit_calculation ISBN-10 check digit calculation
6
+ #
7
+ module ISBN10
8
+ extend self
9
+ extend Shared
10
+
11
+ #
12
+ # Calculates ISBN 10 checksum
13
+ #
14
+ # @example
15
+ # Sjekksum::ISBN10.of("147743025") #=> 3
16
+ # Sjekksum::ISBN10.of("193435600") #=> "X"
17
+ #
18
+ # @param number [Integer, String] number for which the checksum should be calculated
19
+ #
20
+ # @return [Integer, String] calculated checksum
21
+ def of number
22
+ raise_on_type_mismatch number
23
+ digits = convert_number_to_digits(number)[0..9]
24
+ sum = digits.reverse_each.with_index.reduce(0) do |check, (digit, idx)|
25
+ check += digit * (idx+2)
26
+ end
27
+ check = (11 - sum % 11) % 11
28
+ check == 10 ? "X" : check
29
+ end
30
+ alias_method :checksum, :of
31
+
32
+ #
33
+ # ISBN 10 validation of provided number
34
+ #
35
+ # @example
36
+ # Sjekksum::ISBN10.valid?("1477430253") #=> true
37
+ # Sjekksum::ISBN10.valid?("193435600X") #=> true
38
+ #
39
+ # @param number [Integer, String] number with included checksum
40
+ #
41
+ # @return [Boolean]
42
+ def valid? number
43
+ raise_on_type_mismatch number
44
+ num, check = split_isbn_number(number)
45
+ convert_number_to_digits(num).length == 9 && self.of(num) == check
46
+ end
47
+ alias_method :is_valid?, :valid?
48
+
49
+ #
50
+ # Transforms a number by appending the ISBN 10 checksum digit
51
+ #
52
+ # @example
53
+ # Sjekksum::ISBN10.convert("147743025") #=> "1477430253"
54
+ # Sjekksum::ISBN10.convert("193435600") #=> "193435600X"
55
+ #
56
+ # @param number [Integer, String] number without a checksum
57
+ #
58
+ # @return [Integer, String] final number including the checksum
59
+ def convert number
60
+ raise_on_type_mismatch number
61
+ check = self.of(number)
62
+ if number.is_a?(String) or check.is_a?(String)
63
+ number.to_s << self.of(number).to_s
64
+ else
65
+ convert_to_int(number) * 10 + self.of(number)
66
+ end
67
+ end
68
+ alias_method :transform, :convert
69
+
70
+ private
71
+
72
+ #
73
+ # [split_isbn_number description]
74
+ # @param number [type] [description]
75
+ #
76
+ # @return [type] [description]
77
+ def split_isbn_number number
78
+ if number.is_a?(String)
79
+ [ number[0..-2], (%w[X x].include?(number[-1]) ? "X" : number[-1].to_i) ]
80
+ else
81
+ number.divmod(10)
82
+ end
83
+ end
84
+
85
+ end
86
+ end
@@ -1,4 +1,4 @@
1
1
  module Sjekksum
2
2
  # Version constant
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
data/lib/sjekksum.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "sjekksum/version"
2
2
  require "sjekksum/shared"
3
3
  require "sjekksum/damm"
4
+ require "sjekksum/isbn10"
4
5
  require "sjekksum/luhn"
5
6
  require "sjekksum/upc"
6
7
  require "sjekksum/verhoeff"
@@ -29,6 +30,23 @@ module Sjekksum
29
30
  end
30
31
  alias_method :damm, :damm_of
31
32
 
33
+ #
34
+ # Calculates ISBN 10 checksum
35
+ #
36
+ # @example
37
+ # Sjekksum::ISBN10.of("147743025") #=> 3
38
+ # Sjekksum::ISBN10.of("193435600") #=> "X"
39
+ #
40
+ # @see Sjekksum::Damm#of
41
+ #
42
+ # @param number [Integer, String] number for which the checksum should be calculated
43
+ #
44
+ # @return [Integer, String] calculated checksum
45
+ def isbn10_of number
46
+ ISBN10.of number
47
+ end
48
+ alias_method :isbn10, :isbn10_of
49
+
32
50
  #
33
51
  # Calculates Luhn checksum
34
52
  #
@@ -125,6 +143,23 @@ module Sjekksum
125
143
  end
126
144
  alias_method :damm?, :valid_damm?
127
145
 
146
+ #
147
+ # ISBN 10 validation of provided number
148
+ #
149
+ # @example
150
+ # Sjekksum::ISBN10.valid?("1477430253") #=> true
151
+ # Sjekksum::ISBN10.valid?("193435600X") #=> true
152
+ #
153
+ # @see Sjekksum::ISBN10#valid?
154
+ #
155
+ # @param number [Integer, String] number with included checksum
156
+ #
157
+ # @return [Boolean]
158
+ def valid_isbn10? number
159
+ ISBN10.valid? number
160
+ end
161
+ alias_method :isbn10?, :valid_isbn10?
162
+
128
163
  #
129
164
  # Luhn validation of provided number
130
165
  #
@@ -221,6 +256,23 @@ module Sjekksum
221
256
  end
222
257
  alias_method :damm!, :make_damm
223
258
 
259
+ #
260
+ # Transforms a number by appending the ISBN 10 checksum digit
261
+ #
262
+ # @example
263
+ # Sjekksum::ISBN10.convert("147743025") #=> "1477430253"
264
+ # Sjekksum::ISBN10.convert("193435600") #=> "193435600X"
265
+ #
266
+ # @see Sjekksum::ISBN10#convert
267
+ #
268
+ # @param number [Integer, String] number without a checksum
269
+ #
270
+ # @return [Integer, String] final number including the checksum
271
+ def make_isbn10 number
272
+ ISBN10.convert number
273
+ end
274
+ alias_method :isbn10!, :make_isbn10
275
+
224
276
  #
225
277
  # Transforms a number by appending the Luhn checksum digit
226
278
  #
data/sjekksum.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Sjekksum::VERSION
9
9
  spec.authors = ["Christoph Grabo"]
10
10
  spec.email = ["chris@dinarrr.com"]
11
- spec.summary = %q{Collection of some checksum/validation algorithms}
12
- spec.description = %q{A gem to provide some checksum algorithms like Luhn, Damm and Verhoeff.}
11
+ spec.summary = %q{Collection of some checksum validation algorithms}
12
+ spec.description = %q{A gem to provide some checksum algorithms like Damm, Luhn, UPC and Verhoeff.}
13
13
  spec.homepage = "https://github.com/asaaki/sjekksum"
14
14
  spec.license = "MIT"
15
15
 
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+ require "shared_implementation"
3
+
4
+ describe Sjekksum::ISBN10 do
5
+
6
+ success_spec_matrix = [
7
+ [ "363941830", 1 ],
8
+ [ "193435600", "X" ],
9
+ [ 364222974, 3 ],
10
+ [ "345330684", 8 ],
11
+ [ "3-882-21068", 0 ],
12
+ [ 388221591, 7 ],
13
+ [ "359617218", 7 ],
14
+ [ "147743025", 3 ],
15
+ [ "0-306-40615-", 2 ],
16
+ ]
17
+
18
+ fail_spec_matrix = [
19
+ [ 123, 9 ],
20
+ [ "123", 9 ],
21
+ [ "147743025", 9 ],
22
+ [ "0-306-40615-", 9 ],
23
+ ]
24
+
25
+ it_behaves_like "a checksum implementation:", success_spec_matrix, fail_spec_matrix
26
+
27
+ end
@@ -2,106 +2,34 @@ require "spec_helper"
2
2
 
3
3
  describe Sjekksum do
4
4
 
5
+ spec_input = 193435600
6
+
5
7
  spec_values = {
6
- input: 12345,
7
8
  damm: 9,
9
+ isbn10: "X",
8
10
  luhn: 5,
9
- upc: 7,
10
- verhoeff: 1,
11
- primitive: 6,
12
- primitive97: 8
11
+ upc: 3,
12
+ verhoeff: 4,
13
+ primitive: 4,
14
+ primitive97: 7
13
15
  }
14
16
 
15
17
  specify { expect(described_class).to be_a(Module) }
16
18
 
17
- it "#damm" do
18
- expected = spec_values[:input] * 10 + spec_values[:damm]
19
- expect(described_class.damm(spec_values[:input])).to eq(spec_values[:damm])
20
- end
21
-
22
- it "#damm?" do
23
- value = spec_values[:input] * 10 + spec_values[:damm]
24
- expect(described_class.damm?(value)).to be_true
25
- end
26
-
27
- it "#damm!" do
28
- expected = spec_values[:input] * 10 + spec_values[:damm]
29
- expect(described_class.damm!(spec_values[:input])).to eq(expected)
30
- end
31
-
32
- it "#luhn" do
33
- expected = spec_values[:input] * 10 + spec_values[:luhn]
34
- expect(described_class.luhn(spec_values[:input])).to eq(spec_values[:luhn])
35
- end
36
-
37
- it "#luhn?" do
38
- value = spec_values[:input] * 10 + spec_values[:luhn]
39
- expect(described_class.luhn?(value)).to be_true
40
- end
41
-
42
- it "#luhn!" do
43
- expected = spec_values[:input] * 10 + spec_values[:luhn]
44
- expect(described_class.luhn!(spec_values[:input])).to eq(expected)
45
- end
46
-
47
- it "#upc" do
48
- expected = spec_values[:input] * 10 + spec_values[:upc]
49
- expect(described_class.upc(spec_values[:input])).to eq(spec_values[:upc])
50
- end
51
-
52
- it "#upc?" do
53
- value = spec_values[:input] * 10 + spec_values[:upc]
54
- expect(described_class.upc?(value)).to be_true
55
- end
56
-
57
- it "#upc!" do
58
- expected = spec_values[:input] * 10 + spec_values[:upc]
59
- expect(described_class.upc!(spec_values[:input])).to eq(expected)
60
- end
61
-
62
- it "#verhoeff" do
63
- expected = spec_values[:input] * 10 + spec_values[:verhoeff]
64
- expect(described_class.verhoeff(spec_values[:input])).to eq(spec_values[:verhoeff])
65
- end
66
-
67
- it "#verhoeff?" do
68
- value = spec_values[:input] * 10 + spec_values[:verhoeff]
69
- expect(described_class.verhoeff?(value)).to be_true
70
- end
71
-
72
- it "#verhoeff!" do
73
- expected = spec_values[:input] * 10 + spec_values[:verhoeff]
74
- expect(described_class.verhoeff!(spec_values[:input])).to eq(expected)
75
- end
76
-
77
- it "#primitive" do
78
- expected = spec_values[:input] * 10 + spec_values[:primitive]
79
- expect(described_class.primitive(spec_values[:input])).to eq(spec_values[:primitive])
80
- end
81
-
82
- it "#primitive?" do
83
- value = spec_values[:input] * 10 + spec_values[:primitive]
84
- expect(described_class.primitive?(value)).to be_true
85
- end
86
-
87
- it "#primitive!" do
88
- expected = spec_values[:input] * 10 + spec_values[:primitive]
89
- expect(described_class.primitive!(spec_values[:input])).to eq(expected)
90
- end
19
+ spec_values.each do |algo, check|
20
+ final_value = check.is_a?(String) ? "#{spec_input}#{check}" : spec_input * 10 + check
91
21
 
92
- it "#primitive97" do
93
- expected = spec_values[:input] * 10 + spec_values[:primitive97]
94
- expect(described_class.primitive97(spec_values[:input])).to eq(spec_values[:primitive97])
95
- end
22
+ it "##{algo}" do
23
+ expect(described_class.send(algo, spec_input)).to eq(check)
24
+ end
96
25
 
97
- it "#primitive97?" do
98
- value = spec_values[:input] * 10 + spec_values[:primitive97]
99
- expect(described_class.primitive97?(value)).to be_true
100
- end
26
+ it "##{algo}?" do
27
+ expect(described_class.send(:"#{algo}?", final_value)).to be_true
28
+ end
101
29
 
102
- it "#primitive97!" do
103
- expected = spec_values[:input] * 10 + spec_values[:primitive97]
104
- expect(described_class.primitive97!(spec_values[:input])).to eq(expected)
30
+ it "##{algo}!" do
31
+ expect(described_class.send(:"#{algo}!", spec_input)).to eq(final_value)
32
+ end
105
33
  end
106
34
 
107
35
  end
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christoph Grabo
@@ -94,7 +94,7 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- description: A gem to provide some checksum algorithms like Luhn, Damm and Verhoeff.
97
+ description: A gem to provide some checksum algorithms like Damm, Luhn, UPC and Verhoeff.
98
98
  email:
99
99
  - chris@dinarrr.com
100
100
  executables: []
@@ -110,6 +110,7 @@ files:
110
110
  - Rakefile
111
111
  - lib/sjekksum.rb
112
112
  - lib/sjekksum/damm.rb
113
+ - lib/sjekksum/isbn10.rb
113
114
  - lib/sjekksum/luhn.rb
114
115
  - lib/sjekksum/primitive.rb
115
116
  - lib/sjekksum/primitive97.rb
@@ -120,6 +121,7 @@ files:
120
121
  - sjekksum.gemspec
121
122
  - spec/shared_implementation.rb
122
123
  - spec/sjekksum/damm_spec.rb
124
+ - spec/sjekksum/isbn10_spec.rb
123
125
  - spec/sjekksum/luhn_spec.rb
124
126
  - spec/sjekksum/primitive97_spec.rb
125
127
  - spec/sjekksum/primitive_spec.rb
@@ -150,10 +152,11 @@ rubyforge_project:
150
152
  rubygems_version: 2.2.0
151
153
  signing_key:
152
154
  specification_version: 4
153
- summary: Collection of some checksum/validation algorithms
155
+ summary: Collection of some checksum validation algorithms
154
156
  test_files:
155
157
  - spec/shared_implementation.rb
156
158
  - spec/sjekksum/damm_spec.rb
159
+ - spec/sjekksum/isbn10_spec.rb
157
160
  - spec/sjekksum/luhn_spec.rb
158
161
  - spec/sjekksum/primitive97_spec.rb
159
162
  - spec/sjekksum/primitive_spec.rb