Skip to content

Commit

Permalink
feat: added imei validator
Browse files Browse the repository at this point in the history
  • Loading branch information
mastermunj committed Apr 27, 2020
1 parent 0ad8567 commit 76f6c16
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
44 changes: 44 additions & 0 deletions __tests__/luhn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Luhn } from '../src/luhn';

describe('Luhn', () => {
const tests: [string, boolean][] = [
['378282246310005', true],
['371449635398431', true],
['378734493671000', true],
['5610591081018250', true],
['30569309025904', true],
['38520000023237', true],
['6011111111111117', true],
['6011000990139424', true],
['3530111333300000', true],
['3566002020360505', true],
['5555555555554444', true],
['5105105105105100', true],
['4111111111111111', true],
['4012888888881881', true],
['4222222222222', true],
['5019717010103742', true],
['6331101999990016', true],

['378282246310006', false],
['371449635398432', false],
['378734493671001', false],
['5610591081018251', false],
['30569309025905', false],
['38520000023238', false],
['6011111111111118', false],
['6011000990139425', false],
['3530111333300001', false],
['3566002020360506', false],
['5555555555554445', false],
['5105105105105101', false],
['4111111111111112', false],
['4012888888881882', false],
['4222222222223', false],
['5019717010103743', false],
['6331101999990017', false],
];
test.each(tests)(`Check %s => %s`, (value, expected) => {
expect(Luhn.validate(value)).toBe(expected);
});
});
18 changes: 18 additions & 0 deletions __tests__/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,21 @@ describe('ESIC', () => {
expect(Validator.esic(value)).toBe(expected);
});
});

describe('IMEI', () => {
const tests: [string, boolean][] = [
['49-015420-323751-8', true],
['490154203237518', true],
['356938035643809', true],

['49-015420-323751-9', false],
['490154203237519', false],
['4901542032375191', false],
['49015420323751', false],
['a49015420323751', false],
['49015420323751a', false],
];
test.each(tests)(`Check %s => %s`, (value, expected) => {
expect(Validator.imei(value)).toBe(expected);
});
});
16 changes: 16 additions & 0 deletions src/luhn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export class Luhn {
static validate(value: string): boolean {
const digits = [...value.replace(/\s/g, '')].reverse();

const sum = digits
.map((digit: number | string, index) => {
digit = parseInt(digit as string, 10) * (index % 2 !== 0 ? 2 : 1);
return digit > 9 ? digit - 9 : digit;
})
.reduce((prev, current) => {
return prev + current;
});

return sum > 0 && sum % 10 === 0;
}
}
7 changes: 7 additions & 0 deletions src/validator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Luhn } from './luhn';

export class Validator {
static mobile(value: string): boolean {
return /^[6789]\d{9}$/.test(value);
Expand Down Expand Up @@ -26,4 +28,9 @@ export class Validator {
static esic(value: string): boolean {
return /^\d{17}$/.test(value);
}

static imei(value: string): boolean {
value = value.replace(/[\s-]+/g, '');
return /^\d{15}$/.test(value) && Luhn.validate(value);
}
}

0 comments on commit 76f6c16

Please sign in to comment.