From 5bfcf61a6f2bf905c3c744c1b95b839e0a7b189c Mon Sep 17 00:00:00 2001 From: Julian Aubourg Date: Mon, 12 Aug 2019 22:36:56 +0200 Subject: [PATCH] Allow use of heic/heif identifiers with toFormat (#1834) --- lib/output.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/output.js b/lib/output.js index 19fd0f44..dae7badc 100644 --- a/lib/output.js +++ b/lib/output.js @@ -487,6 +487,17 @@ function raw () { return this._updateFormatOut('raw'); } +const formats = new Map([ + ['heic', 'heif'], + ['heif', 'heif'], + ['jpeg', 'jpeg'], + ['jpg', 'jpeg'], + ['png', 'png'], + ['raw', 'raw'], + ['tiff', 'tiff'], + ['webp', 'webp'] +]); + /** * Force output to a given format. * @@ -502,14 +513,11 @@ function raw () { * @throws {Error} unsupported format or options */ function toFormat (format, options) { - if (is.object(format) && is.string(format.id)) { - format = format.id; + const actualFormat = formats.get(is.object(format) && is.string(format.id) ? format.id : format); + if (!actualFormat) { + throw is.invalidParameterError('format', `one of: ${[...formats.keys()].join(`, `)}`, format); } - if (format === 'jpg') format = 'jpeg'; - if (!is.inArray(format, ['jpeg', 'png', 'webp', 'tiff', 'raw'])) { - throw is.invalidParameterError('format', 'one of: jpeg, png, webp, tiff, raw', format); - } - return this[format](options); + return this[actualFormat](options); } /**