luhn_util 0.2.0 → 0.3.0
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 +6 -0
- data/lib/luhn/version.rb +1 -1
- data/lib/luhn.rb +4 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de11d9f613bff6a60e8a0171b12d6ac314896effa2b2ff9b71bb4ebb3fe4acfa
|
4
|
+
data.tar.gz: 791ac942326d80198704a456fca3942d8d967530d283e01691bf4203f8a28bea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3016fc85a49a12b87197c1935b6569390331b151c9832831adc8af817f299d6373583efa0721ab2f11ba82b2e31f0c3266384fee9e7648cb87e3cc1afa988bac
|
7
|
+
data.tar.gz: 494ff51979775268613b1cf183e2b9afff08e64a02fa2f2d297351f07fc904fcb793ae3b556446e08495d39c8b1c18a8809b318280fbf639261c71646e69db7e
|
data/README.md
CHANGED
@@ -4,6 +4,7 @@ A simple Ruby utility to validate and generate Luhn-compliant numbers.
|
|
4
4
|
|
5
5
|
`luhn_util` is able to:
|
6
6
|
- Validate an input according to the Luhn algorithm
|
7
|
+
- Calculate the check digit for a given numeric input
|
7
8
|
- Generate random Luhn number based on any valid length and prefix
|
8
9
|
|
9
10
|
The algorithm followed is based on [Luhn Algorithm](https://en.wikipedia.org/wiki/Luhn_algorithm).
|
@@ -32,6 +33,11 @@ To check if input is valid:
|
|
32
33
|
$ Luhn.valid?('5185443765446305')
|
33
34
|
$ true
|
34
35
|
```
|
36
|
+
To calculate the check digit:
|
37
|
+
```shell
|
38
|
+
$ Luhn.check_digit('1234567890')
|
39
|
+
$ 3
|
40
|
+
```
|
35
41
|
The `generate` method returns a 16-character number by default:
|
36
42
|
```shell
|
37
43
|
$ Luhn.generate
|
data/lib/luhn/version.rb
CHANGED
data/lib/luhn.rb
CHANGED
@@ -11,6 +11,9 @@ module Luhn
|
|
11
11
|
sum = checksum + non_checksum
|
12
12
|
sum % 10 == 0
|
13
13
|
end
|
14
|
+
def check_digit(partial_num)
|
15
|
+
(10 - (non_checksum_sum(partial_num) % 10)) % 10
|
16
|
+
end
|
14
17
|
|
15
18
|
def generate(length: 16, prefix: nil)
|
16
19
|
raise ArgumentError, 'length must be more than minimum' unless length.to_i > MIN_LENGTH
|
@@ -37,7 +40,7 @@ module Luhn
|
|
37
40
|
partial_num.chars.map(&:to_i).reverse.each_with_index do |num, i|
|
38
41
|
if i.even?
|
39
42
|
num *= 2
|
40
|
-
num
|
43
|
+
num -= 9 if num > 9
|
41
44
|
end
|
42
45
|
sum += num
|
43
46
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: luhn_util
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kat Padilla
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-03-
|
11
|
+
date: 2025-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple Ruby utility to validate and generate Luhn-compliant numbers
|
14
14
|
email:
|