├── .gitignore ├── tests ├── config.nims └── test1.nim ├── luhncheck.nimble ├── README.md ├── src └── luhncheck.nim └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | nimcache/ 2 | -------------------------------------------------------------------------------- /tests/config.nims: -------------------------------------------------------------------------------- 1 | switch("path", "$projectDir/../src") -------------------------------------------------------------------------------- /tests/test1.nim: -------------------------------------------------------------------------------- 1 | import unittest 2 | import luhncheck 3 | 4 | test "can validate card checksums": 5 | check validate(356938035643809) == true 6 | check validate(534618613411236) == false 7 | -------------------------------------------------------------------------------- /luhncheck.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "0.1.0" 4 | author = "sillibird" 5 | description = "Implementation of Luhn algorithm in nim." 6 | license = "MIT" 7 | srcDir = "src" 8 | 9 | 10 | # Dependencies 11 | 12 | requires "nim >= 0.19.4" 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # luhncheck 2 | Implementation of Luhn algorithm in nim. 3 | 4 | ### Usage 5 | Install the package with `nimble` 6 | ```nim 7 | nimble install luhncheck 8 | ``` 9 | 10 | ### Example Usage 11 | Validating a cards checksum 12 | ```nim 13 | if validate(356938035643809): 14 | echo "Cards checksum appears to be valid" 15 | ``` 16 | 17 | License 18 | ---- 19 | 20 | MIT 21 | -------------------------------------------------------------------------------- /src/luhncheck.nim: -------------------------------------------------------------------------------- 1 | from strutils import parseInt 2 | from algorithm import reversed 3 | from math import floor 4 | 5 | proc validate*(number: int64): bool = 6 | let numberArray = reversed(toOpenArray($number, 0, len($number) - 1)) 7 | var oddSum, evenSum: int 8 | for i in countup(0, numberArray.len() - 1): 9 | if i mod 2 == 0: 10 | oddSum += parseInt($numberArray[i]) 11 | else: 12 | evenSum += toInt(floor((2 * parseInt($numberArray[i])) / 10)) + (2 * parseInt($numberArray[i]) mod 10) 13 | return (evenSum + oddSum) mod 10 == 0 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 SilliBird 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------