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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -0
  3. data/lib/luhn/version.rb +1 -1
  4. data/lib/luhn.rb +4 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 373c083840bb7a732849f0d5f088aba9b248d2bad8d1ba1408975fadd876a33c
4
- data.tar.gz: c3e1e098dc558a7ea542fcbf76a461a4bc4cdc9b697bd586b123996a1eacf4ab
3
+ metadata.gz: de11d9f613bff6a60e8a0171b12d6ac314896effa2b2ff9b71bb4ebb3fe4acfa
4
+ data.tar.gz: 791ac942326d80198704a456fca3942d8d967530d283e01691bf4203f8a28bea
5
5
  SHA512:
6
- metadata.gz: ffb5039ba8e0f0615d872a824ee8a889ac87754fb4a9d6a2ef8a7aebd79bc0eb38af1aa2b4ab225cf23530304e23c475e13f608f267e7ea07b127e9d42b50904
7
- data.tar.gz: f92a243d49cdb52bd538afffeba1688451f1c57968278ea4cd0623ae4af46700128d57f8f0cb3b86c50a815b88d0757bb4610edf60502c64c5f554feb349a8ca
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Luhn
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
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 = num > 9 ? num - 9 : 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.2.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 00:00:00.000000000 Z
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: