Add support for non lower case extensions with toFormat

This commit is contained in:
Florian Busch 2021-02-17 21:46:13 +01:00 committed by GitHub
parent 202083999e
commit df7b8ba738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -188,7 +188,7 @@ function withMetadata (options) {
* @throws {Error} unsupported format or options * @throws {Error} unsupported format or options
*/ */
function toFormat (format, options) { function toFormat (format, options) {
const actualFormat = formats.get(is.object(format) && is.string(format.id) ? format.id : format); const actualFormat = formats.get((is.object(format) && is.string(format.id) ? format.id : format).toLowerCase());
if (!actualFormat) { if (!actualFormat) {
throw is.invalidParameterError('format', `one of: ${[...formats.keys()].join(', ')}`, format); throw is.invalidParameterError('format', `one of: ${[...formats.keys()].join(', ')}`, format);
} }

26
test/unit/toFormat.js Normal file
View File

@ -0,0 +1,26 @@
'use strict';
const assert = require('assert');
const sharp = require('../../');
const fixtures = require('../fixtures');
describe('toFormat', () => {
it('accepts upper case characters as format parameter (string)', function (done) {
sharp(fixtures.inputJpg)
.toFormat('PNG')
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
done();
});
});
it('accepts upper case characters as format parameter (object)', function (done) {
sharp(fixtures.inputJpg)
.toFormat({ id: 'PNG' })
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
done();
});
});
});