Comparing version 2.1.0 to 2.1.1
@@ -28,10 +28,12 @@ /** | ||
} | ||
declare type CharMap = { | ||
input: { | ||
[key: string]: number; | ||
}; | ||
output: string; | ||
}; | ||
export declare const helper: { | ||
parseTail: (num: string, n: number) => [string, string]; | ||
_invCharListMemo: { | ||
[alphabet: string]: { | ||
[character: string]: number; | ||
}; | ||
}; | ||
invertCharList: (alphabet: string) => { | ||
[character: string]: number; | ||
}; | ||
iso7064: { | ||
@@ -41,9 +43,7 @@ numeric: string; | ||
alphanumeric: string; | ||
compileCharMap: (alphabet: string) => CharMap; | ||
/** Implement ISO 7064 pure system recursive method. */ | ||
computePure: (num: string, mod: number, radix: number, hasTwoCCs: boolean, { output, input }: CharMap) => string; | ||
computePure: (num: string, mod: number, radix: number, hasTwoCCs: boolean, alphabet: string) => string; | ||
/** Implement ISO 7064 hybrid system recursive method. */ | ||
computeHybrid: (ds: string, { output, input }: CharMap) => string; | ||
computeHybrid: (ds: string, alphabet: string) => string; | ||
}; | ||
}; | ||
export {}; |
@@ -14,2 +14,12 @@ "use strict"; | ||
}, | ||
_invCharListMemo: {}, | ||
invertCharList: (alphabet) => { | ||
if (exports.helper._invCharListMemo[alphabet] == null) { | ||
exports.helper._invCharListMemo[alphabet] = {}; | ||
for (let i = 0, len = alphabet.length; i < len; i += 1) { | ||
exports.helper._invCharListMemo[alphabet][alphabet[i]] = i; | ||
} | ||
} | ||
return exports.helper._invCharListMemo[alphabet]; | ||
}, | ||
iso7064: { | ||
@@ -19,16 +29,10 @@ numeric: '0123456789X', | ||
alphanumeric: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*', | ||
compileCharMap: (alphabet) => { | ||
const inv = {}; | ||
for (let i = 0, len = alphabet.length; i < len; i += 1) { | ||
inv[alphabet[i]] = i; | ||
} | ||
return { input: inv, output: alphabet }; | ||
}, | ||
/** Implement ISO 7064 pure system recursive method. */ | ||
computePure: (num, mod, radix, hasTwoCCs, { output, input }) => { | ||
const ds = `${num}${output[0].repeat(hasTwoCCs ? 2 : 1)}`; | ||
computePure: (num, mod, radix, hasTwoCCs, alphabet) => { | ||
const ds = `${num}${alphabet[0].repeat(hasTwoCCs ? 2 : 1)}`; | ||
const overflowProtection = Math.floor(0xffffffffffff / radix); | ||
const charmap = exports.helper.invertCharList(alphabet); | ||
let c = 0; | ||
for (let i = 0, len = ds.length; i < len; i += 1) { | ||
c = (c * radix) + input[ds[i]]; | ||
c = (c * radix) + charmap[ds[i]]; | ||
if (c > overflowProtection) { | ||
@@ -40,18 +44,19 @@ c %= mod; | ||
if (hasTwoCCs) { | ||
return `${output[Math.floor(c / radix)]}${output[c % radix]}`; | ||
return `${alphabet[Math.floor(c / radix)]}${alphabet[c % radix]}`; | ||
} | ||
return output[c]; | ||
return alphabet[c]; | ||
}, | ||
/** Implement ISO 7064 hybrid system recursive method. */ | ||
computeHybrid: (ds, { output, input }) => { | ||
const mod = output.length; | ||
computeHybrid: (ds, alphabet) => { | ||
const mod = alphabet.length; | ||
const charmap = exports.helper.invertCharList(alphabet); | ||
let c = mod; | ||
for (let i = 0, len = ds.length; i < len; i += 1) { | ||
c = (c % (mod + 1)) + input[ds[i]]; | ||
c = (c % (mod + 1)) + charmap[ds[i]]; | ||
c = (c % mod || mod) * 2; | ||
} | ||
c %= mod + 1; | ||
return output[(mod + 1 - c) % mod]; | ||
return alphabet[(mod + 1 - c) % mod]; | ||
}, | ||
}, | ||
}; |
@@ -8,3 +8,6 @@ /** | ||
import { Algo } from './common'; | ||
/** Damm algorithm implementation */ | ||
declare class Damm implements Algo { | ||
/** Damm operation table */ | ||
private opTable; | ||
compute(num: string): string; | ||
@@ -11,0 +14,0 @@ generate(num: string): string; |
@@ -10,16 +10,19 @@ "use strict"; | ||
const common_1 = require("./common"); | ||
/** Damm operation table */ | ||
const opTable = [ | ||
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2], | ||
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3], | ||
[4, 2, 0, 6, 8, 7, 1, 3, 5, 9], | ||
[1, 7, 5, 0, 9, 8, 3, 4, 2, 6], | ||
[6, 1, 2, 3, 0, 4, 5, 9, 7, 8], | ||
[3, 6, 7, 4, 2, 0, 9, 5, 8, 1], | ||
[5, 8, 6, 9, 7, 2, 0, 1, 3, 4], | ||
[8, 9, 4, 5, 3, 6, 2, 0, 1, 7], | ||
[9, 4, 3, 8, 6, 1, 7, 2, 0, 5], | ||
[2, 5, 8, 1, 4, 3, 6, 7, 9, 0], | ||
]; | ||
/** Damm algorithm implementation */ | ||
class Damm { | ||
constructor() { | ||
/** Damm operation table */ | ||
this.opTable = [ | ||
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2], | ||
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3], | ||
[4, 2, 0, 6, 8, 7, 1, 3, 5, 9], | ||
[1, 7, 5, 0, 9, 8, 3, 4, 2, 6], | ||
[6, 1, 2, 3, 0, 4, 5, 9, 7, 8], | ||
[3, 6, 7, 4, 2, 0, 9, 5, 8, 1], | ||
[5, 8, 6, 9, 7, 2, 0, 1, 3, 4], | ||
[8, 9, 4, 5, 3, 6, 2, 0, 1, 7], | ||
[9, 4, 3, 8, 6, 1, 7, 2, 0, 5], | ||
[2, 5, 8, 1, 4, 3, 6, 7, 9, 0], | ||
]; | ||
} | ||
compute(num) { | ||
@@ -29,3 +32,3 @@ const ds = `${String(num).replace(/[^0-9]/g, '')}`; | ||
for (let i = 0, len = ds.length; i < len; i += 1) { | ||
c = opTable[c][Number(ds[i])]; | ||
c = this.opTable[c][Number(ds[i])]; | ||
} | ||
@@ -32,0 +35,0 @@ return String(c); |
@@ -8,2 +8,3 @@ /** | ||
import { Algo } from './common'; | ||
/** Luhn algorithm implementation */ | ||
declare class Luhn implements Algo { | ||
@@ -10,0 +11,0 @@ compute(num: string): string; |
@@ -10,12 +10,13 @@ "use strict"; | ||
const common_1 = require("./common"); | ||
const oddLookup = { | ||
0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 1, 6: 3, 7: 5, 8: 7, 9: 9, | ||
}; | ||
/** Luhn algorithm implementation */ | ||
class Luhn { | ||
compute(num) { | ||
const ds = String(num).replace(/[^0-9]/g, ''); | ||
const lookup = { | ||
0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 1, 6: 3, 7: 5, 8: 7, 9: 9, | ||
}; | ||
let sum = 0; | ||
let odd = 1; | ||
for (let i = ds.length - 1; i > -1; i -= 1) { | ||
sum += odd ? oddLookup[ds[i]] : Number(ds[i]); | ||
sum += odd ? lookup[ds[i]] : Number(ds[i]); | ||
odd ^= 1; | ||
@@ -22,0 +23,0 @@ if (sum > 0xffffffffffff) { // ~2^48 at max |
@@ -8,3 +8,5 @@ /** | ||
import { Algo } from './common'; | ||
/** ISO/IEC 7064, MOD 11-10 implementation */ | ||
declare class Mod11_10 implements Algo { | ||
private alphabet; | ||
compute(num: string): string; | ||
@@ -11,0 +13,0 @@ generate(num: string): string; |
@@ -10,7 +10,10 @@ "use strict"; | ||
const common_1 = require("./common"); | ||
const charmap = common_1.helper.iso7064.compileCharMap(common_1.helper.iso7064.numeric.slice(0, -1)); | ||
/** ISO/IEC 7064, MOD 11-10 implementation */ | ||
class Mod11_10 { | ||
constructor() { | ||
this.alphabet = common_1.helper.iso7064.numeric.slice(0, -1); | ||
} | ||
compute(num) { | ||
const ds = String(num).replace(/[^0-9]/g, ''); | ||
return common_1.helper.iso7064.computeHybrid(ds, charmap); | ||
return common_1.helper.iso7064.computeHybrid(ds, this.alphabet); | ||
} | ||
@@ -17,0 +20,0 @@ generate(num) { |
@@ -8,3 +8,5 @@ /** | ||
import { Algo } from './common'; | ||
/** ISO/IEC 7064, MOD 11-2 implementation */ | ||
declare class Mod11_2 implements Algo { | ||
private alphabet; | ||
compute(num: string): string; | ||
@@ -11,0 +13,0 @@ generate(num: string): string; |
@@ -10,7 +10,10 @@ "use strict"; | ||
const common_1 = require("./common"); | ||
const charmap = common_1.helper.iso7064.compileCharMap(common_1.helper.iso7064.numeric); | ||
/** ISO/IEC 7064, MOD 11-2 implementation */ | ||
class Mod11_2 { | ||
constructor() { | ||
this.alphabet = common_1.helper.iso7064.numeric; | ||
} | ||
compute(num) { | ||
const ds = String(num).replace(/[^0-9]/g, ''); | ||
return common_1.helper.iso7064.computePure(ds, 11, 2, false, charmap); | ||
return common_1.helper.iso7064.computePure(ds, 11, 2, false, this.alphabet); | ||
} | ||
@@ -17,0 +20,0 @@ generate(num) { |
@@ -8,3 +8,5 @@ /** | ||
import { Algo } from './common'; | ||
/** ISO/IEC 7064, MOD 1271-36 implementation */ | ||
declare class Mod1271_36 implements Algo { | ||
private alphabet; | ||
compute(num: string): string; | ||
@@ -11,0 +13,0 @@ generate(num: string): string; |
@@ -10,7 +10,10 @@ "use strict"; | ||
const common_1 = require("./common"); | ||
const charmap = common_1.helper.iso7064.compileCharMap(common_1.helper.iso7064.alphanumeric); | ||
/** ISO/IEC 7064, MOD 1271-36 implementation */ | ||
class Mod1271_36 { | ||
constructor() { | ||
this.alphabet = common_1.helper.iso7064.alphanumeric; | ||
} | ||
compute(num) { | ||
const ds = String(num).replace(/[^0-9A-Z]/g, ''); | ||
return common_1.helper.iso7064.computePure(ds, 1271, 36, true, charmap); | ||
return common_1.helper.iso7064.computePure(ds, 1271, 36, true, this.alphabet); | ||
} | ||
@@ -17,0 +20,0 @@ generate(num) { |
@@ -8,3 +8,5 @@ /** | ||
import { Algo } from './common'; | ||
/** ISO/IEC 7064, MOD 27-26 implementation */ | ||
declare class Mod27_26 implements Algo { | ||
private alphabet; | ||
compute(num: string): string; | ||
@@ -11,0 +13,0 @@ generate(num: string): string; |
@@ -10,7 +10,10 @@ "use strict"; | ||
const common_1 = require("./common"); | ||
const charmap = common_1.helper.iso7064.compileCharMap(common_1.helper.iso7064.alphabetic); | ||
/** ISO/IEC 7064, MOD 27-26 implementation */ | ||
class Mod27_26 { | ||
constructor() { | ||
this.alphabet = common_1.helper.iso7064.alphabetic; | ||
} | ||
compute(num) { | ||
const ds = String(num).replace(/[^A-Z]/g, ''); | ||
return common_1.helper.iso7064.computeHybrid(ds, charmap); | ||
return common_1.helper.iso7064.computeHybrid(ds, this.alphabet); | ||
} | ||
@@ -17,0 +20,0 @@ generate(num) { |
@@ -8,3 +8,5 @@ /** | ||
import { Algo } from './common'; | ||
/** ISO/IEC 7064, MOD 37-2 implementation */ | ||
declare class Mod37_2 implements Algo { | ||
private alphabet; | ||
compute(num: string): string; | ||
@@ -11,0 +13,0 @@ generate(num: string): string; |
@@ -10,7 +10,10 @@ "use strict"; | ||
const common_1 = require("./common"); | ||
const charmap = common_1.helper.iso7064.compileCharMap(common_1.helper.iso7064.alphanumeric); | ||
/** ISO/IEC 7064, MOD 37-2 implementation */ | ||
class Mod37_2 { | ||
constructor() { | ||
this.alphabet = common_1.helper.iso7064.alphanumeric; | ||
} | ||
compute(num) { | ||
const ds = String(num).replace(/[^0-9A-Z]/g, ''); | ||
return common_1.helper.iso7064.computePure(ds, 37, 2, false, charmap); | ||
return common_1.helper.iso7064.computePure(ds, 37, 2, false, this.alphabet); | ||
} | ||
@@ -17,0 +20,0 @@ generate(num) { |
@@ -8,3 +8,5 @@ /** | ||
import { Algo } from './common'; | ||
/** ISO/IEC 7064, MOD 37-36 implementation */ | ||
declare class Mod37_36 implements Algo { | ||
private alphabet; | ||
compute(num: string): string; | ||
@@ -11,0 +13,0 @@ generate(num: string): string; |
@@ -10,7 +10,10 @@ "use strict"; | ||
const common_1 = require("./common"); | ||
const charmap = common_1.helper.iso7064.compileCharMap(common_1.helper.iso7064.alphanumeric.slice(0, -1)); | ||
/** ISO/IEC 7064, MOD 37-36 implementation */ | ||
class Mod37_36 { | ||
constructor() { | ||
this.alphabet = common_1.helper.iso7064.alphanumeric.slice(0, -1); | ||
} | ||
compute(num) { | ||
const ds = String(num).replace(/[^0-9A-Z]/g, ''); | ||
return common_1.helper.iso7064.computeHybrid(ds, charmap); | ||
return common_1.helper.iso7064.computeHybrid(ds, this.alphabet); | ||
} | ||
@@ -17,0 +20,0 @@ generate(num) { |
@@ -8,3 +8,5 @@ /** | ||
import { Algo } from './common'; | ||
/** ISO/IEC 7064, MOD 661-26 implementation */ | ||
declare class Mod661_26 implements Algo { | ||
private alphabet; | ||
compute(num: string): string; | ||
@@ -11,0 +13,0 @@ generate(num: string): string; |
@@ -10,7 +10,10 @@ "use strict"; | ||
const common_1 = require("./common"); | ||
const charmap = common_1.helper.iso7064.compileCharMap(common_1.helper.iso7064.alphabetic); | ||
/** ISO/IEC 7064, MOD 661-26 implementation */ | ||
class Mod661_26 { | ||
constructor() { | ||
this.alphabet = common_1.helper.iso7064.alphabetic; | ||
} | ||
compute(num) { | ||
const ds = String(num).replace(/[^A-Z]/g, ''); | ||
return common_1.helper.iso7064.computePure(ds, 661, 26, true, charmap); | ||
return common_1.helper.iso7064.computePure(ds, 661, 26, true, this.alphabet); | ||
} | ||
@@ -17,0 +20,0 @@ generate(num) { |
@@ -8,2 +8,3 @@ /** | ||
import { Algo } from './common'; | ||
/** ISO/IEC 7064, MOD 97-10 implementation */ | ||
declare class Mod97_10 implements Algo { | ||
@@ -10,0 +11,0 @@ compute(num: string): string; |
@@ -10,2 +10,3 @@ "use strict"; | ||
const common_1 = require("./common"); | ||
/** ISO/IEC 7064, MOD 97-10 implementation */ | ||
class Mod97_10 { | ||
@@ -12,0 +13,0 @@ compute(num) { |
@@ -9,2 +9,4 @@ /** | ||
/** | ||
* Verhoeff algorithm implementation | ||
* | ||
* Note: There is not a firm consensus on the direction (left to right or right | ||
@@ -19,2 +21,8 @@ * to left) in which a Verhoeff calculator scans numeric text to construct an | ||
declare class Verhoeff implements Algo { | ||
/** Verhoeff multiplication table */ | ||
private d; | ||
/** Verhoeff permutation table */ | ||
private p; | ||
/** Verhoeff inverse table */ | ||
private inv; | ||
compute(num: string): string; | ||
@@ -21,0 +29,0 @@ generate(num: string): string; |
@@ -10,29 +10,5 @@ "use strict"; | ||
const common_1 = require("./common"); | ||
/** Verhoeff multiplication table */ | ||
const d = [ | ||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5], | ||
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6], | ||
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7], | ||
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8], | ||
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1], | ||
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2], | ||
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3], | ||
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4], | ||
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0], | ||
]; | ||
/** Verhoeff permutation table */ | ||
const p = [ | ||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4], | ||
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2], | ||
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7], | ||
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0], | ||
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1], | ||
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5], | ||
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8], | ||
]; | ||
/** Verhoeff inverse table */ | ||
const inv = ['0', '4', '3', '2', '1', '5', '6', '7', '8', '9']; | ||
/** | ||
* Verhoeff algorithm implementation | ||
* | ||
* Note: There is not a firm consensus on the direction (left to right or right | ||
@@ -47,2 +23,30 @@ * to left) in which a Verhoeff calculator scans numeric text to construct an | ||
class Verhoeff { | ||
constructor() { | ||
/** Verhoeff multiplication table */ | ||
this.d = [ | ||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5], | ||
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6], | ||
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7], | ||
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8], | ||
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1], | ||
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2], | ||
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3], | ||
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4], | ||
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0], | ||
]; | ||
/** Verhoeff permutation table */ | ||
this.p = [ | ||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4], | ||
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2], | ||
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7], | ||
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0], | ||
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1], | ||
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5], | ||
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8], | ||
]; | ||
/** Verhoeff inverse table */ | ||
this.inv = ['0', '4', '3', '2', '1', '5', '6', '7', '8', '9']; | ||
} | ||
compute(num) { | ||
@@ -52,5 +56,5 @@ const ds = `${String(num).replace(/[^0-9]/g, '')}0`; | ||
for (let i = 0, len = ds.length; i < len; i += 1) { | ||
c = d[c][p[i & 7][Number(ds[len - i - 1])]]; | ||
c = this.d[c][this.p[i & 7][Number(ds[len - i - 1])]]; | ||
} | ||
return inv[c]; | ||
return this.inv[c]; | ||
} | ||
@@ -57,0 +61,0 @@ generate(num) { |
{ | ||
"name": "cdigit", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Collection of check digit algorithms implemented in JavaScript", | ||
@@ -11,3 +11,4 @@ "main": "lib/index.js", | ||
"test": "mocha", | ||
"tslint": "tslint -p package.json" | ||
"tslint": "tslint -p package.json", | ||
"typedoc": "typedoc --out docs --mode file" | ||
}, | ||
@@ -45,2 +46,3 @@ "repository": { | ||
"directories": { | ||
"doc": "docs", | ||
"lib": "lib", | ||
@@ -56,4 +58,5 @@ "test": "test" | ||
"tsutils": "^3.5.0", | ||
"typedoc": "^0.13.0", | ||
"typescript": "^3.1.6" | ||
} | ||
} |
@@ -104,3 +104,4 @@ # NAME | ||
* [GitHub Repository](https://github.com/LiosK/cdigit) | ||
* [npm Package](https://www.npmjs.com/package/cdigit) | ||
* [GitHub repository](https://github.com/LiosK/cdigit) | ||
* [npm package](https://www.npmjs.com/package/cdigit) | ||
* [typedoc-generated documentation (experimental)](https://liosk.github.io/cdigit/) |
@@ -33,4 +33,2 @@ /** | ||
type CharMap = { input: {[key: string]: number}, output: string }; | ||
export const helper = { | ||
@@ -41,2 +39,12 @@ parseTail: (num: string, n: number): [string, string] => { | ||
}, | ||
_invCharListMemo: {} as {[alphabet: string]: {[character: string]: number}}, | ||
invertCharList: (alphabet: string): {[character: string]: number} => { | ||
if (helper._invCharListMemo[alphabet] == null) { | ||
helper._invCharListMemo[alphabet] = {}; | ||
for (let i = 0, len = alphabet.length; i < len; i += 1) { | ||
helper._invCharListMemo[alphabet][alphabet[i]] = i; | ||
} | ||
} | ||
return helper._invCharListMemo[alphabet]; | ||
}, | ||
iso7064: { | ||
@@ -46,20 +54,13 @@ numeric: '0123456789X', | ||
alphanumeric: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*', | ||
compileCharMap: (alphabet: string): CharMap => { | ||
const inv: {[key: string]: number} = {}; | ||
for (let i = 0, len = alphabet.length; i < len; i += 1) { | ||
inv[alphabet[i]] = i; | ||
} | ||
return { input: inv, output: alphabet }; | ||
}, | ||
/** Implement ISO 7064 pure system recursive method. */ | ||
computePure: ( | ||
num: string, mod: number, radix: number, | ||
hasTwoCCs: boolean, { output, input }: CharMap, | ||
num: string, mod: number, radix: number, hasTwoCCs: boolean, alphabet: string, | ||
) => { | ||
const ds = `${num}${output[0].repeat(hasTwoCCs ? 2 : 1)}`; | ||
const ds = `${num}${alphabet[0].repeat(hasTwoCCs ? 2 : 1)}`; | ||
const overflowProtection = Math.floor(0xffffffffffff / radix); | ||
const charmap = helper.invertCharList(alphabet); | ||
let c = 0; | ||
for (let i = 0, len = ds.length; i < len; i += 1) { | ||
c = (c * radix) + input[ds[i]]; | ||
c = (c * radix) + charmap[ds[i]]; | ||
if (c > overflowProtection) { | ||
@@ -72,13 +73,14 @@ c %= mod; | ||
if (hasTwoCCs) { | ||
return `${output[Math.floor(c / radix)]}${output[c % radix]}`; | ||
return `${alphabet[Math.floor(c / radix)]}${alphabet[c % radix]}`; | ||
} | ||
return output[c]; | ||
return alphabet[c]; | ||
}, | ||
/** Implement ISO 7064 hybrid system recursive method. */ | ||
computeHybrid: (ds: string, { output, input }: CharMap) => { | ||
const mod = output.length; | ||
computeHybrid: (ds: string, alphabet: string) => { | ||
const mod = alphabet.length; | ||
const charmap = helper.invertCharList(alphabet); | ||
let c = mod; | ||
for (let i = 0, len = ds.length; i < len; i += 1) { | ||
c = (c % (mod + 1)) + input[ds[i]]; | ||
c = (c % (mod + 1)) + charmap[ds[i]]; | ||
c = (c % mod || mod) * 2; | ||
@@ -88,5 +90,5 @@ } | ||
return output[(mod + 1 - c) % mod]; | ||
return alphabet[(mod + 1 - c) % mod]; | ||
}, | ||
}, | ||
}; |
@@ -10,17 +10,18 @@ /** | ||
/** Damm operation table */ | ||
const opTable = [ | ||
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2], | ||
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3], | ||
[4, 2, 0, 6, 8, 7, 1, 3, 5, 9], | ||
[1, 7, 5, 0, 9, 8, 3, 4, 2, 6], | ||
[6, 1, 2, 3, 0, 4, 5, 9, 7, 8], | ||
[3, 6, 7, 4, 2, 0, 9, 5, 8, 1], | ||
[5, 8, 6, 9, 7, 2, 0, 1, 3, 4], | ||
[8, 9, 4, 5, 3, 6, 2, 0, 1, 7], | ||
[9, 4, 3, 8, 6, 1, 7, 2, 0, 5], | ||
[2, 5, 8, 1, 4, 3, 6, 7, 9, 0], | ||
]; | ||
/** Damm algorithm implementation */ | ||
class Damm implements Algo { | ||
/** Damm operation table */ | ||
private opTable = [ | ||
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2], | ||
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3], | ||
[4, 2, 0, 6, 8, 7, 1, 3, 5, 9], | ||
[1, 7, 5, 0, 9, 8, 3, 4, 2, 6], | ||
[6, 1, 2, 3, 0, 4, 5, 9, 7, 8], | ||
[3, 6, 7, 4, 2, 0, 9, 5, 8, 1], | ||
[5, 8, 6, 9, 7, 2, 0, 1, 3, 4], | ||
[8, 9, 4, 5, 3, 6, 2, 0, 1, 7], | ||
[9, 4, 3, 8, 6, 1, 7, 2, 0, 5], | ||
[2, 5, 8, 1, 4, 3, 6, 7, 9, 0], | ||
]; | ||
class Damm implements Algo { | ||
compute(num: string): string { | ||
@@ -31,3 +32,3 @@ const ds = `${String(num).replace(/[^0-9]/g, '')}`; | ||
for (let i = 0, len = ds.length; i < len; i += 1) { | ||
c = opTable[c][Number(ds[i])]; | ||
c = this.opTable[c][Number(ds[i])]; | ||
} | ||
@@ -34,0 +35,0 @@ |
@@ -10,9 +10,9 @@ /** | ||
const oddLookup: {[key: string]: number} = { | ||
0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 1, 6: 3, 7: 5, 8: 7, 9: 9, | ||
}; | ||
/** Luhn algorithm implementation */ | ||
class Luhn implements Algo { | ||
compute(num: string): string { | ||
const ds = String(num).replace(/[^0-9]/g, ''); | ||
const lookup: {[digit: string]: number} = { | ||
0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 1, 6: 3, 7: 5, 8: 7, 9: 9, | ||
}; | ||
@@ -22,3 +22,3 @@ let sum = 0; | ||
for (let i = ds.length - 1; i > -1; i -= 1) { | ||
sum += odd ? oddLookup[ds[i]] : Number(ds[i]); | ||
sum += odd ? lookup[ds[i]] : Number(ds[i]); | ||
odd ^= 1; | ||
@@ -25,0 +25,0 @@ if (sum > 0xffffffffffff) { // ~2^48 at max |
@@ -10,8 +10,9 @@ /** | ||
const charmap = helper.iso7064.compileCharMap(helper.iso7064.numeric.slice(0, -1)); | ||
/** ISO/IEC 7064, MOD 11-10 implementation */ | ||
class Mod11_10 implements Algo { | ||
private alphabet: string = helper.iso7064.numeric.slice(0, -1); | ||
class Mod11_10 implements Algo { | ||
compute(num: string): string { | ||
const ds = String(num).replace(/[^0-9]/g, ''); | ||
return helper.iso7064.computeHybrid(ds, charmap); | ||
return helper.iso7064.computeHybrid(ds, this.alphabet); | ||
} | ||
@@ -18,0 +19,0 @@ |
@@ -10,8 +10,9 @@ /** | ||
const charmap = helper.iso7064.compileCharMap(helper.iso7064.numeric); | ||
/** ISO/IEC 7064, MOD 11-2 implementation */ | ||
class Mod11_2 implements Algo { | ||
private alphabet: string = helper.iso7064.numeric; | ||
class Mod11_2 implements Algo { | ||
compute(num: string): string { | ||
const ds = String(num).replace(/[^0-9]/g, ''); | ||
return helper.iso7064.computePure(ds, 11, 2, false, charmap); | ||
return helper.iso7064.computePure(ds, 11, 2, false, this.alphabet); | ||
} | ||
@@ -18,0 +19,0 @@ |
@@ -10,8 +10,9 @@ /** | ||
const charmap = helper.iso7064.compileCharMap(helper.iso7064.alphanumeric); | ||
/** ISO/IEC 7064, MOD 1271-36 implementation */ | ||
class Mod1271_36 implements Algo { | ||
private alphabet: string = helper.iso7064.alphanumeric; | ||
class Mod1271_36 implements Algo { | ||
compute(num: string): string { | ||
const ds = String(num).replace(/[^0-9A-Z]/g, ''); | ||
return helper.iso7064.computePure(ds, 1271, 36, true, charmap); | ||
return helper.iso7064.computePure(ds, 1271, 36, true, this.alphabet); | ||
} | ||
@@ -18,0 +19,0 @@ |
@@ -10,8 +10,9 @@ /** | ||
const charmap = helper.iso7064.compileCharMap(helper.iso7064.alphabetic); | ||
/** ISO/IEC 7064, MOD 27-26 implementation */ | ||
class Mod27_26 implements Algo { | ||
private alphabet: string = helper.iso7064.alphabetic; | ||
class Mod27_26 implements Algo { | ||
compute(num: string): string { | ||
const ds = String(num).replace(/[^A-Z]/g, ''); | ||
return helper.iso7064.computeHybrid(ds, charmap); | ||
return helper.iso7064.computeHybrid(ds, this.alphabet); | ||
} | ||
@@ -18,0 +19,0 @@ |
@@ -10,8 +10,9 @@ /** | ||
const charmap = helper.iso7064.compileCharMap(helper.iso7064.alphanumeric); | ||
/** ISO/IEC 7064, MOD 37-2 implementation */ | ||
class Mod37_2 implements Algo { | ||
private alphabet: string = helper.iso7064.alphanumeric; | ||
class Mod37_2 implements Algo { | ||
compute(num: string): string { | ||
const ds = String(num).replace(/[^0-9A-Z]/g, ''); | ||
return helper.iso7064.computePure(ds, 37, 2, false, charmap); | ||
return helper.iso7064.computePure(ds, 37, 2, false, this.alphabet); | ||
} | ||
@@ -18,0 +19,0 @@ |
@@ -10,8 +10,9 @@ /** | ||
const charmap = helper.iso7064.compileCharMap(helper.iso7064.alphanumeric.slice(0, -1)); | ||
/** ISO/IEC 7064, MOD 37-36 implementation */ | ||
class Mod37_36 implements Algo { | ||
private alphabet: string = helper.iso7064.alphanumeric.slice(0, -1); | ||
class Mod37_36 implements Algo { | ||
compute(num: string): string { | ||
const ds = String(num).replace(/[^0-9A-Z]/g, ''); | ||
return helper.iso7064.computeHybrid(ds, charmap); | ||
return helper.iso7064.computeHybrid(ds, this.alphabet); | ||
} | ||
@@ -18,0 +19,0 @@ |
@@ -10,8 +10,9 @@ /** | ||
const charmap = helper.iso7064.compileCharMap(helper.iso7064.alphabetic); | ||
/** ISO/IEC 7064, MOD 661-26 implementation */ | ||
class Mod661_26 implements Algo { | ||
private alphabet: string = helper.iso7064.alphabetic; | ||
class Mod661_26 implements Algo { | ||
compute(num: string): string { | ||
const ds = String(num).replace(/[^A-Z]/g, ''); | ||
return helper.iso7064.computePure(ds, 661, 26, true, charmap); | ||
return helper.iso7064.computePure(ds, 661, 26, true, this.alphabet); | ||
} | ||
@@ -18,0 +19,0 @@ |
@@ -10,2 +10,3 @@ /** | ||
/** ISO/IEC 7064, MOD 97-10 implementation */ | ||
class Mod97_10 implements Algo { | ||
@@ -12,0 +13,0 @@ compute(num: string): string { |
@@ -10,32 +10,5 @@ /** | ||
/** Verhoeff multiplication table */ | ||
const d = [ | ||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5], | ||
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6], | ||
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7], | ||
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8], | ||
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1], | ||
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2], | ||
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3], | ||
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4], | ||
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0], | ||
]; | ||
/** Verhoeff permutation table */ | ||
const p = [ | ||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4], | ||
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2], | ||
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7], | ||
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0], | ||
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1], | ||
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5], | ||
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8], | ||
]; | ||
/** Verhoeff inverse table */ | ||
const inv = ['0', '4', '3', '2', '1', '5', '6', '7', '8', '9']; | ||
/** | ||
* Verhoeff algorithm implementation | ||
* | ||
* Note: There is not a firm consensus on the direction (left to right or right | ||
@@ -50,2 +23,31 @@ * to left) in which a Verhoeff calculator scans numeric text to construct an | ||
class Verhoeff implements Algo { | ||
/** Verhoeff multiplication table */ | ||
private d = [ | ||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5], | ||
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6], | ||
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7], | ||
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8], | ||
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1], | ||
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2], | ||
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3], | ||
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4], | ||
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0], | ||
]; | ||
/** Verhoeff permutation table */ | ||
private p = [ | ||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4], | ||
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2], | ||
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7], | ||
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0], | ||
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1], | ||
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5], | ||
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8], | ||
]; | ||
/** Verhoeff inverse table */ | ||
private inv = ['0', '4', '3', '2', '1', '5', '6', '7', '8', '9']; | ||
compute(num: string): string { | ||
@@ -56,6 +58,6 @@ const ds = `${String(num).replace(/[^0-9]/g, '')}0`; | ||
for (let i = 0, len = ds.length; i < len; i += 1) { | ||
c = d[c][p[i & 7][Number(ds[len - i - 1])]]; | ||
c = this.d[c][this.p[i & 7][Number(ds[len - i - 1])]]; | ||
} | ||
return inv[c]; | ||
return this.inv[c]; | ||
} | ||
@@ -62,0 +64,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
846797
79
7137
107
6
1
3