-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Luhn Checksum Operation Added #965
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @author n1073645 [[email protected]] | ||
* @copyright Crown Copyright 2020 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation.mjs"; | ||
import OperationError from "../errors/OperationError.mjs"; | ||
|
||
/** | ||
* Luhn Checksum operation | ||
*/ | ||
class LuhnChecksum extends Operation { | ||
|
||
/** | ||
* LuhnChecksum constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "Luhn Checksum"; | ||
this.module = "Crypto"; | ||
this.description = "The Luhn algorithm, also known as the modulus 10 or mod 10 algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, Canadian Social Insurance Numbers."; | ||
this.infoURL = "https://wikipedia.org/wiki/Luhn_algorithm"; | ||
this.inputType = "string"; | ||
this.outputType = "number"; | ||
this.args = []; | ||
} | ||
|
||
/** | ||
* @param {string} input | ||
* @param {Object[]} args | ||
* @returns {number} | ||
*/ | ||
run(input, args) { | ||
let even = false; | ||
return input.split("").reverse().reduce((acc, elem) => { | ||
|
||
// Convert element to integer. | ||
let temp = parseInt(elem, 10); | ||
|
||
// If element is not an integer. | ||
if (isNaN(temp)) | ||
throw new OperationError("Character: " + elem + " is not a digit."); | ||
|
||
// If element is in an even position | ||
if (even) { | ||
|
||
// Double the element and add the quotient and remainder together. | ||
temp = 2 * elem; | ||
temp = Math.floor(temp/10) + (temp % 10); | ||
} | ||
|
||
even = !even; | ||
return acc + temp; | ||
|
||
}, 0) % 10; | ||
} | ||
|
||
} | ||
|
||
export default LuhnChecksum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* From Decimal tests | ||
* | ||
* @author n1073645 [[email protected]] | ||
* @copyright Crown Copyright 2020 | ||
* @licence Apache-2.0 | ||
*/ | ||
import TestRegister from "../../lib/TestRegister.mjs"; | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "Luhn Checksum on standard data", | ||
input: "35641709012469", | ||
expectedOutput: "7", | ||
recipeConfig: [ | ||
{ | ||
op: "Luhn Checksum", | ||
args: [] | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "Luhn Checksum on invalid data", | ||
input: "35641709b012469", | ||
expectedOutput: "Character: b is not a digit.", | ||
recipeConfig: [ | ||
{ | ||
op: "Luhn Checksum", | ||
args: [] | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "Luhn Checksum on empty data", | ||
input: "", | ||
expectedOutput: "0", | ||
recipeConfig: [ | ||
{ | ||
op: "Luhn Checksum", | ||
args: [] | ||
}, | ||
], | ||
} | ||
]); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to add this to the crypto module since it has no imports. The modules are generally for operations that are likely to import large libraries. I'd just throw this into Default since it's so small. No need to update this PR though, I'll do it myself as I merge it.