Merge pull request #124 from yzwduck/fix-seg-toFile
Fix passing manual segments list to QRCode.toFile
This commit is contained in:
commit
690acce652
2 changed files with 43 additions and 1 deletions
|
@ -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')
|
||||
}
|
||||
|
||||
|
|
|
@ -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')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue