Skip to content

Commit

Permalink
add iso hybrid system
Browse files Browse the repository at this point in the history
  • Loading branch information
helloyou2012 committed Mar 17, 2021
1 parent cc6d2b3 commit 2ea8a21
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gb-util",
"version": "1.0.1",
"version": "1.0.2",
"description": "GB standard tool library",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
30 changes: 25 additions & 5 deletions src/ISO_7064.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,46 @@ export function computePure(
}
return alphabet[c];
}
// from: https://github.com/LiosK/cdigit
export function computeHybrid(ds: string, alphabet: string): string {
const mod = alphabet.length;
const charmap = getCharMap(alphabet);

let c = mod;
for (let i = 0, len = ds.length; i < len; i += 1) {
c = (c % (mod + 1)) + charmap[ds[i]];
c = (c % mod || mod) * 2;
}
c %= mod + 1;

return alphabet[(mod + 1 - c) % mod];
}

const N = '0123456789';
const A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

// Pure systems
export function mod11_2(num: string): string {
return computePure(num, 11, 2, false, N + 'X');
}

export function mod37_2(num: string): string {
return computePure(num, 37, 2, false, N + A + '*');
}

export function mod97_10(num: string): string {
return computePure(num, 97, 10, true, N);
}

export function mod661_26(num: string): string {
return computePure(num, 661, 26, true, A);
}

export function mod1271_36(num: string): string {
return computePure(num, 1271, 36, true, N + A);
}
// Hybrid systems
export function mod11_10(ds: string): string {
return computeHybrid(ds, N);
}
export function mod27_26(ds: string): string {
return computeHybrid(ds, A);
}
export function mod37_36(ds: string): string {
return computeHybrid(ds, N + A);
}
10 changes: 10 additions & 0 deletions src/__tests__/ISO_7064.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ test('mod1271_36', () => {
expect(alg.mod1271_36('ISO79')).toBe('3W');
expect(alg.mod1271_36('ZZZZZZZZZZZZZZZZ')).toBe('UU');
});

test('mod11_10', () => {
expect(alg.mod11_10('0794')).toBe('5');
});
test('mod27_26', () => {
expect(alg.mod27_26('JEJLMGJ')).toBe('S');
});
test('mod37_36', () => {
expect(alg.mod37_36('B7Q3SFTUSH2QN7BIXBPMNZAM')).toBe('I');
});

0 comments on commit 2ea8a21

Please sign in to comment.