Merge pull request #299 from RSLak1/master
fix: binary data not working as described (fixes #289, #231)
This commit is contained in:
commit
1eba16b58e
3 changed files with 47 additions and 2 deletions
|
@ -3,7 +3,10 @@ const Mode = require('./mode')
|
|||
|
||||
function ByteData (data) {
|
||||
this.mode = Mode.BYTE
|
||||
this.data = new Uint8Array(encodeUtf8(data))
|
||||
if (typeof (data) === 'string') {
|
||||
data = encodeUtf8(data)
|
||||
}
|
||||
this.data = new Uint8Array(data)
|
||||
}
|
||||
|
||||
ByteData.getBitsLength = function getBitsLength (length) {
|
||||
|
|
|
@ -231,3 +231,31 @@ test('toString terminal', function (t) {
|
|||
t.equal(code + '\n', expectedTerminal, 'should output a valid symbol (promise)')
|
||||
})
|
||||
})
|
||||
|
||||
test('toString byte-input', function (t) {
|
||||
const expectedOutput = [
|
||||
' ',
|
||||
' ',
|
||||
' █▀▀▀▀▀█ █▄█▀ █▀▀▀▀▀█ ',
|
||||
' █ ███ █ ▀█ █▀ █ ███ █ ',
|
||||
' █ ▀▀▀ █ ▀ █ █ ▀▀▀ █ ',
|
||||
' ▀▀▀▀▀▀▀ █▄▀▄█ ▀▀▀▀▀▀▀ ',
|
||||
' ▀██▄██▀▀▀█▀▀ ▀█ ▄▀▄ ',
|
||||
' ▀█▀▄█▄▀▄ ██ ▀ ▄ ▀▄ ▀ ',
|
||||
' ▀ ▀ ▀▀▀▀█▄ ▄▀▄▀▄▀▄▀▄▀ ',
|
||||
' █▀▀▀▀▀█ █ █▄█▀█▄█ ▀ ',
|
||||
' █ ███ █ ▀█▀▀ ▀██ ▀█▀ ',
|
||||
' █ ▀▀▀ █ ██▀ ▀ ▄ ▀▄▀▄▀ ',
|
||||
' ▀▀▀▀▀▀▀ ▀▀▀ ▀ ▀▀▀ ▀▀▀ ',
|
||||
' ',
|
||||
' '
|
||||
].join('\n')
|
||||
const byteInput = new Uint8ClampedArray([1, 2, 3, 4, 5])
|
||||
|
||||
t.plan(2)
|
||||
|
||||
QRCode.toString([{ data: byteInput, mode: 'byte' }], { errorCorrectionLevel: 'L' }, (err, code) => {
|
||||
t.ok(!err, 'there should be no error')
|
||||
t.equal(code, expectedOutput, 'should output the correct code')
|
||||
})
|
||||
})
|
||||
|
|
|
@ -3,7 +3,7 @@ const BitBuffer = require('core/bit-buffer')
|
|||
const ByteData = require('core/byte-data')
|
||||
const Mode = require('core/mode')
|
||||
|
||||
test('Byte Data', function (t) {
|
||||
test('Byte Data: String Input', function (t) {
|
||||
const text = '1234'
|
||||
const textBitLength = 32
|
||||
const textByte = [49, 50, 51, 52] // 1, 2, 3, 4
|
||||
|
@ -24,3 +24,17 @@ test('Byte Data', function (t) {
|
|||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('Byte Data: Byte Input', function (t) {
|
||||
const bytes = new Uint8ClampedArray([1, 231, 32, 22])
|
||||
|
||||
const byteData = new ByteData(bytes)
|
||||
t.equal(byteData.getLength(), bytes.length, 'Should return correct length')
|
||||
t.equal(byteData.getBitsLength(), bytes.length * 8, 'Should return correct bit length')
|
||||
|
||||
const bitBuffer = new BitBuffer()
|
||||
byteData.write(bitBuffer)
|
||||
t.deepEqual(bitBuffer.buffer, bytes, 'Should write correct data to buffer')
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue