Skip to content

Commit

Permalink
feat(#9227): add validation function for luhn algorithm
Browse files Browse the repository at this point in the history
Co-authored-by: Anro Swart <[email protected]>
  • Loading branch information
ChinHairSaintClair and Anro Swart authored Jul 15, 2024
1 parent 1e9efb7 commit 3c2b140
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 2 deletions.
34 changes: 34 additions & 0 deletions webapp/src/js/enketo/medic-xpath-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const XPR = {
number: v => ({ t: 'num', v }),
string: v => ({ t: 'str', v }),
date: v => ({ t: 'date', v }),
bool: v => ({ t: 'bool', v}),
};
const MOMENT_KEYS = {
YEARS: 'years',
Expand Down Expand Up @@ -162,6 +163,37 @@ const dateDiff = function (startDateObj, endDateObj, key) {
return XPR.number(endDate.diff(startDate, key));
};

const stripWhitespace = function (string) {
return XPR.string(stripSpace(string));
};

const stripSpace = (s) => getValue(s).toString().trim().replace(/\s/g, '');

const luhn = function (number, expLength) {
number = stripSpace(number);

if (!/^\d+$/.test(number) || (expLength && number.length !== expLength)) {
return XPR.bool(false);
}

const digits = number.split('').map(Number);
let sum = 0;
let isSecond = false;
for (let i = digits.length - 1; i >= 0; i--) {
let digit = digits[i];
if (isSecond) {
digit *= 2;
if (digit > 9) {
digit -= 9;
}
}
sum += digit;
isSecond = !isSecond;
}

return XPR.bool(sum % 10 === 0);
};

module.exports = {
getTimezoneOffsetAsTime: getTimezoneOffsetAsTime,
toISOLocalString: toISOLocalString,
Expand Down Expand Up @@ -190,6 +222,8 @@ module.exports = {
'difference-in-months': (d1, d2) => dateDiff(d1, d2, MOMENT_KEYS.MONTHS), // To be deprecated
'cht:difference-in-weeks': (d1, d2) => dateDiff(d1, d2, MOMENT_KEYS.WEEKS),
'cht:difference-in-days': (d1, d2) => dateDiff(d1, d2, MOMENT_KEYS.DAYS),
'cht:strip-whitespace': stripWhitespace,
'cht:validate-luhn': luhn,
'cht:extension-lib': function () {
const args = Array.from(arguments);
const firstArg = args.shift();
Expand Down
104 changes: 102 additions & 2 deletions webapp/tests/mocha/unit/enketo/medic-xpath-extensions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ medicXpathExtensions.init(null, toBik_text, moment);
const func = medicXpathExtensions.func;

describe('medic-xpath-extensions', function () {
const wrapDate = (date) => {
const wrapDate = (date) => {
return { t: 'str', v: date };
};

Expand Down Expand Up @@ -87,7 +87,7 @@ describe('medic-xpath-extensions', function () {

describe('#difference-in-months', function () {
const diffInMonths = func['cht:difference-in-months'];

[
['2015-10-01', '2015-10-01', 0,],
['2015-09-01', '2015-10-01', 1,],
Expand Down Expand Up @@ -380,4 +380,104 @@ describe('medic-xpath-extensions', function () {
});
});
});

describe('#strip-whitespace()', () => {
const test = func['cht:strip-whitespace'];

it('should return a string without any white spaces', () => {
const idNumber = { t: 'arr', v: [{ textContent: ' 65 05\n 28 5125 086 ' }] };
const result = test(idNumber);
assert.strictEqual(/\s/.test(result.v), false);
});
});

describe('#validate-luhn()', () => {
const test = func['cht:validate-luhn'];

it('should return true for a valid South African ID number', () => {
const idNumber = { t: 'arr', v: [{ textContent: '6505285125086' }] };
const result = test(idNumber, 13);
assert.strictEqual(result.v, true);
});
it('should return false for an South African ID number that does not contain 13 digits', () => {
const idNumber = { t: 'arr', v: [{ textContent: '650528512508' }] };
const result = test(idNumber, 13);
assert.strictEqual(result.v, false);
});
it('should return false for an invalid South African ID number ', () => {
const idNumber = { t: 'arr', v: [{ textContent: '6505285125085' }] };
const result = test(idNumber, 13);
assert.strictEqual(result.v, false);
});
it('should return false for a non-numeric South African ID number', () => {
const idNumber = { t: 'arr', v: [{ textContent: '650528512a086' }] };
const result = test(idNumber, 13);
assert.strictEqual(result.v, false);
});
it('should return true for a valid South African ID number with spaces', () => {
const idNumber = { t: 'arr', v: [{ textContent: '6505 2851 2508 6' }] };
const result = test(idNumber, 13);
assert.strictEqual(result.v, true);
});
it('should return true for a valid South African ID number with leading/trailing spaces', () => {
const idNumber = { t: 'arr', v: [{ textContent: ' 6505285125086 ' }] };
const result = test(idNumber, 13);
assert.strictEqual(result.v, true);
});

it('should return true for a valid American Express credit card number', () => {
const idNumber = { t: 'arr', v: [{ textContent: '371449635398431' }] };
const result = test(idNumber, 15);
assert.strictEqual(result.v, true);
});
it('should return false for an invalid American Express credit card number', () => {
const idNumber = { t: 'arr', v: [{ textContent: '371449635398432' }] };
const result = test(idNumber, 15);
assert.strictEqual(result.v, false);
});

it('should return true for a valid Visa credit card number', () => {
const idNumber = { t: 'arr', v: [{ textContent: '4532015112830366' }] };
const result = test(idNumber, 16);
assert.strictEqual(result.v, true);
});
it('should return false for an invalid Visa credit card number', () => {
const idNumber = { t: 'arr', v: [{ textContent: '4532015112830367' }] };
const result = test(idNumber, 16);
assert.strictEqual(result.v, false);
});

it('should return true for a valid MasterCard credit card number', () => {
const idNumber = { t: 'arr', v: [{ textContent: '5555555555554444' }] };
const result = test(idNumber, 16);
assert.strictEqual(result.v, true);
});
it('should return false for an invalid MasterCard credit card number', () => {
const idNumber = { t: 'arr', v: [{ textContent: '5555555555554445' }] };
const result = test(idNumber, 16);
assert.strictEqual(result.v, false);
});

it('should return true for a valid Discover credit card number', () => {
const idNumber = { t: 'arr', v: [{ textContent: '6011000990139424' }] };
const result = test(idNumber, 16);
assert.strictEqual(result.v, true);
});
it('should return false for an invalid Discover credit card number', () => {
const idNumber = { t: 'arr', v: [{ textContent: '6011000990139425' }] };
const result = test(idNumber, 16);
assert.strictEqual(result.v, false);
});

it('should return true for a valid luhn number without specifying a length', () => {
const number = { t: 'arr', v: [{ textContent: '6505285125086' }] };
const result = test(number);
assert.strictEqual(result.v, true);
});
it('should return false for a invalid luhn number without specifying a length', () => {
const number = { t: 'arr', v: [{ textContent: '6011000990139425' }] };
const result = test(number);
assert.strictEqual(result.v, false);
});
});
});

0 comments on commit 3c2b140

Please sign in to comment.