diff --git a/emvqr/emvqr.js b/emvqr/emvqr.js index 82e4b34..ed454af 100644 --- a/emvqr/emvqr.js +++ b/emvqr/emvqr.js @@ -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); @@ -42,7 +41,7 @@ const read = (text, describe, tagId) => { id, name: subtag ? subtag.name : tag.name, len, - data + data, }; } else { @@ -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, }; } }; @@ -71,5 +70,6 @@ const decode = (text, tiny = false) => { }; module.exports = { - decode + decode, + validate, }; diff --git a/test/emvqr.spec.js b/test/emvqr.spec.js index c8da9a7..27784bf 100644 --- a/test/emvqr.spec.js +++ b/test/emvqr.spec.js @@ -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); @@ -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); +});