Merge pull request from yzwduck/fix-seg-toFile

Fix passing manual segments list to QRCode.toFile
This commit is contained in:
Ryan Day 2018-10-01 08:42:34 -07:00 committed by GitHub
commit 690acce652
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 1 deletions

View file

@ -109,7 +109,7 @@ exports.toBuffer = function toBuffer (text, opts, cb) {
}
exports.toFile = function toFile (path, text, opts, cb) {
if (typeof path !== 'string' || typeof text !== 'string') {
if (typeof path !== 'string' || !(typeof text === 'string' || typeof text === 'object')) {
throw new Error('Invalid argument')
}

View file

@ -226,3 +226,45 @@ test('toFile utf8', function (t) {
})
})
})
test('toFile manual segments', function (t) {
var fileName = path.join(tmpDir(), 'qrimage.txt')
var segs = [
{ data: 'ABCDEFG', mode: 'alphanumeric' },
{ data: '0123456', mode: 'numeric' }
]
var expectedOutput = [
' ',
' ',
' █▀▀▀▀▀█ ██▀██ █▀▀▀▀▀█ ',
' █ ███ █ █▀█▄ █ ███ █ ',
' █ ▀▀▀ █ █ ▄ ▀ █ ▀▀▀ █ ',
' ▀▀▀▀▀▀▀ █▄█▄▀ ▀▀▀▀▀▀▀ ',
' ▀██ ▄▀▀▄█▀▀▀▀██▀▀▄ █▀ ',
' ▀█▀▀█▀█▄ ▄ ▄█▀▀▀█▀ ',
' ▀ ▀▀▀ ▀ ▄▀ ▄ ▄▀▄ ▀▄ ',
' █▀▀▀▀▀█ ▄ █▀█ ▀▀▀▄█▄ ',
' █ ███ █ █▀▀▀ ██▀▀ ▀▀ ',
' █ ▀▀▀ █ ██ ▄▀▀▀▀▄▀▀█ ',
' ▀▀▀▀▀▀▀ ▀ ▀▀▀▀ ▀▀▀ ',
' ',
' '].join('\n')
t.plan(3)
QRCode.toFile(fileName, segs, {
errorCorrectionLevel: 'L'
}, function (err) {
t.ok(!err, 'There should be no errors if text is not string')
fs.stat(fileName, function (err) {
t.ok(!err,
'Should save file with correct file name')
})
fs.readFile(fileName, 'utf8', function (err, content) {
if (err) throw err
t.equal(content, expectedOutput,
'Should write correct content')
})
})
})