@jrrembert/luhnjs 0.0.1-rc1
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.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -0
- package/dist/src/luhn.d.ts +20 -0
- package/dist/src/luhn.js +77 -0
- package/package.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 J. Ryan Rembert
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# luhnjs
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { generate, validate } from './src/luhn';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validate = exports.generate = void 0;
|
|
4
|
+
var luhn_1 = require("./src/luhn");
|
|
5
|
+
Object.defineProperty(exports, "generate", { enumerable: true, get: function () { return luhn_1.generate; } });
|
|
6
|
+
Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return luhn_1.validate; } });
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
declare class GenerateOptions {
|
|
2
|
+
checkSumOnly: boolean;
|
|
3
|
+
constructor();
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Return value with checksum calculated using Luhn algorithm
|
|
7
|
+
*
|
|
8
|
+
* @param value value to check
|
|
9
|
+
* @param options.checkSumOnly determine if value + checksum, or only checksum should be returned
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export declare function generate(value: string, options?: GenerateOptions): string;
|
|
13
|
+
/**
|
|
14
|
+
* Determine if the Luhn checksum for a given number is correct
|
|
15
|
+
*
|
|
16
|
+
* @param value number containing a Luhn checksum
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
export declare function validate(value: string): boolean;
|
|
20
|
+
export {};
|
package/dist/src/luhn.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validate = exports.generate = void 0;
|
|
4
|
+
class GenerateOptions {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.checkSumOnly = false;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Verify string is correct length and can be converted to a number
|
|
11
|
+
*
|
|
12
|
+
* @param value potential value
|
|
13
|
+
*/
|
|
14
|
+
function handleErrors(value) {
|
|
15
|
+
if (!value.length) {
|
|
16
|
+
throw new Error('string cannot be empty');
|
|
17
|
+
}
|
|
18
|
+
if (isNaN(+value)) {
|
|
19
|
+
throw new Error('string must be convertible to a number');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Generate a checksum using Luhn algorithm
|
|
24
|
+
*
|
|
25
|
+
* @param value value to check
|
|
26
|
+
*/
|
|
27
|
+
function generateCheckSum(value) {
|
|
28
|
+
// convert to array
|
|
29
|
+
const toArray = Array.from(value);
|
|
30
|
+
// if double is `true`, multiply digit by 2
|
|
31
|
+
let double = true;
|
|
32
|
+
// starting from right, iterate through each value multiplying every 2nd digit by 2
|
|
33
|
+
const sum = toArray.reduceRight((prev, current) => {
|
|
34
|
+
if (double) {
|
|
35
|
+
double = false;
|
|
36
|
+
const temp = parseInt(current) * 2;
|
|
37
|
+
// if value is greater than or equal to 10, sum each digit of value ie. if value is 15, use 1 + 5 to get value
|
|
38
|
+
if (temp >= 10) {
|
|
39
|
+
return prev + Array.from(temp.toString()).reduce((prev, current) => {
|
|
40
|
+
return prev + parseInt(current);
|
|
41
|
+
}, 0);
|
|
42
|
+
}
|
|
43
|
+
return prev + parseInt(current) * 2;
|
|
44
|
+
}
|
|
45
|
+
double = true;
|
|
46
|
+
return prev + parseInt(current);
|
|
47
|
+
}, 0);
|
|
48
|
+
return (10 - (sum % 10)) % 10;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Return value with checksum calculated using Luhn algorithm
|
|
52
|
+
*
|
|
53
|
+
* @param value value to check
|
|
54
|
+
* @param options.checkSumOnly determine if value + checksum, or only checksum should be returned
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
function generate(value, options) {
|
|
58
|
+
handleErrors(value);
|
|
59
|
+
const checkSum = generateCheckSum(value).toString();
|
|
60
|
+
return (options === null || options === void 0 ? void 0 : options.checkSumOnly) ? checkSum : value.concat(checkSum);
|
|
61
|
+
}
|
|
62
|
+
exports.generate = generate;
|
|
63
|
+
/**
|
|
64
|
+
* Determine if the Luhn checksum for a given number is correct
|
|
65
|
+
*
|
|
66
|
+
* @param value number containing a Luhn checksum
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
function validate(value) {
|
|
70
|
+
handleErrors(value);
|
|
71
|
+
if (value.length === 1) {
|
|
72
|
+
throw new Error('string must be longer than 1 character');
|
|
73
|
+
}
|
|
74
|
+
const valueWithoutCheckSum = value.substring(0, value.length - 1);
|
|
75
|
+
return value === generate(valueWithoutCheckSum);
|
|
76
|
+
}
|
|
77
|
+
exports.validate = validate;
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jrrembert/luhnjs",
|
|
3
|
+
"version": "0.0.1-rc1",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"/dist"
|
|
8
|
+
],
|
|
9
|
+
"repository": "https://github.com/jrrembert/luhnjs.git",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"private": false,
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc && node dist/index.js",
|
|
14
|
+
"lint": "eslint .",
|
|
15
|
+
"test": "jest"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/jest": "^29.2.3",
|
|
19
|
+
"@typescript-eslint/eslint-plugin": "^5.44.0",
|
|
20
|
+
"@typescript-eslint/parser": "^5.44.0",
|
|
21
|
+
"eslint": "^8.28.0",
|
|
22
|
+
"jest": "^29.3.1",
|
|
23
|
+
"ts-jest": "^29.0.3",
|
|
24
|
+
"ts-node": "^10.9.1",
|
|
25
|
+
"typescript": "^4.9.3"
|
|
26
|
+
}
|
|
27
|
+
}
|