Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions emvqr/emvqr.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

const crc = require('./crc');
const { getTag, getSubTag } = require('./scheme');

const validate = (text) => {
const validate = text => {
const data = text.substring(0, text.length - 4);
const checksum = text.substring(text.length - 4);

Expand Down Expand Up @@ -42,7 +41,7 @@ const read = (text, describe, tagId) => {
id,
name: subtag ? subtag.name : tag.name,
len,
data
data,
};
}
else {
Expand All @@ -52,12 +51,12 @@ const read = (text, describe, tagId) => {
if (next.length) {
return {
[id]: value,
...read(next, describe, tagId)
...read(next, describe, tagId),
};
}
else {
return {
[id]: value
[id]: value,
};
}
};
Expand All @@ -71,5 +70,6 @@ const decode = (text, tiny = false) => {
};

module.exports = {
decode
decode,
validate,
};
15 changes: 13 additions & 2 deletions test/emvqr.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const emvqr = require('../emvqr/emvqr');

const emvqrHelloWorldExample = '00020101021229300012D156000000000510A93FO3230Q31280012D15600000001030812345678520441115802CN5914BEST TRANSPORT6007BEIJING64200002ZH0104最佳运输0202北京540523.7253031565502016233030412340603***0708A60086670902ME91320016A0112233449988770708123456786304A13A';
const emvqrHelloWorldExample =
'00020101021229300012D156000000000510A93FO3230Q31280012D15600000001030812345678520441115802CN5914BEST TRANSPORT6007BEIJING64200002ZH0104最佳运输0202北京540523.7253031565502016233030412340603***0708A60086670902ME91320016A0112233449988770708123456786304A13A';

const emvqrBadChecksumExample =
'00020101021229300012D156000000000510A93FO3230Q31280012D15600000001030812345678520441115802CN5914BEZT TRANSPORT6007BEIJING64200002ZH0104最佳运输0202北京540523.7253031565502016233030412340603***0708A60086670902ME91320016A0112233449988770708123456786304A13A';

test('should return a valid object, when its called', () => {
const emvObject = emvqr.decode(emvqrHelloWorldExample);
Expand All @@ -21,8 +25,15 @@ test('valid expected output for emvqr string example', () => {
});

test('should throw an error, when its called with an invalid checksum', () => {
const emvqrBadChecksumExample = '00020101021229300012D156000000000510A93FO3230Q31280012D15600000001030812345678520441115802CN5914BEZT TRANSPORT6007BEIJING64200002ZH0104最佳运输0202北京540523.7253031565502016233030412340603***0708A60086670902ME91320016A0112233449988770708123456786304A13A';
expect(() => {
emvqr.decode(emvqrBadChecksumExample);
}).toThrow(Error);
});

test('should return true, when its called with a valid checksum', () => {
expect(emvqr.validate(emvqrHelloWorldExample)).toBe(true);
});

test('should return false, when its called with a invalid checksum', () => {
expect(emvqr.validate(emvqrBadChecksumExample)).toBe(false);
});