Linter: apply all recommended biome settings

Enforces previously-skipped useArrowFunction check
This commit is contained in:
Lovell Fuller 2025-11-03 21:14:45 +00:00
parent 09d5aa8cfa
commit 4f9f8179a6
No known key found for this signature in database
GPG Key ID: 26C1635893ACB81F
66 changed files with 1823 additions and 1910 deletions

View File

@ -1,5 +1,5 @@
{ {
"$schema": "https://biomejs.dev/schemas/2.3.2/schema.json", "$schema": "https://biomejs.dev/schemas/2.3.3/schema.json",
"vcs": { "vcs": {
"enabled": true, "enabled": true,
"clientKind": "git", "clientKind": "git",
@ -11,10 +11,7 @@
"linter": { "linter": {
"enabled": true, "enabled": true,
"rules": { "rules": {
"recommended": true, "recommended": true
"complexity": {
"useArrowFunction": "off"
}
} }
}, },
"formatter": { "formatter": {

View File

@ -163,7 +163,7 @@ function bandbool (boolOp) {
* @module Sharp * @module Sharp
* @private * @private
*/ */
module.exports = function (Sharp) { module.exports = (Sharp) => {
Object.assign(Sharp.prototype, { Object.assign(Sharp.prototype, {
// Public instance functions // Public instance functions
removeAlpha, removeAlpha,

View File

@ -175,7 +175,7 @@ function _setBackgroundColourOption (key, value) {
* @module Sharp * @module Sharp
* @private * @private
*/ */
module.exports = function (Sharp) { module.exports = (Sharp) => {
Object.assign(Sharp.prototype, { Object.assign(Sharp.prototype, {
// Public // Public
tint, tint,

View File

@ -206,7 +206,7 @@ function composite (images) {
* @module Sharp * @module Sharp
* @private * @private
*/ */
module.exports = function (Sharp) { module.exports = (Sharp) => {
Sharp.prototype.composite = composite; Sharp.prototype.composite = composite;
Sharp.blend = blend; Sharp.blend = blend;
}; };

View File

@ -12,6 +12,10 @@ require('./sharp');
// Use NODE_DEBUG=sharp to enable libvips warnings // Use NODE_DEBUG=sharp to enable libvips warnings
const debuglog = util.debuglog('sharp'); const debuglog = util.debuglog('sharp');
const queueListener = (queueLength) => {
Sharp.queue.emit('change', queueLength);
};
/** /**
* Constructor factory to create an instance of `sharp`, to which further methods are chained. * Constructor factory to create an instance of `sharp`, to which further methods are chained.
* *
@ -398,9 +402,7 @@ const Sharp = function (input, options) {
debuglog(warning); debuglog(warning);
}, },
// Function to notify of queue length changes // Function to notify of queue length changes
queueListener: function (queueLength) { queueListener
Sharp.queue.emit('change', queueLength);
}
}; };
this.options.input = this._createInputDescriptor(input, options, { allowStream: true }); this.options.input = this._createInputDescriptor(input, options, { allowStream: true });
return this; return this;

View File

@ -792,7 +792,7 @@ function stats (callback) {
* @module Sharp * @module Sharp
* @private * @private
*/ */
module.exports = function (Sharp) { module.exports = (Sharp) => {
Object.assign(Sharp.prototype, { Object.assign(Sharp.prototype, {
// Private // Private
_inputOptionsFromObject, _inputOptionsFromObject,

View File

@ -7,55 +7,43 @@
* Is this value defined and not null? * Is this value defined and not null?
* @private * @private
*/ */
const defined = function (val) { const defined = (val) => typeof val !== 'undefined' && val !== null;
return typeof val !== 'undefined' && val !== null;
};
/** /**
* Is this value an object? * Is this value an object?
* @private * @private
*/ */
const object = function (val) { const object = (val) => typeof val === 'object';
return typeof val === 'object';
};
/** /**
* Is this value a plain object? * Is this value a plain object?
* @private * @private
*/ */
const plainObject = function (val) { const plainObject = (val) => Object.prototype.toString.call(val) === '[object Object]';
return Object.prototype.toString.call(val) === '[object Object]';
};
/** /**
* Is this value a function? * Is this value a function?
* @private * @private
*/ */
const fn = function (val) { const fn = (val) => typeof val === 'function';
return typeof val === 'function';
};
/** /**
* Is this value a boolean? * Is this value a boolean?
* @private * @private
*/ */
const bool = function (val) { const bool = (val) => typeof val === 'boolean';
return typeof val === 'boolean';
};
/** /**
* Is this value a Buffer object? * Is this value a Buffer object?
* @private * @private
*/ */
const buffer = function (val) { const buffer = (val) => val instanceof Buffer;
return val instanceof Buffer;
};
/** /**
* Is this value a typed array object?. E.g. Uint8Array or Uint8ClampedArray? * Is this value a typed array object?. E.g. Uint8Array or Uint8ClampedArray?
* @private * @private
*/ */
const typedArray = function (val) { const typedArray = (val) => {
if (defined(val)) { if (defined(val)) {
switch (val.constructor) { switch (val.constructor) {
case Uint8Array: case Uint8Array:
@ -78,49 +66,37 @@ const typedArray = function (val) {
* Is this value an ArrayBuffer object? * Is this value an ArrayBuffer object?
* @private * @private
*/ */
const arrayBuffer = function (val) { const arrayBuffer = (val) => val instanceof ArrayBuffer;
return val instanceof ArrayBuffer;
};
/** /**
* Is this value a non-empty string? * Is this value a non-empty string?
* @private * @private
*/ */
const string = function (val) { const string = (val) => typeof val === 'string' && val.length > 0;
return typeof val === 'string' && val.length > 0;
};
/** /**
* Is this value a real number? * Is this value a real number?
* @private * @private
*/ */
const number = function (val) { const number = (val) => typeof val === 'number' && !Number.isNaN(val);
return typeof val === 'number' && !Number.isNaN(val);
};
/** /**
* Is this value an integer? * Is this value an integer?
* @private * @private
*/ */
const integer = function (val) { const integer = (val) => Number.isInteger(val);
return Number.isInteger(val);
};
/** /**
* Is this value within an inclusive given range? * Is this value within an inclusive given range?
* @private * @private
*/ */
const inRange = function (val, min, max) { const inRange = (val, min, max) => val >= min && val <= max;
return val >= min && val <= max;
};
/** /**
* Is this value within the elements of an array? * Is this value within the elements of an array?
* @private * @private
*/ */
const inArray = function (val, list) { const inArray = (val, list) => list.includes(val);
return list.includes(val);
};
/** /**
* Create an Error with a message relating to an invalid parameter. * Create an Error with a message relating to an invalid parameter.
@ -131,11 +107,9 @@ const inArray = function (val, list) {
* @returns {Error} Containing the formatted message. * @returns {Error} Containing the formatted message.
* @private * @private
*/ */
const invalidParameterError = function (name, expected, actual) { const invalidParameterError = (name, expected, actual) => new Error(
return new Error(
`Expected ${expected} for ${name} but received ${actual} of type ${typeof actual}` `Expected ${expected} for ${name} but received ${actual} of type ${typeof actual}`
); );
};
/** /**
* Ensures an Error from C++ contains a JS stack. * Ensures an Error from C++ contains a JS stack.
@ -145,7 +119,7 @@ const invalidParameterError = function (name, expected, actual) {
* @returns {Error} Error with message and stack. * @returns {Error} Error with message and stack.
* @private * @private
*/ */
const nativeError = function (native, context) { const nativeError = (native, context) => {
context.message = native.message; context.message = native.message;
return context; return context;
}; };

View File

@ -742,9 +742,7 @@ function convolve (kernel) {
} }
// Default scale is sum of kernel values // Default scale is sum of kernel values
if (!is.integer(kernel.scale)) { if (!is.integer(kernel.scale)) {
kernel.scale = kernel.kernel.reduce(function (a, b) { kernel.scale = kernel.kernel.reduce((a, b) => a + b, 0);
return a + b;
}, 0);
} }
// Clip scale to a minimum value of 1 // Clip scale to a minimum value of 1
if (kernel.scale < 1) { if (kernel.scale < 1) {
@ -989,7 +987,7 @@ function modulate (options) {
* @module Sharp * @module Sharp
* @private * @private
*/ */
module.exports = function (Sharp) { module.exports = (Sharp) => {
Object.assign(Sharp.prototype, { Object.assign(Sharp.prototype, {
autoOrient, autoOrient,
rotate, rotate,

View File

@ -1623,7 +1623,7 @@ function _pipeline (callback, stack) {
* @module Sharp * @module Sharp
* @private * @private
*/ */
module.exports = function (Sharp) { module.exports = (Sharp) => {
Object.assign(Sharp.prototype, { Object.assign(Sharp.prototype, {
// Public // Public
toFile, toFile,

View File

@ -579,7 +579,7 @@ function trim (options) {
* @module Sharp * @module Sharp
* @private * @private
*/ */
module.exports = function (Sharp) { module.exports = (Sharp) => {
Object.assign(Sharp.prototype, { Object.assign(Sharp.prototype, {
resize, resize,
extend, extend,

View File

@ -277,7 +277,7 @@ function unblock (options) {
* @module Sharp * @module Sharp
* @private * @private
*/ */
module.exports = function (Sharp) { module.exports = (Sharp) => {
Sharp.cache = cache; Sharp.cache = cache;
Sharp.concurrency = concurrency; Sharp.concurrency = concurrency;
Sharp.counters = counters; Sharp.counters = counters;

View File

@ -170,7 +170,7 @@
"@img/sharp-win32-x64": "0.34.4" "@img/sharp-win32-x64": "0.34.4"
}, },
"devDependencies": { "devDependencies": {
"@biomejs/biome": "^2.3.2", "@biomejs/biome": "^2.3.3",
"@cpplint/cli": "^0.1.0", "@cpplint/cli": "^0.1.0",
"@emnapi/runtime": "^1.6.0", "@emnapi/runtime": "^1.6.0",
"@img/sharp-libvips-dev": "1.2.4", "@img/sharp-libvips-dev": "1.2.4",

View File

@ -16,31 +16,29 @@ const height = 480;
sharp.concurrency(1); sharp.concurrency(1);
const timer = setInterval(function () { const timer = setInterval(() => {
console.dir(sharp.counters()); console.dir(sharp.counters());
}, 100); }, 100);
async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64], function (parallelism, next) { async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64], (parallelism, next) => {
const start = Date.now(); const start = Date.now();
async.times(parallelism, async.times(parallelism,
function (_id, callback) { (_id, callback) => {
sharp(fixtures.inputJpg).resize(width, height).toBuffer(function (err, buffer) { sharp(fixtures.inputJpg).resize(width, height).toBuffer((err, buffer) => {
buffer = null; buffer = null;
callback(err, Date.now() - start); callback(err, Date.now() - start);
}); });
}, },
function (err, ids) { (err, ids) => {
assert(!err); assert(!err);
assert(ids.length === parallelism); assert(ids.length === parallelism);
ids.sort(); ids.sort();
const mean = ids.reduce(function (a, b) { const mean = ids.reduce((a, b) => a + b) / ids.length;
return a + b;
}) / ids.length;
console.log(`${parallelism} parallel calls: fastest=${ids[0]}ms slowest=${ids[ids.length - 1]}ms mean=${mean}ms`); console.log(`${parallelism} parallel calls: fastest=${ids[0]}ms slowest=${ids[ids.length - 1]}ms mean=${mean}ms`);
next(); next();
} }
); );
}, function () { }, () => {
clearInterval(timer); clearInterval(timer);
console.dir(sharp.counters()); console.dir(sharp.counters());
}); });

View File

@ -45,13 +45,13 @@ console.log(`Detected ${physicalCores} physical cores`);
sharp.concurrency(physicalCores); sharp.concurrency(physicalCores);
async.series({ async.series({
jpeg: function (callback) { jpeg: (callback) => {
const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg); const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg);
const jpegSuite = new Benchmark.Suite('jpeg'); const jpegSuite = new Benchmark.Suite('jpeg');
// jimp // jimp
jpegSuite.add('jimp-buffer-buffer', { jpegSuite.add('jimp-buffer-buffer', {
defer: true, defer: true,
fn: async function (deferred) { fn: async (deferred) => {
const image = await Jimp.read(inputJpgBuffer); const image = await Jimp.read(inputJpgBuffer);
await image await image
.resize({ w: width, h: height, mode: Jimp.RESIZE_BICUBIC }) .resize({ w: width, h: height, mode: Jimp.RESIZE_BICUBIC })
@ -60,7 +60,7 @@ async.series({
} }
}).add('jimp-file-file', { }).add('jimp-file-file', {
defer: true, defer: true,
fn: async function (deferred) { fn: async (deferred) => {
const image = await Jimp.read(fixtures.inputJpg); const image = await Jimp.read(fixtures.inputJpg);
await image await image
.resize({ w: width, h: height, mode: Jimp.RESIZE_BICUBIC }) .resize({ w: width, h: height, mode: Jimp.RESIZE_BICUBIC })
@ -71,14 +71,14 @@ async.series({
// mapnik // mapnik
mapnik && jpegSuite.add('mapnik-file-file', { mapnik && jpegSuite.add('mapnik-file-file', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
mapnik.Image.open(fixtures.inputJpg, function (err, img) { mapnik.Image.open(fixtures.inputJpg, (err, img) => {
if (err) throw err; if (err) throw err;
img img
.resize(width, height, { .resize(width, height, {
scaling_method: mapnik.imageScaling.lanczos scaling_method: mapnik.imageScaling.lanczos
}) })
.save(outputJpg, 'jpeg:quality=80', function (err) { .save(outputJpg, 'jpeg:quality=80', (err) => {
if (err) throw err; if (err) throw err;
deferred.resolve(); deferred.resolve();
}); });
@ -86,14 +86,14 @@ async.series({
} }
}).add('mapnik-buffer-buffer', { }).add('mapnik-buffer-buffer', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
mapnik.Image.fromBytes(inputJpgBuffer, { max_size: 3000 }, function (err, img) { mapnik.Image.fromBytes(inputJpgBuffer, { max_size: 3000 }, (err, img) => {
if (err) throw err; if (err) throw err;
img img
.resize(width, height, { .resize(width, height, {
scaling_method: mapnik.imageScaling.lanczos scaling_method: mapnik.imageScaling.lanczos
}) })
.encode('jpeg:quality=80', function (err) { .encode('jpeg:quality=80', (err) => {
if (err) throw err; if (err) throw err;
deferred.resolve(); deferred.resolve();
}); });
@ -103,7 +103,7 @@ async.series({
// imagemagick // imagemagick
jpegSuite.add('imagemagick-file-file', { jpegSuite.add('imagemagick-file-file', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
imagemagick.resize({ imagemagick.resize({
srcPath: fixtures.inputJpg, srcPath: fixtures.inputJpg,
dstPath: outputJpg, dstPath: outputJpg,
@ -112,7 +112,7 @@ async.series({
height, height,
format: 'jpg', format: 'jpg',
filter: 'Lanczos' filter: 'Lanczos'
}, function (err) { }, (err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -124,12 +124,12 @@ async.series({
// gm // gm
jpegSuite.add('gm-buffer-file', { jpegSuite.add('gm-buffer-file', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
gm(inputJpgBuffer) gm(inputJpgBuffer)
.filter('Lanczos') .filter('Lanczos')
.resize(width, height) .resize(width, height)
.quality(80) .quality(80)
.write(outputJpg, function (err) { .write(outputJpg, (err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -139,12 +139,12 @@ async.series({
} }
}).add('gm-buffer-buffer', { }).add('gm-buffer-buffer', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
gm(inputJpgBuffer) gm(inputJpgBuffer)
.filter('Lanczos') .filter('Lanczos')
.resize(width, height) .resize(width, height)
.quality(80) .quality(80)
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -154,12 +154,12 @@ async.series({
} }
}).add('gm-file-file', { }).add('gm-file-file', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
gm(fixtures.inputJpg) gm(fixtures.inputJpg)
.filter('Lanczos') .filter('Lanczos')
.resize(width, height) .resize(width, height)
.quality(80) .quality(80)
.write(outputJpg, function (err) { .write(outputJpg, (err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -169,12 +169,12 @@ async.series({
} }
}).add('gm-file-buffer', { }).add('gm-file-buffer', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
gm(fixtures.inputJpg) gm(fixtures.inputJpg)
.filter('Lanczos') .filter('Lanczos')
.resize(width, height) .resize(width, height)
.quality(80) .quality(80)
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -186,17 +186,17 @@ async.series({
// tfjs // tfjs
tfjs && jpegSuite.add('tfjs-node-buffer-buffer', { tfjs && jpegSuite.add('tfjs-node-buffer-buffer', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
const decoded = tfjs.node.decodeJpeg(inputJpgBuffer); const decoded = tfjs.node.decodeJpeg(inputJpgBuffer);
const resized = tfjs.image.resizeBilinear(decoded, [height, width]); const resized = tfjs.image.resizeBilinear(decoded, [height, width]);
tfjs tfjs
.node .node
.encodeJpeg(resized, 'rgb', 80) .encodeJpeg(resized, 'rgb', 80)
.then(function () { .then(() => {
deferred.resolve(); deferred.resolve();
tfjs.disposeVariables(); tfjs.disposeVariables();
}) })
.catch(function (err) { .catch((err) => {
throw err; throw err;
}); });
} }
@ -204,10 +204,10 @@ async.series({
// sharp // sharp
jpegSuite.add('sharp-buffer-file', { jpegSuite.add('sharp-buffer-file', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.toFile(outputJpg, function (err) { .toFile(outputJpg, (err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -217,10 +217,10 @@ async.series({
} }
}).add('sharp-buffer-buffer', { }).add('sharp-buffer-buffer', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -230,10 +230,10 @@ async.series({
} }
}).add('sharp-file-file', { }).add('sharp-file-file', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(width, height) .resize(width, height)
.toFile(outputJpg, function (err) { .toFile(outputJpg, (err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -243,10 +243,10 @@ async.series({
} }
}).add('sharp-stream-stream', { }).add('sharp-stream-stream', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
const readable = fs.createReadStream(fixtures.inputJpg); const readable = fs.createReadStream(fixtures.inputJpg);
const writable = fs.createWriteStream(outputJpg); const writable = fs.createWriteStream(outputJpg);
writable.on('finish', function () { writable.on('finish', () => {
deferred.resolve(); deferred.resolve();
}); });
const pipeline = sharp() const pipeline = sharp()
@ -255,10 +255,10 @@ async.series({
} }
}).add('sharp-file-buffer', { }).add('sharp-file-buffer', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(width, height) .resize(width, height)
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -268,34 +268,34 @@ async.series({
} }
}).add('sharp-promise', { }).add('sharp-promise', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.toBuffer() .toBuffer()
.then(function () { .then(() => {
deferred.resolve(); deferred.resolve();
}) })
.catch(function (err) { .catch((err) => {
throw err; throw err;
}); });
} }
}).on('cycle', function (event) { }).on('cycle', (event) => {
console.log(`jpeg ${String(event.target)}`); console.log(`jpeg ${String(event.target)}`);
}).on('complete', function () { }).on('complete', function () {
callback(null, this.filter('fastest').map('name')); callback(null, this.filter('fastest').map('name'));
}).run(); }).run();
}, },
// Effect of applying operations // Effect of applying operations
operations: function (callback) { operations: (callback) => {
const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg); const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg);
const operationsSuite = new Benchmark.Suite('operations'); const operationsSuite = new Benchmark.Suite('operations');
operationsSuite.add('sharp-sharpen-mild', { operationsSuite.add('sharp-sharpen-mild', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.sharpen() .sharpen()
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -305,11 +305,11 @@ async.series({
} }
}).add('sharp-sharpen-radius', { }).add('sharp-sharpen-radius', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.sharpen(3, 1, 3) .sharpen(3, 1, 3)
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -319,11 +319,11 @@ async.series({
} }
}).add('sharp-blur-mild', { }).add('sharp-blur-mild', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.blur() .blur()
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -333,11 +333,11 @@ async.series({
} }
}).add('sharp-blur-radius', { }).add('sharp-blur-radius', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.blur(3) .blur(3)
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -347,11 +347,11 @@ async.series({
} }
}).add('sharp-gamma', { }).add('sharp-gamma', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.gamma() .gamma()
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -361,11 +361,11 @@ async.series({
} }
}).add('sharp-normalise', { }).add('sharp-normalise', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.normalise() .normalise()
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -375,11 +375,11 @@ async.series({
} }
}).add('sharp-greyscale', { }).add('sharp-greyscale', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.greyscale() .greyscale()
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -389,12 +389,12 @@ async.series({
} }
}).add('sharp-greyscale-gamma', { }).add('sharp-greyscale-gamma', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.gamma() .gamma()
.greyscale() .greyscale()
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -404,11 +404,11 @@ async.series({
} }
}).add('sharp-progressive', { }).add('sharp-progressive', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.jpeg({ progressive: true }) .jpeg({ progressive: true })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -418,11 +418,11 @@ async.series({
} }
}).add('sharp-without-chroma-subsampling', { }).add('sharp-without-chroma-subsampling', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.jpeg({ chromaSubsampling: '4:4:4' }) .jpeg({ chromaSubsampling: '4:4:4' })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -432,11 +432,11 @@ async.series({
} }
}).add('sharp-rotate', { }).add('sharp-rotate', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.rotate(90) .rotate(90)
.resize(width, height) .resize(width, height)
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -446,11 +446,11 @@ async.series({
} }
}).add('sharp-without-simd', { }).add('sharp-without-simd', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp.simd(false); sharp.simd(false);
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height) .resize(width, height)
.toBuffer(function (err) { .toBuffer((err) => {
sharp.simd(true); sharp.simd(true);
if (err) { if (err) {
throw err; throw err;
@ -461,10 +461,10 @@ async.series({
} }
}).add('sharp-random-access-read', { }).add('sharp-random-access-read', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer, { sequentialRead: false }) sharp(inputJpgBuffer, { sequentialRead: false })
.resize(width, height) .resize(width, height)
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -474,13 +474,13 @@ async.series({
} }
}).add('sharp-crop-entropy', { }).add('sharp-crop-entropy', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height, { .resize(width, height, {
fit: 'cover', fit: 'cover',
position: sharp.strategy.entropy position: sharp.strategy.entropy
}) })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -490,13 +490,13 @@ async.series({
} }
}).add('sharp-crop-attention', { }).add('sharp-crop-attention', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height, { .resize(width, height, {
fit: 'cover', fit: 'cover',
position: sharp.strategy.attention position: sharp.strategy.attention
}) })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -504,21 +504,21 @@ async.series({
} }
}); });
} }
}).on('cycle', function (event) { }).on('cycle', (event) => {
console.log(`operations ${String(event.target)}`); console.log(`operations ${String(event.target)}`);
}).on('complete', function () { }).on('complete', function () {
callback(null, this.filter('fastest').map('name')); callback(null, this.filter('fastest').map('name'));
}).run(); }).run();
}, },
// Comparative speed of kernels // Comparative speed of kernels
kernels: function (callback) { kernels: (callback) => {
const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg); const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg);
(new Benchmark.Suite('kernels')).add('sharp-cubic', { (new Benchmark.Suite('kernels')).add('sharp-cubic', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height, { kernel: 'cubic' }) .resize(width, height, { kernel: 'cubic' })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -528,10 +528,10 @@ async.series({
} }
}).add('sharp-lanczos2', { }).add('sharp-lanczos2', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height, { kernel: 'lanczos2' }) .resize(width, height, { kernel: 'lanczos2' })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -541,10 +541,10 @@ async.series({
} }
}).add('sharp-lanczos3', { }).add('sharp-lanczos3', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height, { kernel: 'lanczos3' }) .resize(width, height, { kernel: 'lanczos3' })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -554,10 +554,10 @@ async.series({
} }
}).add('sharp-mks2013', { }).add('sharp-mks2013', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height, { kernel: 'mks2013' }) .resize(width, height, { kernel: 'mks2013' })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -567,10 +567,10 @@ async.series({
} }
}).add('sharp-mks2021', { }).add('sharp-mks2021', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputJpgBuffer) sharp(inputJpgBuffer)
.resize(width, height, { kernel: 'mks2021' }) .resize(width, height, { kernel: 'mks2021' })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -578,21 +578,21 @@ async.series({
} }
}); });
} }
}).on('cycle', function (event) { }).on('cycle', (event) => {
console.log(`kernels ${String(event.target)}`); console.log(`kernels ${String(event.target)}`);
}).on('complete', function () { }).on('complete', function () {
callback(null, this.filter('fastest').map('name')); callback(null, this.filter('fastest').map('name'));
}).run(); }).run();
}, },
// PNG // PNG
png: function (callback) { png: (callback) => {
const inputPngBuffer = fs.readFileSync(fixtures.inputPngAlphaPremultiplicationLarge); const inputPngBuffer = fs.readFileSync(fixtures.inputPngAlphaPremultiplicationLarge);
const pngSuite = new Benchmark.Suite('png'); const pngSuite = new Benchmark.Suite('png');
const minSamples = 64; const minSamples = 64;
// jimp // jimp
pngSuite.add('jimp-buffer-buffer', { pngSuite.add('jimp-buffer-buffer', {
defer: true, defer: true,
fn: async function (deferred) { fn: async (deferred) => {
const image = await Jimp.read(inputPngBuffer); const image = await Jimp.read(inputPngBuffer);
await image await image
.resize({ w: width, h: heightPng, mode: Jimp.RESIZE_BICUBIC }) .resize({ w: width, h: heightPng, mode: Jimp.RESIZE_BICUBIC })
@ -601,7 +601,7 @@ async.series({
} }
}).add('jimp-file-file', { }).add('jimp-file-file', {
defer: true, defer: true,
fn: async function (deferred) { fn: async (deferred) => {
const image = await Jimp.read(fixtures.inputPngAlphaPremultiplicationLarge); const image = await Jimp.read(fixtures.inputPngAlphaPremultiplicationLarge);
await image await image
.resize({ w: width, h: heightPng, mode: Jimp.RESIZE_BICUBIC }) .resize({ w: width, h: heightPng, mode: Jimp.RESIZE_BICUBIC })
@ -612,18 +612,18 @@ async.series({
// mapnik // mapnik
mapnik && pngSuite.add('mapnik-file-file', { mapnik && pngSuite.add('mapnik-file-file', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
mapnik.Image.open(fixtures.inputPngAlphaPremultiplicationLarge, function (err, img) { mapnik.Image.open(fixtures.inputPngAlphaPremultiplicationLarge, (err, img) => {
if (err) throw err; if (err) throw err;
img.premultiply(function (err, img) { img.premultiply((err, img) => {
if (err) throw err; if (err) throw err;
img.resize(width, heightPng, { img.resize(width, heightPng, {
scaling_method: mapnik.imageScaling.lanczos scaling_method: mapnik.imageScaling.lanczos
}, function (err, img) { }, (err, img) => {
if (err) throw err; if (err) throw err;
img.demultiply(function (err, img) { img.demultiply((err, img) => {
if (err) throw err; if (err) throw err;
img.save(outputPng, 'png32:f=no:z=6', function (err) { img.save(outputPng, 'png32:f=no:z=6', (err) => {
if (err) throw err; if (err) throw err;
deferred.resolve(); deferred.resolve();
}); });
@ -634,18 +634,18 @@ async.series({
} }
}).add('mapnik-buffer-buffer', { }).add('mapnik-buffer-buffer', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
mapnik.Image.fromBytes(inputPngBuffer, { max_size: 3000 }, function (err, img) { mapnik.Image.fromBytes(inputPngBuffer, { max_size: 3000 }, (err, img) => {
if (err) throw err; if (err) throw err;
img.premultiply(function (err, img) { img.premultiply((err, img) => {
if (err) throw err; if (err) throw err;
img.resize(width, heightPng, { img.resize(width, heightPng, {
scaling_method: mapnik.imageScaling.lanczos scaling_method: mapnik.imageScaling.lanczos
}, function (err, img) { }, (err, img) => {
if (err) throw err; if (err) throw err;
img.demultiply(function (err, img) { img.demultiply((err, img) => {
if (err) throw err; if (err) throw err;
img.encode('png32:f=no:z=6', function (err) { img.encode('png32:f=no:z=6', (err) => {
if (err) throw err; if (err) throw err;
deferred.resolve(); deferred.resolve();
}); });
@ -658,7 +658,7 @@ async.series({
// imagemagick // imagemagick
pngSuite.add('imagemagick-file-file', { pngSuite.add('imagemagick-file-file', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
imagemagick.resize({ imagemagick.resize({
srcPath: fixtures.inputPngAlphaPremultiplicationLarge, srcPath: fixtures.inputPngAlphaPremultiplicationLarge,
dstPath: outputPng, dstPath: outputPng,
@ -669,7 +669,7 @@ async.series({
'-define', 'PNG:compression-level=6', '-define', 'PNG:compression-level=6',
'-define', 'PNG:compression-filter=0' '-define', 'PNG:compression-filter=0'
] ]
}, function (err) { }, (err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -681,13 +681,13 @@ async.series({
// gm // gm
pngSuite.add('gm-file-file', { pngSuite.add('gm-file-file', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
gm(fixtures.inputPngAlphaPremultiplicationLarge) gm(fixtures.inputPngAlphaPremultiplicationLarge)
.filter('Lanczos') .filter('Lanczos')
.resize(width, heightPng) .resize(width, heightPng)
.define('PNG:compression-level=6') .define('PNG:compression-level=6')
.define('PNG:compression-filter=0') .define('PNG:compression-filter=0')
.write(outputPng, function (err) { .write(outputPng, (err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -697,13 +697,13 @@ async.series({
} }
}).add('gm-file-buffer', { }).add('gm-file-buffer', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
gm(fixtures.inputPngAlphaPremultiplicationLarge) gm(fixtures.inputPngAlphaPremultiplicationLarge)
.filter('Lanczos') .filter('Lanczos')
.resize(width, heightPng) .resize(width, heightPng)
.define('PNG:compression-level=6') .define('PNG:compression-level=6')
.define('PNG:compression-filter=0') .define('PNG:compression-filter=0')
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -716,11 +716,11 @@ async.series({
pngSuite.add('sharp-buffer-file', { pngSuite.add('sharp-buffer-file', {
defer: true, defer: true,
minSamples, minSamples,
fn: function (deferred) { fn: (deferred) => {
sharp(inputPngBuffer) sharp(inputPngBuffer)
.resize(width, heightPng) .resize(width, heightPng)
.png({ compressionLevel: 6 }) .png({ compressionLevel: 6 })
.toFile(outputPng, function (err) { .toFile(outputPng, (err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -731,11 +731,11 @@ async.series({
}).add('sharp-buffer-buffer', { }).add('sharp-buffer-buffer', {
defer: true, defer: true,
minSamples, minSamples,
fn: function (deferred) { fn: (deferred) => {
sharp(inputPngBuffer) sharp(inputPngBuffer)
.resize(width, heightPng) .resize(width, heightPng)
.png({ compressionLevel: 6 }) .png({ compressionLevel: 6 })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -746,11 +746,11 @@ async.series({
}).add('sharp-file-file', { }).add('sharp-file-file', {
defer: true, defer: true,
minSamples, minSamples,
fn: function (deferred) { fn: (deferred) => {
sharp(fixtures.inputPngAlphaPremultiplicationLarge) sharp(fixtures.inputPngAlphaPremultiplicationLarge)
.resize(width, heightPng) .resize(width, heightPng)
.png({ compressionLevel: 6 }) .png({ compressionLevel: 6 })
.toFile(outputPng, function (err) { .toFile(outputPng, (err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -761,11 +761,11 @@ async.series({
}).add('sharp-file-buffer', { }).add('sharp-file-buffer', {
defer: true, defer: true,
minSamples, minSamples,
fn: function (deferred) { fn: (deferred) => {
sharp(fixtures.inputPngAlphaPremultiplicationLarge) sharp(fixtures.inputPngAlphaPremultiplicationLarge)
.resize(width, heightPng) .resize(width, heightPng)
.png({ compressionLevel: 6 }) .png({ compressionLevel: 6 })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -776,11 +776,11 @@ async.series({
}).add('sharp-progressive', { }).add('sharp-progressive', {
defer: true, defer: true,
minSamples, minSamples,
fn: function (deferred) { fn: (deferred) => {
sharp(inputPngBuffer) sharp(inputPngBuffer)
.resize(width, heightPng) .resize(width, heightPng)
.png({ compressionLevel: 6, progressive: true }) .png({ compressionLevel: 6, progressive: true })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -791,11 +791,11 @@ async.series({
}).add('sharp-adaptiveFiltering', { }).add('sharp-adaptiveFiltering', {
defer: true, defer: true,
minSamples, minSamples,
fn: function (deferred) { fn: (deferred) => {
sharp(inputPngBuffer) sharp(inputPngBuffer)
.resize(width, heightPng) .resize(width, heightPng)
.png({ adaptiveFiltering: true, compressionLevel: 6 }) .png({ adaptiveFiltering: true, compressionLevel: 6 })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -806,11 +806,11 @@ async.series({
}).add('sharp-compressionLevel=9', { }).add('sharp-compressionLevel=9', {
defer: true, defer: true,
minSamples, minSamples,
fn: function (deferred) { fn: (deferred) => {
sharp(inputPngBuffer) sharp(inputPngBuffer)
.resize(width, heightPng) .resize(width, heightPng)
.png({ compressionLevel: 9 }) .png({ compressionLevel: 9 })
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -819,21 +819,21 @@ async.series({
}); });
} }
}); });
pngSuite.on('cycle', function (event) { pngSuite.on('cycle', (event) => {
console.log(` png ${String(event.target)}`); console.log(` png ${String(event.target)}`);
}).on('complete', function () { }).on('complete', function () {
callback(null, this.filter('fastest').map('name')); callback(null, this.filter('fastest').map('name'));
}).run(); }).run();
}, },
// WebP // WebP
webp: function (callback) { webp: (callback) => {
const inputWebPBuffer = fs.readFileSync(fixtures.inputWebP); const inputWebPBuffer = fs.readFileSync(fixtures.inputWebP);
(new Benchmark.Suite('webp')).add('sharp-buffer-file', { (new Benchmark.Suite('webp')).add('sharp-buffer-file', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputWebPBuffer) sharp(inputWebPBuffer)
.resize(width, height) .resize(width, height)
.toFile(outputWebP, function (err) { .toFile(outputWebP, (err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -843,10 +843,10 @@ async.series({
} }
}).add('sharp-buffer-buffer', { }).add('sharp-buffer-buffer', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(inputWebPBuffer) sharp(inputWebPBuffer)
.resize(width, height) .resize(width, height)
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -856,10 +856,10 @@ async.series({
} }
}).add('sharp-file-file', { }).add('sharp-file-file', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(fixtures.inputWebP) sharp(fixtures.inputWebP)
.resize(width, height) .resize(width, height)
.toFile(outputWebP, function (err) { .toFile(outputWebP, (err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -869,10 +869,10 @@ async.series({
} }
}).add('sharp-file-buffer', { }).add('sharp-file-buffer', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(fixtures.inputWebP) sharp(fixtures.inputWebP)
.resize(width, height) .resize(width, height)
.toBuffer(function (err) { .toBuffer((err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -880,17 +880,17 @@ async.series({
} }
}); });
} }
}).on('cycle', function (event) { }).on('cycle', (event) => {
console.log(`webp ${String(event.target)}`); console.log(`webp ${String(event.target)}`);
}).on('complete', function () { }).on('complete', function () {
callback(null, this.filter('fastest').map('name')); callback(null, this.filter('fastest').map('name'));
}).run(); }).run();
} }
}, function (err, results) { }, (err, results) => {
if (err) { if (err) {
throw err; throw err;
} }
Object.keys(results).forEach(function (format) { Object.keys(results).forEach((format) => {
if (results[format].toString().substr(0, 5) !== 'sharp') { if (results[format].toString().substr(0, 5) !== 'sharp') {
console.log(`sharp was slower than ${results[format]} for ${format}`); console.log(`sharp was slower than ${results[format]} for ${format}`);
} }

View File

@ -16,13 +16,11 @@ sharp.cache(false);
const min = 320; const min = 320;
const max = 960; const max = 960;
const randomDimension = function () { const randomDimension = () => Math.ceil((Math.random() * (max - min)) + min);
return Math.ceil((Math.random() * (max - min)) + min);
};
new Benchmark.Suite('random').add('imagemagick', { new Benchmark.Suite('random').add('imagemagick', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
imagemagick.resize({ imagemagick.resize({
srcPath: fixtures.inputJpg, srcPath: fixtures.inputJpg,
dstPath: fixtures.path('output.jpg'), dstPath: fixtures.path('output.jpg'),
@ -31,7 +29,7 @@ new Benchmark.Suite('random').add('imagemagick', {
height: randomDimension(), height: randomDimension(),
format: 'jpg', format: 'jpg',
filter: 'Lanczos' filter: 'Lanczos'
}, function (err) { }, (err) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -41,12 +39,12 @@ new Benchmark.Suite('random').add('imagemagick', {
} }
}).add('gm', { }).add('gm', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
gm(fixtures.inputJpg) gm(fixtures.inputJpg)
.resize(randomDimension(), randomDimension()) .resize(randomDimension(), randomDimension())
.filter('Lanczos') .filter('Lanczos')
.quality(80) .quality(80)
.toBuffer(function (err, buffer) { .toBuffer((err, buffer) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -57,10 +55,10 @@ new Benchmark.Suite('random').add('imagemagick', {
} }
}).add('sharp', { }).add('sharp', {
defer: true, defer: true,
fn: function (deferred) { fn: (deferred) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(randomDimension(), randomDimension()) .resize(randomDimension(), randomDimension())
.toBuffer(function (err, buffer) { .toBuffer((err, buffer) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -69,7 +67,7 @@ new Benchmark.Suite('random').add('imagemagick', {
} }
}); });
} }
}).on('cycle', function (event) { }).on('cycle', (event) => {
console.log(String(event.target)); console.log(String(event.target));
}).on('complete', function () { }).on('complete', function () {
const winner = this.filter('fastest').map('name'); const winner = this.filter('fastest').map('name');

View File

@ -8,9 +8,7 @@ const sharp = require('../../');
const maxColourDistance = require('../../lib/sharp')._maxColourDistance; const maxColourDistance = require('../../lib/sharp')._maxColourDistance;
// Helpers // Helpers
const getPath = function (filename) { const getPath = (filename) => path.join(__dirname, filename);
return path.join(__dirname, filename);
};
// Generates a 64-bit-as-binary-string image fingerprint // Generates a 64-bit-as-binary-string image fingerprint
// Based on the dHash gradient method - see http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html // Based on the dHash gradient method - see http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html
@ -22,7 +20,7 @@ async function fingerprint (image) {
.resize(9, 8, { fit: sharp.fit.fill }) .resize(9, 8, { fit: sharp.fit.fill })
.raw() .raw()
.toBuffer() .toBuffer()
.then(function (data) { .then((data) => {
let fingerprint = ''; let fingerprint = '';
for (let col = 0; col < 8; col++) { for (let col = 0; col < 8; col++) {
for (let row = 0; row < 8; row++) { for (let row = 0; row < 8; row++) {
@ -148,14 +146,12 @@ module.exports = {
path: getPath, path: getPath,
// Path for expected output images // Path for expected output images
expected: function (filename) { expected: (filename) => getPath(path.join('expected', filename)),
return getPath(path.join('expected', filename));
},
// Verify similarity of expected vs actual images via fingerprint // Verify similarity of expected vs actual images via fingerprint
// Specify distance threshold using `options={threshold: 42}`, default // Specify distance threshold using `options={threshold: 42}`, default
// `threshold` is 5; // `threshold` is 5;
assertSimilar: async function (expectedImage, actualImage, options, callback) { assertSimilar: async (expectedImage, actualImage, options, callback) => {
if (typeof options === 'function') { if (typeof options === 'function') {
callback = options; callback = options;
options = {}; options = {};
@ -195,7 +191,7 @@ module.exports = {
} }
}, },
assertMaxColourDistance: function (actualImagePath, expectedImagePath, acceptedDistance) { assertMaxColourDistance: (actualImagePath, expectedImagePath, acceptedDistance) => {
if (typeof actualImagePath !== 'string') { if (typeof actualImagePath !== 'string') {
throw new TypeError(`\`actualImagePath\` must be a string; got ${actualImagePath}`); throw new TypeError(`\`actualImagePath\` must be a string; got ${actualImagePath}`);
} }

View File

@ -8,12 +8,12 @@ const assert = require('node:assert');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
const sharp = require('../../'); const sharp = require('../../');
describe('Alpha transparency', function () { describe('Alpha transparency', () => {
it('Flatten to black', function (_t, done) { it('Flatten to black', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.flatten() .flatten()
.resize(400, 300) .resize(400, 300)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(400, info.width); assert.strictEqual(400, info.width);
assert.strictEqual(300, info.height); assert.strictEqual(300, info.height);
@ -21,14 +21,14 @@ describe('Alpha transparency', function () {
}); });
}); });
it('Flatten to RGB orange', function (_t, done) { it('Flatten to RGB orange', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(400, 300) .resize(400, 300)
.flatten({ .flatten({
background: { r: 255, g: 102, b: 0 } background: { r: 255, g: 102, b: 0 }
}) })
.jpeg({ chromaSubsampling: '4:4:4' }) .jpeg({ chromaSubsampling: '4:4:4' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(400, info.width); assert.strictEqual(400, info.width);
assert.strictEqual(300, info.height); assert.strictEqual(300, info.height);
@ -36,12 +36,12 @@ describe('Alpha transparency', function () {
}); });
}); });
it('Flatten to CSS/hex orange', function (_t, done) { it('Flatten to CSS/hex orange', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(400, 300) .resize(400, 300)
.flatten({ background: '#ff6600' }) .flatten({ background: '#ff6600' })
.jpeg({ chromaSubsampling: '4:4:4' }) .jpeg({ chromaSubsampling: '4:4:4' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(400, info.width); assert.strictEqual(400, info.width);
assert.strictEqual(300, info.height); assert.strictEqual(300, info.height);
@ -49,13 +49,13 @@ describe('Alpha transparency', function () {
}); });
}); });
it('Flatten 16-bit PNG with transparency to orange', function (_t, done) { it('Flatten 16-bit PNG with transparency to orange', (_t, done) => {
const output = fixtures.path('output.flatten-rgb16-orange.jpg'); const output = fixtures.path('output.flatten-rgb16-orange.jpg');
sharp(fixtures.inputPngWithTransparency16bit) sharp(fixtures.inputPngWithTransparency16bit)
.flatten({ .flatten({
background: { r: 255, g: 102, b: 0 } background: { r: 255, g: 102, b: 0 }
}) })
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
assert.strictEqual(32, info.width); assert.strictEqual(32, info.width);
@ -65,10 +65,10 @@ describe('Alpha transparency', function () {
}); });
}); });
it('Do not flatten', function (_t, done) { it('Do not flatten', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.flatten(false) .flatten(false)
.toBuffer(function (err, _data, info) { .toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(4, info.channels); assert.strictEqual(4, info.channels);
@ -76,10 +76,10 @@ describe('Alpha transparency', function () {
}); });
}); });
it('Ignored for JPEG', function (_t, done) { it('Ignored for JPEG', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.flatten({ background: '#ff0000' }) .flatten({ background: '#ff0000' })
.toBuffer(function (err, _data, info) { .toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
@ -99,68 +99,60 @@ describe('Alpha transparency', function () {
}); });
}); });
it('Enlargement with non-nearest neighbor interpolation shouldnt cause dark edges', function () { it('Enlargement with non-nearest neighbor interpolation shouldnt cause dark edges', () => {
const base = 'alpha-premultiply-enlargement-2048x1536-paper.png'; const base = 'alpha-premultiply-enlargement-2048x1536-paper.png';
const actual = fixtures.path(`output.${base}`); const actual = fixtures.path(`output.${base}`);
const expected = fixtures.expected(base); const expected = fixtures.expected(base);
return sharp(fixtures.inputPngAlphaPremultiplicationSmall) return sharp(fixtures.inputPngAlphaPremultiplicationSmall)
.resize(2048, 1536) .resize(2048, 1536)
.toFile(actual) .toFile(actual)
.then(function () { .then(() => {
fixtures.assertMaxColourDistance(actual, expected, 102); fixtures.assertMaxColourDistance(actual, expected, 102);
}); });
}); });
it('Reduction with non-nearest neighbor interpolation shouldnt cause dark edges', function () { it('Reduction with non-nearest neighbor interpolation shouldnt cause dark edges', () => {
const base = 'alpha-premultiply-reduction-1024x768-paper.png'; const base = 'alpha-premultiply-reduction-1024x768-paper.png';
const actual = fixtures.path(`output.${base}`); const actual = fixtures.path(`output.${base}`);
const expected = fixtures.expected(base); const expected = fixtures.expected(base);
return sharp(fixtures.inputPngAlphaPremultiplicationLarge) return sharp(fixtures.inputPngAlphaPremultiplicationLarge)
.resize(1024, 768) .resize(1024, 768)
.toFile(actual) .toFile(actual)
.then(function () { .then(() => {
fixtures.assertMaxColourDistance(actual, expected, 102); fixtures.assertMaxColourDistance(actual, expected, 102);
}); });
}); });
it('Removes alpha from fixtures with transparency, ignores those without', function () { it('Removes alpha from fixtures with transparency, ignores those without', () => Promise.all([
return Promise.all([
fixtures.inputPngWithTransparency, fixtures.inputPngWithTransparency,
fixtures.inputPngWithTransparency16bit, fixtures.inputPngWithTransparency16bit,
fixtures.inputWebPWithTransparency, fixtures.inputWebPWithTransparency,
fixtures.inputJpg, fixtures.inputJpg,
fixtures.inputPng, fixtures.inputPng,
fixtures.inputWebP fixtures.inputWebP
].map(function (input) { ].map((input) => sharp(input)
return sharp(input)
.resize(10) .resize(10)
.removeAlpha() .removeAlpha()
.toBuffer({ resolveWithObject: true }) .toBuffer({ resolveWithObject: true })
.then(function (result) { .then((result) => {
assert.strictEqual(3, result.info.channels); assert.strictEqual(3, result.info.channels);
}); }))));
}));
});
it('Ensures alpha from fixtures without transparency, ignores those with', function () { it('Ensures alpha from fixtures without transparency, ignores those with', () => Promise.all([
return Promise.all([
fixtures.inputPngWithTransparency, fixtures.inputPngWithTransparency,
fixtures.inputPngWithTransparency16bit, fixtures.inputPngWithTransparency16bit,
fixtures.inputWebPWithTransparency, fixtures.inputWebPWithTransparency,
fixtures.inputJpg, fixtures.inputJpg,
fixtures.inputPng, fixtures.inputPng,
fixtures.inputWebP fixtures.inputWebP
].map(function (input) { ].map((input) => sharp(input)
return sharp(input)
.resize(10) .resize(10)
.ensureAlpha() .ensureAlpha()
.png() .png()
.toBuffer({ resolveWithObject: true }) .toBuffer({ resolveWithObject: true })
.then(function (result) { .then((result) => {
assert.strictEqual(4, result.info.channels); assert.strictEqual(4, result.info.channels);
}); }))));
}));
});
it('Valid ensureAlpha value used for alpha channel', async () => { it('Valid ensureAlpha value used for alpha channel', async () => {
const background = { r: 255, g: 0, b: 0 }; const background = { r: 255, g: 0, b: 0 };

View File

@ -8,18 +8,18 @@ const assert = require('node:assert');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
const sharp = require('../../'); const sharp = require('../../');
describe('Bandbool per-channel boolean operations', function () { describe('Bandbool per-channel boolean operations', () => {
[ [
sharp.bool.and, sharp.bool.and,
sharp.bool.or, sharp.bool.or,
sharp.bool.eor sharp.bool.eor
] ]
.forEach(function (op) { .forEach((op) => {
it(`${op} operation`, function (_t, done) { it(`${op} operation`, (_t, done) => {
sharp(fixtures.inputPngBooleanNoAlpha) sharp(fixtures.inputPngBooleanNoAlpha)
.bandbool(op) .bandbool(op)
.toColourspace('b-w') .toColourspace('b-w')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(200, info.width); assert.strictEqual(200, info.width);
assert.strictEqual(200, info.height); assert.strictEqual(200, info.height);
@ -29,24 +29,24 @@ describe('Bandbool per-channel boolean operations', function () {
}); });
}); });
it('sRGB image retains 3 channels', function (_t, done) { it('sRGB image retains 3 channels', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.bandbool('and') .bandbool('and')
.toBuffer(function (err, _data, info) { .toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
done(); done();
}); });
}); });
it('Invalid operation', function () { it('Invalid operation', () => {
assert.throws(function () { assert.throws(() => {
sharp().bandbool('fail'); sharp().bandbool('fail');
}); });
}); });
it('Missing operation', function () { it('Missing operation', () => {
assert.throws(function () { assert.throws(() => {
sharp().bandbool(); sharp().bandbool();
}); });
}); });

View File

@ -9,12 +9,12 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Blur', function () { describe('Blur', () => {
it('specific radius 1', function (_t, done) { it('specific radius 1', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.blur(1) .blur(1)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -23,11 +23,11 @@ describe('Blur', function () {
}); });
}); });
it('specific radius 10', function (_t, done) { it('specific radius 10', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.blur(10) .blur(10)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -36,11 +36,11 @@ describe('Blur', function () {
}); });
}); });
it('specific options.sigma 10', function (_t, done) { it('specific options.sigma 10', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.blur({ sigma: 10 }) .blur({ sigma: 10 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -49,11 +49,11 @@ describe('Blur', function () {
}); });
}); });
it('specific radius 0.3', function (_t, done) { it('specific radius 0.3', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.blur(0.3) .blur(0.3)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -62,11 +62,11 @@ describe('Blur', function () {
}); });
}); });
it('mild blur', function (_t, done) { it('mild blur', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.blur() .blur()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -75,17 +75,17 @@ describe('Blur', function () {
}); });
}); });
it('invalid radius', function () { it('invalid radius', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).blur(0.1); sharp(fixtures.inputJpg).blur(0.1);
}); });
}); });
it('blurred image is smaller than non-blurred', function (_t, done) { it('blurred image is smaller than non-blurred', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.blur(false) .blur(false)
.toBuffer(function (err, notBlurred, info) { .toBuffer((err, notBlurred, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, notBlurred.length > 0); assert.strictEqual(true, notBlurred.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -94,7 +94,7 @@ describe('Blur', function () {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.blur(true) .blur(true)
.toBuffer(function (err, blurred, info) { .toBuffer((err, blurred, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, blurred.length > 0); assert.strictEqual(true, blurred.length > 0);
assert.strictEqual(true, blurred.length < notBlurred.length); assert.strictEqual(true, blurred.length < notBlurred.length);
@ -106,18 +106,18 @@ describe('Blur', function () {
}); });
}); });
it('invalid precision', function () { it('invalid precision', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).blur({ sigma: 1, precision: 'invalid' }); sharp(fixtures.inputJpg).blur({ sigma: 1, precision: 'invalid' });
}, /Expected one of: integer, float, approximate for precision but received invalid of type string/); }, /Expected one of: integer, float, approximate for precision but received invalid of type string/);
}); });
it('invalid minAmplitude', function () { it('invalid minAmplitude', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).blur({ sigma: 1, minAmplitude: 0 }); sharp(fixtures.inputJpg).blur({ sigma: 1, minAmplitude: 0 });
}, /Expected number between 0.001 and 1 for minAmplitude but received 0 of type number/); }, /Expected number between 0.001 and 1 for minAmplitude but received 0 of type number/);
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).blur({ sigma: 1, minAmplitude: 1.01 }); sharp(fixtures.inputJpg).blur({ sigma: 1, minAmplitude: 1.01 });
}, /Expected number between 0.001 and 1 for minAmplitude but received 1.01 of type number/); }, /Expected number between 0.001 and 1 for minAmplitude but received 1.01 of type number/);
}); });
@ -150,8 +150,8 @@ describe('Blur', function () {
await fixtures.assertSimilar(fixtures.expected('blur-10.jpg'), minAmplitudeLow); await fixtures.assertSimilar(fixtures.expected('blur-10.jpg'), minAmplitudeLow);
}); });
it('options.sigma is required if options object is passed', function () { it('options.sigma is required if options object is passed', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).blur({ precision: 'invalid' }); sharp(fixtures.inputJpg).blur({ precision: 'invalid' });
}, /Expected number between 0.3 and 1000 for options.sigma but received undefined of type undefined/); }, /Expected number between 0.3 and 1000 for options.sigma but received undefined of type undefined/);
}); });

View File

@ -10,7 +10,7 @@ const assert = require('node:assert');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
const sharp = require('../../'); const sharp = require('../../');
describe('Boolean operation between two images', function () { describe('Boolean operation between two images', () => {
const inputJpgBooleanTestBuffer = fs.readFileSync(fixtures.inputJpgBooleanTest); const inputJpgBooleanTestBuffer = fs.readFileSync(fixtures.inputJpgBooleanTest);
[ [
@ -18,12 +18,12 @@ describe('Boolean operation between two images', function () {
sharp.bool.or, sharp.bool.or,
sharp.bool.eor sharp.bool.eor
] ]
.forEach(function (op) { .forEach((op) => {
it(`${op} operation, file`, function (_t, done) { it(`${op} operation, file`, (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.boolean(fixtures.inputJpgBooleanTest, op) .boolean(fixtures.inputJpgBooleanTest, op)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -31,11 +31,11 @@ describe('Boolean operation between two images', function () {
}); });
}); });
it(`${op} operation, buffer`, function (_t, done) { it(`${op} operation, buffer`, (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.boolean(inputJpgBooleanTestBuffer, op) .boolean(inputJpgBooleanTestBuffer, op)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -43,15 +43,15 @@ describe('Boolean operation between two images', function () {
}); });
}); });
it(`${op} operation, raw`, function (_t, done) { it(`${op} operation, raw`, (_t, done) => {
sharp(fixtures.inputJpgBooleanTest) sharp(fixtures.inputJpgBooleanTest)
.raw() .raw()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.boolean(data, op, { raw: info }) .boolean(data, op, { raw: info })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -61,20 +61,20 @@ describe('Boolean operation between two images', function () {
}); });
}); });
it('Invalid operation', function () { it('Invalid operation', () => {
assert.throws(function () { assert.throws(() => {
sharp().boolean(fixtures.inputJpgBooleanTest, 'fail'); sharp().boolean(fixtures.inputJpgBooleanTest, 'fail');
}); });
}); });
it('Invalid operation, non-string', function () { it('Invalid operation, non-string', () => {
assert.throws(function () { assert.throws(() => {
sharp().boolean(fixtures.inputJpgBooleanTest, null); sharp().boolean(fixtures.inputJpgBooleanTest, null);
}); });
}); });
it('Missing input', function () { it('Missing input', () => {
assert.throws(function () { assert.throws(() => {
sharp().boolean(); sharp().boolean();
}); });
}); });

View File

@ -9,132 +9,132 @@ const assert = require('node:assert');
const sharp = require('../../lib'); const sharp = require('../../lib');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Clahe', function () { describe('Clahe', () => {
it('width 5 width 5 maxSlope 0', function (_t, done) { it('width 5 width 5 maxSlope 0', (_t, done) => {
sharp(fixtures.inputJpgClahe) sharp(fixtures.inputJpgClahe)
.clahe({ width: 5, height: 5, maxSlope: 0 }) .clahe({ width: 5, height: 5, maxSlope: 0 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
fixtures.assertSimilar(fixtures.expected('clahe-5-5-0.jpg'), data, { threshold: 10 }, done); fixtures.assertSimilar(fixtures.expected('clahe-5-5-0.jpg'), data, { threshold: 10 }, done);
}); });
}); });
it('width 5 width 5 maxSlope 5', function (_t, done) { it('width 5 width 5 maxSlope 5', (_t, done) => {
sharp(fixtures.inputJpgClahe) sharp(fixtures.inputJpgClahe)
.clahe({ width: 5, height: 5, maxSlope: 5 }) .clahe({ width: 5, height: 5, maxSlope: 5 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
fixtures.assertSimilar(fixtures.expected('clahe-5-5-5.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('clahe-5-5-5.jpg'), data, done);
}); });
}); });
it('width 11 width 25 maxSlope 14', function (_t, done) { it('width 11 width 25 maxSlope 14', (_t, done) => {
sharp(fixtures.inputJpgClahe) sharp(fixtures.inputJpgClahe)
.clahe({ width: 11, height: 25, maxSlope: 14 }) .clahe({ width: 11, height: 25, maxSlope: 14 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
fixtures.assertSimilar(fixtures.expected('clahe-11-25-14.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('clahe-11-25-14.jpg'), data, done);
}); });
}); });
it('width 50 width 50 maxSlope 0', function (_t, done) { it('width 50 width 50 maxSlope 0', (_t, done) => {
sharp(fixtures.inputJpgClahe) sharp(fixtures.inputJpgClahe)
.clahe({ width: 50, height: 50, maxSlope: 0 }) .clahe({ width: 50, height: 50, maxSlope: 0 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
fixtures.assertSimilar(fixtures.expected('clahe-50-50-0.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('clahe-50-50-0.jpg'), data, done);
}); });
}); });
it('width 50 width 50 maxSlope 14', function (_t, done) { it('width 50 width 50 maxSlope 14', (_t, done) => {
sharp(fixtures.inputJpgClahe) sharp(fixtures.inputJpgClahe)
.clahe({ width: 50, height: 50, maxSlope: 14 }) .clahe({ width: 50, height: 50, maxSlope: 14 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
fixtures.assertSimilar(fixtures.expected('clahe-50-50-14.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('clahe-50-50-14.jpg'), data, done);
}); });
}); });
it('width 100 width 50 maxSlope 3', function (_t, done) { it('width 100 width 50 maxSlope 3', (_t, done) => {
sharp(fixtures.inputJpgClahe) sharp(fixtures.inputJpgClahe)
.clahe({ width: 100, height: 50, maxSlope: 3 }) .clahe({ width: 100, height: 50, maxSlope: 3 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
fixtures.assertSimilar(fixtures.expected('clahe-100-50-3.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('clahe-100-50-3.jpg'), data, done);
}); });
}); });
it('width 100 width 100 maxSlope 0', function (_t, done) { it('width 100 width 100 maxSlope 0', (_t, done) => {
sharp(fixtures.inputJpgClahe) sharp(fixtures.inputJpgClahe)
.clahe({ width: 100, height: 100, maxSlope: 0 }) .clahe({ width: 100, height: 100, maxSlope: 0 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
fixtures.assertSimilar(fixtures.expected('clahe-100-100-0.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('clahe-100-100-0.jpg'), data, done);
}); });
}); });
it('invalid maxSlope', function () { it('invalid maxSlope', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100, maxSlope: -5 }); sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100, maxSlope: -5 });
}); });
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100, maxSlope: 110 }); sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100, maxSlope: 110 });
}); });
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100, maxSlope: 5.5 }); sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100, maxSlope: 5.5 });
}); });
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100, maxSlope: 'a string' }); sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100, maxSlope: 'a string' });
}); });
}); });
it('invalid width', function () { it('invalid width', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe({ width: 100.5, height: 100 }); sharp(fixtures.inputJpgClahe).clahe({ width: 100.5, height: 100 });
}); });
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe({ width: -5, height: 100 }); sharp(fixtures.inputJpgClahe).clahe({ width: -5, height: 100 });
}); });
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe({ width: true, height: 100 }); sharp(fixtures.inputJpgClahe).clahe({ width: true, height: 100 });
}); });
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe({ width: 'string test', height: 100 }); sharp(fixtures.inputJpgClahe).clahe({ width: 'string test', height: 100 });
}); });
}); });
it('invalid height', function () { it('invalid height', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100.5 }); sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 100.5 });
}); });
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: -5 }); sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: -5 });
}); });
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: true }); sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: true });
}); });
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 'string test' }); sharp(fixtures.inputJpgClahe).clahe({ width: 100, height: 'string test' });
}); });
}); });
it('invalid options object', function () { it('invalid options object', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgClahe).clahe(100, 100, 5); sharp(fixtures.inputJpgClahe).clahe(100, 100, 5);
}); });
}); });
it('uses default maxSlope of 3', function (_t, done) { it('uses default maxSlope of 3', (_t, done) => {
sharp(fixtures.inputJpgClahe) sharp(fixtures.inputJpgClahe)
.clahe({ width: 100, height: 50 }) .clahe({ width: 100, height: 50 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
fixtures.assertSimilar(fixtures.expected('clahe-100-50-3.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('clahe-100-50-3.jpg'), data, done);

View File

@ -10,21 +10,21 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Clone', function () { describe('Clone', () => {
beforeEach(function () { beforeEach(() => {
sharp.cache(false); sharp.cache(false);
}); });
afterEach(function () { afterEach(() => {
sharp.cache(true); sharp.cache(true);
}); });
it('Read from Stream and write to multiple Streams', function (_t, done) { it('Read from Stream and write to multiple Streams', (_t, done) => {
let finishEventsExpected = 2; let finishEventsExpected = 2;
// Output stream 1 // Output stream 1
const output1 = fixtures.path('output.multi-stream.1.jpg'); const output1 = fixtures.path('output.multi-stream.1.jpg');
const writable1 = fs.createWriteStream(output1); const writable1 = fs.createWriteStream(output1);
writable1.on('finish', function () { writable1.on('finish', () => {
sharp(output1).toBuffer(function (err, data, info) { sharp(output1).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -41,8 +41,8 @@ describe('Clone', function () {
// Output stream 2 // Output stream 2
const output2 = fixtures.path('output.multi-stream.2.jpg'); const output2 = fixtures.path('output.multi-stream.2.jpg');
const writable2 = fs.createWriteStream(output2); const writable2 = fs.createWriteStream(output2);
writable2.on('finish', function () { writable2.on('finish', () => {
sharp(output2).toBuffer(function (err, data, info) { sharp(output2).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -65,14 +65,14 @@ describe('Clone', function () {
fs.createReadStream(fixtures.inputJpg).pipe(rotator); fs.createReadStream(fixtures.inputJpg).pipe(rotator);
}); });
it('Stream-based input attaches finish event listener to original', function () { it('Stream-based input attaches finish event listener to original', () => {
const original = sharp(); const original = sharp();
const clone = original.clone(); const clone = original.clone();
assert.strictEqual(1, original.listenerCount('finish')); assert.strictEqual(1, original.listenerCount('finish'));
assert.strictEqual(0, clone.listenerCount('finish')); assert.strictEqual(0, clone.listenerCount('finish'));
}); });
it('Non Stream-based input does not attach finish event listeners', function () { it('Non Stream-based input does not attach finish event listeners', () => {
const original = sharp(fixtures.inputJpg); const original = sharp(fixtures.inputJpg);
const clone = original.clone(); const clone = original.clone();
assert.strictEqual(0, original.listenerCount('finish')); assert.strictEqual(0, original.listenerCount('finish'));

View File

@ -9,15 +9,15 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Colour space conversion', function () { describe('Colour space conversion', () => {
it('To greyscale', function (_t, done) { it('To greyscale', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.greyscale() .greyscale()
.toFile(fixtures.path('output.greyscale-gamma-0.0.jpg'), done); .toFile(fixtures.path('output.greyscale-gamma-0.0.jpg'), done);
}); });
it('To greyscale with gamma correction', function (_t, done) { it('To greyscale with gamma correction', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.gamma() .gamma()
@ -25,19 +25,19 @@ describe('Colour space conversion', function () {
.toFile(fixtures.path('output.greyscale-gamma-2.2.jpg'), done); .toFile(fixtures.path('output.greyscale-gamma-2.2.jpg'), done);
}); });
it('Not to greyscale', function (_t, done) { it('Not to greyscale', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.greyscale(false) .greyscale(false)
.toFile(fixtures.path('output.greyscale-not.jpg'), done); .toFile(fixtures.path('output.greyscale-not.jpg'), done);
}); });
it('Greyscale with single channel output', function (_t, done) { it('Greyscale with single channel output', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.greyscale() .greyscale()
.toColourspace('b-w') .toColourspace('b-w')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(1, info.channels); assert.strictEqual(1, info.channels);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -56,10 +56,10 @@ describe('Colour space conversion', function () {
assert.strictEqual(format, 'webp'); assert.strictEqual(format, 'webp');
}); });
it('From CMYK to sRGB', function (_t, done) { it('From CMYK to sRGB', (_t, done) => {
sharp(fixtures.inputJpgWithCmykProfile) sharp(fixtures.inputJpgWithCmykProfile)
.resize(320) .resize(320)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -68,13 +68,13 @@ describe('Colour space conversion', function () {
}); });
}); });
it('From CMYK to sRGB with white background, not yellow', function (_t, done) { it('From CMYK to sRGB with white background, not yellow', (_t, done) => {
sharp(fixtures.inputJpgWithCmykProfile) sharp(fixtures.inputJpgWithCmykProfile)
.resize(320, 240, { .resize(320, 240, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: 'white' background: 'white'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -83,10 +83,10 @@ describe('Colour space conversion', function () {
}); });
}); });
it('From profile-less CMYK to sRGB', function (_t, done) { it('From profile-less CMYK to sRGB', (_t, done) => {
sharp(fixtures.inputJpgWithCmykNoProfile) sharp(fixtures.inputJpgWithCmykNoProfile)
.resize(320) .resize(320)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -130,7 +130,7 @@ describe('Colour space conversion', function () {
.pipelineColourspace('cmyk') .pipelineColourspace('cmyk')
.withIccProfile(fixtures.path('XCMYK 2017.icc')) .withIccProfile(fixtures.path('XCMYK 2017.icc'))
.negate() .negate()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('tiff', info.format); assert.strictEqual('tiff', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -144,13 +144,13 @@ describe('Colour space conversion', function () {
}); });
}); });
it('From sRGB with RGB16 pipeline, resize with gamma, to sRGB', function (_t, done) { it('From sRGB with RGB16 pipeline, resize with gamma, to sRGB', (_t, done) => {
sharp(fixtures.inputPngGradients) sharp(fixtures.inputPngGradients)
.pipelineColourspace('rgb16') .pipelineColourspace('rgb16')
.resize(320) .resize(320)
.gamma() .gamma()
.toColourspace('srgb') .toColourspace('srgb')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
fixtures.assertSimilar(fixtures.expected('colourspace-gradients-gamma-resize.png'), data, { fixtures.assertSimilar(fixtures.expected('colourspace-gradients-gamma-resize.png'), data, {
@ -178,15 +178,15 @@ describe('Colour space conversion', function () {
assert.strictEqual(b, 34); assert.strictEqual(b, 34);
}); });
it('Invalid pipelineColourspace input', function () { it('Invalid pipelineColourspace input', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.pipelineColorspace(null); .pipelineColorspace(null);
}, /Expected string for colourspace but received null of type object/); }, /Expected string for colourspace but received null of type object/);
}); });
it('Invalid toColourspace input', function () { it('Invalid toColourspace input', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.toColourspace(null); .toColourspace(null);
}); });

View File

@ -9,8 +9,8 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Convolve', function () { describe('Convolve', () => {
it('specific convolution kernel 1', function (_t, done) { it('specific convolution kernel 1', (_t, done) => {
sharp(fixtures.inputPngStripesV) sharp(fixtures.inputPngStripesV)
.convolve({ .convolve({
width: 3, width: 3,
@ -23,7 +23,7 @@ describe('Convolve', function () {
10, 20, 10 10, 20, 10
] ]
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -32,7 +32,7 @@ describe('Convolve', function () {
}); });
}); });
it('specific convolution kernel 2', function (_t, done) { it('specific convolution kernel 2', (_t, done) => {
sharp(fixtures.inputPngStripesH) sharp(fixtures.inputPngStripesH)
.convolve({ .convolve({
width: 3, width: 3,
@ -43,7 +43,7 @@ describe('Convolve', function () {
1, 0, 1 1, 0, 1
] ]
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -52,7 +52,7 @@ describe('Convolve', function () {
}); });
}); });
it('horizontal Sobel operator', function (_t, done) { it('horizontal Sobel operator', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.convolve({ .convolve({
@ -64,7 +64,7 @@ describe('Convolve', function () {
-1, 0, 1 -1, 0, 1
] ]
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -73,14 +73,14 @@ describe('Convolve', function () {
}); });
}); });
describe('invalid kernel specification', function () { describe('invalid kernel specification', () => {
it('missing', function () { it('missing', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).convolve({}); sharp(fixtures.inputJpg).convolve({});
}); });
}); });
it('incorrect data format', function () { it('incorrect data format', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).convolve({ sharp(fixtures.inputJpg).convolve({
width: 3, width: 3,
height: 3, height: 3,
@ -88,8 +88,8 @@ describe('Convolve', function () {
}); });
}); });
}); });
it('incorrect dimensions', function () { it('incorrect dimensions', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).convolve({ sharp(fixtures.inputJpg).convolve({
width: 3, width: 3,
height: 4, height: 4,

View File

@ -4,11 +4,11 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Dilate', function () { describe('Dilate', () => {
it('dilate 1 png', function (_t, done) { it('dilate 1 png', (_t, done) => {
sharp(fixtures.inputPngDotAndLines) sharp(fixtures.inputPngDotAndLines)
.dilate(1) .dilate(1)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(100, info.width); assert.strictEqual(100, info.width);
@ -17,10 +17,10 @@ describe('Dilate', function () {
}); });
}); });
it('dilate 1 png - default width', function (_t, done) { it('dilate 1 png - default width', (_t, done) => {
sharp(fixtures.inputPngDotAndLines) sharp(fixtures.inputPngDotAndLines)
.dilate() .dilate()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(100, info.width); assert.strictEqual(100, info.width);
@ -29,8 +29,8 @@ describe('Dilate', function () {
}); });
}); });
it('invalid dilation width', function () { it('invalid dilation width', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).dilate(-1); sharp(fixtures.inputJpg).dilate(-1);
}); });
}); });

View File

@ -4,11 +4,11 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Erode', function () { describe('Erode', () => {
it('erode 1 png', function (_t, done) { it('erode 1 png', (_t, done) => {
sharp(fixtures.inputPngDotAndLines) sharp(fixtures.inputPngDotAndLines)
.erode(1) .erode(1)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(100, info.width); assert.strictEqual(100, info.width);
@ -17,10 +17,10 @@ describe('Erode', function () {
}); });
}); });
it('erode 1 png - default width', function (_t, done) { it('erode 1 png - default width', (_t, done) => {
sharp(fixtures.inputPngDotAndLines) sharp(fixtures.inputPngDotAndLines)
.erode() .erode()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(100, info.width); assert.strictEqual(100, info.width);
@ -29,8 +29,8 @@ describe('Erode', function () {
}); });
}); });
it('invalid erosion width', function () { it('invalid erosion width', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).erode(-1); sharp(fixtures.inputJpg).erode(-1);
}); });
}); });

View File

@ -9,13 +9,13 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Extend', function () { describe('Extend', () => {
describe('extend all sides equally via a single value', function () { describe('extend all sides equally via a single value', () => {
it('JPEG', function (_t, done) { it('JPEG', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(120) .resize(120)
.extend(10) .extend(10)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(140, info.width); assert.strictEqual(140, info.width);
assert.strictEqual(118, info.height); assert.strictEqual(118, info.height);
@ -23,11 +23,11 @@ describe('Extend', function () {
}); });
}); });
it('Animated WebP', function (_t, done) { it('Animated WebP', (_t, done) => {
sharp(fixtures.inputWebPAnimated, { pages: -1 }) sharp(fixtures.inputWebPAnimated, { pages: -1 })
.resize(120) .resize(120)
.extend(10) .extend(10)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(140, info.width); assert.strictEqual(140, info.width);
assert.strictEqual(140 * 9, info.height); assert.strictEqual(140 * 9, info.height);
@ -37,7 +37,7 @@ describe('Extend', function () {
}); });
['background', 'copy', 'mirror', 'repeat'].forEach(extendWith => { ['background', 'copy', 'mirror', 'repeat'].forEach(extendWith => {
it(`extends all sides with animated WebP (${extendWith})`, function (_t, done) { it(`extends all sides with animated WebP (${extendWith})`, (_t, done) => {
sharp(fixtures.inputWebPAnimated, { pages: -1 }) sharp(fixtures.inputWebPAnimated, { pages: -1 })
.resize(120) .resize(120)
.extend({ .extend({
@ -47,7 +47,7 @@ describe('Extend', function () {
left: 40, left: 40,
right: 40 right: 40
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(200, info.width); assert.strictEqual(200, info.width);
assert.strictEqual(200 * 9, info.height); assert.strictEqual(200 * 9, info.height);
@ -55,7 +55,7 @@ describe('Extend', function () {
}); });
}); });
it(`extend all sides equally with RGB (${extendWith})`, function (_t, done) { it(`extend all sides equally with RGB (${extendWith})`, (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(120) .resize(120)
.extend({ .extend({
@ -66,7 +66,7 @@ describe('Extend', function () {
right: 10, right: 10,
background: { r: 255, g: 0, b: 0 } background: { r: 255, g: 0, b: 0 }
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(140, info.width); assert.strictEqual(140, info.width);
assert.strictEqual(118, info.height); assert.strictEqual(118, info.height);
@ -74,7 +74,7 @@ describe('Extend', function () {
}); });
}); });
it(`extend sides unequally with RGBA (${extendWith})`, function (_t, done) { it(`extend sides unequally with RGBA (${extendWith})`, (_t, done) => {
sharp(fixtures.inputPngWithTransparency16bit) sharp(fixtures.inputPngWithTransparency16bit)
.resize(120) .resize(120)
.extend({ .extend({
@ -84,7 +84,7 @@ describe('Extend', function () {
right: 35, right: 35,
background: { r: 0, g: 0, b: 0, alpha: 0 } background: { r: 0, g: 0, b: 0, alpha: 0 }
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(165, info.width); assert.strictEqual(165, info.width);
assert.strictEqual(170, info.height); assert.strictEqual(170, info.height);
@ -92,7 +92,7 @@ describe('Extend', function () {
}); });
}); });
it(`PNG with 2 channels (${extendWith})`, function (_t, done) { it(`PNG with 2 channels (${extendWith})`, (_t, done) => {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.extend({ .extend({
extendWith, extendWith,
@ -102,7 +102,7 @@ describe('Extend', function () {
right: 80, right: 80,
background: 'transparent' background: 'transparent'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -147,13 +147,13 @@ describe('Extend', function () {
assert.strictEqual(1470, height); assert.strictEqual(1470, height);
}); });
it('missing parameter fails', function () { it('missing parameter fails', () => {
assert.throws(function () { assert.throws(() => {
sharp().extend(); sharp().extend();
}); });
}); });
it('negative fails', function () { it('negative fails', () => {
assert.throws(function () { assert.throws(() => {
sharp().extend(-1); sharp().extend(-1);
}); });
}); });
@ -191,7 +191,7 @@ describe('Extend', function () {
assert.doesNotThrow(() => sharp().extend({ top: 1, left: 2, bottom: 3 })); assert.doesNotThrow(() => sharp().extend({ top: 1, left: 2, bottom: 3 }));
}); });
it('should add alpha channel before extending with a transparent Background', function (_t, done) { it('should add alpha channel before extending with a transparent Background', (_t, done) => {
sharp(fixtures.inputJpgWithLandscapeExif1) sharp(fixtures.inputJpgWithLandscapeExif1)
.extend({ .extend({
bottom: 10, bottom: 10,
@ -199,7 +199,7 @@ describe('Extend', function () {
background: { r: 0, g: 0, b: 0, alpha: 0 } background: { r: 0, g: 0, b: 0, alpha: 0 }
}) })
.toFormat(sharp.format.png) .toFormat(sharp.format.png)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(610, info.width); assert.strictEqual(610, info.width);
assert.strictEqual(460, info.height); assert.strictEqual(460, info.height);

View File

@ -9,11 +9,11 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Partial image extraction', function () { describe('Partial image extraction', () => {
it('JPEG', function (_t, done) { it('JPEG', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.extract({ left: 2, top: 2, width: 20, height: 20 }) .extract({ left: 2, top: 2, width: 20, height: 20 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(20, info.width); assert.strictEqual(20, info.width);
assert.strictEqual(20, info.height); assert.strictEqual(20, info.height);
@ -21,10 +21,10 @@ describe('Partial image extraction', function () {
}); });
}); });
it('PNG', function (_t, done) { it('PNG', (_t, done) => {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.extract({ left: 200, top: 300, width: 400, height: 200 }) .extract({ left: 200, top: 300, width: 400, height: 200 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(400, info.width); assert.strictEqual(400, info.width);
assert.strictEqual(200, info.height); assert.strictEqual(200, info.height);
@ -32,10 +32,10 @@ describe('Partial image extraction', function () {
}); });
}); });
it('WebP', function (_t, done) { it('WebP', (_t, done) => {
sharp(fixtures.inputWebP) sharp(fixtures.inputWebP)
.extract({ left: 100, top: 50, width: 125, height: 200 }) .extract({ left: 100, top: 50, width: 125, height: 200 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(125, info.width); assert.strictEqual(125, info.width);
assert.strictEqual(200, info.height); assert.strictEqual(200, info.height);
@ -43,12 +43,12 @@ describe('Partial image extraction', function () {
}); });
}); });
describe('Animated WebP', function () { describe('Animated WebP', () => {
it('Before resize', function (_t, done) { it('Before resize', (_t, done) => {
sharp(fixtures.inputWebPAnimated, { pages: -1 }) sharp(fixtures.inputWebPAnimated, { pages: -1 })
.extract({ left: 0, top: 30, width: 80, height: 20 }) .extract({ left: 0, top: 30, width: 80, height: 20 })
.resize(320, 80) .resize(320, 80)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(80 * 9, info.height); assert.strictEqual(80 * 9, info.height);
@ -56,11 +56,11 @@ describe('Partial image extraction', function () {
}); });
}); });
it('After resize', function (_t, done) { it('After resize', (_t, done) => {
sharp(fixtures.inputWebPAnimated, { pages: -1 }) sharp(fixtures.inputWebPAnimated, { pages: -1 })
.resize(320, 320) .resize(320, 320)
.extract({ left: 0, top: 120, width: 320, height: 80 }) .extract({ left: 0, top: 120, width: 320, height: 80 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(80 * 9, info.height); assert.strictEqual(80 * 9, info.height);
@ -69,10 +69,10 @@ describe('Partial image extraction', function () {
}); });
}); });
it('TIFF', function (_t, done) { it('TIFF', (_t, done) => {
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)
.extract({ left: 34, top: 63, width: 341, height: 529 }) .extract({ left: 34, top: 63, width: 341, height: 529 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(341, info.width); assert.strictEqual(341, info.width);
assert.strictEqual(529, info.height); assert.strictEqual(529, info.height);
@ -80,11 +80,11 @@ describe('Partial image extraction', function () {
}); });
}); });
it('Before resize', function (_t, done) { it('Before resize', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.extract({ left: 10, top: 10, width: 10, height: 500 }) .extract({ left: 10, top: 10, width: 10, height: 500 })
.resize(100, 100) .resize(100, 100)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(100, info.width); assert.strictEqual(100, info.width);
assert.strictEqual(100, info.height); assert.strictEqual(100, info.height);
@ -92,13 +92,13 @@ describe('Partial image extraction', function () {
}); });
}); });
it('After resize and crop', function (_t, done) { it('After resize and crop', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(500, 500, { .resize(500, 500, {
position: sharp.gravity.north position: sharp.gravity.north
}) })
.extract({ left: 10, top: 10, width: 100, height: 100 }) .extract({ left: 10, top: 10, width: 100, height: 100 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(100, info.width); assert.strictEqual(100, info.width);
assert.strictEqual(100, info.height); assert.strictEqual(100, info.height);
@ -106,14 +106,14 @@ describe('Partial image extraction', function () {
}); });
}); });
it('Before and after resize and crop', function (_t, done) { it('Before and after resize and crop', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.extract({ left: 0, top: 0, width: 700, height: 700 }) .extract({ left: 0, top: 0, width: 700, height: 700 })
.resize(500, 500, { .resize(500, 500, {
position: sharp.gravity.north position: sharp.gravity.north
}) })
.extract({ left: 10, top: 10, width: 100, height: 100 }) .extract({ left: 10, top: 10, width: 100, height: 100 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(100, info.width); assert.strictEqual(100, info.width);
assert.strictEqual(100, info.height); assert.strictEqual(100, info.height);
@ -121,12 +121,12 @@ describe('Partial image extraction', function () {
}); });
}); });
it('Extract then rotate', function (_t, done) { it('Extract then rotate', (_t, done) => {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.extract({ left: 20, top: 10, width: 380, height: 280 }) .extract({ left: 20, top: 10, width: 380, height: 280 })
.rotate(90) .rotate(90)
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(280, info.width); assert.strictEqual(280, info.width);
assert.strictEqual(380, info.height); assert.strictEqual(380, info.height);
@ -134,11 +134,11 @@ describe('Partial image extraction', function () {
}); });
}); });
it('Rotate then extract', function (_t, done) { it('Rotate then extract', (_t, done) => {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.rotate(90) .rotate(90)
.extract({ left: 20, top: 10, width: 280, height: 380 }) .extract({ left: 20, top: 10, width: 280, height: 380 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(280, info.width); assert.strictEqual(280, info.width);
assert.strictEqual(380, info.height); assert.strictEqual(380, info.height);
@ -146,12 +146,12 @@ describe('Partial image extraction', function () {
}); });
}); });
it('Extract then rotate then extract', function (_t, done) { it('Extract then rotate then extract', (_t, done) => {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.extract({ left: 20, top: 10, width: 180, height: 280 }) .extract({ left: 20, top: 10, width: 180, height: 280 })
.rotate(90) .rotate(90)
.extract({ left: 20, top: 10, width: 200, height: 100 }) .extract({ left: 20, top: 10, width: 200, height: 100 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(200, info.width); assert.strictEqual(200, info.width);
assert.strictEqual(100, info.height); assert.strictEqual(100, info.height);
@ -159,12 +159,12 @@ describe('Partial image extraction', function () {
}); });
}); });
it('Extract then rotate non-90 anagle', function (_t, done) { it('Extract then rotate non-90 anagle', (_t, done) => {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.extract({ left: 20, top: 10, width: 380, height: 280 }) .extract({ left: 20, top: 10, width: 380, height: 280 })
.rotate(45) .rotate(45)
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(467, info.width); assert.strictEqual(467, info.width);
assert.strictEqual(467, info.height); assert.strictEqual(467, info.height);
@ -172,12 +172,12 @@ describe('Partial image extraction', function () {
}); });
}); });
it('Rotate then extract non-90 angle', function (_t, done) { it('Rotate then extract non-90 angle', (_t, done) => {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.rotate(45) .rotate(45)
.extract({ left: 20, top: 10, width: 380, height: 280 }) .extract({ left: 20, top: 10, width: 380, height: 280 })
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(380, info.width); assert.strictEqual(380, info.width);
assert.strictEqual(280, info.height); assert.strictEqual(280, info.height);
@ -220,11 +220,11 @@ describe('Partial image extraction', function () {
image: fixtures.inputJpgWithLandscapeExif8 image: fixtures.inputJpgWithLandscapeExif8
} }
].forEach(({ name, image }) => { ].forEach(({ name, image }) => {
it(name, function (_t, done) { it(name, (_t, done) => {
sharp(image) sharp(image)
.rotate() .rotate()
.extract({ left: 0, top: 208, width: 60, height: 40 }) .extract({ left: 0, top: 208, width: 60, height: 40 })
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.expected('rotate-mirror-extract.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('rotate-mirror-extract.jpg'), data, done);
}); });
@ -232,67 +232,67 @@ describe('Partial image extraction', function () {
}); });
}); });
describe('Invalid parameters', function () { describe('Invalid parameters', () => {
describe('using the legacy extract(top,left,width,height) syntax', function () { describe('using the legacy extract(top,left,width,height) syntax', () => {
it('String top', function () { it('String top', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).extract('spoons', 10, 10, 10); sharp(fixtures.inputJpg).extract('spoons', 10, 10, 10);
}); });
}); });
it('Non-integral left', function () { it('Non-integral left', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).extract(10, 10.2, 10, 10); sharp(fixtures.inputJpg).extract(10, 10.2, 10, 10);
}); });
}); });
it('Negative width - negative', function () { it('Negative width - negative', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).extract(10, 10, -10, 10); sharp(fixtures.inputJpg).extract(10, 10, -10, 10);
}); });
}); });
it('Null height', function () { it('Null height', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).extract(10, 10, 10, null); sharp(fixtures.inputJpg).extract(10, 10, 10, null);
}); });
}); });
}); });
it('Undefined', function () { it('Undefined', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).extract(); sharp(fixtures.inputJpg).extract();
}); });
}); });
it('String top', function () { it('String top', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).extract({ left: 10, top: 'spoons', width: 10, height: 10 }); sharp(fixtures.inputJpg).extract({ left: 10, top: 'spoons', width: 10, height: 10 });
}); });
}); });
it('Non-integral left', function () { it('Non-integral left', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).extract({ left: 10.2, top: 10, width: 10, height: 10 }); sharp(fixtures.inputJpg).extract({ left: 10.2, top: 10, width: 10, height: 10 });
}); });
}); });
it('Negative width - negative', function () { it('Negative width - negative', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).extract({ left: 10, top: 10, width: -10, height: 10 }); sharp(fixtures.inputJpg).extract({ left: 10, top: 10, width: -10, height: 10 });
}); });
}); });
it('Null height', function () { it('Null height', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).extract({ left: 10, top: 10, width: 10, height: null }); sharp(fixtures.inputJpg).extract({ left: 10, top: 10, width: 10, height: null });
}); });
}); });
it('Bad image area', function (_t, done) { it('Bad image area', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.extract({ left: 3000, top: 10, width: 10, height: 10 }) .extract({ left: 3000, top: 10, width: 10, height: 10 })
.toBuffer(function (err) { .toBuffer((err) => {
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual(err.message, 'extract_area: bad extract area'); assert.strictEqual(err.message, 'extract_area: bad extract area');
done(); done();
@ -302,7 +302,7 @@ describe('Partial image extraction', function () {
it('Multiple extract emits warning', () => { it('Multiple extract emits warning', () => {
let warningMessage = ''; let warningMessage = '';
const s = sharp(); const s = sharp();
s.on('warning', function (msg) { warningMessage = msg; }); s.on('warning', (msg) => { warningMessage = msg; });
const options = { top: 0, left: 0, width: 1, height: 1 }; const options = { top: 0, left: 0, width: 1, height: 1 };
s.extract(options).extract(options); s.extract(options).extract(options);
assert.strictEqual(warningMessage, ''); assert.strictEqual(warningMessage, '');
@ -313,7 +313,7 @@ describe('Partial image extraction', function () {
it('Multiple rotate+extract emits warning', () => { it('Multiple rotate+extract emits warning', () => {
let warningMessage = ''; let warningMessage = '';
const s = sharp().rotate(); const s = sharp().rotate();
s.on('warning', function (msg) { warningMessage = msg; }); s.on('warning', (msg) => { warningMessage = msg; });
const options = { top: 0, left: 0, width: 1, height: 1 }; const options = { top: 0, left: 0, width: 1, height: 1 };
s.extract(options).extract(options); s.extract(options).extract(options);
assert.strictEqual(warningMessage, ''); assert.strictEqual(warningMessage, '');
@ -324,7 +324,7 @@ describe('Partial image extraction', function () {
it('Multiple extract+resize emits warning', () => { it('Multiple extract+resize emits warning', () => {
let warningMessage = ''; let warningMessage = '';
const s = sharp(); const s = sharp();
s.on('warning', function (msg) { warningMessage = msg; }); s.on('warning', (msg) => { warningMessage = msg; });
const options = { top: 0, left: 0, width: 1, height: 1 }; const options = { top: 0, left: 0, width: 1, height: 1 };
s.extract(options).extract(options); s.extract(options).extract(options);
assert.strictEqual(warningMessage, ''); assert.strictEqual(warningMessage, '');

View File

@ -9,12 +9,12 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Image channel extraction', function () { describe('Image channel extraction', () => {
it('Red channel', function (_t, done) { it('Red channel', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.extractChannel('red') .extractChannel('red')
.resize(320, 240) .resize(320, 240)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -22,11 +22,11 @@ describe('Image channel extraction', function () {
}); });
}); });
it('Green channel', function (_t, done) { it('Green channel', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.extractChannel('green') .extractChannel('green')
.resize(320, 240) .resize(320, 240)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -34,11 +34,11 @@ describe('Image channel extraction', function () {
}); });
}); });
it('Blue channel', function (_t, done) { it('Blue channel', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.extractChannel('blue') .extractChannel('blue')
.resize(320, 240) .resize(320, 240)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -46,11 +46,11 @@ describe('Image channel extraction', function () {
}); });
}); });
it('Blue channel by number', function (_t, done) { it('Blue channel by number', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.extractChannel(2) .extractChannel(2)
.resize(320, 240) .resize(320, 240)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -67,23 +67,23 @@ describe('Image channel extraction', function () {
assert.strictEqual(chroma, 104); assert.strictEqual(chroma, 104);
}); });
it('Alpha from 16-bit PNG', function (_t, done) { it('Alpha from 16-bit PNG', (_t, done) => {
const output = fixtures.path('output.extract-alpha-16bit.png'); const output = fixtures.path('output.extract-alpha-16bit.png');
sharp(fixtures.inputPngWithTransparency16bit) sharp(fixtures.inputPngWithTransparency16bit)
.resize(16) .resize(16)
.extractChannel(3) .extractChannel(3)
.toFile(output, function (err) { .toFile(output, (err) => {
if (err) throw err; if (err) throw err;
fixtures.assertMaxColourDistance(output, fixtures.expected('extract-alpha-16bit.png')); fixtures.assertMaxColourDistance(output, fixtures.expected('extract-alpha-16bit.png'));
done(); done();
}); });
}); });
it('Alpha from 2-channel input', function (_t, done) { it('Alpha from 2-channel input', (_t, done) => {
const output = fixtures.path('output.extract-alpha-2-channel.png'); const output = fixtures.path('output.extract-alpha-2-channel.png');
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.extractChannel('alpha') .extractChannel('alpha')
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(1, info.channels); assert.strictEqual(1, info.channels);
fixtures.assertMaxColourDistance(output, fixtures.expected('extract-alpha-2-channel.png')); fixtures.assertMaxColourDistance(output, fixtures.expected('extract-alpha-2-channel.png'));
@ -91,15 +91,15 @@ describe('Image channel extraction', function () {
}); });
}); });
it('Invalid channel number', function () { it('Invalid channel number', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.extractChannel(-1); .extractChannel(-1);
}); });
}); });
it('No arguments', function () { it('No arguments', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.extractChannel(); .extractChannel();
}); });

View File

@ -11,10 +11,10 @@ const sharp = require('../../lib');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('failOn', () => { describe('failOn', () => {
it('handles truncated JPEG', function (_t, done) { it('handles truncated JPEG', (_t, done) => {
sharp(fixtures.inputJpgTruncated, { failOn: 'none' }) sharp(fixtures.inputJpgTruncated, { failOn: 'none' })
.resize(32, 24) .resize(32, 24)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(32, info.width); assert.strictEqual(32, info.width);
@ -23,17 +23,17 @@ describe('failOn', () => {
}); });
}); });
it('handles truncated PNG, emits warnings', function (_t, done) { it('handles truncated PNG, emits warnings', (_t, done) => {
let isWarningEmitted = false; let isWarningEmitted = false;
sharp(fixtures.inputPngTruncated, { failOn: 'none' }) sharp(fixtures.inputPngTruncated, { failOn: 'none' })
.on('warning', function (warning) { .on('warning', (warning) => {
assert.ok( assert.ok(
['read gave 2 warnings', 'not enough data', 'end of stream'] ['read gave 2 warnings', 'not enough data', 'end of stream']
.some(m => warning.includes(m))); .some(m => warning.includes(m)));
isWarningEmitted = true; isWarningEmitted = true;
}) })
.resize(32, 24) .resize(32, 24)
.toBuffer(function (err, _data, info) { .toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, isWarningEmitted); assert.strictEqual(true, isWarningEmitted);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -71,8 +71,8 @@ describe('failOn', () => {
); );
}); });
it('returns errors to callback for truncated JPEG', function (_t, done) { it('returns errors to callback for truncated JPEG', (_t, done) => {
sharp(fixtures.inputJpgTruncated, { failOn: 'truncated' }).toBuffer(function (err, data, info) { sharp(fixtures.inputJpgTruncated, { failOn: 'truncated' }).toBuffer((err, data, info) => {
assert.ok(err.message.includes('VipsJpeg: premature end of'), err); assert.ok(err.message.includes('VipsJpeg: premature end of'), err);
assert.strictEqual(data, undefined); assert.strictEqual(data, undefined);
assert.strictEqual(info, undefined); assert.strictEqual(info, undefined);
@ -80,8 +80,8 @@ describe('failOn', () => {
}); });
}); });
it('returns errors to callback for truncated PNG', function (_t, done) { it('returns errors to callback for truncated PNG', (_t, done) => {
sharp(fixtures.inputPngTruncated, { failOn: 'truncated' }).toBuffer(function (err, data, info) { sharp(fixtures.inputPngTruncated, { failOn: 'truncated' }).toBuffer((err, data, info) => {
assert.ok(err.message.includes('read error'), err); assert.ok(err.message.includes('read error'), err);
assert.strictEqual(data, undefined); assert.strictEqual(data, undefined);
assert.strictEqual(info, undefined); assert.strictEqual(info, undefined);
@ -89,7 +89,7 @@ describe('failOn', () => {
}); });
}); });
it('rejects promises for truncated JPEG', function (_t, done) { it('rejects promises for truncated JPEG', (_t, done) => {
sharp(fixtures.inputJpgTruncated, { failOn: 'error' }) sharp(fixtures.inputJpgTruncated, { failOn: 'error' })
.toBuffer() .toBuffer()
.then(() => { .then(() => {

View File

@ -7,23 +7,23 @@ const { describe, it } = require('node:test');
const assert = require('node:assert'); const assert = require('node:assert');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Test fixtures', function () { describe('Test fixtures', () => {
describe('assertMaxColourDistance', function () { describe('assertMaxColourDistance', () => {
it('should throw an Error when images have a different number of channels', function () { it('should throw an Error when images have a different number of channels', () => {
assert.throws(function () { assert.throws(() => {
fixtures.assertMaxColourDistance(fixtures.inputPngOverlayLayer1, fixtures.inputJpg); fixtures.assertMaxColourDistance(fixtures.inputPngOverlayLayer1, fixtures.inputJpg);
}); });
}); });
it('should throw an Error when images have different dimensions', function () { it('should throw an Error when images have different dimensions', () => {
assert.throws(function () { assert.throws(() => {
fixtures.assertMaxColourDistance(fixtures.inputJpg, fixtures.inputJpgWithExif); fixtures.assertMaxColourDistance(fixtures.inputJpg, fixtures.inputJpgWithExif);
}); });
}); });
it('should accept a zero threshold when comparing an image to itself', function () { it('should accept a zero threshold when comparing an image to itself', () => {
const image = fixtures.inputPngOverlayLayer0; const image = fixtures.inputPngOverlayLayer0;
fixtures.assertMaxColourDistance(image, image, 0); fixtures.assertMaxColourDistance(image, image, 0);
}); });
it('should accept a numeric threshold for two different images', function () { it('should accept a numeric threshold for two different images', () => {
fixtures.assertMaxColourDistance(fixtures.inputPngOverlayLayer0, fixtures.inputPngOverlayLayer1, 100); fixtures.assertMaxColourDistance(fixtures.inputPngOverlayLayer0, fixtures.inputPngOverlayLayer1, 100);
}); });
}); });

View File

@ -9,11 +9,11 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Gamma correction', function () { describe('Gamma correction', () => {
it('value of 0.0 (disabled)', function (_t, done) { it('value of 0.0 (disabled)', (_t, done) => {
sharp(fixtures.inputJpgWithGammaHoliness) sharp(fixtures.inputJpgWithGammaHoliness)
.resize(129, 111) .resize(129, 111)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(129, info.width); assert.strictEqual(129, info.width);
@ -22,11 +22,11 @@ describe('Gamma correction', function () {
}); });
}); });
it('value of 2.2 (default)', function (_t, done) { it('value of 2.2 (default)', (_t, done) => {
sharp(fixtures.inputJpgWithGammaHoliness) sharp(fixtures.inputJpgWithGammaHoliness)
.resize(129, 111) .resize(129, 111)
.gamma() .gamma()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(129, info.width); assert.strictEqual(129, info.width);
@ -35,11 +35,11 @@ describe('Gamma correction', function () {
}); });
}); });
it('value of 3.0', function (_t, done) { it('value of 3.0', (_t, done) => {
sharp(fixtures.inputJpgWithGammaHoliness) sharp(fixtures.inputJpgWithGammaHoliness)
.resize(129, 111) .resize(129, 111)
.gamma(3) .gamma(3)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(129, info.width); assert.strictEqual(129, info.width);
@ -48,11 +48,11 @@ describe('Gamma correction', function () {
}); });
}); });
it('input value of 2.2, output value of 3.0', function (_t, done) { it('input value of 2.2, output value of 3.0', (_t, done) => {
sharp(fixtures.inputJpgWithGammaHoliness) sharp(fixtures.inputJpgWithGammaHoliness)
.resize(129, 111) .resize(129, 111)
.gamma(2.2, 3.0) .gamma(2.2, 3.0)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(129, info.width); assert.strictEqual(129, info.width);
@ -61,12 +61,12 @@ describe('Gamma correction', function () {
}); });
}); });
it('alpha transparency', function (_t, done) { it('alpha transparency', (_t, done) => {
sharp(fixtures.inputPngOverlayLayer1) sharp(fixtures.inputPngOverlayLayer1)
.resize(320) .resize(320)
.gamma() .gamma()
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -74,14 +74,14 @@ describe('Gamma correction', function () {
}); });
}); });
it('invalid first parameter value', function () { it('invalid first parameter value', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgWithGammaHoliness).gamma(4); sharp(fixtures.inputJpgWithGammaHoliness).gamma(4);
}); });
}); });
it('invalid second parameter value', function () { it('invalid second parameter value', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpgWithGammaHoliness).gamma(2.2, 4); sharp(fixtures.inputJpgWithGammaHoliness).gamma(2.2, 4);
}); });
}); });

View File

@ -199,11 +199,11 @@ describe('GIF input', () => {
); );
}); });
it('should work with streams when only animated is set', function (_t, done) { it('should work with streams when only animated is set', (_t, done) => {
fs.createReadStream(fixtures.inputGifAnimated) fs.createReadStream(fixtures.inputGifAnimated)
.pipe(sharp({ animated: true })) .pipe(sharp({ animated: true }))
.gif() .gif()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('gif', info.format); assert.strictEqual('gif', info.format);
@ -211,11 +211,11 @@ describe('GIF input', () => {
}); });
}); });
it('should work with streams when only pages is set', function (_t, done) { it('should work with streams when only pages is set', (_t, done) => {
fs.createReadStream(fixtures.inputGifAnimated) fs.createReadStream(fixtures.inputGifAnimated)
.pipe(sharp({ pages: -1 })) .pipe(sharp({ pages: -1 }))
.gif() .gif()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('gif', info.format); assert.strictEqual('gif', info.format);

View File

@ -13,18 +13,18 @@ const fixtures = require('../fixtures');
const outputJpg = fixtures.path('output.jpg'); const outputJpg = fixtures.path('output.jpg');
describe('Input/output', function () { describe('Input/output', () => {
beforeEach(function () { beforeEach(() => {
sharp.cache(false); sharp.cache(false);
}); });
afterEach(function () { afterEach(() => {
sharp.cache(true); sharp.cache(true);
}); });
it('Read from File and write to Stream', function (_t, done) { it('Read from File and write to Stream', (_t, done) => {
const writable = fs.createWriteStream(outputJpg); const writable = fs.createWriteStream(outputJpg);
writable.on('close', function () { writable.on('close', () => {
sharp(outputJpg).toBuffer(function (err, data, info) { sharp(outputJpg).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -37,11 +37,11 @@ describe('Input/output', function () {
sharp(fixtures.inputJpg).resize(320, 240).pipe(writable); sharp(fixtures.inputJpg).resize(320, 240).pipe(writable);
}); });
it('Read from Buffer and write to Stream', function (_t, done) { it('Read from Buffer and write to Stream', (_t, done) => {
const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg); const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg);
const writable = fs.createWriteStream(outputJpg); const writable = fs.createWriteStream(outputJpg);
writable.on('close', function () { writable.on('close', () => {
sharp(outputJpg).toBuffer(function (err, data, info) { sharp(outputJpg).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -54,9 +54,9 @@ describe('Input/output', function () {
sharp(inputJpgBuffer).resize(320, 240).pipe(writable); sharp(inputJpgBuffer).resize(320, 240).pipe(writable);
}); });
it('Read from Stream and write to File', function (_t, done) { it('Read from Stream and write to File', (_t, done) => {
const readable = fs.createReadStream(fixtures.inputJpg); const readable = fs.createReadStream(fixtures.inputJpg);
const pipeline = sharp().resize(320, 240).toFile(outputJpg, function (err, info) { const pipeline = sharp().resize(320, 240).toFile(outputJpg, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -67,9 +67,9 @@ describe('Input/output', function () {
readable.pipe(pipeline); readable.pipe(pipeline);
}); });
it('Read from Stream and write to Buffer', function (_t, done) { it('Read from Stream and write to Buffer', (_t, done) => {
const readable = fs.createReadStream(fixtures.inputJpg); const readable = fs.createReadStream(fixtures.inputJpg);
const pipeline = sharp().resize(320, 240).toBuffer(function (err, data, info) { const pipeline = sharp().resize(320, 240).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -81,23 +81,23 @@ describe('Input/output', function () {
readable.pipe(pipeline); readable.pipe(pipeline);
}); });
it('Read from Stream and write to Buffer via Promise resolved with Buffer', function () { it('Read from Stream and write to Buffer via Promise resolved with Buffer', () => {
const pipeline = sharp().resize(1, 1); const pipeline = sharp().resize(1, 1);
fs.createReadStream(fixtures.inputJpg).pipe(pipeline); fs.createReadStream(fixtures.inputJpg).pipe(pipeline);
return pipeline return pipeline
.toBuffer({ resolveWithObject: false }) .toBuffer({ resolveWithObject: false })
.then(function (data) { .then((data) => {
assert.strictEqual(true, data instanceof Buffer); assert.strictEqual(true, data instanceof Buffer);
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
}); });
}); });
it('Read from Stream and write to Buffer via Promise resolved with Object', function () { it('Read from Stream and write to Buffer via Promise resolved with Object', () => {
const pipeline = sharp().resize(1, 1); const pipeline = sharp().resize(1, 1);
fs.createReadStream(fixtures.inputJpg).pipe(pipeline); fs.createReadStream(fixtures.inputJpg).pipe(pipeline);
return pipeline return pipeline
.toBuffer({ resolveWithObject: true }) .toBuffer({ resolveWithObject: true })
.then(function (object) { .then((object) => {
assert.strictEqual('object', typeof object); assert.strictEqual('object', typeof object);
assert.strictEqual('object', typeof object.info); assert.strictEqual('object', typeof object.info);
assert.strictEqual('jpeg', object.info.format); assert.strictEqual('jpeg', object.info.format);
@ -109,21 +109,18 @@ describe('Input/output', function () {
}); });
}); });
it('Read from File and write to Buffer via Promise resolved with Buffer', function () { it('Read from File and write to Buffer via Promise resolved with Buffer', () => sharp(fixtures.inputJpg)
return sharp(fixtures.inputJpg)
.resize(1, 1) .resize(1, 1)
.toBuffer({ resolveWithObject: false }) .toBuffer({ resolveWithObject: false })
.then(function (data) { .then((data) => {
assert.strictEqual(true, data instanceof Buffer); assert.strictEqual(true, data instanceof Buffer);
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
}); }));
});
it('Read from File and write to Buffer via Promise resolved with Object', function () { it('Read from File and write to Buffer via Promise resolved with Object', () => sharp(fixtures.inputJpg)
return sharp(fixtures.inputJpg)
.resize(1, 1) .resize(1, 1)
.toBuffer({ resolveWithObject: true }) .toBuffer({ resolveWithObject: true })
.then(function (object) { .then((object) => {
assert.strictEqual('object', typeof object); assert.strictEqual('object', typeof object);
assert.strictEqual('object', typeof object.info); assert.strictEqual('object', typeof object.info);
assert.strictEqual('jpeg', object.info.format); assert.strictEqual('jpeg', object.info.format);
@ -132,14 +129,13 @@ describe('Input/output', function () {
assert.strictEqual(3, object.info.channels); assert.strictEqual(3, object.info.channels);
assert.strictEqual(true, object.data instanceof Buffer); assert.strictEqual(true, object.data instanceof Buffer);
assert.strictEqual(true, object.data.length > 0); assert.strictEqual(true, object.data.length > 0);
}); }));
});
it('Read from Stream and write to Stream', function (_t, done) { it('Read from Stream and write to Stream', (_t, done) => {
const readable = fs.createReadStream(fixtures.inputJpg); const readable = fs.createReadStream(fixtures.inputJpg);
const writable = fs.createWriteStream(outputJpg); const writable = fs.createWriteStream(outputJpg);
writable.on('close', function () { writable.on('close', () => {
sharp(outputJpg).toBuffer(function (err, data, info) { sharp(outputJpg).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -220,46 +216,46 @@ describe('Input/output', function () {
assert.strictEqual(info.height, 1); assert.strictEqual(info.height, 1);
}); });
it('Stream should emit info event', function (_t, done) { it('Stream should emit info event', (_t, done) => {
const readable = fs.createReadStream(fixtures.inputJpg); const readable = fs.createReadStream(fixtures.inputJpg);
const writable = fs.createWriteStream(outputJpg); const writable = fs.createWriteStream(outputJpg);
const pipeline = sharp().resize(320, 240); const pipeline = sharp().resize(320, 240);
let infoEventEmitted = false; let infoEventEmitted = false;
pipeline.on('info', function (info) { pipeline.on('info', (info) => {
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
infoEventEmitted = true; infoEventEmitted = true;
}); });
writable.on('close', function () { writable.on('close', () => {
assert.strictEqual(true, infoEventEmitted); assert.strictEqual(true, infoEventEmitted);
fs.rm(outputJpg, done); fs.rm(outputJpg, done);
}); });
readable.pipe(pipeline).pipe(writable); readable.pipe(pipeline).pipe(writable);
}); });
it('Stream should emit close event', function (_t, done) { it('Stream should emit close event', (_t, done) => {
const readable = fs.createReadStream(fixtures.inputJpg); const readable = fs.createReadStream(fixtures.inputJpg);
const writable = fs.createWriteStream(outputJpg); const writable = fs.createWriteStream(outputJpg);
const pipeline = sharp().resize(320, 240); const pipeline = sharp().resize(320, 240);
let closeEventEmitted = false; let closeEventEmitted = false;
pipeline.on('close', function () { pipeline.on('close', () => {
closeEventEmitted = true; closeEventEmitted = true;
}); });
writable.on('close', function () { writable.on('close', () => {
assert.strictEqual(true, closeEventEmitted); assert.strictEqual(true, closeEventEmitted);
fs.rm(outputJpg, done); fs.rm(outputJpg, done);
}); });
readable.pipe(pipeline).pipe(writable); readable.pipe(pipeline).pipe(writable);
}); });
it('Handle Stream to Stream error ', function (_t, done) { it('Handle Stream to Stream error ', (_t, done) => {
const pipeline = sharp().resize(320, 240); const pipeline = sharp().resize(320, 240);
let anErrorWasEmitted = false; let anErrorWasEmitted = false;
pipeline.on('error', function (err) { pipeline.on('error', (err) => {
anErrorWasEmitted = !!err; anErrorWasEmitted = !!err;
}).on('end', function () { }).on('end', () => {
assert(anErrorWasEmitted); assert(anErrorWasEmitted);
fs.rm(outputJpg, done); fs.rm(outputJpg, done);
}); });
@ -268,12 +264,12 @@ describe('Input/output', function () {
readableButNotAnImage.pipe(pipeline).pipe(writable); readableButNotAnImage.pipe(pipeline).pipe(writable);
}); });
it('Handle File to Stream error', function (_t, done) { it('Handle File to Stream error', (_t, done) => {
const readableButNotAnImage = sharp(__filename).resize(320, 240); const readableButNotAnImage = sharp(__filename).resize(320, 240);
let anErrorWasEmitted = false; let anErrorWasEmitted = false;
readableButNotAnImage.on('error', function (err) { readableButNotAnImage.on('error', (err) => {
anErrorWasEmitted = !!err; anErrorWasEmitted = !!err;
}).on('end', function () { }).on('end', () => {
assert(anErrorWasEmitted); assert(anErrorWasEmitted);
fs.rm(outputJpg, done); fs.rm(outputJpg, done);
}); });
@ -281,11 +277,11 @@ describe('Input/output', function () {
readableButNotAnImage.pipe(writable); readableButNotAnImage.pipe(writable);
}); });
it('Readable side of Stream can start flowing after Writable side has finished', function (_t, done) { it('Readable side of Stream can start flowing after Writable side has finished', (_t, done) => {
const readable = fs.createReadStream(fixtures.inputJpg); const readable = fs.createReadStream(fixtures.inputJpg);
const writable = fs.createWriteStream(outputJpg); const writable = fs.createWriteStream(outputJpg);
writable.on('close', function () { writable.on('close', () => {
sharp(outputJpg).toBuffer(function (err, data, info) { sharp(outputJpg).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -297,7 +293,7 @@ describe('Input/output', function () {
}); });
const pipeline = sharp().resize(320, 240); const pipeline = sharp().resize(320, 240);
readable.pipe(pipeline); readable.pipe(pipeline);
pipeline.on('finish', function () { pipeline.on('finish', () => {
pipeline.pipe(writable); pipeline.pipe(writable);
}); });
}); });
@ -350,11 +346,11 @@ describe('Input/output', function () {
}) })
); );
it('Support output to jpg format', function (_t, done) { it('Support output to jpg format', (_t, done) => {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(320, 240) .resize(320, 240)
.toFormat('jpg') .toFormat('jpg')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -365,11 +361,11 @@ describe('Input/output', function () {
}); });
}); });
it('Support output to tif format', function (_t, done) { it('Support output to tif format', (_t, done) => {
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)
.resize(320, 240) .resize(320, 240)
.toFormat('tif') .toFormat('tif')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -394,76 +390,76 @@ describe('Input/output', function () {
assert.strictEqual(Buffer.isBuffer(data), true); assert.strictEqual(Buffer.isBuffer(data), true);
}); });
it('Fail when output File is input File', function (_t, done) { it('Fail when output File is input File', (_t, done) => {
sharp(fixtures.inputJpg).toFile(fixtures.inputJpg, function (err) { sharp(fixtures.inputJpg).toFile(fixtures.inputJpg, (err) => {
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual('Cannot use same file for input and output', err.message); assert.strictEqual('Cannot use same file for input and output', err.message);
done(); done();
}); });
}); });
it('Fail when output File is input File via Promise', function (_t, done) { it('Fail when output File is input File via Promise', (_t, done) => {
sharp(fixtures.inputJpg).toFile(fixtures.inputJpg).then(function () { sharp(fixtures.inputJpg).toFile(fixtures.inputJpg).then(() => {
done(new Error('Unexpectedly resolved Promise')); done(new Error('Unexpectedly resolved Promise'));
}).catch(function (err) { }).catch((err) => {
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual('Cannot use same file for input and output', err.message); assert.strictEqual('Cannot use same file for input and output', err.message);
done(); done();
}); });
}); });
it('Fail when output File is input File (relative output, absolute input)', function (_t, done) { it('Fail when output File is input File (relative output, absolute input)', (_t, done) => {
const relativePath = path.relative(process.cwd(), fixtures.inputJpg); const relativePath = path.relative(process.cwd(), fixtures.inputJpg);
sharp(fixtures.inputJpg).toFile(relativePath, function (err) { sharp(fixtures.inputJpg).toFile(relativePath, (err) => {
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual('Cannot use same file for input and output', err.message); assert.strictEqual('Cannot use same file for input and output', err.message);
done(); done();
}); });
}); });
it('Fail when output File is input File via Promise (relative output, absolute input)', function (_t, done) { it('Fail when output File is input File via Promise (relative output, absolute input)', (_t, done) => {
const relativePath = path.relative(process.cwd(), fixtures.inputJpg); const relativePath = path.relative(process.cwd(), fixtures.inputJpg);
sharp(fixtures.inputJpg).toFile(relativePath).then(function () { sharp(fixtures.inputJpg).toFile(relativePath).then(() => {
done(new Error('Unexpectedly resolved Promise')); done(new Error('Unexpectedly resolved Promise'));
}).catch(function (err) { }).catch((err) => {
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual('Cannot use same file for input and output', err.message); assert.strictEqual('Cannot use same file for input and output', err.message);
done(); done();
}); });
}); });
it('Fail when output File is input File (relative input, absolute output)', function (_t, done) { it('Fail when output File is input File (relative input, absolute output)', (_t, done) => {
const relativePath = path.relative(process.cwd(), fixtures.inputJpg); const relativePath = path.relative(process.cwd(), fixtures.inputJpg);
sharp(relativePath).toFile(fixtures.inputJpg, function (err) { sharp(relativePath).toFile(fixtures.inputJpg, (err) => {
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual('Cannot use same file for input and output', err.message); assert.strictEqual('Cannot use same file for input and output', err.message);
done(); done();
}); });
}); });
it('Fail when output File is input File via Promise (relative input, absolute output)', function (_t, done) { it('Fail when output File is input File via Promise (relative input, absolute output)', (_t, done) => {
const relativePath = path.relative(process.cwd(), fixtures.inputJpg); const relativePath = path.relative(process.cwd(), fixtures.inputJpg);
sharp(relativePath).toFile(fixtures.inputJpg).then(function () { sharp(relativePath).toFile(fixtures.inputJpg).then(() => {
done(new Error('Unexpectedly resolved Promise')); done(new Error('Unexpectedly resolved Promise'));
}).catch(function (err) { }).catch((err) => {
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual('Cannot use same file for input and output', err.message); assert.strictEqual('Cannot use same file for input and output', err.message);
done(); done();
}); });
}); });
it('Fail when output File is empty', function (_t, done) { it('Fail when output File is empty', (_t, done) => {
sharp(fixtures.inputJpg).toFile('', function (err) { sharp(fixtures.inputJpg).toFile('', (err) => {
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual('Missing output file path', err.message); assert.strictEqual('Missing output file path', err.message);
done(); done();
}); });
}); });
it('Fail when output File is empty via Promise', function (_t, done) { it('Fail when output File is empty via Promise', (_t, done) => {
sharp(fixtures.inputJpg).toFile('').then(function () { sharp(fixtures.inputJpg).toFile('').then(() => {
done(new Error('Unexpectedly resolved Promise')); done(new Error('Unexpectedly resolved Promise'));
}).catch(function (err) { }).catch((err) => {
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual('Missing output file path', err.message); assert.strictEqual('Missing output file path', err.message);
done(); done();
@ -494,41 +490,39 @@ describe('Input/output', function () {
) )
); );
describe('Fail for unsupported input', function () { describe('Fail for unsupported input', () => {
it('Undefined', function () { it('Undefined', () => {
assert.throws(function () { assert.throws(() => {
sharp(undefined); sharp(undefined);
}); });
}); });
it('Null', function () { it('Null', () => {
assert.throws(function () { assert.throws(() => {
sharp(null); sharp(null);
}); });
}); });
it('Numeric', function () { it('Numeric', () => {
assert.throws(function () { assert.throws(() => {
sharp(1); sharp(1);
}); });
}); });
it('Boolean', function () { it('Boolean', () => {
assert.throws(function () { assert.throws(() => {
sharp(true); sharp(true);
}); });
}); });
it('Error Object', function () { it('Error Object', () => {
assert.throws(function () { assert.throws(() => {
sharp(new Error()); sharp(new Error());
}); });
}); });
}); });
it('Promises/A+', function () { it('Promises/A+', () => sharp(fixtures.inputJpg)
return sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.toBuffer(); .toBuffer());
});
it('Invalid output format', function (_t, done) { it('Invalid output format', (_t, done) => {
let isValid = false; let isValid = false;
try { try {
sharp().toFormat('zoinks'); sharp().toFormat('zoinks');
@ -538,30 +532,30 @@ describe('Input/output', function () {
done(); done();
}); });
it('File input with corrupt header fails gracefully', function (_t, done) { it('File input with corrupt header fails gracefully', (_t, done) => {
sharp(fixtures.inputJpgWithCorruptHeader) sharp(fixtures.inputJpgWithCorruptHeader)
.toBuffer(function (err) { .toBuffer((err) => {
assert.strictEqual(true, !!err); assert.strictEqual(true, !!err);
done(); done();
}); });
}); });
it('Buffer input with corrupt header fails gracefully', function (_t, done) { it('Buffer input with corrupt header fails gracefully', (_t, done) => {
sharp(fs.readFileSync(fixtures.inputJpgWithCorruptHeader)) sharp(fs.readFileSync(fixtures.inputJpgWithCorruptHeader))
.toBuffer(function (err) { .toBuffer((err) => {
assert.strictEqual(true, !!err); assert.strictEqual(true, !!err);
done(); done();
}); });
}); });
it('Stream input with corrupt header fails gracefully', function (_t, done) { it('Stream input with corrupt header fails gracefully', (_t, done) => {
const transformer = sharp(); const transformer = sharp();
transformer transformer
.toBuffer() .toBuffer()
.then(function () { .then(() => {
done(new Error('Unexpectedly resolved Promise')); done(new Error('Unexpectedly resolved Promise'));
}) })
.catch(function (err) { .catch((err) => {
assert.strictEqual(true, !!err); assert.strictEqual(true, !!err);
done(); done();
}); });
@ -570,13 +564,13 @@ describe('Input/output', function () {
.pipe(transformer); .pipe(transformer);
}); });
describe('Output filename with unknown extension', function () { describe('Output filename with unknown extension', () => {
const outputZoinks = fixtures.path('output.zoinks'); const outputZoinks = fixtures.path('output.zoinks');
it('Match JPEG input', function (_t, done) { it('Match JPEG input', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 80) .resize(320, 80)
.toFile(outputZoinks, function (err, info) { .toFile(outputZoinks, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -586,10 +580,10 @@ describe('Input/output', function () {
}); });
}); });
it('Match PNG input', function (_t, done) { it('Match PNG input', (_t, done) => {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(320, 80) .resize(320, 80)
.toFile(outputZoinks, function (err, info) { .toFile(outputZoinks, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -599,10 +593,10 @@ describe('Input/output', function () {
}); });
}); });
it('Match WebP input', function (_t, done) { it('Match WebP input', (_t, done) => {
sharp(fixtures.inputWebP) sharp(fixtures.inputWebP)
.resize(320, 80) .resize(320, 80)
.toFile(outputZoinks, function (err, info) { .toFile(outputZoinks, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
@ -612,10 +606,10 @@ describe('Input/output', function () {
}); });
}); });
it('Match TIFF input', function (_t, done) { it('Match TIFF input', (_t, done) => {
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)
.resize(320, 80) .resize(320, 80)
.toFile(outputZoinks, function (err, info) { .toFile(outputZoinks, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
assert.strictEqual('tiff', info.format); assert.strictEqual('tiff', info.format);
@ -625,11 +619,11 @@ describe('Input/output', function () {
}); });
}); });
it('Force JPEG format for PNG input', function (_t, done) { it('Force JPEG format for PNG input', (_t, done) => {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(320, 80) .resize(320, 80)
.jpeg() .jpeg()
.toFile(outputZoinks, function (err, info) { .toFile(outputZoinks, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -640,11 +634,11 @@ describe('Input/output', function () {
}); });
}); });
it('Input and output formats match when not forcing', function (_t, done) { it('Input and output formats match when not forcing', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.png({ compressionLevel: 1, force: false }) .png({ compressionLevel: 1, force: false })
.toBuffer(function (err, _data, info) { .toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -653,44 +647,42 @@ describe('Input/output', function () {
}); });
}); });
it('Can force output format with output chaining', function () { it('Can force output format with output chaining', () => sharp(fixtures.inputJpg)
return sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.png({ force: true }) .png({ force: true })
.jpeg({ force: false }) .jpeg({ force: false })
.toBuffer({ resolveWithObject: true }) .toBuffer({ resolveWithObject: true })
.then(function (out) { .then((out) => {
assert.strictEqual('png', out.info.format); assert.strictEqual('png', out.info.format);
}); }));
});
it('toFormat=JPEG takes precedence over WebP extension', function (_t, done) { it('toFormat=JPEG takes precedence over WebP extension', (_t, done) => {
const outputWebP = fixtures.path('output.webp'); const outputWebP = fixtures.path('output.webp');
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(8) .resize(8)
.jpeg() .jpeg()
.toFile(outputWebP, function (err, info) { .toFile(outputWebP, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
fs.rm(outputWebP, done); fs.rm(outputWebP, done);
}); });
}); });
it('toFormat=WebP takes precedence over JPEG extension', function (_t, done) { it('toFormat=WebP takes precedence over JPEG extension', (_t, done) => {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(8) .resize(8)
.webp() .webp()
.toFile(outputJpg, function (err, info) { .toFile(outputJpg, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
done(); done();
}); });
}); });
it('Load Vips V file', function (_t, done) { it('Load Vips V file', (_t, done) => {
sharp(fixtures.inputV) sharp(fixtures.inputV)
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -700,11 +692,11 @@ describe('Input/output', function () {
}); });
}); });
it('Save Vips V file', function (_t, done) { it('Save Vips V file', (_t, done) => {
const outputV = fixtures.path('output.v'); const outputV = fixtures.path('output.v');
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.extract({ left: 910, top: 1105, width: 70, height: 60 }) .extract({ left: 910, top: 1105, width: 70, height: 60 })
.toFile(outputV, function (err, info) { .toFile(outputV, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
assert.strictEqual('v', info.format); assert.strictEqual('v', info.format);
@ -827,62 +819,62 @@ describe('Input/output', function () {
); );
}); });
describe('Input options', function () { describe('Input options', () => {
it('Option-less', function () { it('Option-less', () => {
sharp(); sharp();
}); });
it('Ignore unknown attribute', function () { it('Ignore unknown attribute', () => {
sharp({ unknown: true }); sharp({ unknown: true });
}); });
it('undefined with options fails', function () { it('undefined with options fails', () => {
assert.throws(function () { assert.throws(() => {
sharp(undefined, {}); sharp(undefined, {});
}, /Unsupported input 'undefined' of type undefined when also providing options of type object/); }, /Unsupported input 'undefined' of type undefined when also providing options of type object/);
}); });
it('null with options fails', function () { it('null with options fails', () => {
assert.throws(function () { assert.throws(() => {
sharp(null, {}); sharp(null, {});
}, /Unsupported input 'null' of type object when also providing options of type object/); }, /Unsupported input 'null' of type object when also providing options of type object/);
}); });
it('Non-Object options fails', function () { it('Non-Object options fails', () => {
assert.throws(function () { assert.throws(() => {
sharp('test', 'zoinks'); sharp('test', 'zoinks');
}, /Invalid input options zoinks/); }, /Invalid input options zoinks/);
}); });
it('Invalid density: string', function () { it('Invalid density: string', () => {
assert.throws(function () { assert.throws(() => {
sharp({ density: 'zoinks' }); sharp({ density: 'zoinks' });
}, /Expected number between 1 and 100000 for density but received zoinks of type string/); }, /Expected number between 1 and 100000 for density but received zoinks of type string/);
}); });
it('Invalid ignoreIcc: string', function () { it('Invalid ignoreIcc: string', () => {
assert.throws(function () { assert.throws(() => {
sharp({ ignoreIcc: 'zoinks' }); sharp({ ignoreIcc: 'zoinks' });
}, /Expected boolean for ignoreIcc but received zoinks of type string/); }, /Expected boolean for ignoreIcc but received zoinks of type string/);
}); });
it('Setting animated property updates pages property', function () { it('Setting animated property updates pages property', () => {
assert.strictEqual(sharp({ animated: false }).options.input.pages, 1); assert.strictEqual(sharp({ animated: false }).options.input.pages, 1);
assert.strictEqual(sharp({ animated: true }).options.input.pages, -1); assert.strictEqual(sharp({ animated: true }).options.input.pages, -1);
}); });
it('Invalid animated property throws', function () { it('Invalid animated property throws', () => {
assert.throws(function () { assert.throws(() => {
sharp({ animated: -1 }); sharp({ animated: -1 });
}, /Expected boolean for animated but received -1 of type number/); }, /Expected boolean for animated but received -1 of type number/);
}); });
it('Invalid page property throws', function () { it('Invalid page property throws', () => {
assert.throws(function () { assert.throws(() => {
sharp({ page: -1 }); sharp({ page: -1 });
}, /Expected integer between 0 and 100000 for page but received -1 of type number/); }, /Expected integer between 0 and 100000 for page but received -1 of type number/);
}); });
it('Invalid pages property throws', function () { it('Invalid pages property throws', () => {
assert.throws(function () { assert.throws(() => {
sharp({ pages: '1' }); sharp({ pages: '1' });
}, /Expected integer between -1 and 100000 for pages but received 1 of type string/); }, /Expected integer between -1 and 100000 for pages but received 1 of type string/);
}); });
it('Valid openSlide.level property', function () { it('Valid openSlide.level property', () => {
sharp({ openSlide: { level: 1 } }); sharp({ openSlide: { level: 1 } });
sharp({ level: 1 }); sharp({ level: 1 });
}); });
it('Invalid openSlide.level property (string) throws', function () { it('Invalid openSlide.level property (string) throws', () => {
assert.throws( assert.throws(
() => sharp({ openSlide: { level: '1' } }), () => sharp({ openSlide: { level: '1' } }),
/Expected integer between 0 and 256 for openSlide.level but received 1 of type string/ /Expected integer between 0 and 256 for openSlide.level but received 1 of type string/
@ -892,7 +884,7 @@ describe('Input/output', function () {
/Expected integer between 0 and 256 for level but received 1 of type string/ /Expected integer between 0 and 256 for level but received 1 of type string/
); );
}); });
it('Invalid openSlide.level property (negative) throws', function () { it('Invalid openSlide.level property (negative) throws', () => {
assert.throws( assert.throws(
() => sharp({ openSlide: { level: -1 } }), () => sharp({ openSlide: { level: -1 } }),
/Expected integer between 0 and 256 for openSlide\.level but received -1 of type number/ /Expected integer between 0 and 256 for openSlide\.level but received -1 of type number/
@ -902,11 +894,11 @@ describe('Input/output', function () {
/Expected integer between 0 and 256 for level but received -1 of type number/ /Expected integer between 0 and 256 for level but received -1 of type number/
); );
}); });
it('Valid tiff.subifd property', function () { it('Valid tiff.subifd property', () => {
sharp({ tiff: { subifd: 1 } }); sharp({ tiff: { subifd: 1 } });
sharp({ subifd: 1 }); sharp({ subifd: 1 });
}); });
it('Invalid tiff.subifd property (string) throws', function () { it('Invalid tiff.subifd property (string) throws', () => {
assert.throws( assert.throws(
() => sharp({ tiff: { subifd: '1' } }), () => sharp({ tiff: { subifd: '1' } }),
/Expected integer between -1 and 100000 for tiff\.subifd but received 1 of type string/ /Expected integer between -1 and 100000 for tiff\.subifd but received 1 of type string/
@ -916,7 +908,7 @@ describe('Input/output', function () {
/Expected integer between -1 and 100000 for subifd but received 1 of type string/ /Expected integer between -1 and 100000 for subifd but received 1 of type string/
); );
}); });
it('Invalid tiff.subifd property (float) throws', function () { it('Invalid tiff.subifd property (float) throws', () => {
assert.throws( assert.throws(
() => sharp({ tiff: { subifd: 1.2 } }), () => sharp({ tiff: { subifd: 1.2 } }),
/Expected integer between -1 and 100000 for tiff\.subifd but received 1.2 of type number/ /Expected integer between -1 and 100000 for tiff\.subifd but received 1.2 of type number/
@ -926,15 +918,15 @@ describe('Input/output', function () {
/Expected integer between -1 and 100000 for subifd but received 1.2 of type number/ /Expected integer between -1 and 100000 for subifd but received 1.2 of type number/
); );
}); });
it('Valid pdf.background property (string)', function () { it('Valid pdf.background property (string)', () => {
sharp({ pdf: { background: '#00ff00' } }); sharp({ pdf: { background: '#00ff00' } });
sharp({ pdfBackground: '#00ff00' }); sharp({ pdfBackground: '#00ff00' });
}); });
it('Valid pdf.background property (object)', function () { it('Valid pdf.background property (object)', () => {
sharp({ pdf: { background: { r: 0, g: 255, b: 0 } } }); sharp({ pdf: { background: { r: 0, g: 255, b: 0 } } });
sharp({ pdfBackground: { r: 0, g: 255, b: 0 } }); sharp({ pdfBackground: { r: 0, g: 255, b: 0 } });
}); });
it('Invalid pdf.background property (string) throws', function () { it('Invalid pdf.background property (string) throws', () => {
assert.throws( assert.throws(
() => sharp({ pdf: { background: '00ff00' } }), () => sharp({ pdf: { background: '00ff00' } }),
/Unable to parse color from string/ /Unable to parse color from string/
@ -944,7 +936,7 @@ describe('Input/output', function () {
/Unable to parse color from string/ /Unable to parse color from string/
); );
}); });
it('Invalid pdf.background property (number) throws', function () { it('Invalid pdf.background property (number) throws', () => {
assert.throws( assert.throws(
() => sharp({ pdf: { background: 255 } }), () => sharp({ pdf: { background: 255 } }),
/Expected object or string for background/ /Expected object or string for background/
@ -954,7 +946,7 @@ describe('Input/output', function () {
/Expected object or string for background/ /Expected object or string for background/
); );
}); });
it('Invalid pdf.background property (object)', function () { it('Invalid pdf.background property (object)', () => {
assert.throws( assert.throws(
() => sharp({ pdf: { background: { red: 0, green: 255, blue: 0 } } }), () => sharp({ pdf: { background: { red: 0, green: 255, blue: 0 } } }),
/Unable to parse color from object/ /Unable to parse color from object/
@ -979,8 +971,8 @@ describe('Input/output', function () {
); );
}); });
describe('create new image', function () { describe('create new image', () => {
it('RGB', function (_t, done) { it('RGB', (_t, done) => {
const create = { const create = {
width: 10, width: 10,
height: 20, height: 20,
@ -989,7 +981,7 @@ describe('Input/output', function () {
}; };
sharp({ create }) sharp({ create })
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(create.width, info.width); assert.strictEqual(create.width, info.width);
assert.strictEqual(create.height, info.height); assert.strictEqual(create.height, info.height);
@ -998,7 +990,7 @@ describe('Input/output', function () {
fixtures.assertSimilar(fixtures.expected('create-rgb.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('create-rgb.jpg'), data, done);
}); });
}); });
it('RGBA', function (_t, done) { it('RGBA', (_t, done) => {
const create = { const create = {
width: 20, width: 20,
height: 10, height: 10,
@ -1007,7 +999,7 @@ describe('Input/output', function () {
}; };
sharp({ create }) sharp({ create })
.png() .png()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(create.width, info.width); assert.strictEqual(create.width, info.width);
assert.strictEqual(create.height, info.height); assert.strictEqual(create.height, info.height);
@ -1016,40 +1008,40 @@ describe('Input/output', function () {
fixtures.assertSimilar(fixtures.expected('create-rgba.png'), data, done); fixtures.assertSimilar(fixtures.expected('create-rgba.png'), data, done);
}); });
}); });
it('Invalid channels', function () { it('Invalid channels', () => {
const create = { const create = {
width: 10, width: 10,
height: 20, height: 20,
channels: 2, channels: 2,
background: { r: 0, g: 0, b: 0 } background: { r: 0, g: 0, b: 0 }
}; };
assert.throws(function () { assert.throws(() => {
sharp({ create }); sharp({ create });
}); });
}); });
it('Missing background', function () { it('Missing background', () => {
const create = { const create = {
width: 10, width: 10,
height: 20, height: 20,
channels: 3 channels: 3
}; };
assert.throws(function () { assert.throws(() => {
sharp({ create }); sharp({ create });
}); });
}); });
}); });
it('Queue length change events', function (_t, done) { it('Queue length change events', (_t, done) => {
let eventCounter = 0; let eventCounter = 0;
const queueListener = function (queueLength) { const queueListener = (queueLength) => {
assert.strictEqual(true, queueLength === 0 || queueLength === 1); assert.strictEqual(true, queueLength === 0 || queueLength === 1);
eventCounter++; eventCounter++;
}; };
sharp.queue.on('change', queueListener); sharp.queue.on('change', queueListener);
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.toBuffer(function (err) { .toBuffer((err) => {
process.nextTick(function () { process.nextTick(() => {
sharp.queue.removeListener('change', queueListener); sharp.queue.removeListener('change', queueListener);
if (err) throw err; if (err) throw err;
assert.strictEqual(2, eventCounter); assert.strictEqual(2, eventCounter);
@ -1058,19 +1050,19 @@ describe('Input/output', function () {
}); });
}); });
it('Info event data', function (_t, done) { it('Info event data', (_t, done) => {
const readable = fs.createReadStream(fixtures.inputJPGBig); const readable = fs.createReadStream(fixtures.inputJPGBig);
const inPipeline = sharp() const inPipeline = sharp()
.resize(840, 472) .resize(840, 472)
.raw() .raw()
.on('info', function (info) { .on('info', (info) => {
assert.strictEqual(840, info.width); assert.strictEqual(840, info.width);
assert.strictEqual(472, info.height); assert.strictEqual(472, info.height);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
}); });
const badPipeline = sharp({ raw: { width: 840, height: 500, channels: 3 } }) const badPipeline = sharp({ raw: { width: 840, height: 500, channels: 3 } })
.toFormat('jpeg') .toFormat('jpeg')
.toBuffer(function (err) { .toBuffer((err) => {
assert.strictEqual(err.message.indexOf('memory area too small') > 0, true); assert.strictEqual(err.message.indexOf('memory area too small') > 0, true);
const readable = fs.createReadStream(fixtures.inputJPGBig); const readable = fs.createReadStream(fixtures.inputJPGBig);
const inPipeline = sharp() const inPipeline = sharp()

View File

@ -9,7 +9,7 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Join input images together', function () { describe('Join input images together', () => {
it('Join two images horizontally', async () => { it('Join two images horizontally', async () => {
const data = await sharp([ const data = await sharp([
fixtures.inputPngPalette, fixtures.inputPngPalette,

View File

@ -10,13 +10,13 @@ const fs = require('node:fs');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Image channel insertion', function () { describe('Image channel insertion', () => {
it('Grayscale to RGB, buffer', function (_t, done) { it('Grayscale to RGB, buffer', (_t, done) => {
sharp(fixtures.inputPng) // gray -> red sharp(fixtures.inputPng) // gray -> red
.resize(320, 240) .resize(320, 240)
.joinChannel(fixtures.inputPngTestJoinChannel) // new green channel .joinChannel(fixtures.inputPngTestJoinChannel) // new green channel
.joinChannel(fixtures.inputPngStripesH) // new blue channel .joinChannel(fixtures.inputPngStripesH) // new blue channel
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -25,12 +25,12 @@ describe('Image channel insertion', function () {
}); });
}); });
it('Grayscale to RGB, file', function (_t, done) { it('Grayscale to RGB, file', (_t, done) => {
sharp(fixtures.inputPng) // gray -> red sharp(fixtures.inputPng) // gray -> red
.resize(320, 240) .resize(320, 240)
.joinChannel(fs.readFileSync(fixtures.inputPngTestJoinChannel)) // new green channel .joinChannel(fs.readFileSync(fixtures.inputPngTestJoinChannel)) // new green channel
.joinChannel(fs.readFileSync(fixtures.inputPngStripesH)) // new blue channel .joinChannel(fs.readFileSync(fixtures.inputPngStripesH)) // new blue channel
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -39,7 +39,7 @@ describe('Image channel insertion', function () {
}); });
}); });
it('Grayscale to RGBA, buffer', function (_t, done) { it('Grayscale to RGBA, buffer', (_t, done) => {
sharp(fixtures.inputPng) // gray -> red sharp(fixtures.inputPng) // gray -> red
.resize(320, 240) .resize(320, 240)
.joinChannel([ .joinChannel([
@ -48,7 +48,7 @@ describe('Image channel insertion', function () {
fixtures.inputPngStripesV fixtures.inputPngStripesV
]) // new green + blue + alpha channel ]) // new green + blue + alpha channel
.toColourspace(sharp.colourspace.srgb) .toColourspace(sharp.colourspace.srgb)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -57,7 +57,7 @@ describe('Image channel insertion', function () {
}); });
}); });
it('Grayscale to RGBA, file', function (_t, done) { it('Grayscale to RGBA, file', (_t, done) => {
sharp(fixtures.inputPng) // gray -> red sharp(fixtures.inputPng) // gray -> red
.resize(320, 240) .resize(320, 240)
.joinChannel([ .joinChannel([
@ -66,7 +66,7 @@ describe('Image channel insertion', function () {
fs.readFileSync(fixtures.inputPngStripesV) // new alpha channel fs.readFileSync(fixtures.inputPngStripesV) // new alpha channel
]) ])
.toColourspace('srgb') .toColourspace('srgb')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -75,7 +75,7 @@ describe('Image channel insertion', function () {
}); });
}); });
it('Grayscale to CMYK, buffers', function (_t, done) { it('Grayscale to CMYK, buffers', (_t, done) => {
sharp(fixtures.inputPng) // gray -> magenta sharp(fixtures.inputPng) // gray -> magenta
.resize(320, 240) .resize(320, 240)
.joinChannel([ .joinChannel([
@ -85,7 +85,7 @@ describe('Image channel insertion', function () {
]) ])
.toColorspace('cmyk') .toColorspace('cmyk')
.toFormat('jpeg') .toFormat('jpeg')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -94,12 +94,12 @@ describe('Image channel insertion', function () {
}); });
}); });
it('Join raw buffers to RGB', function (_t, done) { it('Join raw buffers to RGB', (_t, done) => {
Promise.all([ Promise.all([
sharp(fixtures.inputPngTestJoinChannel).toColourspace('b-w').raw().toBuffer(), sharp(fixtures.inputPngTestJoinChannel).toColourspace('b-w').raw().toBuffer(),
sharp(fixtures.inputPngStripesH).toColourspace('b-w').raw().toBuffer() sharp(fixtures.inputPngStripesH).toColourspace('b-w').raw().toBuffer()
]) ])
.then(function (buffers) { .then((buffers) => {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(320, 240) .resize(320, 240)
.joinChannel(buffers, { .joinChannel(buffers, {
@ -109,7 +109,7 @@ describe('Image channel insertion', function () {
channels: 1 channels: 1
} }
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -117,12 +117,12 @@ describe('Image channel insertion', function () {
fixtures.assertSimilar(fixtures.expected('joinChannel-rgb.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('joinChannel-rgb.jpg'), data, done);
}); });
}) })
.catch(function (err) { .catch((err) => {
throw err; throw err;
}); });
}); });
it('Grayscale to RGBA, files, two arrays', function (_t, done) { it('Grayscale to RGBA, files, two arrays', (_t, done) => {
sharp(fixtures.inputPng) // gray -> red sharp(fixtures.inputPng) // gray -> red
.resize(320, 240) .resize(320, 240)
.joinChannel([fs.readFileSync(fixtures.inputPngTestJoinChannel)]) // new green channel .joinChannel([fs.readFileSync(fixtures.inputPngTestJoinChannel)]) // new green channel
@ -131,7 +131,7 @@ describe('Image channel insertion', function () {
fs.readFileSync(fixtures.inputPngStripesV) // new alpha channel fs.readFileSync(fixtures.inputPngStripesV) // new alpha channel
]) ])
.toColourspace('srgb') .toColourspace('srgb')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -140,20 +140,20 @@ describe('Image channel insertion', function () {
}); });
}); });
it('Invalid raw buffer description', function () { it('Invalid raw buffer description', () => {
assert.throws(function () { assert.throws(() => {
sharp().joinChannel(fs.readFileSync(fixtures.inputPng), { raw: {} }); sharp().joinChannel(fs.readFileSync(fixtures.inputPng), { raw: {} });
}); });
}); });
it('Invalid input', function () { it('Invalid input', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).joinChannel(1); sharp(fixtures.inputJpg).joinChannel(1);
}); });
}); });
it('No arguments', function () { it('No arguments', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).joinChannel(); sharp(fixtures.inputJpg).joinChannel();
}); });
}); });

View File

@ -49,15 +49,15 @@ describe('JP2 output', () => {
}); });
}); });
it('JP2 quality', function (done) { it('JP2 quality', (done) => {
sharp(fixtures.inputJp2) sharp(fixtures.inputJp2)
.resize(320, 240) .resize(320, 240)
.jp2({ quality: 70 }) .jp2({ quality: 70 })
.toBuffer(function (err, buffer70) { .toBuffer((err, buffer70) => {
if (err) throw err; if (err) throw err;
sharp(fixtures.inputJp2) sharp(fixtures.inputJp2)
.resize(320, 240) .resize(320, 240)
.toBuffer(function (err, buffer80) { .toBuffer((err, buffer80) => {
if (err) throw err; if (err) throw err;
assert(buffer70.length < buffer80.length); assert(buffer70.length < buffer80.length);
done(); done();
@ -65,12 +65,12 @@ describe('JP2 output', () => {
}); });
}); });
it('Without chroma subsampling generates larger file', function (done) { it('Without chroma subsampling generates larger file', (done) => {
// First generate with chroma subsampling (default) // First generate with chroma subsampling (default)
sharp(fixtures.inputJp2) sharp(fixtures.inputJp2)
.resize(320, 240) .resize(320, 240)
.jp2({ chromaSubsampling: '4:2:0' }) .jp2({ chromaSubsampling: '4:2:0' })
.toBuffer(function (err, withChromaSubsamplingData, withChromaSubsamplingInfo) { .toBuffer((err, withChromaSubsamplingData, withChromaSubsamplingInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withChromaSubsamplingData.length > 0); assert.strictEqual(true, withChromaSubsamplingData.length > 0);
assert.strictEqual(withChromaSubsamplingData.length, withChromaSubsamplingInfo.size); assert.strictEqual(withChromaSubsamplingData.length, withChromaSubsamplingInfo.size);
@ -81,7 +81,7 @@ describe('JP2 output', () => {
sharp(fixtures.inputJp2) sharp(fixtures.inputJp2)
.resize(320, 240) .resize(320, 240)
.jp2({ chromaSubsampling: '4:4:4' }) .jp2({ chromaSubsampling: '4:4:4' })
.toBuffer(function (err, withoutChromaSubsamplingData, withoutChromaSubsamplingInfo) { .toBuffer((err, withoutChromaSubsamplingData, withoutChromaSubsamplingInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withoutChromaSubsamplingData.length > 0); assert.strictEqual(true, withoutChromaSubsamplingData.length > 0);
assert.strictEqual(withoutChromaSubsamplingData.length, withoutChromaSubsamplingInfo.size); assert.strictEqual(withoutChromaSubsamplingData.length, withoutChromaSubsamplingInfo.size);

View File

@ -9,21 +9,21 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('JPEG', function () { describe('JPEG', () => {
it('JPEG quality', function (_t, done) { it('JPEG quality', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ quality: 70 }) .jpeg({ quality: 70 })
.toBuffer(function (err, buffer70) { .toBuffer((err, buffer70) => {
if (err) throw err; if (err) throw err;
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.toBuffer(function (err, buffer80) { .toBuffer((err, buffer80) => {
if (err) throw err; if (err) throw err;
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ quality: 90 }) .jpeg({ quality: 90 })
.toBuffer(function (err, buffer90) { .toBuffer((err, buffer90) => {
if (err) throw err; if (err) throw err;
assert(buffer70.length < buffer80.length); assert(buffer70.length < buffer80.length);
assert(buffer80.length < buffer90.length); assert(buffer80.length < buffer90.length);
@ -33,31 +33,31 @@ describe('JPEG', function () {
}); });
}); });
describe('Invalid JPEG quality', function () { describe('Invalid JPEG quality', () => {
[-1, 88.2, 'test'].forEach(function (quality) { [-1, 88.2, 'test'].forEach((quality) => {
it(quality.toString(), function () { it(quality.toString(), () => {
assert.throws(function () { assert.throws(() => {
sharp().jpeg({ quality }); sharp().jpeg({ quality });
}); });
}); });
}); });
}); });
describe('Invalid JPEG quantisation table', function () { describe('Invalid JPEG quantisation table', () => {
[-1, 88.2, 'test'].forEach(function (table) { [-1, 88.2, 'test'].forEach((table) => {
it(table.toString(), function () { it(table.toString(), () => {
assert.throws(function () { assert.throws(() => {
sharp().jpeg({ quantisationTable: table }); sharp().jpeg({ quantisationTable: table });
}); });
}); });
}); });
}); });
it('Progressive JPEG image', function (_t, done) { it('Progressive JPEG image', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ progressive: false }) .jpeg({ progressive: false })
.toBuffer(function (err, nonProgressiveData, nonProgressiveInfo) { .toBuffer((err, nonProgressiveData, nonProgressiveInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, nonProgressiveData.length > 0); assert.strictEqual(true, nonProgressiveData.length > 0);
assert.strictEqual(nonProgressiveData.length, nonProgressiveInfo.size); assert.strictEqual(nonProgressiveData.length, nonProgressiveInfo.size);
@ -67,7 +67,7 @@ describe('JPEG', function () {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ progressive: true }) .jpeg({ progressive: true })
.toBuffer(function (err, progressiveData, progressiveInfo) { .toBuffer((err, progressiveData, progressiveInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, progressiveData.length > 0); assert.strictEqual(true, progressiveData.length > 0);
assert.strictEqual(progressiveData.length, progressiveInfo.size); assert.strictEqual(progressiveData.length, progressiveInfo.size);
@ -80,12 +80,12 @@ describe('JPEG', function () {
}); });
}); });
it('Without chroma subsampling generates larger file', function (_t, done) { it('Without chroma subsampling generates larger file', (_t, done) => {
// First generate with chroma subsampling (default) // First generate with chroma subsampling (default)
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ chromaSubsampling: '4:2:0' }) .jpeg({ chromaSubsampling: '4:2:0' })
.toBuffer(function (err, withChromaSubsamplingData, withChromaSubsamplingInfo) { .toBuffer((err, withChromaSubsamplingData, withChromaSubsamplingInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withChromaSubsamplingData.length > 0); assert.strictEqual(true, withChromaSubsamplingData.length > 0);
assert.strictEqual(withChromaSubsamplingData.length, withChromaSubsamplingInfo.size); assert.strictEqual(withChromaSubsamplingData.length, withChromaSubsamplingInfo.size);
@ -96,7 +96,7 @@ describe('JPEG', function () {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ chromaSubsampling: '4:4:4' }) .jpeg({ chromaSubsampling: '4:4:4' })
.toBuffer(function (err, withoutChromaSubsamplingData, withoutChromaSubsamplingInfo) { .toBuffer((err, withoutChromaSubsamplingData, withoutChromaSubsamplingInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withoutChromaSubsamplingData.length > 0); assert.strictEqual(true, withoutChromaSubsamplingData.length > 0);
assert.strictEqual(withoutChromaSubsamplingData.length, withoutChromaSubsamplingInfo.size); assert.strictEqual(withoutChromaSubsamplingData.length, withoutChromaSubsamplingInfo.size);
@ -109,18 +109,18 @@ describe('JPEG', function () {
}); });
}); });
it('Invalid JPEG chromaSubsampling value throws error', function () { it('Invalid JPEG chromaSubsampling value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().jpeg({ chromaSubsampling: '4:2:2' }); sharp().jpeg({ chromaSubsampling: '4:2:2' });
}); });
}); });
it('Trellis quantisation', function (_t, done) { it('Trellis quantisation', (_t, done) => {
// First generate without // First generate without
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ trellisQuantisation: false }) .jpeg({ trellisQuantisation: false })
.toBuffer(function (err, withoutData, withoutInfo) { .toBuffer((err, withoutData, withoutInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withoutData.length > 0); assert.strictEqual(true, withoutData.length > 0);
assert.strictEqual(withoutData.length, withoutInfo.size); assert.strictEqual(withoutData.length, withoutInfo.size);
@ -131,7 +131,7 @@ describe('JPEG', function () {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ trellisQuantization: true }) .jpeg({ trellisQuantization: true })
.toBuffer(function (err, withData, withInfo) { .toBuffer((err, withData, withInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withData.length > 0); assert.strictEqual(true, withData.length > 0);
assert.strictEqual(withData.length, withInfo.size); assert.strictEqual(withData.length, withInfo.size);
@ -145,12 +145,12 @@ describe('JPEG', function () {
}); });
}); });
it('Overshoot deringing', function (_t, done) { it('Overshoot deringing', (_t, done) => {
// First generate without // First generate without
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ overshootDeringing: false }) .jpeg({ overshootDeringing: false })
.toBuffer(function (err, withoutData, withoutInfo) { .toBuffer((err, withoutData, withoutInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withoutData.length > 0); assert.strictEqual(true, withoutData.length > 0);
assert.strictEqual(withoutData.length, withoutInfo.size); assert.strictEqual(withoutData.length, withoutInfo.size);
@ -161,7 +161,7 @@ describe('JPEG', function () {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ overshootDeringing: true }) .jpeg({ overshootDeringing: true })
.toBuffer(function (err, withData, withInfo) { .toBuffer((err, withData, withInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withData.length > 0); assert.strictEqual(true, withData.length > 0);
assert.strictEqual(withData.length, withInfo.size); assert.strictEqual(withData.length, withInfo.size);
@ -173,12 +173,12 @@ describe('JPEG', function () {
}); });
}); });
it('Optimise scans generates different output length', function (_t, done) { it('Optimise scans generates different output length', (_t, done) => {
// First generate without // First generate without
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ optimiseScans: false }) .jpeg({ optimiseScans: false })
.toBuffer(function (err, withoutData, withoutInfo) { .toBuffer((err, withoutData, withoutInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withoutData.length > 0); assert.strictEqual(true, withoutData.length > 0);
assert.strictEqual(withoutData.length, withoutInfo.size); assert.strictEqual(withoutData.length, withoutInfo.size);
@ -189,7 +189,7 @@ describe('JPEG', function () {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ optimizeScans: true }) .jpeg({ optimizeScans: true })
.toBuffer(function (err, withData, withInfo) { .toBuffer((err, withData, withInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withData.length > 0); assert.strictEqual(true, withData.length > 0);
assert.strictEqual(withData.length, withInfo.size); assert.strictEqual(withData.length, withInfo.size);
@ -203,12 +203,12 @@ describe('JPEG', function () {
}); });
}); });
it('Optimise coding generates smaller output length', function (_t, done) { it('Optimise coding generates smaller output length', (_t, done) => {
// First generate with optimize coding enabled (default) // First generate with optimize coding enabled (default)
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg() .jpeg()
.toBuffer(function (err, withOptimiseCoding, withInfo) { .toBuffer((err, withOptimiseCoding, withInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withOptimiseCoding.length > 0); assert.strictEqual(true, withOptimiseCoding.length > 0);
assert.strictEqual(withOptimiseCoding.length, withInfo.size); assert.strictEqual(withOptimiseCoding.length, withInfo.size);
@ -219,7 +219,7 @@ describe('JPEG', function () {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ optimizeCoding: false }) .jpeg({ optimizeCoding: false })
.toBuffer(function (err, withoutOptimiseCoding, withoutInfo) { .toBuffer((err, withoutOptimiseCoding, withoutInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withoutOptimiseCoding.length > 0); assert.strictEqual(true, withoutOptimiseCoding.length > 0);
assert.strictEqual(withoutOptimiseCoding.length, withoutInfo.size); assert.strictEqual(withoutOptimiseCoding.length, withoutInfo.size);
@ -233,12 +233,12 @@ describe('JPEG', function () {
}); });
}); });
it('Specifying quantisation table provides different JPEG', function (_t, done) { it('Specifying quantisation table provides different JPEG', (_t, done) => {
// First generate with default quantisation table // First generate with default quantisation table
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ optimiseCoding: false }) .jpeg({ optimiseCoding: false })
.toBuffer(function (err, withDefaultQuantisationTable, withInfo) { .toBuffer((err, withDefaultQuantisationTable, withInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withDefaultQuantisationTable.length > 0); assert.strictEqual(true, withDefaultQuantisationTable.length > 0);
assert.strictEqual(withDefaultQuantisationTable.length, withInfo.size); assert.strictEqual(withDefaultQuantisationTable.length, withInfo.size);
@ -249,7 +249,7 @@ describe('JPEG', function () {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ optimiseCoding: false, quantisationTable: 3 }) .jpeg({ optimiseCoding: false, quantisationTable: 3 })
.toBuffer(function (err, withQuantTable3, withoutInfo) { .toBuffer((err, withQuantTable3, withoutInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withQuantTable3.length > 0); assert.strictEqual(true, withQuantTable3.length > 0);
assert.strictEqual(withQuantTable3.length, withoutInfo.size); assert.strictEqual(withQuantTable3.length, withoutInfo.size);
@ -264,12 +264,12 @@ describe('JPEG', function () {
}); });
}); });
it('Specifying quantization table provides different JPEG', function (_t, done) { it('Specifying quantization table provides different JPEG', (_t, done) => {
// First generate with default quantization table // First generate with default quantization table
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ optimiseCoding: false }) .jpeg({ optimiseCoding: false })
.toBuffer(function (err, withDefaultQuantizationTable, withInfo) { .toBuffer((err, withDefaultQuantizationTable, withInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withDefaultQuantizationTable.length > 0); assert.strictEqual(true, withDefaultQuantizationTable.length > 0);
assert.strictEqual(withDefaultQuantizationTable.length, withInfo.size); assert.strictEqual(withDefaultQuantizationTable.length, withInfo.size);
@ -280,7 +280,7 @@ describe('JPEG', function () {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.jpeg({ optimiseCoding: false, quantizationTable: 3 }) .jpeg({ optimiseCoding: false, quantizationTable: 3 })
.toBuffer(function (err, withQuantTable3, withoutInfo) { .toBuffer((err, withQuantTable3, withoutInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withQuantTable3.length > 0); assert.strictEqual(true, withQuantTable3.length > 0);
assert.strictEqual(withQuantTable3.length, withoutInfo.size); assert.strictEqual(withQuantTable3.length, withoutInfo.size);

View File

@ -11,55 +11,55 @@ const libvips = require('../../lib/libvips');
const originalPlatform = process.platform; const originalPlatform = process.platform;
const setPlatform = function (platform) { const setPlatform = (platform) => {
Object.defineProperty(process, 'platform', { value: platform }); Object.defineProperty(process, 'platform', { value: platform });
}; };
const restorePlatform = function () { const restorePlatform = () => {
setPlatform(originalPlatform); setPlatform(originalPlatform);
}; };
describe('libvips binaries', function () { describe('libvips binaries', () => {
describe('Windows platform', function () { describe('Windows platform', () => {
before(function () { setPlatform('win32'); }); before(() => { setPlatform('win32'); });
after(restorePlatform); after(restorePlatform);
it('pkgConfigPath returns empty string', function () { it('pkgConfigPath returns empty string', () => {
assert.strictEqual('', libvips.pkgConfigPath()); assert.strictEqual('', libvips.pkgConfigPath());
}); });
it('globalLibvipsVersion returns empty string', function () { it('globalLibvipsVersion returns empty string', () => {
assert.strictEqual('', libvips.globalLibvipsVersion()); assert.strictEqual('', libvips.globalLibvipsVersion());
}); });
it('globalLibvipsVersion is always false', function () { it('globalLibvipsVersion is always false', () => {
assert.strictEqual(false, libvips.useGlobalLibvips()); assert.strictEqual(false, libvips.useGlobalLibvips());
}); });
}); });
describe('non-Windows platforms', function () { describe('non-Windows platforms', () => {
before(function () { setPlatform('linux'); }); before(() => { setPlatform('linux'); });
after(restorePlatform); after(restorePlatform);
it('pkgConfigPath returns a string', function () { it('pkgConfigPath returns a string', () => {
const pkgConfigPath = libvips.pkgConfigPath(); const pkgConfigPath = libvips.pkgConfigPath();
assert.strictEqual('string', typeof pkgConfigPath); assert.strictEqual('string', typeof pkgConfigPath);
}); });
it('globalLibvipsVersion returns a string', function () { it('globalLibvipsVersion returns a string', () => {
const globalLibvipsVersion = libvips.globalLibvipsVersion(); const globalLibvipsVersion = libvips.globalLibvipsVersion();
assert.strictEqual('string', typeof globalLibvipsVersion); assert.strictEqual('string', typeof globalLibvipsVersion);
}); });
it('globalLibvipsVersion returns a boolean', function () { it('globalLibvipsVersion returns a boolean', () => {
const useGlobalLibvips = libvips.useGlobalLibvips(); const useGlobalLibvips = libvips.useGlobalLibvips();
assert.strictEqual('boolean', typeof useGlobalLibvips); assert.strictEqual('boolean', typeof useGlobalLibvips);
}); });
}); });
describe('platform agnostic', function () { describe('platform agnostic', () => {
it('minimumLibvipsVersion returns a valid semver', function () { it('minimumLibvipsVersion returns a valid semver', () => {
const minimumLibvipsVersion = libvips.minimumLibvipsVersion; const minimumLibvipsVersion = libvips.minimumLibvipsVersion;
assert.strictEqual('string', typeof minimumLibvipsVersion); assert.strictEqual('string', typeof minimumLibvipsVersion);
assert.notStrictEqual(null, semver.valid(minimumLibvipsVersion)); assert.notStrictEqual(null, semver.valid(minimumLibvipsVersion));
}); });
it('useGlobalLibvips can be ignored via an env var', function () { it('useGlobalLibvips can be ignored via an env var', () => {
process.env.SHARP_IGNORE_GLOBAL_LIBVIPS = 1; process.env.SHARP_IGNORE_GLOBAL_LIBVIPS = 1;
const useGlobalLibvips = libvips.useGlobalLibvips(); const useGlobalLibvips = libvips.useGlobalLibvips();
@ -67,14 +67,14 @@ describe('libvips binaries', function () {
delete process.env.SHARP_IGNORE_GLOBAL_LIBVIPS; delete process.env.SHARP_IGNORE_GLOBAL_LIBVIPS;
}); });
it('useGlobalLibvips can be forced via an env var', function () { it('useGlobalLibvips can be forced via an env var', () => {
process.env.SHARP_FORCE_GLOBAL_LIBVIPS = 1; process.env.SHARP_FORCE_GLOBAL_LIBVIPS = 1;
const useGlobalLibvips = libvips.useGlobalLibvips(); const useGlobalLibvips = libvips.useGlobalLibvips();
assert.strictEqual(true, useGlobalLibvips); assert.strictEqual(true, useGlobalLibvips);
let logged = false; let logged = false;
const logger = function (message) { const logger = (message) => {
assert.strictEqual(message, 'Detected SHARP_FORCE_GLOBAL_LIBVIPS, skipping search for globally-installed libvips'); assert.strictEqual(message, 'Detected SHARP_FORCE_GLOBAL_LIBVIPS, skipping search for globally-installed libvips');
logged = true; logged = true;
}; };
@ -146,25 +146,25 @@ describe('libvips binaries', function () {
}); });
}); });
describe('logger', function () { describe('logger', () => {
const consoleLog = console.log; const consoleLog = console.log;
const consoleError = console.error; const consoleError = console.error;
after(function () { after(() => {
console.log = consoleLog; console.log = consoleLog;
console.error = consoleError; console.error = consoleError;
}); });
it('logs an info message', function (_t, done) { it('logs an info message', (_t, done) => {
console.log = function (msg) { console.log = (msg) => {
assert.strictEqual(msg, 'sharp: progress'); assert.strictEqual(msg, 'sharp: progress');
done(); done();
}; };
libvips.log('progress'); libvips.log('progress');
}); });
it('logs an error message', function (_t, done) { it('logs an error message', (_t, done) => {
console.error = function (msg) { console.error = (msg) => {
assert.strictEqual(msg, 'sharp: Installation error: problem'); assert.strictEqual(msg, 'sharp: Installation error: problem');
done(); done();
}; };

View File

@ -9,82 +9,82 @@ const fixtures = require('../fixtures');
const { describe, it } = require('node:test'); const { describe, it } = require('node:test');
const assert = require('node:assert'); const assert = require('node:assert');
describe('Linear adjustment', function () { describe('Linear adjustment', () => {
const blackPoint = 70; const blackPoint = 70;
const whitePoint = 203; const whitePoint = 203;
const a = 255 / (whitePoint - blackPoint); const a = 255 / (whitePoint - blackPoint);
const b = -blackPoint * a; const b = -blackPoint * a;
it('applies linear levels adjustment w/o alpha ch', function (_t, done) { it('applies linear levels adjustment w/o alpha ch', (_t, done) => {
sharp(fixtures.inputJpgWithLowContrast) sharp(fixtures.inputJpgWithLowContrast)
.linear(a, b) .linear(a, b)
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.expected('low-contrast-linear.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('low-contrast-linear.jpg'), data, done);
}); });
}); });
it('applies slope level adjustment w/o alpha ch', function (_t, done) { it('applies slope level adjustment w/o alpha ch', (_t, done) => {
sharp(fixtures.inputJpgWithLowContrast) sharp(fixtures.inputJpgWithLowContrast)
.linear(a) .linear(a)
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.expected('low-contrast-slope.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('low-contrast-slope.jpg'), data, done);
}); });
}); });
it('applies offset level adjustment w/o alpha ch', function (_t, done) { it('applies offset level adjustment w/o alpha ch', (_t, done) => {
sharp(fixtures.inputJpgWithLowContrast) sharp(fixtures.inputJpgWithLowContrast)
.linear(null, b) .linear(null, b)
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.expected('low-contrast-offset.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('low-contrast-offset.jpg'), data, done);
}); });
}); });
it('applies linear levels adjustment w alpha ch', function (_t, done) { it('applies linear levels adjustment w alpha ch', (_t, done) => {
sharp(fixtures.inputPngOverlayLayer1) sharp(fixtures.inputPngOverlayLayer1)
.resize(240) .resize(240)
.linear(a, b) .linear(a, b)
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-linear.png'), data, done); fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-linear.png'), data, done);
}); });
}); });
it('applies linear levels adjustment to 16-bit w alpha ch', function (_t, done) { it('applies linear levels adjustment to 16-bit w alpha ch', (_t, done) => {
sharp(fixtures.inputPngWithTransparency16bit) sharp(fixtures.inputPngWithTransparency16bit)
.linear(a, b) .linear(a, b)
.png({ compressionLevel: 0 }) .png({ compressionLevel: 0 })
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.expected('linear-16bit.png'), data, done); fixtures.assertSimilar(fixtures.expected('linear-16bit.png'), data, done);
}); });
}); });
it('applies slope level adjustment w alpha ch', function (_t, done) { it('applies slope level adjustment w alpha ch', (_t, done) => {
sharp(fixtures.inputPngOverlayLayer1) sharp(fixtures.inputPngOverlayLayer1)
.resize(240) .resize(240)
.linear(a) .linear(a)
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-slope.png'), data, done); fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-slope.png'), data, done);
}); });
}); });
it('applies offset level adjustment w alpha ch', function (_t, done) { it('applies offset level adjustment w alpha ch', (_t, done) => {
sharp(fixtures.inputPngOverlayLayer1) sharp(fixtures.inputPngOverlayLayer1)
.resize(240) .resize(240)
.linear(null, b) .linear(null, b)
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-offset.png'), data, done); fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-offset.png'), data, done);
}); });
}); });
it('per channel level adjustment', function (_t, done) { it('per channel level adjustment', (_t, done) => {
sharp(fixtures.inputWebP) sharp(fixtures.inputWebP)
.linear([0.25, 0.5, 0.75], [150, 100, 50]).toBuffer(function (err, data) { .linear([0.25, 0.5, 0.75], [150, 100, 50]).toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.expected('linear-per-channel.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('linear-per-channel.jpg'), data, done);
}); });
@ -112,7 +112,7 @@ describe('Linear adjustment', function () {
assert.strictEqual(depth, 'uchar'); assert.strictEqual(depth, 'uchar');
}); });
it('Invalid linear arguments', function () { it('Invalid linear arguments', () => {
assert.throws( assert.throws(
() => sharp().linear('foo'), () => sharp().linear('foo'),
/Expected number or array of numbers for a but received foo of type string/ /Expected number or array of numbers for a but received foo of type string/

View File

@ -16,7 +16,7 @@ const raw = {
channels: 1 channels: 1
}; };
describe('Median filter', function () { describe('Median filter', () => {
it('default window (3x3)', async () => { it('default window (3x3)', async () => {
const data = await sharp(input, { raw }) const data = await sharp(input, { raw })
.median() .median()

View File

@ -14,9 +14,9 @@ const fixtures = require('../fixtures');
const create = { width: 1, height: 1, channels: 3, background: 'red' }; const create = { width: 1, height: 1, channels: 3, background: 'red' };
describe('Image metadata', function () { describe('Image metadata', () => {
it('JPEG', function (_t, done) { it('JPEG', (_t, done) => {
sharp(fixtures.inputJpg).metadata(function (err, metadata) { sharp(fixtures.inputJpg).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', metadata.format); assert.strictEqual('jpeg', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
@ -37,8 +37,8 @@ describe('Image metadata', function () {
}); });
}); });
it('JPEG with EXIF/ICC', function (_t, done) { it('JPEG with EXIF/ICC', (_t, done) => {
sharp(fixtures.inputJpgWithExif).metadata(function (err, metadata) { sharp(fixtures.inputJpgWithExif).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', metadata.format); assert.strictEqual('jpeg', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
@ -70,8 +70,8 @@ describe('Image metadata', function () {
}); });
}); });
it('JPEG with IPTC/XMP', function (_t, done) { it('JPEG with IPTC/XMP', (_t, done) => {
sharp(fixtures.inputJpgWithIptcAndXmp).metadata(function (err, metadata) { sharp(fixtures.inputJpgWithIptcAndXmp).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
// IPTC // IPTC
assert.strictEqual('object', typeof metadata.iptc); assert.strictEqual('object', typeof metadata.iptc);
@ -88,8 +88,8 @@ describe('Image metadata', function () {
}); });
}); });
it('TIFF', function (_t, done) { it('TIFF', (_t, done) => {
sharp(fixtures.inputTiff).metadata(function (err, metadata) { sharp(fixtures.inputTiff).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('tiff', metadata.format); assert.strictEqual('tiff', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
@ -115,8 +115,8 @@ describe('Image metadata', function () {
}); });
}); });
it('Multipage TIFF', function (_t, done) { it('Multipage TIFF', (_t, done) => {
sharp(fixtures.inputTiffMultipage).metadata(function (err, metadata) { sharp(fixtures.inputTiffMultipage).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('tiff', metadata.format); assert.strictEqual('tiff', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
@ -138,8 +138,8 @@ describe('Image metadata', function () {
}); });
}); });
it('PNG', function (_t, done) { it('PNG', (_t, done) => {
sharp(fixtures.inputPng).metadata(function (err, metadata) { sharp(fixtures.inputPng).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', metadata.format); assert.strictEqual('png', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
@ -162,8 +162,8 @@ describe('Image metadata', function () {
}); });
}); });
it('PNG with comment', function (_t, done) { it('PNG with comment', (_t, done) => {
sharp(fixtures.inputPngTestJoinChannel).metadata(function (err, metadata) { sharp(fixtures.inputPngTestJoinChannel).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', metadata.format); assert.strictEqual('png', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
@ -187,8 +187,8 @@ describe('Image metadata', function () {
}); });
}); });
it('Transparent PNG', function (_t, done) { it('Transparent PNG', (_t, done) => {
sharp(fixtures.inputPngWithTransparency).metadata(function (err, metadata) { sharp(fixtures.inputPngWithTransparency).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', metadata.format); assert.strictEqual('png', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
@ -259,8 +259,8 @@ describe('Image metadata', function () {
}); });
}); });
it('WebP', function (_t, done) { it('WebP', (_t, done) => {
sharp(fixtures.inputWebP).metadata(function (err, metadata) { sharp(fixtures.inputWebP).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('webp', metadata.format); assert.strictEqual('webp', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
@ -351,8 +351,8 @@ describe('Image metadata', function () {
}) })
); );
it('GIF', function (_t, done) { it('GIF', (_t, done) => {
sharp(fixtures.inputGif).metadata(function (err, metadata) { sharp(fixtures.inputGif).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('gif', metadata.format); assert.strictEqual('gif', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
@ -371,8 +371,8 @@ describe('Image metadata', function () {
done(); done();
}); });
}); });
it('GIF grey+alpha', function (_t, done) { it('GIF grey+alpha', (_t, done) => {
sharp(fixtures.inputGifGreyPlusAlpha).metadata(function (err, metadata) { sharp(fixtures.inputGifGreyPlusAlpha).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('gif', metadata.format); assert.strictEqual('gif', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
@ -459,8 +459,8 @@ describe('Image metadata', function () {
}) })
); );
it('File in, Promise out', function (_t, done) { it('File in, Promise out', (_t, done) => {
sharp(fixtures.inputJpg).metadata().then(function (metadata) { sharp(fixtures.inputJpg).metadata().then((metadata) => {
assert.strictEqual('jpeg', metadata.format); assert.strictEqual('jpeg', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
assert.strictEqual(2725, metadata.width); assert.strictEqual(2725, metadata.width);
@ -503,10 +503,10 @@ describe('Image metadata', function () {
); );
}); });
it('Stream in, Promise out', function (_t, done) { it('Stream in, Promise out', (_t, done) => {
const readable = fs.createReadStream(fixtures.inputJpg); const readable = fs.createReadStream(fixtures.inputJpg);
const pipeline = sharp(); const pipeline = sharp();
pipeline.metadata().then(function (metadata) { pipeline.metadata().then((metadata) => {
assert.strictEqual('jpeg', metadata.format); assert.strictEqual('jpeg', metadata.format);
assert.strictEqual(829183, metadata.size); assert.strictEqual(829183, metadata.size);
assert.strictEqual(2725, metadata.width); assert.strictEqual(2725, metadata.width);
@ -554,9 +554,9 @@ describe('Image metadata', function () {
}, 500); }, 500);
}); });
it('Stream', function (_t, done) { it('Stream', (_t, done) => {
const readable = fs.createReadStream(fixtures.inputJpg); const readable = fs.createReadStream(fixtures.inputJpg);
const pipeline = sharp().metadata(function (err, metadata) { const pipeline = sharp().metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', metadata.format); assert.strictEqual('jpeg', metadata.format);
assert.strictEqual(829183, metadata.size); assert.strictEqual(829183, metadata.size);
@ -578,9 +578,9 @@ describe('Image metadata', function () {
readable.pipe(pipeline); readable.pipe(pipeline);
}); });
it('Resize to half width using metadata', function (_t, done) { it('Resize to half width using metadata', (_t, done) => {
const image = sharp(fixtures.inputJpg); const image = sharp(fixtures.inputJpg);
image.metadata(function (err, metadata) { image.metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', metadata.format); assert.strictEqual('jpeg', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
@ -597,7 +597,7 @@ describe('Image metadata', function () {
assert.strictEqual('undefined', typeof metadata.orientation); assert.strictEqual('undefined', typeof metadata.orientation);
assert.strictEqual('undefined', typeof metadata.exif); assert.strictEqual('undefined', typeof metadata.exif);
assert.strictEqual('undefined', typeof metadata.icc); assert.strictEqual('undefined', typeof metadata.icc);
image.resize(Math.floor(metadata.width / 2)).toBuffer(function (err, data, info) { image.resize(Math.floor(metadata.width / 2)).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(1362, info.width); assert.strictEqual(1362, info.width);
@ -607,13 +607,13 @@ describe('Image metadata', function () {
}); });
}); });
it('Keep EXIF metadata and add sRGB profile after a resize', function (_t, done) { it('Keep EXIF metadata and add sRGB profile after a resize', (_t, done) => {
sharp(fixtures.inputJpgWithExif) sharp(fixtures.inputJpgWithExif)
.resize(320, 240) .resize(320, 240)
.withMetadata() .withMetadata()
.toBuffer(function (err, buffer) { .toBuffer((err, buffer) => {
if (err) throw err; if (err) throw err;
sharp(buffer).metadata(function (err, metadata) { sharp(buffer).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, metadata.hasProfile); assert.strictEqual(true, metadata.hasProfile);
assert.strictEqual(8, metadata.orientation); assert.strictEqual(8, metadata.orientation);
@ -727,14 +727,14 @@ describe('Image metadata', function () {
assert.strictEqual(undefined, metadata.icc); assert.strictEqual(undefined, metadata.icc);
}); });
it('Apply CMYK output ICC profile', function (_t, done) { it('Apply CMYK output ICC profile', (_t, done) => {
const output = fixtures.path('output.icc-cmyk.jpg'); const output = fixtures.path('output.icc-cmyk.jpg');
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(64) .resize(64)
.withIccProfile('cmyk') .withIccProfile('cmyk')
.toFile(output, function (err) { .toFile(output, (err) => {
if (err) throw err; if (err) throw err;
sharp(output).metadata(function (err, metadata) { sharp(output).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, metadata.hasProfile); assert.strictEqual(true, metadata.hasProfile);
assert.strictEqual('cmyk', metadata.space); assert.strictEqual('cmyk', metadata.space);
@ -752,12 +752,12 @@ describe('Image metadata', function () {
}); });
}); });
it('Apply custom output ICC profile', function (_t, done) { it('Apply custom output ICC profile', (_t, done) => {
const output = fixtures.path('output.hilutite.jpg'); const output = fixtures.path('output.hilutite.jpg');
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(64) .resize(64)
.withIccProfile(fixtures.path('hilutite.icm')) .withIccProfile(fixtures.path('hilutite.icm'))
.toFile(output, function (err) { .toFile(output, (err) => {
if (err) throw err; if (err) throw err;
fixtures.assertMaxColourDistance(output, fixtures.expected('hilutite.jpg'), 9); fixtures.assertMaxColourDistance(output, fixtures.expected('hilutite.jpg'), 9);
done(); done();
@ -792,12 +792,12 @@ describe('Image metadata', function () {
) )
); );
it('Remove EXIF metadata after a resize', function (_t, done) { it('Remove EXIF metadata after a resize', (_t, done) => {
sharp(fixtures.inputJpgWithExif) sharp(fixtures.inputJpgWithExif)
.resize(320, 240) .resize(320, 240)
.toBuffer(function (err, buffer) { .toBuffer((err, buffer) => {
if (err) throw err; if (err) throw err;
sharp(buffer).metadata(function (err, metadata) { sharp(buffer).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(false, metadata.hasProfile); assert.strictEqual(false, metadata.hasProfile);
assert.strictEqual('undefined', typeof metadata.orientation); assert.strictEqual('undefined', typeof metadata.orientation);
@ -808,12 +808,12 @@ describe('Image metadata', function () {
}); });
}); });
it('Remove metadata from PNG output', function (_t, done) { it('Remove metadata from PNG output', (_t, done) => {
sharp(fixtures.inputJpgWithExif) sharp(fixtures.inputJpgWithExif)
.png() .png()
.toBuffer(function (err, buffer) { .toBuffer((err, buffer) => {
if (err) throw err; if (err) throw err;
sharp(buffer).metadata(function (err, metadata) { sharp(buffer).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(false, metadata.hasProfile); assert.strictEqual(false, metadata.hasProfile);
assert.strictEqual('undefined', typeof metadata.orientation); assert.strictEqual('undefined', typeof metadata.orientation);
@ -865,55 +865,41 @@ describe('Image metadata', function () {
assert.strictEqual(density, 96); assert.strictEqual(density, 96);
}); });
it('chromaSubsampling 4:4:4:4 CMYK JPEG', function () { it('chromaSubsampling 4:4:4:4 CMYK JPEG', () => sharp(fixtures.inputJpgWithCmykProfile)
return sharp(fixtures.inputJpgWithCmykProfile)
.metadata() .metadata()
.then(function (metadata) { .then((metadata) => {
assert.strictEqual('4:4:4:4', metadata.chromaSubsampling); assert.strictEqual('4:4:4:4', metadata.chromaSubsampling);
}); }));
});
it('chromaSubsampling 4:4:4 RGB JPEG', function () { it('chromaSubsampling 4:4:4 RGB JPEG', () => sharp(fixtures.inputJpg)
return sharp(fixtures.inputJpg)
.resize(10, 10) .resize(10, 10)
.jpeg({ chromaSubsampling: '4:4:4' }) .jpeg({ chromaSubsampling: '4:4:4' })
.toBuffer() .toBuffer()
.then(function (data) { .then((data) => sharp(data)
return sharp(data)
.metadata() .metadata()
.then(function (metadata) { .then((metadata) => {
assert.strictEqual('4:4:4', metadata.chromaSubsampling); assert.strictEqual('4:4:4', metadata.chromaSubsampling);
}); })));
});
});
it('isProgressive JPEG', function () { it('isProgressive JPEG', () => sharp(fixtures.inputJpg)
return sharp(fixtures.inputJpg)
.resize(10, 10) .resize(10, 10)
.jpeg({ progressive: true }) .jpeg({ progressive: true })
.toBuffer() .toBuffer()
.then(function (data) { .then((data) => sharp(data)
return sharp(data)
.metadata() .metadata()
.then(function (metadata) { .then((metadata) => {
assert.strictEqual(true, metadata.isProgressive); assert.strictEqual(true, metadata.isProgressive);
}); })));
});
});
it('isProgressive PNG', function () { it('isProgressive PNG', () => sharp(fixtures.inputJpg)
return sharp(fixtures.inputJpg)
.resize(10, 10) .resize(10, 10)
.png({ progressive: true }) .png({ progressive: true })
.toBuffer() .toBuffer()
.then(function (data) { .then((data) => sharp(data)
return sharp(data)
.metadata() .metadata()
.then(function (metadata) { .then((metadata) => {
assert.strictEqual(true, metadata.isProgressive); assert.strictEqual(true, metadata.isProgressive);
}); })));
});
});
it('16-bit TIFF with TIFFTAG_PHOTOSHOP metadata', () => it('16-bit TIFF with TIFFTAG_PHOTOSHOP metadata', () =>
sharp(fixtures.inputTifftagPhotoshop) sharp(fixtures.inputTifftagPhotoshop)
@ -995,18 +981,18 @@ describe('Image metadata', function () {
assert.strictEqual(description, 'sP3C'); assert.strictEqual(description, 'sP3C');
}); });
it('File input with corrupt header fails gracefully', function (_t, done) { it('File input with corrupt header fails gracefully', (_t, done) => {
sharp(fixtures.inputJpgWithCorruptHeader) sharp(fixtures.inputJpgWithCorruptHeader)
.metadata(function (err) { .metadata((err) => {
assert.strictEqual(true, !!err); assert.strictEqual(true, !!err);
assert.ok(err.message.includes('Input file has corrupt header: VipsJpeg: premature end of'), err); assert.ok(err.message.includes('Input file has corrupt header: VipsJpeg: premature end of'), err);
done(); done();
}); });
}); });
it('Buffer input with corrupt header fails gracefully', function (_t, done) { it('Buffer input with corrupt header fails gracefully', (_t, done) => {
sharp(fs.readFileSync(fixtures.inputJpgWithCorruptHeader)) sharp(fs.readFileSync(fixtures.inputJpgWithCorruptHeader))
.metadata(function (err) { .metadata((err) => {
assert.strictEqual(true, !!err); assert.strictEqual(true, !!err);
assert.ok(err.message.includes('Input buffer has corrupt header: VipsJpeg: premature end of'), err); assert.ok(err.message.includes('Input buffer has corrupt header: VipsJpeg: premature end of'), err);
done(); done();
@ -1114,7 +1100,7 @@ describe('Image metadata', function () {
assert.strictEqual(exif2.Image.Software, 'sharp'); assert.strictEqual(exif2.Image.Software, 'sharp');
}); });
describe('XMP metadata tests', function () { describe('XMP metadata tests', () => {
it('withMetadata preserves existing XMP metadata from input', async () => { it('withMetadata preserves existing XMP metadata from input', async () => {
const data = await sharp(fixtures.inputJpgWithIptcAndXmp) const data = await sharp(fixtures.inputJpgWithIptcAndXmp)
.resize(320, 240) .resize(320, 240)
@ -1256,21 +1242,21 @@ describe('Image metadata', function () {
assert.strictEqual(true, xmpString.includes('image/webp')); assert.strictEqual(true, xmpString.includes('image/webp'));
}); });
it('withXmp XMP validation - non-string input', function () { it('withXmp XMP validation - non-string input', () => {
assert.throws( assert.throws(
() => sharp().withXmp(123), () => sharp().withXmp(123),
/Expected non-empty string for xmp but received 123 of type number/ /Expected non-empty string for xmp but received 123 of type number/
); );
}); });
it('withXmp XMP validation - null input', function () { it('withXmp XMP validation - null input', () => {
assert.throws( assert.throws(
() => sharp().withXmp(null), () => sharp().withXmp(null),
/Expected non-empty string for xmp but received null of type object/ /Expected non-empty string for xmp but received null of type object/
); );
}); });
it('withXmp XMP validation - empty string', function () { it('withXmp XMP validation - empty string', () => {
assert.throws( assert.throws(
() => sharp().withXmp(''), () => sharp().withXmp(''),
/Expected non-empty string for xmp/ /Expected non-empty string for xmp/
@ -1278,54 +1264,54 @@ describe('Image metadata', function () {
}); });
}); });
describe('Invalid parameters', function () { describe('Invalid parameters', () => {
it('String orientation', function () { it('String orientation', () => {
assert.throws(function () { assert.throws(() => {
sharp().withMetadata({ orientation: 'zoinks' }); sharp().withMetadata({ orientation: 'zoinks' });
}); });
}); });
it('Negative orientation', function () { it('Negative orientation', () => {
assert.throws(function () { assert.throws(() => {
sharp().withMetadata({ orientation: -1 }); sharp().withMetadata({ orientation: -1 });
}); });
}); });
it('Zero orientation', function () { it('Zero orientation', () => {
assert.throws(function () { assert.throws(() => {
sharp().withMetadata({ orientation: 0 }); sharp().withMetadata({ orientation: 0 });
}); });
}); });
it('Too large orientation', function () { it('Too large orientation', () => {
assert.throws(function () { assert.throws(() => {
sharp().withMetadata({ orientation: 9 }); sharp().withMetadata({ orientation: 9 });
}); });
}); });
it('Non-numeric density', function () { it('Non-numeric density', () => {
assert.throws(function () { assert.throws(() => {
sharp().withMetadata({ density: '1' }); sharp().withMetadata({ density: '1' });
}); });
}); });
it('Negative density', function () { it('Negative density', () => {
assert.throws(function () { assert.throws(() => {
sharp().withMetadata({ density: -1 }); sharp().withMetadata({ density: -1 });
}); });
}); });
it('Non string icc', function () { it('Non string icc', () => {
assert.throws(function () { assert.throws(() => {
sharp().withMetadata({ icc: true }); sharp().withMetadata({ icc: true });
}); });
}); });
it('Non object exif', function () { it('Non object exif', () => {
assert.throws(function () { assert.throws(() => {
sharp().withMetadata({ exif: false }); sharp().withMetadata({ exif: false });
}); });
}); });
it('Non string value in object exif', function () { it('Non string value in object exif', () => {
assert.throws(function () { assert.throws(() => {
sharp().withMetadata({ exif: { ifd0: false } }); sharp().withMetadata({ exif: { ifd0: false } });
}); });
}); });
it('Non string value in nested object exif', function () { it('Non string value in nested object exif', () => {
assert.throws(function () { assert.throws(() => {
sharp().withMetadata({ exif: { ifd0: { fail: false } } }); sharp().withMetadata({ exif: { ifd0: { fail: false } } });
}); });
}); });

View File

@ -8,8 +8,8 @@ const { describe, it } = require('node:test');
const assert = require('node:assert'); const assert = require('node:assert');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Modulate', function () { describe('Modulate', () => {
describe('Invalid options', function () { describe('Invalid options', () => {
[ [
null, null,
undefined, undefined,
@ -25,9 +25,9 @@ describe('Modulate', function () {
{ hue: null }, { hue: null },
{ lightness: '+50' }, { lightness: '+50' },
{ lightness: null } { lightness: null }
].forEach(function (options) { ].forEach((options) => {
it('should throw', function () { it('should throw', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).modulate(options); sharp(fixtures.inputJpg).modulate(options);
}); });
}); });

View File

@ -9,12 +9,12 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Negate', function () { describe('Negate', () => {
it('negate (jpeg)', function (_t, done) { it('negate (jpeg)', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.negate() .negate()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -23,11 +23,11 @@ describe('Negate', function () {
}); });
}); });
it('negate (png)', function (_t, done) { it('negate (png)', (_t, done) => {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(320, 240) .resize(320, 240)
.negate() .negate()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -36,11 +36,11 @@ describe('Negate', function () {
}); });
}); });
it('negate (png, trans)', function (_t, done) { it('negate (png, trans)', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(320, 240) .resize(320, 240)
.negate() .negate()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -49,11 +49,11 @@ describe('Negate', function () {
}); });
}); });
it('negate (png, alpha)', function (_t, done) { it('negate (png, alpha)', (_t, done) => {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.resize(320, 240) .resize(320, 240)
.negate() .negate()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -62,11 +62,11 @@ describe('Negate', function () {
}); });
}); });
it('negate (webp)', function (_t, done) { it('negate (webp)', (_t, done) => {
sharp(fixtures.inputWebP) sharp(fixtures.inputWebP)
.resize(320, 240) .resize(320, 240)
.negate() .negate()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -75,11 +75,11 @@ describe('Negate', function () {
}); });
}); });
it('negate (webp, trans)', function (_t, done) { it('negate (webp, trans)', (_t, done) => {
sharp(fixtures.inputWebPWithTransparency) sharp(fixtures.inputWebPWithTransparency)
.resize(320, 240) .resize(320, 240)
.negate() .negate()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -88,11 +88,11 @@ describe('Negate', function () {
}); });
}); });
it('negate (true)', function (_t, done) { it('negate (true)', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.negate(true) .negate(true)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -101,22 +101,22 @@ describe('Negate', function () {
}); });
}); });
it('negate (false)', function (_t, done) { it('negate (false)', (_t, done) => {
const output = fixtures.path('output.unmodified-by-negate.png'); const output = fixtures.path('output.unmodified-by-negate.png');
sharp(fixtures.inputJpgWithLowContrast) sharp(fixtures.inputJpgWithLowContrast)
.negate(false) .negate(false)
.toFile(output, function (err) { .toFile(output, (err) => {
if (err) throw err; if (err) throw err;
fixtures.assertMaxColourDistance(output, fixtures.inputJpgWithLowContrast, 0); fixtures.assertMaxColourDistance(output, fixtures.inputJpgWithLowContrast, 0);
done(); done();
}); });
}); });
it('negate ({alpha: true})', function (_t, done) { it('negate ({alpha: true})', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.negate({ alpha: true }) .negate({ alpha: true })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -125,11 +125,11 @@ describe('Negate', function () {
}); });
}); });
it('negate non-alpha channels (png)', function (_t, done) { it('negate non-alpha channels (png)', (_t, done) => {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(320, 240) .resize(320, 240)
.negate({ alpha: false }) .negate({ alpha: false })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -138,11 +138,11 @@ describe('Negate', function () {
}); });
}); });
it('negate non-alpha channels (png, trans)', function (_t, done) { it('negate non-alpha channels (png, trans)', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(320, 240) .resize(320, 240)
.negate({ alpha: false }) .negate({ alpha: false })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -151,11 +151,11 @@ describe('Negate', function () {
}); });
}); });
it('negate non-alpha channels (png, alpha)', function (_t, done) { it('negate non-alpha channels (png, alpha)', (_t, done) => {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.resize(320, 240) .resize(320, 240)
.negate({ alpha: false }) .negate({ alpha: false })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -164,11 +164,11 @@ describe('Negate', function () {
}); });
}); });
it('negate non-alpha channels (webp)', function (_t, done) { it('negate non-alpha channels (webp)', (_t, done) => {
sharp(fixtures.inputWebP) sharp(fixtures.inputWebP)
.resize(320, 240) .resize(320, 240)
.negate({ alpha: false }) .negate({ alpha: false })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -177,11 +177,11 @@ describe('Negate', function () {
}); });
}); });
it('negate non-alpha channels (webp, trans)', function (_t, done) { it('negate non-alpha channels (webp, trans)', (_t, done) => {
sharp(fixtures.inputWebPWithTransparency) sharp(fixtures.inputWebPWithTransparency)
.resize(320, 240) .resize(320, 240)
.negate({ alpha: false }) .negate({ alpha: false })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -206,8 +206,8 @@ describe('Negate', function () {
assert.deepStrictEqual({ r, g, b }, { r: 245, g: 235, b: 225 }); assert.deepStrictEqual({ r, g, b }, { r: 245, g: 235, b: 225 });
}); });
it('invalid alpha value', function () { it('invalid alpha value', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputWebPWithTransparency).negate({ alpha: 'non-bool' }); sharp(fixtures.inputWebPWithTransparency).negate({ alpha: 'non-bool' });
}); });
}); });

View File

@ -9,8 +9,8 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Gaussian noise', function () { describe('Gaussian noise', () => {
it('generate single-channel gaussian noise', function (_t, done) { it('generate single-channel gaussian noise', (_t, done) => {
const output = fixtures.path('output.noise-1-channel.png'); const output = fixtures.path('output.noise-1-channel.png');
const noise = sharp({ const noise = sharp({
create: { create: {
@ -24,13 +24,13 @@ describe('Gaussian noise', function () {
} }
} }
}).toColourspace('b-w'); }).toColourspace('b-w');
noise.toFile(output, function (err, info) { noise.toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(1024, info.width); assert.strictEqual(1024, info.width);
assert.strictEqual(768, info.height); assert.strictEqual(768, info.height);
assert.strictEqual(1, info.channels); assert.strictEqual(1, info.channels);
sharp(output).metadata(function (err, metadata) { sharp(output).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('b-w', metadata.space); assert.strictEqual('b-w', metadata.space);
assert.strictEqual('uchar', metadata.depth); assert.strictEqual('uchar', metadata.depth);
@ -39,7 +39,7 @@ describe('Gaussian noise', function () {
}); });
}); });
it('generate 3-channels gaussian noise', function (_t, done) { it('generate 3-channels gaussian noise', (_t, done) => {
const output = fixtures.path('output.noise-3-channels.png'); const output = fixtures.path('output.noise-3-channels.png');
const noise = sharp({ const noise = sharp({
create: { create: {
@ -53,13 +53,13 @@ describe('Gaussian noise', function () {
} }
} }
}); });
noise.toFile(output, function (err, info) { noise.toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(1024, info.width); assert.strictEqual(1024, info.width);
assert.strictEqual(768, info.height); assert.strictEqual(768, info.height);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
sharp(output).metadata(function (err, metadata) { sharp(output).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('srgb', metadata.space); assert.strictEqual('srgb', metadata.space);
assert.strictEqual('uchar', metadata.depth); assert.strictEqual('uchar', metadata.depth);
@ -68,7 +68,7 @@ describe('Gaussian noise', function () {
}); });
}); });
it('overlay 3-channels gaussian noise over image', function (_t, done) { it('overlay 3-channels gaussian noise over image', (_t, done) => {
const output = fixtures.path('output.noise-image.jpg'); const output = fixtures.path('output.noise-image.jpg');
const noise = sharp({ const noise = sharp({
create: { create: {
@ -82,7 +82,7 @@ describe('Gaussian noise', function () {
} }
} }
}); });
noise.toBuffer(function (err, data, info) { noise.toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
@ -98,14 +98,14 @@ describe('Gaussian noise', function () {
} }
} }
]) ])
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
// perceptual hashing detects that images are the same (difference is <=1%) // perceptual hashing detects that images are the same (difference is <=1%)
fixtures.assertSimilar(output, fixtures.inputJpg, function (err) { fixtures.assertSimilar(output, fixtures.inputJpg, (err) => {
if (err) throw err; if (err) throw err;
done(); done();
}); });
@ -113,7 +113,7 @@ describe('Gaussian noise', function () {
}); });
}); });
it('overlay strong single-channel (sRGB) gaussian noise with 25% transparency over transparent png image', function (_t, done) { it('overlay strong single-channel (sRGB) gaussian noise with 25% transparency over transparent png image', (_t, done) => {
const output = fixtures.path('output.noise-image-transparent.png'); const output = fixtures.path('output.noise-image-transparent.png');
const width = 320; const width = 320;
const height = 240; const height = 240;
@ -136,14 +136,14 @@ describe('Gaussian noise', function () {
}); });
noise noise
.toColourspace('b-w') .toColourspace('b-w')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(1, info.channels); assert.strictEqual(1, info.channels);
sharp(data, { raw: rawData }) sharp(data, { raw: rawData })
.joinChannel(data, { raw: rawData }) // r channel .joinChannel(data, { raw: rawData }) // r channel
.joinChannel(data, { raw: rawData }) // b channel .joinChannel(data, { raw: rawData }) // b channel
.joinChannel(Buffer.alloc(width * height, 64), { raw: rawData }) // alpha channel .joinChannel(Buffer.alloc(width * height, 64), { raw: rawData }) // alpha channel
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(4, info.channels); assert.strictEqual(4, info.channels);
sharp(fixtures.inputPngRGBWithAlpha) sharp(fixtures.inputPngRGBWithAlpha)
@ -159,13 +159,13 @@ describe('Gaussian noise', function () {
} }
} }
]) ])
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(width, info.width); assert.strictEqual(width, info.width);
assert.strictEqual(height, info.height); assert.strictEqual(height, info.height);
assert.strictEqual(4, info.channels); assert.strictEqual(4, info.channels);
fixtures.assertSimilar(output, fixtures.inputPngRGBWithAlpha, { threshold: 10 }, function (err) { fixtures.assertSimilar(output, fixtures.inputPngRGBWithAlpha, { threshold: 10 }, (err) => {
if (err) throw err; if (err) throw err;
done(); done();
}); });
@ -194,16 +194,16 @@ describe('Gaussian noise', function () {
assert.strictEqual(delay.length, 4); assert.strictEqual(delay.length, 4);
}); });
it('no create object properties specified', function () { it('no create object properties specified', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
create: {} create: {}
}); });
}); });
}); });
it('invalid noise object', function () { it('invalid noise object', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
create: { create: {
width: 100, width: 100,
@ -215,8 +215,8 @@ describe('Gaussian noise', function () {
}); });
}); });
it('unknown type of noise', function () { it('unknown type of noise', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
create: { create: {
width: 100, width: 100,
@ -230,8 +230,8 @@ describe('Gaussian noise', function () {
}); });
}); });
it('gaussian noise, invalid amount of channels', function () { it('gaussian noise, invalid amount of channels', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
create: { create: {
width: 100, width: 100,
@ -247,8 +247,8 @@ describe('Gaussian noise', function () {
}); });
}); });
it('gaussian noise, invalid mean', function () { it('gaussian noise, invalid mean', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
create: { create: {
width: 100, width: 100,
@ -264,8 +264,8 @@ describe('Gaussian noise', function () {
}); });
}); });
it('gaussian noise, invalid sigma', function () { it('gaussian noise, invalid sigma', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
create: { create: {
width: 100, width: 100,

View File

@ -9,7 +9,7 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
const assertNormalized = function (data) { const assertNormalized = (data) => {
let min = 255; let min = 255;
let max = 0; let max = 0;
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
@ -20,48 +20,48 @@ const assertNormalized = function (data) {
assert.ok(max > 248, 'max too low'); assert.ok(max > 248, 'max too low');
}; };
describe('Normalization', function () { describe('Normalization', () => {
it('spreads rgb image values between 0 and 255', function (_t, done) { it('spreads rgb image values between 0 and 255', (_t, done) => {
sharp(fixtures.inputJpgWithLowContrast) sharp(fixtures.inputJpgWithLowContrast)
.normalise() .normalise()
.raw() .raw()
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
assertNormalized(data); assertNormalized(data);
done(); done();
}); });
}); });
it('spreads grayscaled image values between 0 and 255', function (_t, done) { it('spreads grayscaled image values between 0 and 255', (_t, done) => {
sharp(fixtures.inputJpgWithLowContrast) sharp(fixtures.inputJpgWithLowContrast)
.greyscale() .greyscale()
.normalize() .normalize()
.raw() .raw()
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
assertNormalized(data); assertNormalized(data);
done(); done();
}); });
}); });
it('stretches greyscale images with alpha channel', function (_t, done) { it('stretches greyscale images with alpha channel', (_t, done) => {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.normalise() .normalise()
.raw() .raw()
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
assertNormalized(data); assertNormalized(data);
done(); done();
}); });
}); });
it('keeps an existing alpha channel', function (_t, done) { it('keeps an existing alpha channel', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(8, 8) .resize(8, 8)
.normalize() .normalize()
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
sharp(data).metadata(function (err, metadata) { sharp(data).metadata((err, metadata) => {
if (err) return done(err); if (err) return done(err);
assert.strictEqual(4, metadata.channels); assert.strictEqual(4, metadata.channels);
assert.strictEqual(true, metadata.hasAlpha); assert.strictEqual(true, metadata.hasAlpha);
@ -71,13 +71,13 @@ describe('Normalization', function () {
}); });
}); });
it('keeps the alpha channel of greyscale images intact', function (_t, done) { it('keeps the alpha channel of greyscale images intact', (_t, done) => {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.resize(8, 8) .resize(8, 8)
.normalise() .normalise()
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
sharp(data).metadata(function (err, metadata) { sharp(data).metadata((err, metadata) => {
if (err) return done(err); if (err) return done(err);
assert.strictEqual(true, metadata.hasAlpha); assert.strictEqual(true, metadata.hasAlpha);
assert.strictEqual(4, metadata.channels); assert.strictEqual(4, metadata.channels);
@ -87,76 +87,76 @@ describe('Normalization', function () {
}); });
}); });
it('does not alter images with only one color', function (_t, done) { it('does not alter images with only one color', (_t, done) => {
const output = fixtures.path('output.unmodified-png-with-one-color.png'); const output = fixtures.path('output.unmodified-png-with-one-color.png');
sharp(fixtures.inputPngWithOneColor) sharp(fixtures.inputPngWithOneColor)
.normalize() .normalize()
.toFile(output, function (err) { .toFile(output, (err) => {
if (err) done(err); if (err) done(err);
fixtures.assertMaxColourDistance(output, fixtures.inputPngWithOneColor, 0); fixtures.assertMaxColourDistance(output, fixtures.inputPngWithOneColor, 0);
done(); done();
}); });
}); });
it('works with 16-bit RGBA images', function (_t, done) { it('works with 16-bit RGBA images', (_t, done) => {
sharp(fixtures.inputPngWithTransparency16bit) sharp(fixtures.inputPngWithTransparency16bit)
.normalise() .normalise()
.raw() .raw()
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
assertNormalized(data); assertNormalized(data);
done(); done();
}); });
}); });
it('should handle luminance range', function (_t, done) { it('should handle luminance range', (_t, done) => {
sharp(fixtures.inputJpgWithLowContrast) sharp(fixtures.inputJpgWithLowContrast)
.normalise({ lower: 10, upper: 70 }) .normalise({ lower: 10, upper: 70 })
.raw() .raw()
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
assertNormalized(data); assertNormalized(data);
done(); done();
}); });
}); });
it('should allow lower without upper', function () { it('should allow lower without upper', () => {
assert.doesNotThrow(() => sharp().normalize({ lower: 2 })); assert.doesNotThrow(() => sharp().normalize({ lower: 2 }));
}); });
it('should allow upper without lower', function () { it('should allow upper without lower', () => {
assert.doesNotThrow(() => sharp().normalize({ upper: 98 })); assert.doesNotThrow(() => sharp().normalize({ upper: 98 }));
}); });
it('should throw when lower is out of range', function () { it('should throw when lower is out of range', () => {
assert.throws( assert.throws(
() => sharp().normalise({ lower: -10 }), () => sharp().normalise({ lower: -10 }),
/Expected number between 0 and 99 for lower but received -10 of type number/ /Expected number between 0 and 99 for lower but received -10 of type number/
); );
}); });
it('should throw when upper is out of range', function () { it('should throw when upper is out of range', () => {
assert.throws( assert.throws(
() => sharp().normalise({ upper: 110 }), () => sharp().normalise({ upper: 110 }),
/Expected number between 1 and 100 for upper but received 110 of type number/ /Expected number between 1 and 100 for upper but received 110 of type number/
); );
}); });
it('should throw when lower is not a number', function () { it('should throw when lower is not a number', () => {
assert.throws( assert.throws(
() => sharp().normalise({ lower: 'fail' }), () => sharp().normalise({ lower: 'fail' }),
/Expected number between 0 and 99 for lower but received fail of type string/ /Expected number between 0 and 99 for lower but received fail of type string/
); );
}); });
it('should throw when upper is not a number', function () { it('should throw when upper is not a number', () => {
assert.throws( assert.throws(
() => sharp().normalise({ upper: 'fail' }), () => sharp().normalise({ upper: 'fail' }),
/Expected number between 1 and 100 for upper but received fail of type string/ /Expected number between 1 and 100 for upper but received fail of type string/
); );
}); });
it('should throw when the lower and upper are equal', function () { it('should throw when the lower and upper are equal', () => {
assert.throws( assert.throws(
() => sharp().normalise({ lower: 2, upper: 2 }), () => sharp().normalise({ lower: 2, upper: 2 }),
/Expected lower to be less than upper for range but received 2 >= 2/ /Expected lower to be less than upper for range but received 2 >= 2/
); );
}); });
it('should throw when the lower is greater than upper', function () { it('should throw when the lower is greater than upper', () => {
assert.throws( assert.throws(
() => sharp().normalise({ lower: 3, upper: 2 }), () => sharp().normalise({ lower: 3, upper: 2 }),
/Expected lower to be less than upper for range but received 3 >= 2/ /Expected lower to be less than upper for range but received 3 >= 2/

View File

@ -10,25 +10,25 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('PNG', function () { describe('PNG', () => {
it('compression level is valid', function () { it('compression level is valid', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().png({ compressionLevel: 0 }); sharp().png({ compressionLevel: 0 });
}); });
}); });
it('compression level is invalid', function () { it('compression level is invalid', () => {
assert.throws(function () { assert.throws(() => {
sharp().png({ compressionLevel: -1 }); sharp().png({ compressionLevel: -1 });
}); });
}); });
it('default compressionLevel generates smaller file than compressionLevel=0', function (_t, done) { it('default compressionLevel generates smaller file than compressionLevel=0', (_t, done) => {
// First generate with default compressionLevel // First generate with default compressionLevel
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(320, 240) .resize(320, 240)
.png() .png()
.toBuffer(function (err, defaultData, defaultInfo) { .toBuffer((err, defaultData, defaultInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, defaultData.length > 0); assert.strictEqual(true, defaultData.length > 0);
assert.strictEqual('png', defaultInfo.format); assert.strictEqual('png', defaultInfo.format);
@ -36,7 +36,7 @@ describe('PNG', function () {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(320, 240) .resize(320, 240)
.png({ compressionLevel: 0 }) .png({ compressionLevel: 0 })
.toBuffer(function (err, largerData, largerInfo) { .toBuffer((err, largerData, largerInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, largerData.length > 0); assert.strictEqual(true, largerData.length > 0);
assert.strictEqual('png', largerInfo.format); assert.strictEqual('png', largerInfo.format);
@ -46,12 +46,12 @@ describe('PNG', function () {
}); });
}); });
it('without adaptiveFiltering generates smaller file', function (_t, done) { it('without adaptiveFiltering generates smaller file', (_t, done) => {
// First generate with adaptive filtering // First generate with adaptive filtering
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(320, 240) .resize(320, 240)
.png({ adaptiveFiltering: true }) .png({ adaptiveFiltering: true })
.toBuffer(function (err, adaptiveData, adaptiveInfo) { .toBuffer((err, adaptiveData, adaptiveInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, adaptiveData.length > 0); assert.strictEqual(true, adaptiveData.length > 0);
assert.strictEqual(adaptiveData.length, adaptiveInfo.size); assert.strictEqual(adaptiveData.length, adaptiveInfo.size);
@ -62,7 +62,7 @@ describe('PNG', function () {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(320, 240) .resize(320, 240)
.png({ adaptiveFiltering: false }) .png({ adaptiveFiltering: false })
.toBuffer(function (err, withoutAdaptiveData, withoutAdaptiveInfo) { .toBuffer((err, withoutAdaptiveData, withoutAdaptiveInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, withoutAdaptiveData.length > 0); assert.strictEqual(true, withoutAdaptiveData.length > 0);
assert.strictEqual(withoutAdaptiveData.length, withoutAdaptiveInfo.size); assert.strictEqual(withoutAdaptiveData.length, withoutAdaptiveInfo.size);
@ -75,17 +75,17 @@ describe('PNG', function () {
}); });
}); });
it('Invalid PNG adaptiveFiltering value throws error', function () { it('Invalid PNG adaptiveFiltering value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().png({ adaptiveFiltering: 1 }); sharp().png({ adaptiveFiltering: 1 });
}); });
}); });
it('Progressive PNG image', function (_t, done) { it('Progressive PNG image', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.png({ progressive: false }) .png({ progressive: false })
.toBuffer(function (err, nonProgressiveData, nonProgressiveInfo) { .toBuffer((err, nonProgressiveData, nonProgressiveInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, nonProgressiveData.length > 0); assert.strictEqual(true, nonProgressiveData.length > 0);
assert.strictEqual(nonProgressiveData.length, nonProgressiveInfo.size); assert.strictEqual(nonProgressiveData.length, nonProgressiveInfo.size);
@ -94,7 +94,7 @@ describe('PNG', function () {
assert.strictEqual(240, nonProgressiveInfo.height); assert.strictEqual(240, nonProgressiveInfo.height);
sharp(nonProgressiveData) sharp(nonProgressiveData)
.png({ progressive: true }) .png({ progressive: true })
.toBuffer(function (err, progressiveData, progressiveInfo) { .toBuffer((err, progressiveData, progressiveInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, progressiveData.length > 0); assert.strictEqual(true, progressiveData.length > 0);
assert.strictEqual(progressiveData.length, progressiveInfo.size); assert.strictEqual(progressiveData.length, progressiveInfo.size);
@ -107,11 +107,11 @@ describe('PNG', function () {
}); });
}); });
it('16-bit grey+alpha PNG identity transform', function () { it('16-bit grey+alpha PNG identity transform', () => {
const actual = fixtures.path('output.16-bit-grey-alpha-identity.png'); const actual = fixtures.path('output.16-bit-grey-alpha-identity.png');
return sharp(fixtures.inputPng16BitGreyAlpha) return sharp(fixtures.inputPng16BitGreyAlpha)
.toFile(actual) .toFile(actual)
.then(function () { .then(() => {
fixtures.assertMaxColourDistance(actual, fixtures.expected('16-bit-grey-alpha-identity.png')); fixtures.assertMaxColourDistance(actual, fixtures.expected('16-bit-grey-alpha-identity.png'));
}); });
}); });
@ -160,30 +160,30 @@ describe('PNG', function () {
}); });
}); });
it('Valid PNG libimagequant palette value does not throw error', function () { it('Valid PNG libimagequant palette value does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().png({ palette: false }); sharp().png({ palette: false });
}); });
}); });
it('Invalid PNG libimagequant palette value throws error', function () { it('Invalid PNG libimagequant palette value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().png({ palette: 'fail' }); sharp().png({ palette: 'fail' });
}); });
}); });
it('Valid PNG libimagequant quality value produces image of same size or smaller', function () { it('Valid PNG libimagequant quality value produces image of same size or smaller', () => {
const inputPngBuffer = fs.readFileSync(fixtures.inputPng); const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
return Promise.all([ return Promise.all([
sharp(inputPngBuffer).resize(10).png({ effort: 1, quality: 80 }).toBuffer(), sharp(inputPngBuffer).resize(10).png({ effort: 1, quality: 80 }).toBuffer(),
sharp(inputPngBuffer).resize(10).png({ effort: 1, quality: 100 }).toBuffer() sharp(inputPngBuffer).resize(10).png({ effort: 1, quality: 100 }).toBuffer()
]).then(function (data) { ]).then((data) => {
assert.strictEqual(true, data[0].length <= data[1].length); assert.strictEqual(true, data[0].length <= data[1].length);
}); });
}); });
it('Invalid PNG libimagequant quality value throws error', function () { it('Invalid PNG libimagequant quality value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().png({ quality: 101 }); sharp().png({ quality: 101 });
}); });
}); });
@ -194,24 +194,24 @@ describe('PNG', function () {
}); });
}); });
it('Valid PNG libimagequant colours value produces image of same size or smaller', function () { it('Valid PNG libimagequant colours value produces image of same size or smaller', () => {
const inputPngBuffer = fs.readFileSync(fixtures.inputPng); const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
return Promise.all([ return Promise.all([
sharp(inputPngBuffer).resize(10).png({ colours: 100 }).toBuffer(), sharp(inputPngBuffer).resize(10).png({ colours: 100 }).toBuffer(),
sharp(inputPngBuffer).resize(10).png({ colours: 200 }).toBuffer() sharp(inputPngBuffer).resize(10).png({ colours: 200 }).toBuffer()
]).then(function (data) { ]).then((data) => {
assert.strictEqual(true, data[0].length <= data[1].length); assert.strictEqual(true, data[0].length <= data[1].length);
}); });
}); });
it('Invalid PNG libimagequant colours value throws error', function () { it('Invalid PNG libimagequant colours value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().png({ colours: -1 }); sharp().png({ colours: -1 });
}); });
}); });
it('Invalid PNG libimagequant colors value throws error', function () { it('Invalid PNG libimagequant colors value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().png({ colors: 0.1 }); sharp().png({ colors: 0.1 });
}); });
}); });
@ -235,18 +235,18 @@ describe('PNG', function () {
assert.strictEqual(space, 'b-w'); assert.strictEqual(space, 'b-w');
}); });
it('Valid PNG libimagequant dither value produces image of same size or smaller', function () { it('Valid PNG libimagequant dither value produces image of same size or smaller', () => {
const inputPngBuffer = fs.readFileSync(fixtures.inputPng); const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
return Promise.all([ return Promise.all([
sharp(inputPngBuffer).resize(10).png({ dither: 0.1 }).toBuffer(), sharp(inputPngBuffer).resize(10).png({ dither: 0.1 }).toBuffer(),
sharp(inputPngBuffer).resize(10).png({ dither: 0.9 }).toBuffer() sharp(inputPngBuffer).resize(10).png({ dither: 0.9 }).toBuffer()
]).then(function (data) { ]).then((data) => {
assert.strictEqual(true, data[0].length <= data[1].length); assert.strictEqual(true, data[0].length <= data[1].length);
}); });
}); });
it('Invalid PNG libimagequant dither value throws error', function () { it('Invalid PNG libimagequant dither value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().png({ dither: 'fail' }); sharp().png({ dither: 'fail' });
}); });
}); });

View File

@ -9,49 +9,49 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Raw pixel data', function () { describe('Raw pixel data', () => {
describe('Raw pixel input', function () { describe('Raw pixel input', () => {
it('Empty data', function () { it('Empty data', () => {
assert.throws(function () { assert.throws(() => {
sharp(Buffer.from('')); sharp(Buffer.from(''));
}, /empty/); }, /empty/);
assert.throws(function () { assert.throws(() => {
sharp(new ArrayBuffer(0)); sharp(new ArrayBuffer(0));
}, /empty/); }, /empty/);
assert.throws(function () { assert.throws(() => {
sharp(new Uint8Array(0)); sharp(new Uint8Array(0));
}, /empty/); }, /empty/);
assert.throws(function () { assert.throws(() => {
sharp(new Uint8ClampedArray(0)); sharp(new Uint8ClampedArray(0));
}, /empty/); }, /empty/);
}); });
it('Missing options', function () { it('Missing options', () => {
assert.throws(function () { assert.throws(() => {
sharp({ raw: {} }); sharp({ raw: {} });
}); });
}); });
it('Incomplete options', function () { it('Incomplete options', () => {
assert.throws(function () { assert.throws(() => {
sharp({ raw: { width: 1, height: 1 } }); sharp({ raw: { width: 1, height: 1 } });
}); });
}); });
it('Invalid channels', function () { it('Invalid channels', () => {
assert.throws(function () { assert.throws(() => {
sharp({ raw: { width: 1, height: 1, channels: 5 } }); sharp({ raw: { width: 1, height: 1, channels: 5 } });
}); });
}); });
it('Invalid height', function () { it('Invalid height', () => {
assert.throws(function () { assert.throws(() => {
sharp({ raw: { width: 1, height: 0, channels: 4 } }); sharp({ raw: { width: 1, height: 0, channels: 4 } });
}); });
}); });
it('Invalid width', function () { it('Invalid width', () => {
assert.throws(function () { assert.throws(() => {
sharp({ raw: { width: 'zoinks', height: 1, channels: 4 } }); sharp({ raw: { width: 'zoinks', height: 1, channels: 4 } });
}); });
}); });
@ -85,12 +85,12 @@ describe('Raw pixel data', function () {
); );
}); });
it('RGB', function (_t, done) { it('RGB', (_t, done) => {
// Convert to raw pixel data // Convert to raw pixel data
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(256) .resize(256)
.raw() .raw()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(256, info.width); assert.strictEqual(256, info.width);
assert.strictEqual(209, info.height); assert.strictEqual(209, info.height);
@ -104,7 +104,7 @@ describe('Raw pixel data', function () {
} }
}) })
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(256, info.width); assert.strictEqual(256, info.width);
assert.strictEqual(209, info.height); assert.strictEqual(209, info.height);
@ -114,12 +114,12 @@ describe('Raw pixel data', function () {
}); });
}); });
it('RGBA', function (_t, done) { it('RGBA', (_t, done) => {
// Convert to raw pixel data // Convert to raw pixel data
sharp(fixtures.inputPngOverlayLayer1) sharp(fixtures.inputPngOverlayLayer1)
.resize(256) .resize(256)
.raw() .raw()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(256, info.width); assert.strictEqual(256, info.width);
assert.strictEqual(192, info.height); assert.strictEqual(192, info.height);
@ -133,7 +133,7 @@ describe('Raw pixel data', function () {
} }
}) })
.png() .png()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(256, info.width); assert.strictEqual(256, info.width);
assert.strictEqual(192, info.height); assert.strictEqual(192, info.height);
@ -143,12 +143,12 @@ describe('Raw pixel data', function () {
}); });
}); });
it('RGBA premultiplied', function (_t, done) { it('RGBA premultiplied', (_t, done) => {
// Convert to raw pixel data // Convert to raw pixel data
sharp(fixtures.inputPngSolidAlpha) sharp(fixtures.inputPngSolidAlpha)
.resize(256) .resize(256)
.raw() .raw()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(256, info.width); assert.strictEqual(256, info.width);
assert.strictEqual(192, info.height); assert.strictEqual(192, info.height);
@ -178,7 +178,7 @@ describe('Raw pixel data', function () {
} }
}) })
.raw() .raw()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(256, info.width); assert.strictEqual(256, info.width);
assert.strictEqual(192, info.height); assert.strictEqual(192, info.height);
@ -189,7 +189,7 @@ describe('Raw pixel data', function () {
}); });
}); });
it('JPEG to raw Stream and back again', function (_t, done) { it('JPEG to raw Stream and back again', (_t, done) => {
const width = 32; const width = 32;
const height = 24; const height = 24;
const writable = sharp({ const writable = sharp({
@ -201,7 +201,7 @@ describe('Raw pixel data', function () {
}); });
writable writable
.jpeg() .jpeg()
.toBuffer(function (err, _data, info) { .toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(32, info.width); assert.strictEqual(32, info.width);
@ -215,13 +215,13 @@ describe('Raw pixel data', function () {
}); });
}); });
describe('Output raw, uncompressed image data', function () { describe('Output raw, uncompressed image data', () => {
it('1 channel greyscale image', function (_t, done) { it('1 channel greyscale image', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.greyscale() .greyscale()
.resize(32, 24) .resize(32, 24)
.raw() .raw()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(32 * 24 * 1, info.size); assert.strictEqual(32 * 24 * 1, info.size);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -233,11 +233,11 @@ describe('Raw pixel data', function () {
}); });
}); });
it('3 channel colour image without transparency', function (_t, done) { it('3 channel colour image without transparency', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(32, 24) .resize(32, 24)
.toFormat('raw') .toFormat('raw')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(32 * 24 * 3, info.size); assert.strictEqual(32 * 24 * 3, info.size);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -248,11 +248,11 @@ describe('Raw pixel data', function () {
}); });
}); });
it('4 channel colour image with transparency', function (_t, done) { it('4 channel colour image with transparency', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(32, 24) .resize(32, 24)
.toFormat(sharp.format.raw) .toFormat(sharp.format.raw)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(32 * 24 * 4, info.size); assert.strictEqual(32 * 24 * 4, info.size);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -278,9 +278,9 @@ describe('Raw pixel data', function () {
); );
}); });
describe('Raw pixel depths', function () { describe('Raw pixel depths', () => {
it('Invalid depth', function () { it('Invalid depth', () => {
assert.throws(function () { assert.throws(() => {
sharp(Buffer.alloc(3), { raw: { width: 1, height: 1, channels: 3 } }) sharp(Buffer.alloc(3), { raw: { width: 1, height: 1, channels: 3 } })
.raw({ depth: 'zoinks' }); .raw({ depth: 'zoinks' });
}); });

View File

@ -15,12 +15,12 @@ const sepia = [
[0.2392, 0.4696, 0.0912] [0.2392, 0.4696, 0.0912]
]; ];
describe('Recomb', function () { describe('Recomb', () => {
it('applies a sepia filter using recomb', function (_t, done) { it('applies a sepia filter using recomb', (_t, done) => {
const output = fixtures.path('output.recomb-sepia.jpg'); const output = fixtures.path('output.recomb-sepia.jpg');
sharp(fixtures.inputJpgWithLandscapeExif1) sharp(fixtures.inputJpgWithLandscapeExif1)
.recomb(sepia) .recomb(sepia)
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(600, info.width); assert.strictEqual(600, info.width);
@ -34,11 +34,11 @@ describe('Recomb', function () {
}); });
}); });
it('applies a sepia filter using recomb to an PNG with Alpha', function (_t, done) { it('applies a sepia filter using recomb to an PNG with Alpha', (_t, done) => {
const output = fixtures.path('output.recomb-sepia.png'); const output = fixtures.path('output.recomb-sepia.png');
sharp(fixtures.inputPngAlphaPremultiplicationSmall) sharp(fixtures.inputPngAlphaPremultiplicationSmall)
.recomb(sepia) .recomb(sepia)
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(1024, info.width); assert.strictEqual(1024, info.width);
@ -66,7 +66,7 @@ describe('Recomb', function () {
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
}); });
it('applies a different sepia filter using recomb', function (_t, done) { it('applies a different sepia filter using recomb', (_t, done) => {
const output = fixtures.path('output.recomb-sepia2.jpg'); const output = fixtures.path('output.recomb-sepia2.jpg');
sharp(fixtures.inputJpgWithLandscapeExif1) sharp(fixtures.inputJpgWithLandscapeExif1)
.recomb([ .recomb([
@ -74,7 +74,7 @@ describe('Recomb', function () {
[0.349, 0.686, 0.168], [0.349, 0.686, 0.168],
[0.272, 0.534, 0.131] [0.272, 0.534, 0.131]
]) ])
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(600, info.width); assert.strictEqual(600, info.width);
@ -87,7 +87,7 @@ describe('Recomb', function () {
done(); done();
}); });
}); });
it('increases the saturation of the image', function (_t, done) { it('increases the saturation of the image', (_t, done) => {
const saturationLevel = 1; const saturationLevel = 1;
const output = fixtures.path('output.recomb-saturation.jpg'); const output = fixtures.path('output.recomb-saturation.jpg');
sharp(fixtures.inputJpgWithLandscapeExif1) sharp(fixtures.inputJpgWithLandscapeExif1)
@ -108,7 +108,7 @@ describe('Recomb', function () {
saturationLevel + 1 - 0.114 saturationLevel + 1 - 0.114
] ]
]) ])
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(600, info.width); assert.strictEqual(600, info.width);
@ -122,7 +122,7 @@ describe('Recomb', function () {
}); });
}); });
it('applies opacity 30% to the image', function (_t, done) { it('applies opacity 30% to the image', (_t, done) => {
const output = fixtures.path('output.recomb-opacity.png'); const output = fixtures.path('output.recomb-opacity.png');
sharp(fixtures.inputPngWithTransparent) sharp(fixtures.inputPngWithTransparent)
.recomb([ .recomb([
@ -131,7 +131,7 @@ describe('Recomb', function () {
[0, 0, 1, 0], [0, 0, 1, 0],
[0, 0, 0, 0.3] [0, 0, 0, 0.3]
]) ])
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(48, info.width); assert.strictEqual(48, info.width);
@ -145,19 +145,19 @@ describe('Recomb', function () {
}); });
}); });
describe('invalid matrix specification', function () { describe('invalid matrix specification', () => {
it('missing', function () { it('missing', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).recomb(); sharp(fixtures.inputJpg).recomb();
}); });
}); });
it('incorrect flat data', function () { it('incorrect flat data', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).recomb([1, 2, 3, 4, 5, 6, 7, 8, 9]); sharp(fixtures.inputJpg).recomb([1, 2, 3, 4, 5, 6, 7, 8, 9]);
}); });
}); });
it('incorrect sub size', function () { it('incorrect sub size', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).recomb([ sharp(fixtures.inputJpg).recomb([
[1, 2, 3, 4], [1, 2, 3, 4],
[5, 6, 7, 8], [5, 6, 7, 8],
@ -165,8 +165,8 @@ describe('Recomb', function () {
]); ]);
}); });
}); });
it('incorrect top size', function () { it('incorrect top size', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).recomb([[1, 2, 3, 4], [5, 6, 7, 8]]); sharp(fixtures.inputJpg).recomb([[1, 2, 3, 4], [5, 6, 7, 8]]);
}); });
}); });

View File

@ -9,15 +9,15 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Resize fit=contain', function () { describe('Resize fit=contain', () => {
it('Allows specifying the position as a string', function (_t, done) { it('Allows specifying the position as a string', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240, { .resize(320, 240, {
fit: 'contain', fit: 'contain',
position: 'center' position: 'center'
}) })
.png() .png()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -25,11 +25,11 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('JPEG within PNG, no alpha channel', function (_t, done) { it('JPEG within PNG, no alpha channel', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240, { fit: 'contain' }) .resize(320, 240, { fit: 'contain' })
.png() .png()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -40,14 +40,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('JPEG within WebP, to include alpha channel', function (_t, done) { it('JPEG within WebP, to include alpha channel', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240, { .resize(320, 240, {
fit: 'contain', fit: 'contain',
background: { r: 0, g: 0, b: 0, alpha: 0 } background: { r: 0, g: 0, b: 0, alpha: 0 }
}) })
.webp() .webp()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
@ -58,10 +58,10 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('PNG with alpha channel', function (_t, done) { it('PNG with alpha channel', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(50, 50, { fit: 'contain' }) .resize(50, 50, { fit: 'contain' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -72,10 +72,10 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('16-bit PNG with alpha channel', function (_t, done) { it('16-bit PNG with alpha channel', (_t, done) => {
sharp(fixtures.inputPngWithTransparency16bit) sharp(fixtures.inputPngWithTransparency16bit)
.resize(32, 16, { fit: 'contain' }) .resize(32, 16, { fit: 'contain' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -86,13 +86,13 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('16-bit PNG with alpha channel onto RGBA', function (_t, done) { it('16-bit PNG with alpha channel onto RGBA', (_t, done) => {
sharp(fixtures.inputPngWithTransparency16bit) sharp(fixtures.inputPngWithTransparency16bit)
.resize(32, 16, { .resize(32, 16, {
fit: 'contain', fit: 'contain',
background: { r: 0, g: 0, b: 0, alpha: 0 } background: { r: 0, g: 0, b: 0, alpha: 0 }
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -103,13 +103,13 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('PNG with 2 channels', function (_t, done) { it('PNG with 2 channels', (_t, done) => {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.resize(32, 16, { .resize(32, 16, {
fit: 'contain', fit: 'contain',
background: { r: 0, g: 0, b: 0, alpha: 0 } background: { r: 0, g: 0, b: 0, alpha: 0 }
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -120,14 +120,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('TIFF in LAB colourspace onto RGBA background', function (_t, done) { it('TIFF in LAB colourspace onto RGBA background', (_t, done) => {
sharp(fixtures.inputTiffCielab) sharp(fixtures.inputTiffCielab)
.resize(64, 128, { .resize(64, 128, {
fit: 'contain', fit: 'contain',
background: { r: 255, g: 102, b: 0, alpha: 0.5 } background: { r: 255, g: 102, b: 0, alpha: 0.5 }
}) })
.png() .png()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -138,10 +138,10 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Enlarge', function (_t, done) { it('Enlarge', (_t, done) => {
sharp(fixtures.inputPngWithOneColor) sharp(fixtures.inputPngWithOneColor)
.resize(320, 240, { fit: 'contain' }) .resize(320, 240, { fit: 'contain' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -152,14 +152,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
describe('Animated WebP', function () { describe('Animated WebP', () => {
it('Width only', function (_t, done) { it('Width only', (_t, done) => {
sharp(fixtures.inputWebPAnimated, { pages: -1 }) sharp(fixtures.inputWebPAnimated, { pages: -1 })
.resize(320, 240, { .resize(320, 240, {
fit: 'contain', fit: 'contain',
background: { r: 255, g: 0, b: 0 } background: { r: 255, g: 0, b: 0 }
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
@ -170,13 +170,13 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Height only', function (_t, done) { it('Height only', (_t, done) => {
sharp(fixtures.inputWebPAnimated, { pages: -1 }) sharp(fixtures.inputWebPAnimated, { pages: -1 })
.resize(240, 320, { .resize(240, 320, {
fit: 'contain', fit: 'contain',
background: { r: 255, g: 0, b: 0 } background: { r: 255, g: 0, b: 0 }
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
@ -188,22 +188,22 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Invalid position values should fail', function () { it('Invalid position values should fail', () => {
[-1, 8.1, 9, 1000000, false, 'vallejo'].forEach(function (position) { [-1, 8.1, 9, 1000000, false, 'vallejo'].forEach((position) => {
assert.throws(function () { assert.throws(() => {
sharp().resize(null, null, { fit: 'contain', position }); sharp().resize(null, null, { fit: 'contain', position });
}); });
}); });
}); });
it('Position horizontal top', function (_t, done) { it('Position horizontal top', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'top' position: 'top'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -214,14 +214,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal right top', function (_t, done) { it('Position horizontal right top', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'right top' position: 'right top'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -232,14 +232,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal right', function (_t, done) { it('Position horizontal right', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'right' position: 'right'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -250,14 +250,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal right bottom', function (_t, done) { it('Position horizontal right bottom', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'right bottom' position: 'right bottom'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -268,14 +268,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal bottom', function (_t, done) { it('Position horizontal bottom', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'bottom' position: 'bottom'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -286,14 +286,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal left bottom', function (_t, done) { it('Position horizontal left bottom', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'left bottom' position: 'left bottom'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -304,14 +304,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal left', function (_t, done) { it('Position horizontal left', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'left' position: 'left'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -322,14 +322,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal left top', function (_t, done) { it('Position horizontal left top', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'left top' position: 'left top'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -340,14 +340,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal north', function (_t, done) { it('Position horizontal north', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.north position: sharp.gravity.north
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -358,14 +358,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal northeast', function (_t, done) { it('Position horizontal northeast', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.northeast position: sharp.gravity.northeast
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -376,14 +376,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal east', function (_t, done) { it('Position horizontal east', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.east position: sharp.gravity.east
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -394,14 +394,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal southeast', function (_t, done) { it('Position horizontal southeast', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.southeast position: sharp.gravity.southeast
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -412,14 +412,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal south', function (_t, done) { it('Position horizontal south', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.south position: sharp.gravity.south
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -430,14 +430,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal southwest', function (_t, done) { it('Position horizontal southwest', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.southwest position: sharp.gravity.southwest
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -448,14 +448,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal west', function (_t, done) { it('Position horizontal west', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.west position: sharp.gravity.west
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -466,14 +466,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal northwest', function (_t, done) { it('Position horizontal northwest', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.northwest position: sharp.gravity.northwest
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -484,14 +484,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position horizontal center', function (_t, done) { it('Position horizontal center', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 100, { .resize(200, 100, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.center position: sharp.gravity.center
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -502,14 +502,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical top', function (_t, done) { it('Position vertical top', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'top' position: 'top'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -520,14 +520,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical right top', function (_t, done) { it('Position vertical right top', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'right top' position: 'right top'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -538,14 +538,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical right', function (_t, done) { it('Position vertical right', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'right' position: 'right'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -556,14 +556,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical right bottom', function (_t, done) { it('Position vertical right bottom', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'right bottom' position: 'right bottom'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -574,14 +574,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical bottom', function (_t, done) { it('Position vertical bottom', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'bottom' position: 'bottom'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -592,14 +592,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical left bottom', function (_t, done) { it('Position vertical left bottom', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'left bottom' position: 'left bottom'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -610,14 +610,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical left', function (_t, done) { it('Position vertical left', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'left' position: 'left'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -628,14 +628,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical left top', function (_t, done) { it('Position vertical left top', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: 'left top' position: 'left top'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -646,14 +646,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical north', function (_t, done) { it('Position vertical north', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.north position: sharp.gravity.north
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -664,14 +664,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical northeast', function (_t, done) { it('Position vertical northeast', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.northeast position: sharp.gravity.northeast
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -682,14 +682,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical east', function (_t, done) { it('Position vertical east', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.east position: sharp.gravity.east
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -700,14 +700,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical southeast', function (_t, done) { it('Position vertical southeast', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.southeast position: sharp.gravity.southeast
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -718,14 +718,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical south', function (_t, done) { it('Position vertical south', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.south position: sharp.gravity.south
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -736,14 +736,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical southwest', function (_t, done) { it('Position vertical southwest', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.southwest position: sharp.gravity.southwest
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -754,14 +754,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical west', function (_t, done) { it('Position vertical west', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.west position: sharp.gravity.west
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -772,14 +772,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical northwest', function (_t, done) { it('Position vertical northwest', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.northwest position: sharp.gravity.northwest
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -790,14 +790,14 @@ describe('Resize fit=contain', function () {
}); });
}); });
it('Position vertical center', function (_t, done) { it('Position vertical center', (_t, done) => {
sharp(fixtures.inputPngEmbed) sharp(fixtures.inputPngEmbed)
.resize(200, 200, { .resize(200, 200, {
fit: sharp.fit.contain, fit: sharp.fit.contain,
background: { r: 0, g: 0, b: 0, alpha: 0 }, background: { r: 0, g: 0, b: 0, alpha: 0 },
position: sharp.gravity.center position: sharp.gravity.center
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);

View File

@ -9,7 +9,7 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Resize fit=cover', function () { describe('Resize fit=cover', () => {
[ [
// Position // Position
{ {
@ -202,14 +202,14 @@ describe('Resize fit=cover', function () {
gravity: sharp.gravity.northwest, gravity: sharp.gravity.northwest,
fixture: 'gravity-west.jpg' fixture: 'gravity-west.jpg'
} }
].forEach(function (settings) { ].forEach((settings) => {
it(settings.name, function (_t, done) { it(settings.name, (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(settings.width, settings.height, { .resize(settings.width, settings.height, {
fit: sharp.fit.cover, fit: sharp.fit.cover,
position: settings.gravity position: settings.gravity
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(settings.width, info.width); assert.strictEqual(settings.width, info.width);
assert.strictEqual(settings.height, info.height); assert.strictEqual(settings.height, info.height);
@ -218,13 +218,13 @@ describe('Resize fit=cover', function () {
}); });
}); });
it('Allows specifying the gravity as a string', function (_t, done) { it('Allows specifying the gravity as a string', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(80, 320, { .resize(80, 320, {
fit: sharp.fit.cover, fit: sharp.fit.cover,
position: 'east' position: 'east'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(80, info.width); assert.strictEqual(80, info.width);
assert.strictEqual(320, info.height); assert.strictEqual(320, info.height);
@ -232,52 +232,48 @@ describe('Resize fit=cover', function () {
}); });
}); });
it('Invalid position values fail', function () { it('Invalid position values fail', () => {
assert.throws(function () { assert.throws(() => {
sharp().resize(null, null, { fit: 'cover', position: 9 }); sharp().resize(null, null, { fit: 'cover', position: 9 });
}, /Expected valid position\/gravity\/strategy for position but received 9 of type number/); }, /Expected valid position\/gravity\/strategy for position but received 9 of type number/);
assert.throws(function () { assert.throws(() => {
sharp().resize(null, null, { fit: 'cover', position: 1.1 }); sharp().resize(null, null, { fit: 'cover', position: 1.1 });
}, /Expected valid position\/gravity\/strategy for position but received 1.1 of type number/); }, /Expected valid position\/gravity\/strategy for position but received 1.1 of type number/);
assert.throws(function () { assert.throws(() => {
sharp().resize(null, null, { fit: 'cover', position: -1 }); sharp().resize(null, null, { fit: 'cover', position: -1 });
}, /Expected valid position\/gravity\/strategy for position but received -1 of type number/); }, /Expected valid position\/gravity\/strategy for position but received -1 of type number/);
assert.throws(function () { assert.throws(() => {
sharp().resize(null, null, { fit: 'cover', position: 'zoinks' }).crop(); sharp().resize(null, null, { fit: 'cover', position: 'zoinks' }).crop();
}, /Expected valid position\/gravity\/strategy for position but received zoinks of type string/); }, /Expected valid position\/gravity\/strategy for position but received zoinks of type string/);
}); });
it('Uses default value when none specified', function () { it('Uses default value when none specified', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().resize(null, null, { fit: 'cover' }); sharp().resize(null, null, { fit: 'cover' });
}); });
}); });
it('Skip crop when post-resize dimensions are at target', function () { it('Skip crop when post-resize dimensions are at target', () => sharp(fixtures.inputJpg)
return sharp(fixtures.inputJpg)
.resize(1600, 1200) .resize(1600, 1200)
.toBuffer() .toBuffer()
.then(function (input) { .then((input) => sharp(input)
return sharp(input)
.resize(1110, null, { .resize(1110, null, {
fit: sharp.fit.cover, fit: sharp.fit.cover,
position: sharp.strategy.attention position: sharp.strategy.attention
}) })
.toBuffer({ resolveWithObject: true }) .toBuffer({ resolveWithObject: true })
.then(function (result) { .then((result) => {
assert.strictEqual(1110, result.info.width); assert.strictEqual(1110, result.info.width);
assert.strictEqual(832, result.info.height); assert.strictEqual(832, result.info.height);
assert.strictEqual(undefined, result.info.cropOffsetLeft); assert.strictEqual(undefined, result.info.cropOffsetLeft);
assert.strictEqual(undefined, result.info.cropOffsetTop); assert.strictEqual(undefined, result.info.cropOffsetTop);
}); })));
});
});
describe('Animated WebP', function () { describe('Animated WebP', () => {
it('Width only', function (_t, done) { it('Width only', (_t, done) => {
sharp(fixtures.inputWebPAnimated, { pages: -1 }) sharp(fixtures.inputWebPAnimated, { pages: -1 })
.resize(80, 320, { fit: sharp.fit.cover }) .resize(80, 320, { fit: sharp.fit.cover })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(80, info.width); assert.strictEqual(80, info.width);
assert.strictEqual(320 * 9, info.height); assert.strictEqual(320 * 9, info.height);
@ -285,10 +281,10 @@ describe('Resize fit=cover', function () {
}); });
}); });
it('Height only', function (_t, done) { it('Height only', (_t, done) => {
sharp(fixtures.inputWebPAnimated, { pages: -1 }) sharp(fixtures.inputWebPAnimated, { pages: -1 })
.resize(320, 80, { fit: sharp.fit.cover }) .resize(320, 80, { fit: sharp.fit.cover })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(80 * 9, info.height); assert.strictEqual(80 * 9, info.height);
@ -297,14 +293,14 @@ describe('Resize fit=cover', function () {
}); });
}); });
describe('Entropy-based strategy', function () { describe('Entropy-based strategy', () => {
it('JPEG', function (_t, done) { it('JPEG', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(80, 320, { .resize(80, 320, {
fit: 'cover', fit: 'cover',
position: sharp.strategy.entropy position: sharp.strategy.entropy
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
@ -316,13 +312,13 @@ describe('Resize fit=cover', function () {
}); });
}); });
it('PNG', function (_t, done) { it('PNG', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(320, 80, { .resize(320, 80, {
fit: 'cover', fit: 'cover',
position: sharp.strategy.entropy position: sharp.strategy.entropy
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(4, info.channels); assert.strictEqual(4, info.channels);
@ -334,13 +330,13 @@ describe('Resize fit=cover', function () {
}); });
}); });
it('supports the strategy passed as a string', function (_t, done) { it('supports the strategy passed as a string', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(320, 80, { .resize(320, 80, {
fit: 'cover', fit: 'cover',
position: 'entropy' position: 'entropy'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(4, info.channels); assert.strictEqual(4, info.channels);
@ -365,14 +361,14 @@ describe('Resize fit=cover', function () {
); );
}); });
describe('Attention strategy', function () { describe('Attention strategy', () => {
it('JPEG', function (_t, done) { it('JPEG', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(80, 320, { .resize(80, 320, {
fit: 'cover', fit: 'cover',
position: sharp.strategy.attention position: sharp.strategy.attention
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
@ -386,13 +382,13 @@ describe('Resize fit=cover', function () {
}); });
}); });
it('PNG', function (_t, done) { it('PNG', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(320, 80, { .resize(320, 80, {
fit: 'cover', fit: 'cover',
position: sharp.strategy.attention position: sharp.strategy.attention
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(4, info.channels); assert.strictEqual(4, info.channels);
@ -406,13 +402,13 @@ describe('Resize fit=cover', function () {
}); });
}); });
it('WebP', function (_t, done) { it('WebP', (_t, done) => {
sharp(fixtures.inputWebP) sharp(fixtures.inputWebP)
.resize(320, 80, { .resize(320, 80, {
fit: 'cover', fit: 'cover',
position: sharp.strategy.attention position: sharp.strategy.attention
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
@ -426,13 +422,13 @@ describe('Resize fit=cover', function () {
}); });
}); });
it('supports the strategy passed as a string', function (_t, done) { it('supports the strategy passed as a string', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(320, 80, { .resize(320, 80, {
fit: 'cover', fit: 'cover',
position: 'attention' position: 'attention'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(4, info.channels); assert.strictEqual(4, info.channels);

View File

@ -9,9 +9,9 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Resize dimensions', function () { describe('Resize dimensions', () => {
it('Exact crop', function (_t, done) { it('Exact crop', (_t, done) => {
sharp(fixtures.inputJpg).resize(320, 240).toBuffer(function (err, data, info) { sharp(fixtures.inputJpg).resize(320, 240).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -21,8 +21,8 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Fixed width', function (_t, done) { it('Fixed width', (_t, done) => {
sharp(fixtures.inputJpg).resize(320).toBuffer(function (err, data, info) { sharp(fixtures.inputJpg).resize(320).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -32,8 +32,8 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Fixed height', function (_t, done) { it('Fixed height', (_t, done) => {
sharp(fixtures.inputJpg).resize(null, 320).toBuffer(function (err, data, info) { sharp(fixtures.inputJpg).resize(null, 320).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -43,8 +43,8 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Identity transform', function (_t, done) { it('Identity transform', (_t, done) => {
sharp(fixtures.inputJpg).toBuffer(function (err, data, info) { sharp(fixtures.inputJpg).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -54,10 +54,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Upscale', function (_t, done) { it('Upscale', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(3000) .resize(3000)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -67,26 +67,26 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Invalid width - NaN', function () { it('Invalid width - NaN', () => {
assert.throws(function () { assert.throws(() => {
sharp().resize('spoons', 240); sharp().resize('spoons', 240);
}, /Expected positive integer for width but received spoons of type string/); }, /Expected positive integer for width but received spoons of type string/);
}); });
it('Invalid height - NaN', function () { it('Invalid height - NaN', () => {
assert.throws(function () { assert.throws(() => {
sharp().resize(320, 'spoons'); sharp().resize(320, 'spoons');
}, /Expected positive integer for height but received spoons of type string/); }, /Expected positive integer for height but received spoons of type string/);
}); });
it('Invalid width - float', function () { it('Invalid width - float', () => {
assert.throws(function () { assert.throws(() => {
sharp().resize(1.5, 240); sharp().resize(1.5, 240);
}, /Expected positive integer for width but received 1.5 of type number/); }, /Expected positive integer for width but received 1.5 of type number/);
}); });
it('Invalid height - float', function () { it('Invalid height - float', () => {
assert.throws(function () { assert.throws(() => {
sharp().resize(320, 1.5); sharp().resize(320, 1.5);
}, /Expected positive integer for height but received 1.5 of type number/); }, /Expected positive integer for height but received 1.5 of type number/);
}); });
@ -103,34 +103,34 @@ describe('Resize dimensions', function () {
}, /Expected positive integer for height but received 1.5 of type number/); }, /Expected positive integer for height but received 1.5 of type number/);
}); });
it('Invalid width - too large', function (_t, done) { it('Invalid width - too large', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(0x4000, 1) .resize(0x4000, 1)
.webp() .webp()
.toBuffer(function (err) { .toBuffer((err) => {
assert.strictEqual(true, err instanceof Error); assert.strictEqual(true, err instanceof Error);
assert.strictEqual('Processed image is too large for the WebP format', err.message); assert.strictEqual('Processed image is too large for the WebP format', err.message);
done(); done();
}); });
}); });
it('Invalid height - too large', function (_t, done) { it('Invalid height - too large', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(1, 0x4000) .resize(1, 0x4000)
.webp() .webp()
.toBuffer(function (err) { .toBuffer((err) => {
assert.strictEqual(true, err instanceof Error); assert.strictEqual(true, err instanceof Error);
assert.strictEqual('Processed image is too large for the WebP format', err.message); assert.strictEqual('Processed image is too large for the WebP format', err.message);
done(); done();
}); });
}); });
it('Webp resize then extract large image', function (_t, done) { it('Webp resize then extract large image', (_t, done) => {
sharp(fixtures.inputWebP) sharp(fixtures.inputWebP)
.resize(0x4000, 0x4000) .resize(0x4000, 0x4000)
.extract({ top: 0x2000, left: 0x2000, width: 256, height: 256 }) .extract({ top: 0x2000, left: 0x2000, width: 256, height: 256 })
.webp() .webp()
.toBuffer(function (err, _data, info) { .toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
assert.strictEqual(256, info.width); assert.strictEqual(256, info.width);
@ -139,18 +139,18 @@ describe('Resize dimensions', function () {
}); });
}); });
it('WebP shrink-on-load rounds to zero, ensure recalculation is correct', function (_t, done) { it('WebP shrink-on-load rounds to zero, ensure recalculation is correct', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(1080, 607) .resize(1080, 607)
.webp() .webp()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
assert.strictEqual(1080, info.width); assert.strictEqual(1080, info.width);
assert.strictEqual(607, info.height); assert.strictEqual(607, info.height);
sharp(data) sharp(data)
.resize(233, 131) .resize(233, 131)
.toBuffer(function (err, _data, info) { .toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
assert.strictEqual(233, info.width); assert.strictEqual(233, info.width);
@ -160,17 +160,17 @@ describe('Resize dimensions', function () {
}); });
}); });
it('JPEG shrink-on-load with 90 degree rotation, ensure recalculation is correct', function (_t, done) { it('JPEG shrink-on-load with 90 degree rotation, ensure recalculation is correct', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(1920, 1280) .resize(1920, 1280)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(1920, info.width); assert.strictEqual(1920, info.width);
assert.strictEqual(1280, info.height); assert.strictEqual(1280, info.height);
sharp(data) sharp(data)
.rotate(90) .rotate(90)
.resize(533, 800) .resize(533, 800)
.toBuffer(function (err, _data, info) { .toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(533, info.width); assert.strictEqual(533, info.width);
assert.strictEqual(800, info.height); assert.strictEqual(800, info.height);
@ -179,11 +179,11 @@ describe('Resize dimensions', function () {
}); });
}); });
it('TIFF embed known to cause rounding errors', function (_t, done) { it('TIFF embed known to cause rounding errors', (_t, done) => {
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)
.resize(240, 320, { fit: sharp.fit.contain }) .resize(240, 320, { fit: sharp.fit.contain })
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -193,11 +193,11 @@ describe('Resize dimensions', function () {
}); });
}); });
it('TIFF known to cause rounding errors', function (_t, done) { it('TIFF known to cause rounding errors', (_t, done) => {
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)
.resize(240, 320) .resize(240, 320)
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -207,11 +207,11 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=inside, portrait', function (_t, done) { it('fit=inside, portrait', (_t, done) => {
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)
.resize(320, 320, { fit: sharp.fit.inside }) .resize(320, 320, { fit: sharp.fit.inside })
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -221,11 +221,11 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=outside, portrait', function (_t, done) { it('fit=outside, portrait', (_t, done) => {
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)
.resize(320, 320, { fit: sharp.fit.outside }) .resize(320, 320, { fit: sharp.fit.outside })
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -235,10 +235,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=inside, landscape', function (_t, done) { it('fit=inside, landscape', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 320, { fit: sharp.fit.inside }) .resize(320, 320, { fit: sharp.fit.inside })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -248,10 +248,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=outside, landscape', function (_t, done) { it('fit=outside, landscape', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 320, { fit: sharp.fit.outside }) .resize(320, 320, { fit: sharp.fit.outside })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -261,13 +261,13 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=inside, provide only one dimension', function (_t, done) { it('fit=inside, provide only one dimension', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize({ .resize({
width: 320, width: 320,
fit: sharp.fit.inside fit: sharp.fit.inside
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -277,13 +277,13 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=outside, provide only one dimension', function (_t, done) { it('fit=outside, provide only one dimension', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize({ .resize({
width: 320, width: 320,
fit: sharp.fit.outside fit: sharp.fit.outside
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -293,13 +293,13 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Do not enlarge when input width is already less than output width', function (_t, done) { it('Do not enlarge when input width is already less than output width', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize({ .resize({
width: 2800, width: 2800,
withoutEnlargement: true withoutEnlargement: true
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -309,13 +309,13 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Do not enlarge when input height is already less than output height', function (_t, done) { it('Do not enlarge when input height is already less than output height', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize({ .resize({
height: 2300, height: 2300,
withoutEnlargement: true withoutEnlargement: true
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -325,14 +325,14 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Do crop when fit = cover and withoutEnlargement = true and width >= outputWidth, and height < outputHeight', function (_t, done) { it('Do crop when fit = cover and withoutEnlargement = true and width >= outputWidth, and height < outputHeight', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize({ .resize({
width: 3000, width: 3000,
height: 1000, height: 1000,
withoutEnlargement: true withoutEnlargement: true
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -342,14 +342,14 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Do crop when fit = cover and withoutEnlargement = true and width < outputWidth, and height >= outputHeight', function (_t, done) { it('Do crop when fit = cover and withoutEnlargement = true and width < outputWidth, and height >= outputHeight', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize({ .resize({
width: 1500, width: 1500,
height: 2226, height: 2226,
withoutEnlargement: true withoutEnlargement: true
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -359,13 +359,13 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Do enlarge when input width is less than output width', function (_t, done) { it('Do enlarge when input width is less than output width', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize({ .resize({
width: 2800, width: 2800,
withoutEnlargement: false withoutEnlargement: false
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -375,13 +375,13 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Do enlarge when input width is less than output width', function (_t, done) { it('Do enlarge when input width is less than output width', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize({ .resize({
width: 2800, width: 2800,
withoutReduction: true withoutReduction: true
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -391,13 +391,13 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Do enlarge when input height is less than output height', function (_t, done) { it('Do enlarge when input height is less than output height', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize({ .resize({
height: 2300, height: 2300,
withoutReduction: true withoutReduction: true
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -407,13 +407,13 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Do enlarge when input width is less than output width', function (_t, done) { it('Do enlarge when input width is less than output width', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize({ .resize({
width: 2800, width: 2800,
withoutReduction: false withoutReduction: false
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -423,10 +423,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Do not resize when both withoutEnlargement and withoutReduction are true', function (_t, done) { it('Do not resize when both withoutEnlargement and withoutReduction are true', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 320, { fit: 'fill', withoutEnlargement: true, withoutReduction: true }) .resize(320, 320, { fit: 'fill', withoutEnlargement: true, withoutReduction: true })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -436,10 +436,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Do not reduce size when fit = outside and withoutReduction are true and height > outputHeight and width > outputWidth', function (_t, done) { it('Do not reduce size when fit = outside and withoutReduction are true and height > outputHeight and width > outputWidth', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 320, { fit: 'outside', withoutReduction: true }) .resize(320, 320, { fit: 'outside', withoutReduction: true })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -449,10 +449,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Do resize when fit = outside and withoutReduction are true and input height > height and input width > width ', function (_t, done) { it('Do resize when fit = outside and withoutReduction are true and input height > height and input width > width ', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(3000, 3000, { fit: 'outside', withoutReduction: true }) .resize(3000, 3000, { fit: 'outside', withoutReduction: true })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -462,10 +462,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=fill, downscale width and height', function (_t, done) { it('fit=fill, downscale width and height', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 320, { fit: 'fill' }) .resize(320, 320, { fit: 'fill' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -475,13 +475,13 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=fill, downscale width', function (_t, done) { it('fit=fill, downscale width', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize({ .resize({
width: 320, width: 320,
fit: 'fill' fit: 'fill'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -491,13 +491,13 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=fill, downscale height', function (_t, done) { it('fit=fill, downscale height', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize({ .resize({
height: 320, height: 320,
fit: 'fill' fit: 'fill'
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -507,10 +507,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=fill, upscale width and height', function (_t, done) { it('fit=fill, upscale width and height', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(3000, 3000, { fit: 'fill' }) .resize(3000, 3000, { fit: 'fill' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -520,10 +520,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=fill, upscale width', function (_t, done) { it('fit=fill, upscale width', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(3000, null, { fit: 'fill' }) .resize(3000, null, { fit: 'fill' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -533,10 +533,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=fill, upscale height', function (_t, done) { it('fit=fill, upscale height', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(null, 3000, { fit: 'fill' }) .resize(null, 3000, { fit: 'fill' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -546,10 +546,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=fill, downscale width, upscale height', function (_t, done) { it('fit=fill, downscale width, upscale height', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 3000, { fit: 'fill' }) .resize(320, 3000, { fit: 'fill' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -559,10 +559,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=fill, upscale width, downscale height', function (_t, done) { it('fit=fill, upscale width, downscale height', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(3000, 320, { fit: 'fill' }) .resize(3000, 320, { fit: 'fill' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -572,10 +572,10 @@ describe('Resize dimensions', function () {
}); });
}); });
it('fit=fill, identity transform', function (_t, done) { it('fit=fill, identity transform', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(null, null, { fit: 'fill' }) .resize(null, null, { fit: 'fill' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -585,16 +585,16 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Dimensions that result in differing even shrinks on each axis', function (_t, done) { it('Dimensions that result in differing even shrinks on each axis', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(645, 399) .resize(645, 399)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(645, info.width); assert.strictEqual(645, info.width);
assert.strictEqual(399, info.height); assert.strictEqual(399, info.height);
sharp(data) sharp(data)
.resize(150, 100) .resize(150, 100)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(150, info.width); assert.strictEqual(150, info.width);
assert.strictEqual(100, info.height); assert.strictEqual(100, info.height);
@ -603,33 +603,31 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Dimensions that result in differing odd shrinks on each axis', function (_t, done) { it('Dimensions that result in differing odd shrinks on each axis', (_t, done) => sharp(fixtures.inputJpg)
return sharp(fixtures.inputJpg)
.resize(600, 399) .resize(600, 399)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(600, info.width); assert.strictEqual(600, info.width);
assert.strictEqual(399, info.height); assert.strictEqual(399, info.height);
sharp(data) sharp(data)
.resize(200) .resize(200)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(200, info.width); assert.strictEqual(200, info.width);
assert.strictEqual(133, info.height); assert.strictEqual(133, info.height);
fixtures.assertSimilar(fixtures.expected('resize-diff-shrink-odd.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('resize-diff-shrink-odd.jpg'), data, done);
}); });
}); }));
});
[ [
true, true,
false false
].forEach(function (value) { ].forEach((value) => {
it(`fastShrinkOnLoad: ${value} does not causes image shifts`, function (_t, done) { it(`fastShrinkOnLoad: ${value} does not causes image shifts`, (_t, done) => {
sharp(fixtures.inputJpgCenteredImage) sharp(fixtures.inputJpgCenteredImage)
.resize(9, 8, { fastShrinkOnLoad: value }) .resize(9, 8, { fastShrinkOnLoad: value })
.png() .png()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(9, info.width); assert.strictEqual(9, info.width);
assert.strictEqual(8, info.height); assert.strictEqual(8, info.height);
@ -644,11 +642,11 @@ describe('Resize dimensions', function () {
sharp.kernel.mitchell, sharp.kernel.mitchell,
sharp.kernel.lanczos2, sharp.kernel.lanczos2,
sharp.kernel.lanczos3 sharp.kernel.lanczos3
].forEach(function (kernel) { ].forEach((kernel) => {
it(`kernel ${kernel}`, function (_t, done) { it(`kernel ${kernel}`, (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, null, { kernel }) .resize(320, null, { kernel })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -657,11 +655,11 @@ describe('Resize dimensions', function () {
}); });
}); });
it('nearest upsampling with integral factor', function (_t, done) { it('nearest upsampling with integral factor', (_t, done) => {
sharp(fixtures.inputTiff8BitDepth) sharp(fixtures.inputTiff8BitDepth)
.resize(210, 210, { kernel: 'nearest' }) .resize(210, 210, { kernel: 'nearest' })
.png() .png()
.toBuffer(function (err, _data, info) { .toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(210, info.width); assert.strictEqual(210, info.width);
assert.strictEqual(210, info.height); assert.strictEqual(210, info.height);
@ -669,8 +667,7 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Ensure shortest edge (height) is at least 1 pixel', function () { it('Ensure shortest edge (height) is at least 1 pixel', () => sharp({
return sharp({
create: { create: {
width: 10, width: 10,
height: 2, height: 2,
@ -680,14 +677,12 @@ describe('Resize dimensions', function () {
}) })
.resize(2) .resize(2)
.toBuffer({ resolveWithObject: true }) .toBuffer({ resolveWithObject: true })
.then(function (output) { .then((output) => {
assert.strictEqual(2, output.info.width); assert.strictEqual(2, output.info.width);
assert.strictEqual(1, output.info.height); assert.strictEqual(1, output.info.height);
}); }));
});
it('Ensure shortest edge (width) is at least 1 pixel', function () { it('Ensure shortest edge (width) is at least 1 pixel', () => sharp({
return sharp({
create: { create: {
width: 2, width: 2,
height: 10, height: 10,
@ -697,14 +692,12 @@ describe('Resize dimensions', function () {
}) })
.resize(null, 2) .resize(null, 2)
.toBuffer({ resolveWithObject: true }) .toBuffer({ resolveWithObject: true })
.then(function (output) { .then((output) => {
assert.strictEqual(1, output.info.width); assert.strictEqual(1, output.info.width);
assert.strictEqual(2, output.info.height); assert.strictEqual(2, output.info.height);
}); }));
});
it('Ensure embedded shortest edge (height) is at least 1 pixel', function () { it('Ensure embedded shortest edge (height) is at least 1 pixel', () => sharp({
return sharp({
create: { create: {
width: 200, width: 200,
height: 1, height: 1,
@ -714,14 +707,12 @@ describe('Resize dimensions', function () {
}) })
.resize({ width: 50, height: 50, fit: sharp.fit.contain }) .resize({ width: 50, height: 50, fit: sharp.fit.contain })
.toBuffer({ resolveWithObject: true }) .toBuffer({ resolveWithObject: true })
.then(function (output) { .then((output) => {
assert.strictEqual(50, output.info.width); assert.strictEqual(50, output.info.width);
assert.strictEqual(50, output.info.height); assert.strictEqual(50, output.info.height);
}); }));
});
it('Ensure embedded shortest edge (width) is at least 1 pixel', function () { it('Ensure embedded shortest edge (width) is at least 1 pixel', () => sharp({
return sharp({
create: { create: {
width: 1, width: 1,
height: 200, height: 200,
@ -731,11 +722,10 @@ describe('Resize dimensions', function () {
}) })
.resize({ width: 50, height: 50, fit: sharp.fit.contain }) .resize({ width: 50, height: 50, fit: sharp.fit.contain })
.toBuffer({ resolveWithObject: true }) .toBuffer({ resolveWithObject: true })
.then(function (output) { .then((output) => {
assert.strictEqual(50, output.info.width); assert.strictEqual(50, output.info.width);
assert.strictEqual(50, output.info.height); assert.strictEqual(50, output.info.height);
}); }));
});
it('Skip shrink-on-load where one dimension <4px', async () => { it('Skip shrink-on-load where one dimension <4px', async () => {
const jpeg = await sharp({ const jpeg = await sharp({
@ -778,20 +768,20 @@ describe('Resize dimensions', function () {
assert.strictEqual(height, 334); assert.strictEqual(height, 334);
}); });
it('unknown kernel throws', function () { it('unknown kernel throws', () => {
assert.throws(function () { assert.throws(() => {
sharp().resize(null, null, { kernel: 'unknown' }); sharp().resize(null, null, { kernel: 'unknown' });
}); });
}); });
it('unknown fit throws', function () { it('unknown fit throws', () => {
assert.throws(function () { assert.throws(() => {
sharp().resize(null, null, { fit: 'unknown' }); sharp().resize(null, null, { fit: 'unknown' });
}); });
}); });
it('unknown position throws', function () { it('unknown position throws', () => {
assert.throws(function () { assert.throws(() => {
sharp().resize(null, null, { position: 'unknown' }); sharp().resize(null, null, { position: 'unknown' });
}); });
}); });
@ -799,7 +789,7 @@ describe('Resize dimensions', function () {
it('Multiple resize emits warning', () => { it('Multiple resize emits warning', () => {
let warningMessage = ''; let warningMessage = '';
const s = sharp(); const s = sharp();
s.on('warning', function (msg) { warningMessage = msg; }); s.on('warning', (msg) => { warningMessage = msg; });
s.resize(1); s.resize(1);
assert.strictEqual(warningMessage, ''); assert.strictEqual(warningMessage, '');
s.resize(2); s.resize(2);

View File

@ -9,22 +9,22 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Rotation', function () { describe('Rotation', () => {
['autoOrient', 'constructor'].forEach(function (rotateMethod) { ['autoOrient', 'constructor'].forEach((rotateMethod) => {
describe(`Auto orientation via ${rotateMethod}:`, () => { describe(`Auto orientation via ${rotateMethod}:`, () => {
const options = rotateMethod === 'constructor' ? { autoOrient: true } : {}; const options = rotateMethod === 'constructor' ? { autoOrient: true } : {};
['Landscape', 'Portrait'].forEach(function (orientation) { ['Landscape', 'Portrait'].forEach((orientation) => {
[1, 2, 3, 4, 5, 6, 7, 8].forEach(function (exifTag) { [1, 2, 3, 4, 5, 6, 7, 8].forEach((exifTag) => {
const input = fixtures[`inputJpgWith${orientation}Exif${exifTag}`]; const input = fixtures[`inputJpgWith${orientation}Exif${exifTag}`];
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}-out.jpg`); const expectedOutput = fixtures.expected(`${orientation}_${exifTag}-out.jpg`);
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate`, function (_t, done) { it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate`, (_t, done) => {
const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
const img = sharp(input, options); const img = sharp(input, options);
rotateMethod === 'autoOrient' && img.autoOrient(); rotateMethod === 'autoOrient' && img.autoOrient();
img.toBuffer(function (err, data, info) { img.toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(info.width, expectedWidth); assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight); assert.strictEqual(info.height, expectedHeight);
@ -32,14 +32,14 @@ describe('Rotation', function () {
}); });
}); });
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then resize`, function (_t, done) { it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then resize`, (_t, done) => {
const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [320, 240] : [320, 427]; const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [320, 240] : [320, 427];
const img = sharp(input, options); const img = sharp(input, options);
rotateMethod === 'autoOrient' && img.autoOrient(); rotateMethod === 'autoOrient' && img.autoOrient();
img.resize({ width: 320 }) img.resize({ width: 320 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(info.width, expectedWidth); assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight); assert.strictEqual(info.height, expectedHeight);
@ -48,7 +48,7 @@ describe('Rotation', function () {
}); });
if (rotateMethod !== 'constructor') { if (rotateMethod !== 'constructor') {
it(`${orientation} image with EXIF Orientation ${exifTag}: Resize then auto-rotate`, function (_t, done) { it(`${orientation} image with EXIF Orientation ${exifTag}: Resize then auto-rotate`, (_t, done) => {
const [expectedWidth, expectedHeight] = orientation === 'Landscape' const [expectedWidth, expectedHeight] = orientation === 'Landscape'
? (exifTag < 5) ? [320, 240] : [320, 240] ? (exifTag < 5) ? [320, 240] : [320, 240]
: [320, 427]; : [320, 427];
@ -57,7 +57,7 @@ describe('Rotation', function () {
.resize({ width: 320 }); .resize({ width: 320 });
rotateMethod === 'autoOrient' && img.autoOrient(); rotateMethod === 'autoOrient' && img.autoOrient();
img.toBuffer(function (err, data, info) { img.toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(info.width, expectedWidth); assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight); assert.strictEqual(info.height, expectedHeight);
@ -67,10 +67,10 @@ describe('Rotation', function () {
} }
[true, false].forEach((doResize) => { [true, false].forEach((doResize) => {
[90, 180, 270, 45].forEach(function (angle) { [90, 180, 270, 45].forEach((angle) => {
const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_rotate${angle}-out.jpg`); const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_rotate${angle}-out.jpg`);
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then rotate ${angle} ${doResize ? 'and resize' : ''}`, function (_t, done) { it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then rotate ${angle} ${doResize ? 'and resize' : ''}`, (_t, done) => {
const [width, height] = (angle === 45 ? [742, 742] : [inputWidth, inputHeight]).map((x) => doResize ? Math.floor(x / 1.875) : x); const [width, height] = (angle === 45 ? [742, 742] : [inputWidth, inputHeight]).map((x) => doResize ? Math.floor(x / 1.875) : x);
const [expectedWidth, expectedHeight] = angle % 180 === 0 ? [width, height] : [height, width]; const [expectedWidth, expectedHeight] = angle % 180 === 0 ? [width, height] : [height, width];
@ -80,7 +80,7 @@ describe('Rotation', function () {
img.rotate(angle); img.rotate(angle);
doResize && img.resize(expectedWidth); doResize && img.resize(expectedWidth);
img.toBuffer(function (err, data, info) { img.toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(info.width, expectedWidth); assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight); assert.strictEqual(info.height, expectedHeight);
@ -89,11 +89,11 @@ describe('Rotation', function () {
}); });
}); });
[[true, true], [true, false], [false, true]].forEach(function ([flip, flop]) { [[true, true], [true, false], [false, true]].forEach(([flip, flop]) => {
const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
const flipFlopFileName = [flip && 'flip', flop && 'flop'].filter(Boolean).join('_'); const flipFlopFileName = [flip && 'flip', flop && 'flop'].filter(Boolean).join('_');
const flipFlopTestName = [flip && 'flip', flop && 'flop'].filter(Boolean).join(' & '); const flipFlopTestName = [flip && 'flip', flop && 'flop'].filter(Boolean).join(' & ');
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then ${flipFlopTestName} ${doResize ? 'and resize' : ''}`, function (_t, done) { it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then ${flipFlopTestName} ${doResize ? 'and resize' : ''}`, (_t, done) => {
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_${flipFlopFileName}-out.jpg`); const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_${flipFlopFileName}-out.jpg`);
const img = sharp(input, options); const img = sharp(input, options);
@ -104,7 +104,7 @@ describe('Rotation', function () {
flop && img.flop(); flop && img.flop();
doResize && img.resize(orientation === 'Landscape' ? 320 : 240); doResize && img.resize(orientation === 'Landscape' ? 320 : 240);
img.toBuffer(function (err, data, info) { img.toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(info.width, inputWidth / (doResize ? 1.875 : 1)); assert.strictEqual(info.width, inputWidth / (doResize ? 1.875 : 1));
assert.strictEqual(info.height, inputHeight / (doResize ? 1.875 : 1)); assert.strictEqual(info.height, inputHeight / (doResize ? 1.875 : 1));
@ -118,12 +118,12 @@ describe('Rotation', function () {
}); });
}); });
it('Rotate by 30 degrees with semi-transparent background', function (_t, done) { it('Rotate by 30 degrees with semi-transparent background', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320) .resize(320)
.rotate(30, { background: { r: 255, g: 0, b: 0, alpha: 0.5 } }) .rotate(30, { background: { r: 255, g: 0, b: 0, alpha: 0.5 } })
.png() .png()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(408, info.width); assert.strictEqual(408, info.width);
@ -132,11 +132,11 @@ describe('Rotation', function () {
}); });
}); });
it('Rotate by 30 degrees with solid background', function (_t, done) { it('Rotate by 30 degrees with solid background', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320) .resize(320)
.rotate(30, { background: { r: 255, g: 0, b: 0 } }) .rotate(30, { background: { r: 255, g: 0, b: 0 } })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(408, info.width); assert.strictEqual(408, info.width);
@ -145,11 +145,11 @@ describe('Rotation', function () {
}); });
}); });
it('Rotate by 90 degrees, respecting output input size', function (_t, done) { it('Rotate by 90 degrees, respecting output input size', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.rotate(90) .rotate(90)
.resize(320, 240) .resize(320, 240)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -159,11 +159,11 @@ describe('Rotation', function () {
}); });
}); });
it('Resize then rotate by 30 degrees, respecting output input size', function (_t, done) { it('Resize then rotate by 30 degrees, respecting output input size', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.rotate(30) .rotate(30)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -173,9 +173,9 @@ describe('Rotation', function () {
}); });
}); });
[-3690, -450, -90, 90, 450, 3690].forEach(function (angle) { [-3690, -450, -90, 90, 450, 3690].forEach((angle) => {
it(`Rotate by any 90-multiple angle (${angle}deg)`, function (_t, done) { it(`Rotate by any 90-multiple angle (${angle}deg)`, (_t, done) => {
sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer(function (err, _data, info) { sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(240, info.width); assert.strictEqual(240, info.width);
assert.strictEqual(320, info.height); assert.strictEqual(320, info.height);
@ -184,9 +184,9 @@ describe('Rotation', function () {
}); });
}); });
[-3750, -510, -150, 30, 390, 3630].forEach(function (angle) { [-3750, -510, -150, 30, 390, 3630].forEach((angle) => {
it(`Rotate by any 30-multiple angle (${angle}deg)`, function (_t, done) { it(`Rotate by any 30-multiple angle (${angle}deg)`, (_t, done) => {
sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer(function (err, _data, info) { sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(397, info.width); assert.strictEqual(397, info.width);
assert.strictEqual(368, info.height); assert.strictEqual(368, info.height);
@ -195,9 +195,9 @@ describe('Rotation', function () {
}); });
}); });
[-3780, -540, 0, 180, 540, 3780].forEach(function (angle) { [-3780, -540, 0, 180, 540, 3780].forEach((angle) => {
it(`Rotate by any 180-multiple angle (${angle}deg)`, function (_t, done) { it(`Rotate by any 180-multiple angle (${angle}deg)`, (_t, done) => {
sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer(function (err, _data, info) { sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -206,15 +206,15 @@ describe('Rotation', function () {
}); });
}); });
it('Rotate by 270 degrees, square output ignoring aspect ratio', function (_t, done) { it('Rotate by 270 degrees, square output ignoring aspect ratio', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(240, 240, { fit: sharp.fit.fill }) .resize(240, 240, { fit: sharp.fit.fill })
.rotate(270) .rotate(270)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(240, info.width); assert.strictEqual(240, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
sharp(data).metadata(function (err, metadata) { sharp(data).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(240, metadata.width); assert.strictEqual(240, metadata.width);
assert.strictEqual(240, metadata.height); assert.strictEqual(240, metadata.height);
@ -223,15 +223,15 @@ describe('Rotation', function () {
}); });
}); });
it('Rotate by 315 degrees, square output ignoring aspect ratio', function (_t, done) { it('Rotate by 315 degrees, square output ignoring aspect ratio', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(240, 240, { fit: sharp.fit.fill }) .resize(240, 240, { fit: sharp.fit.fill })
.rotate(315) .rotate(315)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(339, info.width); assert.strictEqual(339, info.width);
assert.strictEqual(339, info.height); assert.strictEqual(339, info.height);
sharp(data).metadata(function (err, metadata) { sharp(data).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(339, metadata.width); assert.strictEqual(339, metadata.width);
assert.strictEqual(339, metadata.height); assert.strictEqual(339, metadata.height);
@ -240,15 +240,15 @@ describe('Rotation', function () {
}); });
}); });
it('Rotate by 270 degrees, rectangular output ignoring aspect ratio', function (_t, done) { it('Rotate by 270 degrees, rectangular output ignoring aspect ratio', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.rotate(270) .rotate(270)
.resize(320, 240, { fit: sharp.fit.fill }) .resize(320, 240, { fit: sharp.fit.fill })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
sharp(data).metadata(function (err, metadata) { sharp(data).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, metadata.width); assert.strictEqual(320, metadata.width);
assert.strictEqual(240, metadata.height); assert.strictEqual(240, metadata.height);
@ -257,15 +257,15 @@ describe('Rotation', function () {
}); });
}); });
it('Auto-rotate by 270 degrees, rectangular output ignoring aspect ratio', function (_t, done) { it('Auto-rotate by 270 degrees, rectangular output ignoring aspect ratio', (_t, done) => {
sharp(fixtures.inputJpgWithLandscapeExif8) sharp(fixtures.inputJpgWithLandscapeExif8)
.resize(320, 240, { fit: sharp.fit.fill }) .resize(320, 240, { fit: sharp.fit.fill })
.rotate() .rotate()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
sharp(data).metadata(function (err, metadata) { sharp(data).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, metadata.width); assert.strictEqual(320, metadata.width);
assert.strictEqual(240, metadata.height); assert.strictEqual(240, metadata.height);
@ -274,15 +274,15 @@ describe('Rotation', function () {
}); });
}); });
it('Rotate by 30 degrees, rectangular output ignoring aspect ratio', function (_t, done) { it('Rotate by 30 degrees, rectangular output ignoring aspect ratio', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240, { fit: sharp.fit.fill }) .resize(320, 240, { fit: sharp.fit.fill })
.rotate(30) .rotate(30)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(397, info.width); assert.strictEqual(397, info.width);
assert.strictEqual(368, info.height); assert.strictEqual(368, info.height);
sharp(data).metadata(function (err, metadata) { sharp(data).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(397, metadata.width); assert.strictEqual(397, metadata.width);
assert.strictEqual(368, metadata.height); assert.strictEqual(368, metadata.height);
@ -291,17 +291,17 @@ describe('Rotation', function () {
}); });
}); });
it('Input image has Orientation EXIF tag but do not rotate output', function (_t, done) { it('Input image has Orientation EXIF tag but do not rotate output', (_t, done) => {
sharp(fixtures.inputJpgWithExif) sharp(fixtures.inputJpgWithExif)
.resize(320) .resize(320)
.withMetadata() .withMetadata()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(427, info.height); assert.strictEqual(427, info.height);
sharp(data).metadata(function (err, metadata) { sharp(data).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(8, metadata.orientation); assert.strictEqual(8, metadata.orientation);
done(); done();
@ -309,11 +309,11 @@ describe('Rotation', function () {
}); });
}); });
it('Input image has Orientation EXIF tag value of 8 (270 degrees), auto-rotate', function (_t, done) { it('Input image has Orientation EXIF tag value of 8 (270 degrees), auto-rotate', (_t, done) => {
sharp(fixtures.inputJpgWithExif) sharp(fixtures.inputJpgWithExif)
.rotate() .rotate()
.resize(320) .resize(320)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -322,17 +322,17 @@ describe('Rotation', function () {
}); });
}); });
it('Override EXIF Orientation tag metadata after auto-rotate', function (_t, done) { it('Override EXIF Orientation tag metadata after auto-rotate', (_t, done) => {
sharp(fixtures.inputJpgWithExif) sharp(fixtures.inputJpgWithExif)
.rotate() .rotate()
.resize(320) .resize(320)
.withMetadata({ orientation: 3 }) .withMetadata({ orientation: 3 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
sharp(data).metadata(function (err, metadata) { sharp(data).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(3, metadata.orientation); assert.strictEqual(3, metadata.orientation);
fixtures.assertSimilar(fixtures.expected('exif-8.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('exif-8.jpg'), data, done);
@ -340,17 +340,17 @@ describe('Rotation', function () {
}); });
}); });
it('Input image has Orientation EXIF tag value of 5 (270 degrees + flip), auto-rotate', function (_t, done) { it('Input image has Orientation EXIF tag value of 5 (270 degrees + flip), auto-rotate', (_t, done) => {
sharp(fixtures.inputJpgWithExifMirroring) sharp(fixtures.inputJpgWithExifMirroring)
.rotate() .rotate()
.resize(320) .resize(320)
.withMetadata() .withMetadata()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
sharp(data).metadata(function (err, metadata) { sharp(data).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(1, metadata.orientation); assert.strictEqual(1, metadata.orientation);
fixtures.assertSimilar(fixtures.expected('exif-5.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('exif-5.jpg'), data, done);
@ -358,8 +358,8 @@ describe('Rotation', function () {
}); });
}); });
it('Attempt to auto-rotate using image that has no EXIF', function (_t, done) { it('Attempt to auto-rotate using image that has no EXIF', (_t, done) => {
sharp(fixtures.inputJpg).rotate().resize(320).toBuffer(function (err, data, info) { sharp(fixtures.inputJpg).rotate().resize(320).toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -369,12 +369,12 @@ describe('Rotation', function () {
}); });
}); });
it('Attempt to auto-rotate image format without EXIF support', function (_t, done) { it('Attempt to auto-rotate image format without EXIF support', (_t, done) => {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.rotate() .rotate()
.resize(320) .resize(320)
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -384,8 +384,8 @@ describe('Rotation', function () {
}); });
}); });
it('Rotate with a string argument, should fail', function () { it('Rotate with a string argument, should fail', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).rotate('not-a-number'); sharp(fixtures.inputJpg).rotate('not-a-number');
}); });
}); });
@ -436,18 +436,18 @@ describe('Rotation', function () {
it('Multiple rotate emits warning', () => { it('Multiple rotate emits warning', () => {
let warningMessage = ''; let warningMessage = '';
const s = sharp(); const s = sharp();
s.on('warning', function (msg) { warningMessage = msg; }); s.on('warning', (msg) => { warningMessage = msg; });
s.rotate(90); s.rotate(90);
assert.strictEqual(warningMessage, ''); assert.strictEqual(warningMessage, '');
s.rotate(180); s.rotate(180);
assert.strictEqual(warningMessage, 'ignoring previous rotate options'); assert.strictEqual(warningMessage, 'ignoring previous rotate options');
}); });
it('Multiple rotate: last one wins (cardinal)', function (_t, done) { it('Multiple rotate: last one wins (cardinal)', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.rotate(45) .rotate(45)
.rotate(90) .rotate(90)
.toBuffer(function (err, _data, info) { .toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(2225, info.width); assert.strictEqual(2225, info.width);
assert.strictEqual(2725, info.height); assert.strictEqual(2725, info.height);
@ -455,11 +455,11 @@ describe('Rotation', function () {
}); });
}); });
it('Multiple rotate: last one wins (non cardinal)', function (_t, done) { it('Multiple rotate: last one wins (non cardinal)', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.rotate(90) .rotate(90)
.rotate(45) .rotate(45)
.toBuffer(function (err, _data, info) { .toBuffer((err, _data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(3500, info.width); assert.strictEqual(3500, info.width);
assert.strictEqual(3500, info.height); assert.strictEqual(3500, info.height);
@ -467,17 +467,17 @@ describe('Rotation', function () {
}); });
}); });
it('Flip - vertical', function (_t, done) { it('Flip - vertical', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320) .resize(320)
.flip() .flip()
.withMetadata() .withMetadata()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(261, info.height); assert.strictEqual(261, info.height);
sharp(data).metadata(function (err, metadata) { sharp(data).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(1, metadata.orientation); assert.strictEqual(1, metadata.orientation);
fixtures.assertSimilar(fixtures.expected('flip.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('flip.jpg'), data, done);
@ -485,17 +485,17 @@ describe('Rotation', function () {
}); });
}); });
it('Flop - horizontal', function (_t, done) { it('Flop - horizontal', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320) .resize(320)
.flop() .flop()
.withMetadata() .withMetadata()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(261, info.height); assert.strictEqual(261, info.height);
sharp(data).metadata(function (err, metadata) { sharp(data).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(1, metadata.orientation); assert.strictEqual(1, metadata.orientation);
fixtures.assertSimilar(fixtures.expected('flop.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('flop.jpg'), data, done);
@ -503,12 +503,12 @@ describe('Rotation', function () {
}); });
}); });
it('Flip and flop', function (_t, done) { it('Flip and flop', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320) .resize(320)
.flip() .flip()
.flop() .flop()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -517,12 +517,12 @@ describe('Rotation', function () {
}); });
}); });
it('Neither flip nor flop', function (_t, done) { it('Neither flip nor flop', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320) .resize(320)
.flip(false) .flip(false)
.flop(false) .flop(false)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -531,12 +531,12 @@ describe('Rotation', function () {
}); });
}); });
it('Auto-rotate and flip', function (_t, done) { it('Auto-rotate and flip', (_t, done) => {
sharp(fixtures.inputJpgWithExif) sharp(fixtures.inputJpgWithExif)
.rotate() .rotate()
.flip() .flip()
.resize(320) .resize(320)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -545,12 +545,12 @@ describe('Rotation', function () {
}); });
}); });
it('Auto-rotate and flop', function (_t, done) { it('Auto-rotate and flop', (_t, done) => {
sharp(fixtures.inputJpgWithExif) sharp(fixtures.inputJpgWithExif)
.rotate() .rotate()
.flop() .flop()
.resize(320) .resize(320)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);

View File

@ -9,12 +9,12 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Sharpen', function () { describe('Sharpen', () => {
it('specific radius 10 (sigma 6)', function (_t, done) { it('specific radius 10 (sigma 6)', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.sharpen(6) .sharpen(6)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -23,11 +23,11 @@ describe('Sharpen', function () {
}); });
}); });
it('specific radius 3 (sigma 1.5) and levels 0.5, 2.5', function (_t, done) { it('specific radius 3 (sigma 1.5) and levels 0.5, 2.5', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.sharpen(1.5, 0.5, 2.5) .sharpen(1.5, 0.5, 2.5)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -36,11 +36,11 @@ describe('Sharpen', function () {
}); });
}); });
it('specific radius 5 (sigma 3.5) and levels 2, 4', function (_t, done) { it('specific radius 5 (sigma 3.5) and levels 2, 4', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.sharpen(3.5, 2, 4) .sharpen(3.5, 2, 4)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -66,11 +66,11 @@ describe('Sharpen', function () {
}); });
if (!process.env.SHARP_TEST_WITHOUT_CACHE) { if (!process.env.SHARP_TEST_WITHOUT_CACHE) {
it('specific radius/levels with alpha channel', function (_t, done) { it('specific radius/levels with alpha channel', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(320, 240) .resize(320, 240)
.sharpen(5, 4, 8) .sharpen(5, 4, 8)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(4, info.channels); assert.strictEqual(4, info.channels);
@ -81,11 +81,11 @@ describe('Sharpen', function () {
}); });
} }
it('mild sharpen', function (_t, done) { it('mild sharpen', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.sharpen() .sharpen()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -94,20 +94,20 @@ describe('Sharpen', function () {
}); });
}); });
it('invalid sigma', function () { it('invalid sigma', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).sharpen(-1.5); sharp(fixtures.inputJpg).sharpen(-1.5);
}); });
}); });
it('invalid flat', function () { it('invalid flat', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).sharpen(1, -1); sharp(fixtures.inputJpg).sharpen(1, -1);
}); });
}); });
it('invalid jagged', function () { it('invalid jagged', () => {
assert.throws(function () { assert.throws(() => {
sharp(fixtures.inputJpg).sharpen(1, 1, -1); sharp(fixtures.inputJpg).sharpen(1, 1, -1);
}); });
}); });
@ -142,11 +142,11 @@ describe('Sharpen', function () {
/Expected number between 0 and 1000000 for options\.y3 but received -1 of type number/ /Expected number between 0 and 1000000 for options\.y3 but received -1 of type number/
)); ));
it('sharpened image is larger than non-sharpened', function (_t, done) { it('sharpened image is larger than non-sharpened', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.sharpen(false) .sharpen(false)
.toBuffer(function (err, notSharpened, info) { .toBuffer((err, notSharpened, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, notSharpened.length > 0); assert.strictEqual(true, notSharpened.length > 0);
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
@ -155,7 +155,7 @@ describe('Sharpen', function () {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.sharpen(true) .sharpen(true)
.toBuffer(function (err, sharpened, info) { .toBuffer((err, sharpened, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, sharpened.length > 0); assert.strictEqual(true, sharpened.length > 0);
assert.strictEqual(true, sharpened.length > notSharpened.length); assert.strictEqual(true, sharpened.length > notSharpened.length);

View File

@ -22,9 +22,9 @@ function isInteger (val) {
return Number.isInteger(val); return Number.isInteger(val);
} }
describe('Image Stats', function () { describe('Image Stats', () => {
it('JPEG', function (_t, done) { it('JPEG', (_t, done) => {
sharp(fixtures.inputJpg).stats(function (err, stats) { sharp(fixtures.inputJpg).stats((err, stats) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stats.isOpaque); assert.strictEqual(true, stats.isOpaque);
@ -88,8 +88,8 @@ describe('Image Stats', function () {
}); });
}); });
it('PNG without transparency', function (_t, done) { it('PNG without transparency', (_t, done) => {
sharp(fixtures.inputPng).stats(function (err, stats) { sharp(fixtures.inputPng).stats((err, stats) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stats.isOpaque); assert.strictEqual(true, stats.isOpaque);
@ -120,8 +120,8 @@ describe('Image Stats', function () {
}); });
}); });
it('PNG with transparency', function (_t, done) { it('PNG with transparency', (_t, done) => {
sharp(fixtures.inputPngWithTransparency).stats(function (err, stats) { sharp(fixtures.inputPngWithTransparency).stats((err, stats) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(false, stats.isOpaque); assert.strictEqual(false, stats.isOpaque);
@ -201,8 +201,8 @@ describe('Image Stats', function () {
}); });
}); });
it('PNG fully transparent', function (_t, done) { it('PNG fully transparent', (_t, done) => {
sharp(fixtures.inputPngCompleteTransparency).stats(function (err, stats) { sharp(fixtures.inputPngCompleteTransparency).stats((err, stats) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(false, stats.isOpaque); assert.strictEqual(false, stats.isOpaque);
@ -234,8 +234,8 @@ describe('Image Stats', function () {
}); });
}); });
it('Tiff', function (_t, done) { it('Tiff', (_t, done) => {
sharp(fixtures.inputTiff).stats(function (err, stats) { sharp(fixtures.inputTiff).stats((err, stats) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stats.isOpaque); assert.strictEqual(true, stats.isOpaque);
@ -267,8 +267,8 @@ describe('Image Stats', function () {
}); });
}); });
it('WebP', function (_t, done) { it('WebP', (_t, done) => {
sharp(fixtures.inputWebP).stats(function (err, stats) { sharp(fixtures.inputWebP).stats((err, stats) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stats.isOpaque); assert.strictEqual(true, stats.isOpaque);
@ -332,8 +332,8 @@ describe('Image Stats', function () {
}); });
}); });
it('GIF', function (_t, done) { it('GIF', (_t, done) => {
sharp(fixtures.inputGif).stats(function (err, stats) { sharp(fixtures.inputGif).stats((err, stats) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stats.isOpaque); assert.strictEqual(true, stats.isOpaque);
@ -397,8 +397,8 @@ describe('Image Stats', function () {
}); });
}); });
it('Grayscale GIF with alpha', function (_t, done) { it('Grayscale GIF with alpha', (_t, done) => {
sharp(fixtures.inputGifGreyPlusAlpha).stats(function (err, stats) { sharp(fixtures.inputGifGreyPlusAlpha).stats((err, stats) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(false, stats.isOpaque); assert.strictEqual(false, stats.isOpaque);
@ -479,9 +479,9 @@ describe('Image Stats', function () {
assert.strictEqual(sharpness, 0); assert.strictEqual(sharpness, 0);
}); });
it('Stream in, Callback out', function (_t, done) { it('Stream in, Callback out', (_t, done) => {
const readable = fs.createReadStream(fixtures.inputJpg); const readable = fs.createReadStream(fixtures.inputJpg);
const pipeline = sharp().stats(function (err, stats) { const pipeline = sharp().stats((err, stats) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stats.isOpaque); assert.strictEqual(true, stats.isOpaque);
@ -546,12 +546,12 @@ describe('Image Stats', function () {
readable.pipe(pipeline); readable.pipe(pipeline);
}); });
it('Stream in, Promise out', function () { it('Stream in, Promise out', () => {
const pipeline = sharp(); const pipeline = sharp();
fs.createReadStream(fixtures.inputJpg).pipe(pipeline); fs.createReadStream(fixtures.inputJpg).pipe(pipeline);
return pipeline.stats().then(function (stats) { return pipeline.stats().then((stats) => {
assert.strictEqual(true, stats.isOpaque); assert.strictEqual(true, stats.isOpaque);
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.332915340666659)); assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.332915340666659));
assert.strictEqual(true, isInAcceptableRange(stats.sharpness, 0.788301114707569)); assert.strictEqual(true, isInAcceptableRange(stats.sharpness, 0.788301114707569));
@ -608,13 +608,12 @@ describe('Image Stats', function () {
assert.strictEqual(true, isInRange(stats.channels[0].maxX, 0, 2725)); assert.strictEqual(true, isInRange(stats.channels[0].maxX, 0, 2725));
assert.strictEqual(true, isInteger(stats.channels[0].maxY)); assert.strictEqual(true, isInteger(stats.channels[0].maxY));
assert.strictEqual(true, isInRange(stats.channels[0].maxY, 0, 2725)); assert.strictEqual(true, isInRange(stats.channels[0].maxY, 0, 2725));
}).catch(function (err) { }).catch((err) => {
throw err; throw err;
}); });
}); });
it('File in, Promise out', function () { it('File in, Promise out', () => sharp(fixtures.inputJpg).stats().then((stats) => {
return sharp(fixtures.inputJpg).stats().then(function (stats) {
assert.strictEqual(true, stats.isOpaque); assert.strictEqual(true, stats.isOpaque);
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.332915340666659)); assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.332915340666659));
assert.strictEqual(true, isInAcceptableRange(stats.sharpness, 0.788301114707569)); assert.strictEqual(true, isInAcceptableRange(stats.sharpness, 0.788301114707569));
@ -671,10 +670,9 @@ describe('Image Stats', function () {
assert.strictEqual(true, isInRange(stats.channels[0].maxX, 0, 2725)); assert.strictEqual(true, isInRange(stats.channels[0].maxX, 0, 2725));
assert.strictEqual(true, isInteger(stats.channels[0].maxY)); assert.strictEqual(true, isInteger(stats.channels[0].maxY));
assert.strictEqual(true, isInRange(stats.channels[0].maxY, 0, 2725)); assert.strictEqual(true, isInRange(stats.channels[0].maxY, 0, 2725));
}).catch(function (err) { }).catch((err) => {
throw err; throw err;
}); }));
});
it('Blurred image has lower sharpness than original', () => { it('Blurred image has lower sharpness than original', () => {
const original = sharp(fixtures.inputJpg).stats(); const original = sharp(fixtures.inputJpg).stats();
@ -688,9 +686,9 @@ describe('Image Stats', function () {
}); });
}); });
it('File input with corrupt header fails gracefully', function (_t, done) { it('File input with corrupt header fails gracefully', (_t, done) => {
sharp(fixtures.inputJpgWithCorruptHeader) sharp(fixtures.inputJpgWithCorruptHeader)
.stats(function (err) { .stats((err) => {
assert(err.message.includes('Input file has corrupt header')); assert(err.message.includes('Input file has corrupt header'));
assert(err.stack.includes('at Sharp.stats')); assert(err.stack.includes('at Sharp.stats'));
assert(err.stack.includes(__filename)); assert(err.stack.includes(__filename));
@ -698,10 +696,10 @@ describe('Image Stats', function () {
}); });
}); });
it('Stream input with corrupt header fails gracefully', function (_t, done) { it('Stream input with corrupt header fails gracefully', (_t, done) => {
fs.createReadStream(fixtures.inputJpgWithCorruptHeader).pipe( fs.createReadStream(fixtures.inputJpgWithCorruptHeader).pipe(
sharp() sharp()
.stats(function (err) { .stats((err) => {
assert(err.message.includes('Input buffer has corrupt header')); assert(err.message.includes('Input buffer has corrupt header'));
assert(err.stack.includes('at Sharp.stats')); assert(err.stack.includes('at Sharp.stats'));
assert(err.stack.includes(__filename)); assert(err.stack.includes(__filename));
@ -710,40 +708,38 @@ describe('Image Stats', function () {
); );
}); });
it('File input with corrupt header fails gracefully, Promise out', function () { it('File input with corrupt header fails gracefully, Promise out', () => sharp(fixtures.inputJpgWithCorruptHeader)
return sharp(fixtures.inputJpgWithCorruptHeader) .stats().then(() => {
.stats().then(function () {
throw new Error('Corrupt Header file'); throw new Error('Corrupt Header file');
}).catch(function (err) { }).catch((err) => {
assert.ok(!!err); assert.ok(!!err);
}); }));
});
it('File input with corrupt header fails gracefully, Stream In, Promise Out', function () { it('File input with corrupt header fails gracefully, Stream In, Promise Out', () => {
const pipeline = sharp(); const pipeline = sharp();
fs.createReadStream(fixtures.inputJpgWithCorruptHeader).pipe(pipeline); fs.createReadStream(fixtures.inputJpgWithCorruptHeader).pipe(pipeline);
return pipeline return pipeline
.stats().then(function () { .stats().then(() => {
throw new Error('Corrupt Header file'); throw new Error('Corrupt Header file');
}).catch(function (err) { }).catch((err) => {
assert.ok(!!err); assert.ok(!!err);
}); });
}); });
it('Buffer input with corrupt header fails gracefully', function (_t, done) { it('Buffer input with corrupt header fails gracefully', (_t, done) => {
sharp(fs.readFileSync(fixtures.inputJpgWithCorruptHeader)) sharp(fs.readFileSync(fixtures.inputJpgWithCorruptHeader))
.stats(function (err) { .stats((err) => {
assert.strictEqual(true, !!err); assert.strictEqual(true, !!err);
done(); done();
}); });
}); });
it('Non-existent file in, Promise out', function (_t, done) { it('Non-existent file in, Promise out', (_t, done) => {
sharp('fail').stats().then(function () { sharp('fail').stats().then(() => {
throw new Error('Non-existent file'); throw new Error('Non-existent file');
}, function (err) { }, (err) => {
assert.ok(!!err); assert.ok(!!err);
done(); done();
}); });

View File

@ -10,20 +10,20 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('SVG input', function () { describe('SVG input', () => {
it('Convert SVG to PNG at default 72DPI', function (_t, done) { it('Convert SVG to PNG at default 72DPI', (_t, done) => {
sharp(fixtures.inputSvg) sharp(fixtures.inputSvg)
.resize(1024) .resize(1024)
.extract({ left: 290, top: 760, width: 40, height: 40 }) .extract({ left: 290, top: 760, width: 40, height: 40 })
.toFormat('png') .toFormat('png')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(40, info.width); assert.strictEqual(40, info.width);
assert.strictEqual(40, info.height); assert.strictEqual(40, info.height);
fixtures.assertSimilar(fixtures.expected('svg72.png'), data, function (err) { fixtures.assertSimilar(fixtures.expected('svg72.png'), data, (err) => {
if (err) throw err; if (err) throw err;
sharp(data).metadata(function (err, info) { sharp(data).metadata((err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(72, info.density); assert.strictEqual(72, info.density);
done(); done();
@ -32,19 +32,19 @@ describe('SVG input', function () {
}); });
}); });
it('Convert SVG to PNG at 1200DPI', function (_t, done) { it('Convert SVG to PNG at 1200DPI', (_t, done) => {
sharp(fixtures.inputSvg, { density: 1200 }) sharp(fixtures.inputSvg, { density: 1200 })
.resize(1024) .resize(1024)
.extract({ left: 290, top: 760, width: 40, height: 40 }) .extract({ left: 290, top: 760, width: 40, height: 40 })
.toFormat('png') .toFormat('png')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(40, info.width); assert.strictEqual(40, info.width);
assert.strictEqual(40, info.height); assert.strictEqual(40, info.height);
fixtures.assertSimilar(fixtures.expected('svg1200.png'), data, function (err) { fixtures.assertSimilar(fixtures.expected('svg1200.png'), data, (err) => {
if (err) throw err; if (err) throw err;
sharp(data).metadata(function (err, info) { sharp(data).metadata((err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(1200, info.density); assert.strictEqual(1200, info.density);
done(); done();
@ -53,22 +53,22 @@ describe('SVG input', function () {
}); });
}); });
it('Convert SVG to PNG at DPI larger than 2400', function (_t, done) { it('Convert SVG to PNG at DPI larger than 2400', (_t, done) => {
const size = 1024; const size = 1024;
sharp(fixtures.inputSvgSmallViewBox).metadata(function (err, metadata) { sharp(fixtures.inputSvgSmallViewBox).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
const density = (size / Math.max(metadata.width, metadata.height)) * metadata.density; const density = (size / Math.max(metadata.width, metadata.height)) * metadata.density;
sharp(fixtures.inputSvgSmallViewBox, { density }) sharp(fixtures.inputSvgSmallViewBox, { density })
.resize(size) .resize(size)
.toFormat('png') .toFormat('png')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(size, info.width); assert.strictEqual(size, info.width);
assert.strictEqual(size, info.height); assert.strictEqual(size, info.height);
fixtures.assertSimilar(fixtures.expected('circle.png'), data, function (err) { fixtures.assertSimilar(fixtures.expected('circle.png'), data, (err) => {
if (err) throw err; if (err) throw err;
sharp(data).metadata(function (err, info) { sharp(data).metadata((err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(9216, info.density); assert.strictEqual(9216, info.density);
done(); done();
@ -78,19 +78,19 @@ describe('SVG input', function () {
}); });
}); });
it('Convert SVG to PNG utilizing scale-on-load', function (_t, done) { it('Convert SVG to PNG utilizing scale-on-load', (_t, done) => {
const size = 1024; const size = 1024;
sharp(fixtures.inputSvgSmallViewBox) sharp(fixtures.inputSvgSmallViewBox)
.resize(size) .resize(size)
.toFormat('png') .toFormat('png')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(size, info.width); assert.strictEqual(size, info.width);
assert.strictEqual(size, info.height); assert.strictEqual(size, info.height);
fixtures.assertSimilar(fixtures.expected('circle.png'), data, function (err) { fixtures.assertSimilar(fixtures.expected('circle.png'), data, (err) => {
if (err) throw err; if (err) throw err;
sharp(data).metadata(function (err, info) { sharp(data).metadata((err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(72, info.density); assert.strictEqual(72, info.density);
done(); done();
@ -99,24 +99,24 @@ describe('SVG input', function () {
}); });
}); });
it('Convert SVG to PNG at 14.4DPI', function (_t, done) { it('Convert SVG to PNG at 14.4DPI', (_t, done) => {
sharp(fixtures.inputSvg, { density: 14.4 }) sharp(fixtures.inputSvg, { density: 14.4 })
.toFormat('png') .toFormat('png')
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(20, info.width); assert.strictEqual(20, info.width);
assert.strictEqual(20, info.height); assert.strictEqual(20, info.height);
fixtures.assertSimilar(fixtures.expected('svg14.4.png'), data, function (err) { fixtures.assertSimilar(fixtures.expected('svg14.4.png'), data, (err) => {
if (err) throw err; if (err) throw err;
done(); done();
}); });
}); });
}); });
it('Convert SVG with embedded images to PNG, respecting dimensions, autoconvert to PNG', function (_t, done) { it('Convert SVG with embedded images to PNG, respecting dimensions, autoconvert to PNG', (_t, done) => {
sharp(fixtures.inputSvgWithEmbeddedImages) sharp(fixtures.inputSvgWithEmbeddedImages)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(480, info.width); assert.strictEqual(480, info.width);

View File

@ -10,8 +10,8 @@ const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
const { inRange } = require('../../lib/is'); const { inRange } = require('../../lib/is');
describe('Text to image', function () { describe('Text to image', () => {
it('text with default values', async function (t) { it('text with default values', async (t) => {
const output = fixtures.path('output.text-default.png'); const output = fixtures.path('output.text-default.png');
const text = sharp({ const text = sharp({
text: { text: {
@ -41,7 +41,7 @@ describe('Text to image', function () {
assert.ok(info.textAutofitDpi > 0); assert.ok(info.textAutofitDpi > 0);
}); });
it('text with width and height', async function (t) { it('text with width and height', async (t) => {
const output = fixtures.path('output.text-width-height.png'); const output = fixtures.path('output.text-width-height.png');
const text = sharp({ const text = sharp({
text: { text: {
@ -61,7 +61,7 @@ describe('Text to image', function () {
assert.ok(inRange(info.textAutofitDpi, 900, 1300), `Actual textAutofitDpi ${info.textAutofitDpi}`); assert.ok(inRange(info.textAutofitDpi, 900, 1300), `Actual textAutofitDpi ${info.textAutofitDpi}`);
}); });
it('text with dpi', async function (t) { it('text with dpi', async (t) => {
const output = fixtures.path('output.text-dpi.png'); const output = fixtures.path('output.text-dpi.png');
const dpi = 300; const dpi = 300;
const text = sharp({ const text = sharp({
@ -79,7 +79,7 @@ describe('Text to image', function () {
assert.strictEqual(dpi, metadata.density); assert.strictEqual(dpi, metadata.density);
}); });
it('text with color and pango markup', async function (t) { it('text with color and pango markup', async (t) => {
const output = fixtures.path('output.text-color-pango.png'); const output = fixtures.path('output.text-color-pango.png');
const dpi = 300; const dpi = 300;
const text = sharp({ const text = sharp({
@ -101,7 +101,7 @@ describe('Text to image', function () {
assert.strictEqual(true, metadata.hasAlpha); assert.strictEqual(true, metadata.hasAlpha);
}); });
it('text with font', async function (t) { it('text with font', async (t) => {
const output = fixtures.path('output.text-with-font.png'); const output = fixtures.path('output.text-with-font.png');
const text = sharp({ const text = sharp({
text: { text: {
@ -119,7 +119,7 @@ describe('Text to image', function () {
assert.ok(info.height > 10); assert.ok(info.height > 10);
}); });
it('text with justify and composite', async function (t) { it('text with justify and composite', async (t) => {
const output = fixtures.path('output.text-composite.png'); const output = fixtures.path('output.text-composite.png');
const width = 500; const width = 500;
const dpi = 300; const dpi = 300;
@ -164,8 +164,8 @@ describe('Text to image', function () {
assert.strictEqual(true, metadata.hasAlpha); assert.strictEqual(true, metadata.hasAlpha);
}); });
it('bad text input', function () { it('bad text input', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
text: { text: {
} }
@ -173,8 +173,8 @@ describe('Text to image', function () {
}); });
}); });
it('fontfile input', function () { it('fontfile input', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp({ sharp({
text: { text: {
text: 'text', text: 'text',
@ -184,8 +184,8 @@ describe('Text to image', function () {
}); });
}); });
it('bad font input', function () { it('bad font input', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
text: { text: {
text: 'text', text: 'text',
@ -195,8 +195,8 @@ describe('Text to image', function () {
}); });
}); });
it('bad fontfile input', function () { it('bad fontfile input', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
text: { text: {
text: 'text', text: 'text',
@ -236,8 +236,8 @@ describe('Text to image', function () {
); );
}); });
it('bad align input', function () { it('bad align input', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
text: { text: {
text: 'text', text: 'text',
@ -247,8 +247,8 @@ describe('Text to image', function () {
}); });
}); });
it('bad justify input', function () { it('bad justify input', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
text: { text: {
text: 'text', text: 'text',
@ -273,8 +273,8 @@ describe('Text to image', function () {
); );
}); });
it('bad rgba input', function () { it('bad rgba input', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
text: { text: {
text: 'text', text: 'text',
@ -299,8 +299,8 @@ describe('Text to image', function () {
); );
}); });
it('only height or dpi not both', function () { it('only height or dpi not both', () => {
assert.throws(function () { assert.throws(() => {
sharp({ sharp({
text: { text: {
text: 'text', text: 'text',

View File

@ -9,12 +9,12 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Threshold', function () { describe('Threshold', () => {
it('threshold 1 jpeg', function (_t, done) { it('threshold 1 jpeg', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.threshold(1) .threshold(1)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -23,11 +23,11 @@ describe('Threshold', function () {
}); });
}); });
it('threshold 40 jpeg', function (_t, done) { it('threshold 40 jpeg', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.threshold(40) .threshold(40)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -36,11 +36,11 @@ describe('Threshold', function () {
}); });
}); });
it('threshold 128', function (_t, done) { it('threshold 128', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.threshold(128) .threshold(128)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -49,11 +49,11 @@ describe('Threshold', function () {
}); });
}); });
it('threshold true (=128)', function (_t, done) { it('threshold true (=128)', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.threshold(true) .threshold(true)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -62,20 +62,20 @@ describe('Threshold', function () {
}); });
}); });
it('threshold false (=0)', function (_t, done) { it('threshold false (=0)', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.threshold(false) .threshold(false)
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.inputJpg, data, done); fixtures.assertSimilar(fixtures.inputJpg, data, done);
}); });
}); });
it('threshold grayscale: true (=128)', function (_t, done) { it('threshold grayscale: true (=128)', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.threshold(128, { grayscale: true }) .threshold(128, { grayscale: true })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -84,11 +84,11 @@ describe('Threshold', function () {
}); });
}); });
it('threshold default jpeg', function (_t, done) { it('threshold default jpeg', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.threshold() .threshold()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -97,11 +97,11 @@ describe('Threshold', function () {
}); });
}); });
it('threshold default png transparency', function (_t, done) { it('threshold default png transparency', (_t, done) => {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(320, 240) .resize(320, 240)
.threshold() .threshold()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -110,11 +110,11 @@ describe('Threshold', function () {
}); });
}); });
it('threshold default png alpha', function (_t, done) { it('threshold default png alpha', (_t, done) => {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.resize(320, 240) .resize(320, 240)
.threshold() .threshold()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -123,21 +123,21 @@ describe('Threshold', function () {
}); });
}); });
it('threshold default webp transparency', function (_t, done) { it('threshold default webp transparency', (_t, done) => {
sharp(fixtures.inputWebPWithTransparency) sharp(fixtures.inputWebPWithTransparency)
.threshold() .threshold()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
fixtures.assertSimilar(fixtures.expected('threshold-128-transparency.webp'), data, done); fixtures.assertSimilar(fixtures.expected('threshold-128-transparency.webp'), data, done);
}); });
}); });
it('color threshold', function (_t, done) { it('color threshold', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.threshold(128, { grayscale: false }) .threshold(128, { grayscale: false })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
@ -146,14 +146,14 @@ describe('Threshold', function () {
}); });
}); });
it('invalid threshold -1', function () { it('invalid threshold -1', () => {
assert.throws(function () { assert.throws(() => {
sharp().threshold(-1); sharp().threshold(-1);
}); });
}); });
it('invalid threshold 256', function () { it('invalid threshold 256', () => {
assert.throws(function () { assert.throws(() => {
sharp().threshold(256); sharp().threshold(256);
}); });
}); });

View File

@ -12,13 +12,13 @@ const fixtures = require('../fixtures');
const outputTiff = fixtures.path('output.tiff'); const outputTiff = fixtures.path('output.tiff');
describe('TIFF', function () { describe('TIFF', () => {
it('Load TIFF from Buffer', function (_t, done) { it('Load TIFF from Buffer', (_t, done) => {
const inputTiffBuffer = fs.readFileSync(fixtures.inputTiff); const inputTiffBuffer = fs.readFileSync(fixtures.inputTiff);
sharp(inputTiffBuffer) sharp(inputTiffBuffer)
.resize(320, 240) .resize(320, 240)
.jpeg() .jpeg()
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -29,10 +29,10 @@ describe('TIFF', function () {
}); });
}); });
it('Load multi-page TIFF from file', function (_t, done) { it('Load multi-page TIFF from file', (_t, done) => {
sharp(fixtures.inputTiffMultipage) // defaults to page 0 sharp(fixtures.inputTiffMultipage) // defaults to page 0
.jpeg() .jpeg()
.toBuffer(function (err, defaultData, defaultInfo) { .toBuffer((err, defaultData, defaultInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, defaultData.length > 0); assert.strictEqual(true, defaultData.length > 0);
assert.strictEqual(defaultData.length, defaultInfo.size); assert.strictEqual(defaultData.length, defaultInfo.size);
@ -40,7 +40,7 @@ describe('TIFF', function () {
sharp(fixtures.inputTiffMultipage, { page: 1 }) // 50%-scale copy of page 0 sharp(fixtures.inputTiffMultipage, { page: 1 }) // 50%-scale copy of page 0
.jpeg() .jpeg()
.toBuffer(function (err, scaledData, scaledInfo) { .toBuffer((err, scaledData, scaledInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, scaledData.length > 0); assert.strictEqual(true, scaledData.length > 0);
assert.strictEqual(scaledData.length, scaledInfo.size); assert.strictEqual(scaledData.length, scaledInfo.size);
@ -52,11 +52,11 @@ describe('TIFF', function () {
}); });
}); });
it('Load multi-page TIFF from Buffer', function (_t, done) { it('Load multi-page TIFF from Buffer', (_t, done) => {
const inputTiffBuffer = fs.readFileSync(fixtures.inputTiffMultipage); const inputTiffBuffer = fs.readFileSync(fixtures.inputTiffMultipage);
sharp(inputTiffBuffer) // defaults to page 0 sharp(inputTiffBuffer) // defaults to page 0
.jpeg() .jpeg()
.toBuffer(function (err, defaultData, defaultInfo) { .toBuffer((err, defaultData, defaultInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, defaultData.length > 0); assert.strictEqual(true, defaultData.length > 0);
assert.strictEqual(defaultData.length, defaultInfo.size); assert.strictEqual(defaultData.length, defaultInfo.size);
@ -64,7 +64,7 @@ describe('TIFF', function () {
sharp(inputTiffBuffer, { page: 1 }) // 50%-scale copy of page 0 sharp(inputTiffBuffer, { page: 1 }) // 50%-scale copy of page 0
.jpeg() .jpeg()
.toBuffer(function (err, scaledData, scaledInfo) { .toBuffer((err, scaledData, scaledInfo) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, scaledData.length > 0); assert.strictEqual(true, scaledData.length > 0);
assert.strictEqual(scaledData.length, scaledInfo.size); assert.strictEqual(scaledData.length, scaledInfo.size);
@ -76,10 +76,10 @@ describe('TIFF', function () {
}); });
}); });
it('Save TIFF to Buffer', function (_t, done) { it('Save TIFF to Buffer', (_t, done) => {
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)
.resize(320, 240) .resize(320, 240)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -105,19 +105,19 @@ describe('TIFF', function () {
) )
); );
it('Invalid TIFF quality throws error', function () { it('Invalid TIFF quality throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ quality: 101 }); sharp().tiff({ quality: 101 });
}); });
}); });
it('Missing TIFF quality does not throw error', function () { it('Missing TIFF quality does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff(); sharp().tiff();
}); });
}); });
it('Not squashing TIFF to a bit depth of 1 should not change the file size', function (_t, done) { it('Not squashing TIFF to a bit depth of 1 should not change the file size', (_t, done) => {
const startSize = fs.statSync(fixtures.inputTiff8BitDepth).size; const startSize = fs.statSync(fixtures.inputTiff8BitDepth).size;
sharp(fixtures.inputTiff8BitDepth) sharp(fixtures.inputTiff8BitDepth)
.toColourspace('b-w') // can only squash 1 band uchar images .toColourspace('b-w') // can only squash 1 band uchar images
@ -134,7 +134,7 @@ describe('TIFF', function () {
}); });
}); });
it('Squashing TIFF to a bit depth of 1 should significantly reduce file size', function (_t, done) { it('Squashing TIFF to a bit depth of 1 should significantly reduce file size', (_t, done) => {
const startSize = fs.statSync(fixtures.inputTiff8BitDepth).size; const startSize = fs.statSync(fixtures.inputTiff8BitDepth).size;
sharp(fixtures.inputTiff8BitDepth) sharp(fixtures.inputTiff8BitDepth)
.toColourspace('b-w') // can only squash 1 band uchar images .toColourspace('b-w') // can only squash 1 band uchar images
@ -151,8 +151,8 @@ describe('TIFF', function () {
}); });
}); });
it('Invalid TIFF bitdepth value throws error', function () { it('Invalid TIFF bitdepth value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ bitdepth: 3 }); sharp().tiff({ bitdepth: 3 });
}, /Error: Expected 1, 2, 4 or 8 for bitdepth but received 3 of type number/); }, /Error: Expected 1, 2, 4 or 8 for bitdepth but received 3 of type number/);
}); });
@ -210,19 +210,19 @@ describe('TIFF', function () {
assert.strictEqual(25400, density); assert.strictEqual(25400, density);
}); });
it('TIFF invalid xres value should throw an error', function () { it('TIFF invalid xres value should throw an error', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ xres: '1000.0' }); sharp().tiff({ xres: '1000.0' });
}); });
}); });
it('TIFF invalid yres value should throw an error', function () { it('TIFF invalid yres value should throw an error', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ yres: '1000.0' }); sharp().tiff({ yres: '1000.0' });
}); });
}); });
it('TIFF lzw compression with horizontal predictor shrinks test file', function (_t, done) { it('TIFF lzw compression with horizontal predictor shrinks test file', (_t, done) => {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed) sharp(fixtures.inputTiffUncompressed)
.tiff({ .tiff({
@ -274,7 +274,7 @@ describe('TIFF', function () {
}) })
); );
it('TIFF ccittfax4 compression shrinks b-w test file', function (_t, done) { it('TIFF ccittfax4 compression shrinks b-w test file', (_t, done) => {
const startSize = fs.statSync(fixtures.inputTiff).size; const startSize = fs.statSync(fixtures.inputTiff).size;
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)
.toColourspace('b-w') .toColourspace('b-w')
@ -314,7 +314,7 @@ describe('TIFF', function () {
assert.strictEqual(resolutionUnit, 'cm'); assert.strictEqual(resolutionUnit, 'cm');
}); });
it('TIFF deflate compression with horizontal predictor shrinks test file', function (_t, done) { it('TIFF deflate compression with horizontal predictor shrinks test file', (_t, done) => {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed) sharp(fixtures.inputTiffUncompressed)
.tiff({ .tiff({
@ -329,7 +329,7 @@ describe('TIFF', function () {
}); });
}); });
it('TIFF deflate compression with float predictor shrinks test file', function (_t, done) { it('TIFF deflate compression with float predictor shrinks test file', (_t, done) => {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed) sharp(fixtures.inputTiffUncompressed)
.tiff({ .tiff({
@ -344,7 +344,7 @@ describe('TIFF', function () {
}); });
}); });
it('TIFF deflate compression without predictor shrinks test file', function (_t, done) { it('TIFF deflate compression without predictor shrinks test file', (_t, done) => {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed) sharp(fixtures.inputTiffUncompressed)
.tiff({ .tiff({
@ -359,7 +359,7 @@ describe('TIFF', function () {
}); });
}); });
it('TIFF jpeg compression shrinks test file', function (_t, done) { it('TIFF jpeg compression shrinks test file', (_t, done) => {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed) sharp(fixtures.inputTiffUncompressed)
.tiff({ .tiff({
@ -373,79 +373,79 @@ describe('TIFF', function () {
}); });
}); });
it('TIFF none compression does not throw error', function () { it('TIFF none compression does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff({ compression: 'none' }); sharp().tiff({ compression: 'none' });
}); });
}); });
it('TIFF lzw compression does not throw error', function () { it('TIFF lzw compression does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff({ compression: 'lzw' }); sharp().tiff({ compression: 'lzw' });
}); });
}); });
it('TIFF deflate compression does not throw error', function () { it('TIFF deflate compression does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff({ compression: 'deflate' }); sharp().tiff({ compression: 'deflate' });
}); });
}); });
it('TIFF invalid compression option throws', function () { it('TIFF invalid compression option throws', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ compression: 0 }); sharp().tiff({ compression: 0 });
}); });
}); });
it('TIFF invalid compression option throws', function () { it('TIFF invalid compression option throws', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ compression: 'a' }); sharp().tiff({ compression: 'a' });
}); });
}); });
it('TIFF bigtiff true value does not throw error', function () { it('TIFF bigtiff true value does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff({ bigtiff: true }); sharp().tiff({ bigtiff: true });
}); });
}); });
it('Invalid TIFF bigtiff value throws error', function () { it('Invalid TIFF bigtiff value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ bigtiff: 'true' }); sharp().tiff({ bigtiff: 'true' });
}); });
}); });
it('TIFF invalid predictor option throws', function () { it('TIFF invalid predictor option throws', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ predictor: 'a' }); sharp().tiff({ predictor: 'a' });
}); });
}); });
it('TIFF invalid resolutionUnit option throws', function () { it('TIFF invalid resolutionUnit option throws', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ resolutionUnit: 'none' }); sharp().tiff({ resolutionUnit: 'none' });
}); });
}); });
it('TIFF horizontal predictor does not throw error', function () { it('TIFF horizontal predictor does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff({ predictor: 'horizontal' }); sharp().tiff({ predictor: 'horizontal' });
}); });
}); });
it('TIFF float predictor does not throw error', function () { it('TIFF float predictor does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff({ predictor: 'float' }); sharp().tiff({ predictor: 'float' });
}); });
}); });
it('TIFF none predictor does not throw error', function () { it('TIFF none predictor does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff({ predictor: 'none' }); sharp().tiff({ predictor: 'none' });
}); });
}); });
it('TIFF tiled pyramid image without compression enlarges test file', function (_t, done) { it('TIFF tiled pyramid image without compression enlarges test file', (_t, done) => {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed) sharp(fixtures.inputTiffUncompressed)
.tiff({ .tiff({
@ -463,89 +463,89 @@ describe('TIFF', function () {
}); });
}); });
it('TIFF pyramid true value does not throw error', function () { it('TIFF pyramid true value does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff({ pyramid: true }); sharp().tiff({ pyramid: true });
}); });
}); });
it('Invalid TIFF pyramid value throws error', function () { it('Invalid TIFF pyramid value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ pyramid: 'true' }); sharp().tiff({ pyramid: 'true' });
}); });
}); });
it('TIFF miniswhite true value does not throw error', function () { it('TIFF miniswhite true value does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff({ miniswhite: true }); sharp().tiff({ miniswhite: true });
}); });
}); });
it('Invalid TIFF miniswhite value throws error', function () { it('Invalid TIFF miniswhite value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ miniswhite: 'true' }); sharp().tiff({ miniswhite: 'true' });
}); });
}); });
it('Invalid TIFF tile value throws error', function () { it('Invalid TIFF tile value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ tile: 'true' }); sharp().tiff({ tile: 'true' });
}); });
}); });
it('TIFF tile true value does not throw error', function () { it('TIFF tile true value does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff({ tile: true }); sharp().tiff({ tile: true });
}); });
}); });
it('Valid TIFF tileHeight value does not throw error', function () { it('Valid TIFF tileHeight value does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff({ tileHeight: 512 }); sharp().tiff({ tileHeight: 512 });
}); });
}); });
it('Valid TIFF tileWidth value does not throw error', function () { it('Valid TIFF tileWidth value does not throw error', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tiff({ tileWidth: 512 }); sharp().tiff({ tileWidth: 512 });
}); });
}); });
it('Invalid TIFF tileHeight value throws error', function () { it('Invalid TIFF tileHeight value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ tileHeight: '256' }); sharp().tiff({ tileHeight: '256' });
}); });
}); });
it('Invalid TIFF tileWidth value throws error', function () { it('Invalid TIFF tileWidth value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ tileWidth: '256' }); sharp().tiff({ tileWidth: '256' });
}); });
}); });
it('Invalid TIFF tileHeight value throws error', function () { it('Invalid TIFF tileHeight value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ tileHeight: 0 }); sharp().tiff({ tileHeight: 0 });
}); });
}); });
it('Invalid TIFF tileWidth value throws error', function () { it('Invalid TIFF tileWidth value throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().tiff({ tileWidth: 0 }); sharp().tiff({ tileWidth: 0 });
}); });
}); });
it('TIFF file input with invalid page fails gracefully', function (_t, done) { it('TIFF file input with invalid page fails gracefully', (_t, done) => {
sharp(fixtures.inputTiffMultipage, { page: 2 }) sharp(fixtures.inputTiffMultipage, { page: 2 })
.toBuffer(function (err) { .toBuffer((err) => {
assert.strictEqual(true, !!err); assert.strictEqual(true, !!err);
done(); done();
}); });
}); });
it('TIFF buffer input with invalid page fails gracefully', function (_t, done) { it('TIFF buffer input with invalid page fails gracefully', (_t, done) => {
sharp(fs.readFileSync(fixtures.inputTiffMultipage), { page: 2 }) sharp(fs.readFileSync(fixtures.inputTiffMultipage), { page: 2 })
.toBuffer(function (err) { .toBuffer((err) => {
assert.strictEqual(true, !!err); assert.strictEqual(true, !!err);
done(); done();
}); });

View File

@ -14,27 +14,26 @@ const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
// Verifies all tiles in a given dz output directory are <= size // Verifies all tiles in a given dz output directory are <= size
const assertDeepZoomTiles = function (directory, expectedSize, expectedLevels, done) { const assertDeepZoomTiles = (directory, expectedSize, expectedLevels, done) => {
// Get levels // Get levels
const dirents = fs.readdirSync(directory, { withFileTypes: true }); const dirents = fs.readdirSync(directory, { withFileTypes: true });
const levels = dirents.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name); const levels = dirents.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name);
assert.strictEqual(expectedLevels, levels.length); assert.strictEqual(expectedLevels, levels.length);
// Get tiles // Get tiles
const tiles = []; const tiles = [];
levels.forEach(function (level) { levels.forEach((level) => {
// Verify level directory name // Verify level directory name
assert.strictEqual(true, /^[0-9]+$/.test(level)); assert.strictEqual(true, /^[0-9]+$/.test(level));
fs.readdirSync(path.join(directory, level)).forEach(function (tile) { fs.readdirSync(path.join(directory, level)).forEach((tile) => {
// Verify tile file name // Verify tile file name
assert.strictEqual(true, /^[0-9]+_[0-9]+\.jpeg$/.test(tile)); assert.strictEqual(true, /^[0-9]+_[0-9]+\.jpeg$/.test(tile));
tiles.push(path.join(directory, level, tile)); tiles.push(path.join(directory, level, tile));
}); });
}); });
// Verify each tile is <= expectedSize // Verify each tile is <= expectedSize
Promise.all(tiles.map(function (tile) { Promise.all(tiles.map((tile) => sharp(tile)
return sharp(tile)
.metadata() .metadata()
.then(function (metadata) { .then((metadata) => {
assert.strictEqual('jpeg', metadata.format); assert.strictEqual('jpeg', metadata.format);
assert.strictEqual('srgb', metadata.space); assert.strictEqual('srgb', metadata.space);
assert.strictEqual(3, metadata.channels); assert.strictEqual(3, metadata.channels);
@ -42,20 +41,19 @@ const assertDeepZoomTiles = function (directory, expectedSize, expectedLevels, d
assert.strictEqual(false, metadata.hasAlpha); assert.strictEqual(false, metadata.hasAlpha);
assert.strictEqual(true, metadata.width <= expectedSize); assert.strictEqual(true, metadata.width <= expectedSize);
assert.strictEqual(true, metadata.height <= expectedSize); assert.strictEqual(true, metadata.height <= expectedSize);
}); })))
}))
.then(() => done()) .then(() => done())
.catch(done); .catch(done);
}; };
const assertZoomifyTiles = function (directory, expectedLevels, done) { const assertZoomifyTiles = (directory, expectedLevels, done) => {
fs.stat(path.join(directory, 'ImageProperties.xml'), function (err, stat) { fs.stat(path.join(directory, 'ImageProperties.xml'), (err, stat) => {
if (err) throw err; if (err) throw err;
assert.ok(stat.isFile()); assert.ok(stat.isFile());
assert.ok(stat.size > 0); assert.ok(stat.size > 0);
let maxTileLevel = -1; let maxTileLevel = -1;
fs.readdirSync(path.join(directory, 'TileGroup0')).forEach(function (tile) { fs.readdirSync(path.join(directory, 'TileGroup0')).forEach((tile) => {
// Verify tile file name // Verify tile file name
assert.ok(/^[0-9]+-[0-9]+-[0-9]+\.jpg$/.test(tile)); assert.ok(/^[0-9]+-[0-9]+-[0-9]+\.jpg$/.test(tile));
const level = Number(tile.split('-')[0]); const level = Number(tile.split('-')[0]);
@ -68,24 +66,24 @@ const assertZoomifyTiles = function (directory, expectedLevels, done) {
}); });
}; };
const assertGoogleTiles = function (directory, expectedLevels, done) { const assertGoogleTiles = (directory, expectedLevels, done) => {
// Get levels // Get levels
const dirents = fs.readdirSync(directory, { withFileTypes: true }); const dirents = fs.readdirSync(directory, { withFileTypes: true });
const levels = dirents.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name); const levels = dirents.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name);
assert.strictEqual(expectedLevels, levels.length); assert.strictEqual(expectedLevels, levels.length);
fs.stat(path.join(directory, 'blank.png'), function (err, stat) { fs.stat(path.join(directory, 'blank.png'), (err, stat) => {
if (err) throw err; if (err) throw err;
assert.ok(stat.isFile()); assert.ok(stat.isFile());
assert.ok(stat.size > 0); assert.ok(stat.size > 0);
// Basic check to confirm lowest and highest level tiles exist // Basic check to confirm lowest and highest level tiles exist
fs.stat(path.join(directory, '0', '0', '0.jpg'), function (err, stat) { fs.stat(path.join(directory, '0', '0', '0.jpg'), (err, stat) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.isFile()); assert.strictEqual(true, stat.isFile());
assert.strictEqual(true, stat.size > 0); assert.strictEqual(true, stat.size > 0);
fs.stat(path.join(directory, (expectedLevels - 1).toString(), '0', '0.jpg'), function (err, stat) { fs.stat(path.join(directory, (expectedLevels - 1).toString(), '0', '0.jpg'), (err, stat) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.isFile()); assert.strictEqual(true, stat.isFile());
assert.strictEqual(true, stat.size > 0); assert.strictEqual(true, stat.size > 0);
@ -96,7 +94,7 @@ const assertGoogleTiles = function (directory, expectedLevels, done) {
}; };
// Verifies tiles at specified level in a given output directory are > size+overlap // Verifies tiles at specified level in a given output directory are > size+overlap
const assertTileOverlap = function (directory, tileSize, done) { const assertTileOverlap = (directory, tileSize, done) => {
// Get sorted levels // Get sorted levels
const dirents = fs.readdirSync(directory, { withFileTypes: true }); const dirents = fs.readdirSync(directory, { withFileTypes: true });
const levels = dirents.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name).sort((a, b) => a - b); const levels = dirents.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name).sort((a, b) => a - b);
@ -107,7 +105,7 @@ const assertTileOverlap = function (directory, tileSize, done) {
// Select a tile from the approximate center of the image // Select a tile from the approximate center of the image
const squareTile = path.join(directory, highestLevel, tiles[Math.floor(tiles.length / 2)]); const squareTile = path.join(directory, highestLevel, tiles[Math.floor(tiles.length / 2)]);
sharp(squareTile).metadata(function (err, metadata) { sharp(squareTile).metadata((err, metadata) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -119,10 +117,10 @@ const assertTileOverlap = function (directory, tileSize, done) {
}); });
}; };
describe('Tile', function () { describe('Tile', () => {
it('Valid size values pass', function () { it('Valid size values pass', () => {
[1, 8192].forEach(function (size) { [1, 8192].forEach((size) => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tile({ sharp().tile({
size size
}); });
@ -130,9 +128,9 @@ describe('Tile', function () {
}); });
}); });
it('Invalid size values fail', function () { it('Invalid size values fail', () => {
['zoinks', 1.1, -1, 0, 8193].forEach(function (size) { ['zoinks', 1.1, -1, 0, 8193].forEach((size) => {
assert.throws(function () { assert.throws(() => {
sharp().tile({ sharp().tile({
size size
}); });
@ -140,9 +138,9 @@ describe('Tile', function () {
}); });
}); });
it('Valid overlap values pass', function () { it('Valid overlap values pass', () => {
[0, 8192].forEach(function (overlap) { [0, 8192].forEach((overlap) => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tile({ sharp().tile({
size: 8192, size: 8192,
overlap overlap
@ -151,9 +149,9 @@ describe('Tile', function () {
}); });
}); });
it('Invalid overlap values fail', function () { it('Invalid overlap values fail', () => {
['zoinks', 1.1, -1, 8193].forEach(function (overlap) { ['zoinks', 1.1, -1, 8193].forEach((overlap) => {
assert.throws(function () { assert.throws(() => {
sharp().tile({ sharp().tile({
overlap overlap
}); });
@ -161,9 +159,9 @@ describe('Tile', function () {
}); });
}); });
it('Valid container values pass', function () { it('Valid container values pass', () => {
['fs', 'zip'].forEach(function (container) { ['fs', 'zip'].forEach((container) => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tile({ sharp().tile({
container container
}); });
@ -171,9 +169,9 @@ describe('Tile', function () {
}); });
}); });
it('Invalid container values fail', function () { it('Invalid container values fail', () => {
['zoinks', 1].forEach(function (container) { ['zoinks', 1].forEach((container) => {
assert.throws(function () { assert.throws(() => {
sharp().tile({ sharp().tile({
container container
}); });
@ -181,9 +179,9 @@ describe('Tile', function () {
}); });
}); });
it('Valid layout values pass', function () { it('Valid layout values pass', () => {
['dz', 'google', 'zoomify'].forEach(function (layout) { ['dz', 'google', 'zoomify'].forEach((layout) => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tile({ sharp().tile({
layout layout
}); });
@ -191,9 +189,9 @@ describe('Tile', function () {
}); });
}); });
it('Invalid layout values fail', function () { it('Invalid layout values fail', () => {
['zoinks', 1].forEach(function (layout) { ['zoinks', 1].forEach((layout) => {
assert.throws(function () { assert.throws(() => {
sharp().tile({ sharp().tile({
layout layout
}); });
@ -201,30 +199,30 @@ describe('Tile', function () {
}); });
}); });
it('Valid formats pass', function () { it('Valid formats pass', () => {
['jpeg', 'png', 'webp'].forEach(function (format) { ['jpeg', 'png', 'webp'].forEach((format) => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().toFormat(format).tile(); sharp().toFormat(format).tile();
}); });
}); });
}); });
it('Invalid formats fail', function () { it('Invalid formats fail', () => {
['tiff', 'raw'].forEach(function (format) { ['tiff', 'raw'].forEach((format) => {
assert.throws(function () { assert.throws(() => {
sharp().toFormat(format).tile(); sharp().toFormat(format).tile();
}); });
}); });
}); });
it('Valid depths pass', function () { it('Valid depths pass', () => {
['onepixel', 'onetile', 'one'].forEach(function (depth) { ['onepixel', 'onetile', 'one'].forEach((depth) => {
assert.doesNotThrow(() => sharp().tile({ depth })); assert.doesNotThrow(() => sharp().tile({ depth }));
}); });
}); });
it('Invalid depths fail', function () { it('Invalid depths fail', () => {
['depth', 1].forEach(function (depth) { ['depth', 1].forEach((depth) => {
assert.throws( assert.throws(
() => sharp().tile({ depth }), () => sharp().tile({ depth }),
/Expected one of: onepixel, onetile, one for depth but received/ /Expected one of: onepixel, onetile, one for depth but received/
@ -232,16 +230,16 @@ describe('Tile', function () {
}); });
}); });
it('Prevent larger overlap than default size', function () { it('Prevent larger overlap than default size', () => {
assert.throws(function () { assert.throws(() => {
sharp().tile({ sharp().tile({
overlap: 257 overlap: 257
}); });
}); });
}); });
it('Prevent larger overlap than provided size', function () { it('Prevent larger overlap than provided size', () => {
assert.throws(function () { assert.throws(() => {
sharp().tile({ sharp().tile({
size: 512, size: 512,
overlap: 513 overlap: 513
@ -249,9 +247,9 @@ describe('Tile', function () {
}); });
}); });
it('Valid rotation angle values pass', function () { it('Valid rotation angle values pass', () => {
[90, 270, -90].forEach(function (angle) { [90, 270, -90].forEach((angle) => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tile({ sharp().tile({
angle angle
}); });
@ -259,9 +257,9 @@ describe('Tile', function () {
}); });
}); });
it('Invalid rotation angle values fail', function () { it('Invalid rotation angle values fail', () => {
['zoinks', 1.1, -1, 27].forEach(function (angle) { ['zoinks', 1.1, -1, 27].forEach((angle) => {
assert.throws(function () { assert.throws(() => {
sharp().tile({ sharp().tile({
angle angle
}); });
@ -269,9 +267,9 @@ describe('Tile', function () {
}); });
}); });
it('Valid skipBlanks threshold values pass', function () { it('Valid skipBlanks threshold values pass', () => {
[-1, 0, 255, 65535].forEach(function (skipBlanksThreshold) { [-1, 0, 255, 65535].forEach((skipBlanksThreshold) => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tile({ sharp().tile({
skipBlanks: skipBlanksThreshold skipBlanks: skipBlanksThreshold
}); });
@ -279,9 +277,9 @@ describe('Tile', function () {
}); });
}); });
it('InvalidskipBlanks threshold values fail', function () { it('InvalidskipBlanks threshold values fail', () => {
['zoinks', -2, 65536].forEach(function (skipBlanksThreshold) { ['zoinks', -2, 65536].forEach((skipBlanksThreshold) => {
assert.throws(function () { assert.throws(() => {
sharp().tile({ sharp().tile({
skipBlanks: skipBlanksThreshold skipBlanks: skipBlanksThreshold
}); });
@ -289,42 +287,42 @@ describe('Tile', function () {
}); });
}); });
it('Valid center parameter value passes', function () { it('Valid center parameter value passes', () => {
assert.doesNotThrow( assert.doesNotThrow(
() => sharp().tile({ center: true }) () => sharp().tile({ center: true })
); );
}); });
it('Invalid centre parameter value fails', function () { it('Invalid centre parameter value fails', () => {
assert.throws( assert.throws(
() => sharp().tile({ centre: 'true' }), () => sharp().tile({ centre: 'true' }),
/Expected boolean for tileCentre but received true of type string/ /Expected boolean for tileCentre but received true of type string/
); );
}); });
it('Valid id parameter value passes', function () { it('Valid id parameter value passes', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
sharp().tile({ sharp().tile({
id: 'test' id: 'test'
}); });
}); });
}); });
it('Invalid id parameter value fails', function () { it('Invalid id parameter value fails', () => {
assert.throws(function () { assert.throws(() => {
sharp().tile({ sharp().tile({
id: true id: true
}); });
}); });
}); });
it('Valid basename parameter value passes', function () { it('Valid basename parameter value passes', () => {
assert.doesNotThrow( assert.doesNotThrow(
() => sharp().tile({ basename: 'pass' }) () => sharp().tile({ basename: 'pass' })
); );
}); });
it('Invalid basename parameter value fails', function () { it('Invalid basename parameter value fails', () => {
assert.throws( assert.throws(
() => sharp().tile({ basename: true }), () => sharp().tile({ basename: true }),
/Expected string for basename but received/ /Expected string for basename but received/
@ -332,11 +330,11 @@ describe('Tile', function () {
}); });
if (sharp.format.dz.output.file) { if (sharp.format.dz.output.file) {
it('Deep Zoom layout', function (_t, done) { it('Deep Zoom layout', (_t, done) => {
const directory = fixtures.path('output.dzi_files'); const directory = fixtures.path('output.dzi_files');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.toFile(fixtures.path('output.dzi'), function (err, info) { .toFile(fixtures.path('output.dzi'), (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -348,37 +346,37 @@ describe('Tile', function () {
}); });
}); });
it('Deep Zoom layout with custom size+overlap', function (_t, done) { it('Deep Zoom layout with custom size+overlap', (_t, done) => {
const directory = fixtures.path('output.512.dzi_files'); const directory = fixtures.path('output.512.dzi_files');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
size: 512, size: 512,
overlap: 16 overlap: 16
}) })
.toFile(fixtures.path('output.512.dzi'), function (err, info) { .toFile(fixtures.path('output.512.dzi'), (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
assert.strictEqual(2225, info.height); assert.strictEqual(2225, info.height);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
assert.strictEqual('undefined', typeof info.size); assert.strictEqual('undefined', typeof info.size);
assertDeepZoomTiles(directory, 512 + (2 * 16), 13, function () { assertDeepZoomTiles(directory, 512 + (2 * 16), 13, () => {
assertTileOverlap(directory, 512, done); assertTileOverlap(directory, 512, done);
}); });
}); });
}); });
}); });
it('Deep Zoom layout with custom size+angle', function (_t, done) { it('Deep Zoom layout with custom size+angle', (_t, done) => {
const directory = fixtures.path('output.512_90.dzi_files'); const directory = fixtures.path('output.512_90.dzi_files');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
size: 512, size: 512,
angle: 90 angle: 90
}) })
.toFile(fixtures.path('output.512_90.dzi'), function (err, info) { .toFile(fixtures.path('output.512_90.dzi'), (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -392,7 +390,7 @@ describe('Tile', function () {
// expected are w=512 and h=170 for the 0_1.jpeg. // expected are w=512 and h=170 for the 0_1.jpeg.
// if a 0 angle is supplied to the .tile function // if a 0 angle is supplied to the .tile function
// the expected values are w=170 and h=512 for the 1_0.jpeg // the expected values are w=170 and h=512 for the 1_0.jpeg
sharp(tile).metadata(function (err, metadata) { sharp(tile).metadata((err, metadata) => {
if (err) { if (err) {
throw err; throw err;
} else { } else {
@ -404,15 +402,15 @@ describe('Tile', function () {
}); });
}); });
it('Deep Zoom layout with depth of one', function (_t, done) { it('Deep Zoom layout with depth of one', (_t, done) => {
const directory = fixtures.path('output.512_depth_one.dzi_files'); const directory = fixtures.path('output.512_depth_one.dzi_files');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
size: 512, size: 512,
depth: 'one' depth: 'one'
}) })
.toFile(fixtures.path('output.512_depth_one.dzi'), function (err) { .toFile(fixtures.path('output.512_depth_one.dzi'), (err) => {
if (err) throw err; if (err) throw err;
// Verify only one depth generated // Verify only one depth generated
assertDeepZoomTiles(directory, 512, 1, done); assertDeepZoomTiles(directory, 512, 1, done);
@ -420,15 +418,15 @@ describe('Tile', function () {
}); });
}); });
it('Deep Zoom layout with depth of onepixel', function (_t, done) { it('Deep Zoom layout with depth of onepixel', (_t, done) => {
const directory = fixtures.path('output.512_depth_onepixel.dzi_files'); const directory = fixtures.path('output.512_depth_onepixel.dzi_files');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
size: 512, size: 512,
depth: 'onepixel' depth: 'onepixel'
}) })
.toFile(fixtures.path('output.512_depth_onepixel.dzi'), function (err) { .toFile(fixtures.path('output.512_depth_onepixel.dzi'), (err) => {
if (err) throw err; if (err) throw err;
// Verify only one depth generated // Verify only one depth generated
assertDeepZoomTiles(directory, 512, 13, done); assertDeepZoomTiles(directory, 512, 13, done);
@ -436,15 +434,15 @@ describe('Tile', function () {
}); });
}); });
it('Deep Zoom layout with depth of onetile', function (_t, done) { it('Deep Zoom layout with depth of onetile', (_t, done) => {
const directory = fixtures.path('output.256_depth_onetile.dzi_files'); const directory = fixtures.path('output.256_depth_onetile.dzi_files');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
size: 256, size: 256,
depth: 'onetile' depth: 'onetile'
}) })
.toFile(fixtures.path('output.256_depth_onetile.dzi'), function (err) { .toFile(fixtures.path('output.256_depth_onetile.dzi'), (err) => {
if (err) throw err; if (err) throw err;
// Verify only one depth generated // Verify only one depth generated
assertDeepZoomTiles(directory, 256, 5, done); assertDeepZoomTiles(directory, 256, 5, done);
@ -452,15 +450,15 @@ describe('Tile', function () {
}); });
}); });
it('Deep Zoom layout with skipBlanks', function (_t, done) { it('Deep Zoom layout with skipBlanks', (_t, done) => {
const directory = fixtures.path('output.256_skip_blanks.dzi_files'); const directory = fixtures.path('output.256_skip_blanks.dzi_files');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpgOverlayLayer2) sharp(fixtures.inputJpgOverlayLayer2)
.tile({ .tile({
size: 256, size: 256,
skipBlanks: 0 skipBlanks: 0
}) })
.toFile(fixtures.path('output.256_skip_blanks.dzi'), function (err) { .toFile(fixtures.path('output.256_skip_blanks.dzi'), (err) => {
if (err) throw err; if (err) throw err;
// assert them 0_0.jpeg doesn't exist because it's a white tile // assert them 0_0.jpeg doesn't exist because it's a white tile
const whiteTilePath = path.join(directory, '11', '0_0.jpeg'); const whiteTilePath = path.join(directory, '11', '0_0.jpeg');
@ -471,21 +469,21 @@ describe('Tile', function () {
}); });
}); });
it('Zoomify layout', function (_t, done) { it('Zoomify layout', (_t, done) => {
const directory = fixtures.path('output.zoomify.dzi'); const directory = fixtures.path('output.zoomify.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
layout: 'zoomify' layout: 'zoomify'
}) })
.toFile(fixtures.path('output.zoomify.dzi'), function (err, info) { .toFile(fixtures.path('output.zoomify.dzi'), (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
assert.strictEqual(2225, info.height); assert.strictEqual(2225, info.height);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
assert.strictEqual(undefined, info.size); assert.strictEqual(undefined, info.size);
fs.stat(path.join(directory, 'ImageProperties.xml'), function (err, stat) { fs.stat(path.join(directory, 'ImageProperties.xml'), (err, stat) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.isFile()); assert.strictEqual(true, stat.isFile());
assert.strictEqual(true, stat.size > 0); assert.strictEqual(true, stat.size > 0);
@ -495,16 +493,16 @@ describe('Tile', function () {
}); });
}); });
it('Zoomify layout with depth one', function (_t, done) { it('Zoomify layout with depth one', (_t, done) => {
const directory = fixtures.path('output.zoomify.depth_one.dzi'); const directory = fixtures.path('output.zoomify.depth_one.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
size: 256, size: 256,
layout: 'zoomify', layout: 'zoomify',
depth: 'one' depth: 'one'
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -516,16 +514,16 @@ describe('Tile', function () {
}); });
}); });
it('Zoomify layout with depth onetile', function (_t, done) { it('Zoomify layout with depth onetile', (_t, done) => {
const directory = fixtures.path('output.zoomify.depth_onetile.dzi'); const directory = fixtures.path('output.zoomify.depth_onetile.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
size: 256, size: 256,
layout: 'zoomify', layout: 'zoomify',
depth: 'onetile' depth: 'onetile'
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -537,16 +535,16 @@ describe('Tile', function () {
}); });
}); });
it('Zoomify layout with depth onepixel', function (_t, done) { it('Zoomify layout with depth onepixel', (_t, done) => {
const directory = fixtures.path('output.zoomify.depth_onepixel.dzi'); const directory = fixtures.path('output.zoomify.depth_onepixel.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
size: 256, size: 256,
layout: 'zoomify', layout: 'zoomify',
depth: 'onepixel' depth: 'onepixel'
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -558,16 +556,16 @@ describe('Tile', function () {
}); });
}); });
it('Zoomify layout with skip blanks', function (_t, done) { it('Zoomify layout with skip blanks', (_t, done) => {
const directory = fixtures.path('output.zoomify.skipBlanks.dzi'); const directory = fixtures.path('output.zoomify.skipBlanks.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpgOverlayLayer2) sharp(fixtures.inputJpgOverlayLayer2)
.tile({ .tile({
size: 256, size: 256,
layout: 'zoomify', layout: 'zoomify',
skipBlanks: 0 skipBlanks: 0
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
// assert them 0_0.jpeg doesn't exist because it's a white tile // assert them 0_0.jpeg doesn't exist because it's a white tile
const whiteTilePath = path.join(directory, 'TileGroup0', '2-0-0.jpg'); const whiteTilePath = path.join(directory, 'TileGroup0', '2-0-0.jpg');
@ -582,21 +580,21 @@ describe('Tile', function () {
}); });
}); });
it('Google layout', function (_t, done) { it('Google layout', (_t, done) => {
const directory = fixtures.path('output.google.dzi'); const directory = fixtures.path('output.google.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
layout: 'google' layout: 'google'
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
assert.strictEqual(2225, info.height); assert.strictEqual(2225, info.height);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
assert.strictEqual(undefined, info.size); assert.strictEqual(undefined, info.size);
fs.stat(path.join(directory, '0', '0', '0.jpg'), function (err, stat) { fs.stat(path.join(directory, '0', '0', '0.jpg'), (err, stat) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.isFile()); assert.strictEqual(true, stat.isFile());
assert.strictEqual(true, stat.size > 0); assert.strictEqual(true, stat.size > 0);
@ -606,9 +604,9 @@ describe('Tile', function () {
}); });
}); });
it('Google layout with jpeg format', function (_t, done) { it('Google layout with jpeg format', (_t, done) => {
const directory = fixtures.path('output.jpg.google.dzi'); const directory = fixtures.path('output.jpg.google.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.jpeg({ .jpeg({
quality: 1 quality: 1
@ -616,7 +614,7 @@ describe('Tile', function () {
.tile({ .tile({
layout: 'google' layout: 'google'
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -624,7 +622,7 @@ describe('Tile', function () {
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
assert.strictEqual(undefined, info.size); assert.strictEqual(undefined, info.size);
const sample = path.join(directory, '0', '0', '0.jpg'); const sample = path.join(directory, '0', '0', '0.jpg');
sharp(sample).metadata(function (err, metadata) { sharp(sample).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', metadata.format); assert.strictEqual('jpeg', metadata.format);
assert.strictEqual('srgb', metadata.space); assert.strictEqual('srgb', metadata.space);
@ -633,7 +631,7 @@ describe('Tile', function () {
assert.strictEqual(false, metadata.hasAlpha); assert.strictEqual(false, metadata.hasAlpha);
assert.strictEqual(256, metadata.width); assert.strictEqual(256, metadata.width);
assert.strictEqual(256, metadata.height); assert.strictEqual(256, metadata.height);
fs.stat(sample, function (err, stat) { fs.stat(sample, (err, stat) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.size < 2000); assert.strictEqual(true, stat.size < 2000);
done(); done();
@ -643,9 +641,9 @@ describe('Tile', function () {
}); });
}); });
it('Google layout with png format', function (_t, done) { it('Google layout with png format', (_t, done) => {
const directory = fixtures.path('output.png.google.dzi'); const directory = fixtures.path('output.png.google.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.png({ .png({
compressionLevel: 0 compressionLevel: 0
@ -653,7 +651,7 @@ describe('Tile', function () {
.tile({ .tile({
layout: 'google' layout: 'google'
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -661,7 +659,7 @@ describe('Tile', function () {
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
assert.strictEqual(undefined, info.size); assert.strictEqual(undefined, info.size);
const sample = path.join(directory, '0', '0', '0.png'); const sample = path.join(directory, '0', '0', '0.png');
sharp(sample).metadata(function (err, metadata) { sharp(sample).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', metadata.format); assert.strictEqual('png', metadata.format);
assert.strictEqual('srgb', metadata.space); assert.strictEqual('srgb', metadata.space);
@ -670,7 +668,7 @@ describe('Tile', function () {
assert.strictEqual(false, metadata.hasAlpha); assert.strictEqual(false, metadata.hasAlpha);
assert.strictEqual(256, metadata.width); assert.strictEqual(256, metadata.width);
assert.strictEqual(256, metadata.height); assert.strictEqual(256, metadata.height);
fs.stat(sample, function (err, stat) { fs.stat(sample, (err, stat) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.size > 44000); assert.strictEqual(true, stat.size > 44000);
done(); done();
@ -680,9 +678,9 @@ describe('Tile', function () {
}); });
}); });
it('Google layout with webp format', function (_t, done) { it('Google layout with webp format', (_t, done) => {
const directory = fixtures.path('output.webp.google.dzi'); const directory = fixtures.path('output.webp.google.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.webp({ .webp({
quality: 1, quality: 1,
@ -691,7 +689,7 @@ describe('Tile', function () {
.tile({ .tile({
layout: 'google' layout: 'google'
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -699,7 +697,7 @@ describe('Tile', function () {
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
assert.strictEqual(undefined, info.size); assert.strictEqual(undefined, info.size);
const sample = path.join(directory, '0', '0', '0.webp'); const sample = path.join(directory, '0', '0', '0.webp');
sharp(sample).metadata(function (err, metadata) { sharp(sample).metadata((err, metadata) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('webp', metadata.format); assert.strictEqual('webp', metadata.format);
assert.strictEqual('srgb', metadata.space); assert.strictEqual('srgb', metadata.space);
@ -708,7 +706,7 @@ describe('Tile', function () {
assert.strictEqual(false, metadata.hasAlpha); assert.strictEqual(false, metadata.hasAlpha);
assert.strictEqual(256, metadata.width); assert.strictEqual(256, metadata.width);
assert.strictEqual(256, metadata.height); assert.strictEqual(256, metadata.height);
fs.stat(sample, function (err, stat) { fs.stat(sample, (err, stat) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.size < 2000); assert.strictEqual(true, stat.size < 2000);
done(); done();
@ -718,16 +716,16 @@ describe('Tile', function () {
}); });
}); });
it('Google layout with depth one', function (_t, done) { it('Google layout with depth one', (_t, done) => {
const directory = fixtures.path('output.google_depth_one.dzi'); const directory = fixtures.path('output.google_depth_one.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
layout: 'google', layout: 'google',
depth: 'one', depth: 'one',
size: 256 size: 256
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -739,16 +737,16 @@ describe('Tile', function () {
}); });
}); });
it('Google layout with depth onetile', function (_t, done) { it('Google layout with depth onetile', (_t, done) => {
const directory = fixtures.path('output.google_depth_onetile.dzi'); const directory = fixtures.path('output.google_depth_onetile.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
layout: 'google', layout: 'google',
depth: 'onetile', depth: 'onetile',
size: 256 size: 256
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -760,15 +758,15 @@ describe('Tile', function () {
}); });
}); });
it('Google layout with default skip Blanks', function (_t, done) { it('Google layout with default skip Blanks', (_t, done) => {
const directory = fixtures.path('output.google_depth_skipBlanks.dzi'); const directory = fixtures.path('output.google_depth_skipBlanks.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.tile({ .tile({
layout: 'google', layout: 'google',
size: 256 size: 256
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
const whiteTilePath = path.join(directory, '4', '8', '0.jpg'); const whiteTilePath = path.join(directory, '4', '8', '0.jpg');
@ -784,15 +782,15 @@ describe('Tile', function () {
}); });
}); });
it('Google layout with center image in tile', function (_t, done) { it('Google layout with center image in tile', (_t, done) => {
const directory = fixtures.path('output.google_center.dzi'); const directory = fixtures.path('output.google_center.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
center: true, center: true,
layout: 'google' layout: 'google'
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -804,15 +802,15 @@ describe('Tile', function () {
}); });
}); });
it('Google layout with center image in tile centre', function (_t, done) { it('Google layout with center image in tile centre', (_t, done) => {
const directory = fixtures.path('output.google_center.dzi'); const directory = fixtures.path('output.google_center.dzi');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
centre: true, centre: true,
layout: 'google' layout: 'google'
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -824,17 +822,17 @@ describe('Tile', function () {
}); });
}); });
it('IIIFv2 layout', function (_t, done) { it('IIIFv2 layout', (_t, done) => {
const name = 'output.iiif.info'; const name = 'output.iiif.info';
const directory = fixtures.path(name); const directory = fixtures.path(name);
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
const id = 'https://sharp.test.com/iiif'; const id = 'https://sharp.test.com/iiif';
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
layout: 'iiif', layout: 'iiif',
id id
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -844,7 +842,7 @@ describe('Tile', function () {
const infoJson = require(path.join(directory, 'info.json')); const infoJson = require(path.join(directory, 'info.json'));
assert.strictEqual('http://iiif.io/api/image/2/context.json', infoJson['@context']); assert.strictEqual('http://iiif.io/api/image/2/context.json', infoJson['@context']);
assert.strictEqual(`${id}/${name}`, infoJson['@id']); assert.strictEqual(`${id}/${name}`, infoJson['@id']);
fs.stat(path.join(directory, '0,0,256,256', '256,', '0', 'default.jpg'), function (err, stat) { fs.stat(path.join(directory, '0,0,256,256', '256,', '0', 'default.jpg'), (err, stat) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.isFile()); assert.strictEqual(true, stat.isFile());
assert.strictEqual(true, stat.size > 0); assert.strictEqual(true, stat.size > 0);
@ -854,17 +852,17 @@ describe('Tile', function () {
}); });
}); });
it('IIIFv3 layout', function (_t, done) { it('IIIFv3 layout', (_t, done) => {
const name = 'output.iiif3.info'; const name = 'output.iiif3.info';
const directory = fixtures.path(name); const directory = fixtures.path(name);
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
const id = 'https://sharp.test.com/iiif3'; const id = 'https://sharp.test.com/iiif3';
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
layout: 'iiif3', layout: 'iiif3',
id id
}) })
.toFile(directory, function (err, info) { .toFile(directory, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -875,7 +873,7 @@ describe('Tile', function () {
assert.strictEqual('http://iiif.io/api/image/3/context.json', infoJson['@context']); assert.strictEqual('http://iiif.io/api/image/3/context.json', infoJson['@context']);
assert.strictEqual('ImageService3', infoJson.type); assert.strictEqual('ImageService3', infoJson.type);
assert.strictEqual(`${id}/${name}`, infoJson.id); assert.strictEqual(`${id}/${name}`, infoJson.id);
fs.stat(path.join(directory, '0,0,256,256', '256,256', '0', 'default.jpg'), function (err, stat) { fs.stat(path.join(directory, '0,0,256,256', '256,256', '0', 'default.jpg'), (err, stat) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.isFile()); assert.strictEqual(true, stat.isFile());
assert.strictEqual(true, stat.size > 0); assert.strictEqual(true, stat.size > 0);
@ -885,20 +883,20 @@ describe('Tile', function () {
}); });
}); });
it('Write to ZIP container using file extension', function (_t, done) { it('Write to ZIP container using file extension', (_t, done) => {
const container = fixtures.path('output.dz.container.zip'); const container = fixtures.path('output.dz.container.zip');
const extractTo = fixtures.path('output.dz.container'); const extractTo = fixtures.path('output.dz.container');
const directory = path.join(extractTo, 'output.dz.container_files'); const directory = path.join(extractTo, 'output.dz.container_files');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.toFile(container, function (err, info) { .toFile(container, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
assert.strictEqual(2225, info.height); assert.strictEqual(2225, info.height);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
assert.strictEqual('number', typeof info.size); assert.strictEqual('number', typeof info.size);
fs.stat(container, function (err, stat) { fs.stat(container, (err, stat) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.isFile()); assert.strictEqual(true, stat.isFile());
assert.strictEqual(true, stat.size > 0); assert.strictEqual(true, stat.size > 0);
@ -912,16 +910,16 @@ describe('Tile', function () {
}); });
}); });
it('Write to ZIP container using container tile option', function (_t, done) { it('Write to ZIP container using container tile option', (_t, done) => {
const container = fixtures.path('output.dz.containeropt.zip'); const container = fixtures.path('output.dz.containeropt.zip');
const extractTo = fixtures.path('output.dz.containeropt'); const extractTo = fixtures.path('output.dz.containeropt');
const directory = path.join(extractTo, 'output.dz.containeropt_files'); const directory = path.join(extractTo, 'output.dz.containeropt_files');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
container: 'zip' container: 'zip'
}) })
.toFile(container, function (err, info) { .toFile(container, (err, info) => {
// Vips overrides .dzi extension to .zip used by container var below // Vips overrides .dzi extension to .zip used by container var below
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
@ -929,7 +927,7 @@ describe('Tile', function () {
assert.strictEqual(2225, info.height); assert.strictEqual(2225, info.height);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
assert.strictEqual('number', typeof info.size); assert.strictEqual('number', typeof info.size);
fs.stat(container, function (err, stat) { fs.stat(container, (err, stat) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.isFile()); assert.strictEqual(true, stat.isFile());
assert.strictEqual(true, stat.size > 0); assert.strictEqual(true, stat.size > 0);
@ -943,14 +941,14 @@ describe('Tile', function () {
}); });
}); });
it('Write ZIP container to Buffer', function (_t, done) { it('Write ZIP container to Buffer', (_t, done) => {
const container = fixtures.path('output.dz.tiles.zip'); const container = fixtures.path('output.dz.tiles.zip');
const extractTo = fixtures.path('output.dz.tiles'); const extractTo = fixtures.path('output.dz.tiles');
const directory = path.join(extractTo, 'output.dz.tiles_files'); const directory = path.join(extractTo, 'output.dz.tiles_files');
fs.rm(directory, { recursive: true }, function () { fs.rm(directory, { recursive: true }, () => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ basename: 'output.dz.tiles' }) .tile({ basename: 'output.dz.tiles' })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('dz', info.format); assert.strictEqual('dz', info.format);
assert.strictEqual(2725, info.width); assert.strictEqual(2725, info.width);
@ -958,7 +956,7 @@ describe('Tile', function () {
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
assert.strictEqual('number', typeof info.size); assert.strictEqual('number', typeof info.size);
fs.writeFileSync(container, data); fs.writeFileSync(container, data);
fs.stat(container, function (err, stat) { fs.stat(container, (err, stat) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.isFile()); assert.strictEqual(true, stat.isFile());
assert.strictEqual(true, stat.size > 0); assert.strictEqual(true, stat.size > 0);

View File

@ -9,7 +9,7 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Timeout', function () { describe('Timeout', () => {
it('Will timeout after 1s when performing slow blur operation', () => assert.rejects( it('Will timeout after 1s when performing slow blur operation', () => assert.rejects(
() => sharp(fixtures.inputJpg) () => sharp(fixtures.inputJpg)
.blur(200) .blur(200)

View File

@ -12,13 +12,13 @@ const fixtures = require('../fixtures');
// Allow for small rounding differences between platforms // Allow for small rounding differences between platforms
const maxDistance = 6; const maxDistance = 6;
describe('Tint', function () { describe('Tint', () => {
it('tints rgb image red', function (_t, done) { it('tints rgb image red', (_t, done) => {
const output = fixtures.path('output.tint-red.jpg'); const output = fixtures.path('output.tint-red.jpg');
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.tint('#FF0000') .tint('#FF0000')
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-red.jpg'), maxDistance); fixtures.assertMaxColourDistance(output, fixtures.expected('tint-red.jpg'), maxDistance);
@ -26,12 +26,12 @@ describe('Tint', function () {
}); });
}); });
it('tints rgb image green', function (_t, done) { it('tints rgb image green', (_t, done) => {
const output = fixtures.path('output.tint-green.jpg'); const output = fixtures.path('output.tint-green.jpg');
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.tint('#00FF00') .tint('#00FF00')
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-green.jpg'), maxDistance); fixtures.assertMaxColourDistance(output, fixtures.expected('tint-green.jpg'), maxDistance);
@ -39,12 +39,12 @@ describe('Tint', function () {
}); });
}); });
it('tints rgb image blue', function (_t, done) { it('tints rgb image blue', (_t, done) => {
const output = fixtures.path('output.tint-blue.jpg'); const output = fixtures.path('output.tint-blue.jpg');
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.tint('#0000FF') .tint('#0000FF')
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-blue.jpg'), maxDistance); fixtures.assertMaxColourDistance(output, fixtures.expected('tint-blue.jpg'), maxDistance);
@ -52,12 +52,12 @@ describe('Tint', function () {
}); });
}); });
it('tints rgb image with sepia tone', function (_t, done) { it('tints rgb image with sepia tone', (_t, done) => {
const output = fixtures.path('output.tint-sepia-hex.jpg'); const output = fixtures.path('output.tint-sepia-hex.jpg');
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.tint('#704214') .tint('#704214')
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -66,12 +66,12 @@ describe('Tint', function () {
}); });
}); });
it('tints rgb image with sepia tone with rgb colour', function (_t, done) { it('tints rgb image with sepia tone with rgb colour', (_t, done) => {
const output = fixtures.path('output.tint-sepia-rgb.jpg'); const output = fixtures.path('output.tint-sepia-rgb.jpg');
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.tint([112, 66, 20]) .tint([112, 66, 20])
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -80,12 +80,12 @@ describe('Tint', function () {
}); });
}); });
it('tints rgb image with alpha channel', function (_t, done) { it('tints rgb image with alpha channel', (_t, done) => {
const output = fixtures.path('output.tint-alpha.png'); const output = fixtures.path('output.tint-alpha.png');
sharp(fixtures.inputPngRGBWithAlpha) sharp(fixtures.inputPngRGBWithAlpha)
.resize(320, 240) .resize(320, 240)
.tint('#704214') .tint('#704214')
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
@ -94,12 +94,12 @@ describe('Tint', function () {
}); });
}); });
it('tints cmyk image red', function (_t, done) { it('tints cmyk image red', (_t, done) => {
const output = fixtures.path('output.tint-cmyk.jpg'); const output = fixtures.path('output.tint-cmyk.jpg');
sharp(fixtures.inputJpgWithCmykProfile) sharp(fixtures.inputJpgWithCmykProfile)
.resize(320, 240) .resize(320, 240)
.tint('#FF0000') .tint('#FF0000')
.toFile(output, function (err, info) { .toFile(output, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-cmyk.jpg'), maxDistance); fixtures.assertMaxColourDistance(output, fixtures.expected('tint-cmyk.jpg'), maxDistance);

View File

@ -10,8 +10,8 @@ const sharp = require('../../');
const inRange = require('../../lib/is').inRange; const inRange = require('../../lib/is').inRange;
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Trim borders', function () { describe('Trim borders', () => {
it('Skip shrink-on-load', function (_t, done) { it('Skip shrink-on-load', (_t, done) => {
const expected = fixtures.expected('alpha-layer-2-trim-resize.jpg'); const expected = fixtures.expected('alpha-layer-2-trim-resize.jpg');
sharp(fixtures.inputJpgOverlayLayer2) sharp(fixtures.inputJpgOverlayLayer2)
.trim() .trim()
@ -19,7 +19,7 @@ describe('Trim borders', function () {
width: 300, width: 300,
fastShrinkOnLoad: false fastShrinkOnLoad: false
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(300, info.width); assert.strictEqual(300, info.width);
@ -44,13 +44,13 @@ describe('Trim borders', function () {
}) })
); );
it('16-bit PNG with alpha channel', function (_t, done) { it('16-bit PNG with alpha channel', (_t, done) => {
sharp(fixtures.inputPngWithTransparency16bit) sharp(fixtures.inputPngWithTransparency16bit)
.resize(32, 32) .resize(32, 32)
.trim({ .trim({
threshold: 20 threshold: 20
}) })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -63,7 +63,7 @@ describe('Trim borders', function () {
}); });
}); });
it('Attempt to trim 2x2 pixel image fails', function (_t, done) { it('Attempt to trim 2x2 pixel image fails', (_t, done) => {
sharp({ sharp({
create: { create: {
width: 2, width: 2,
@ -211,7 +211,7 @@ describe('Trim borders', function () {
assert.strictEqual(info.trimOffsetTop, -552); assert.strictEqual(info.trimOffsetTop, -552);
}); });
describe('Invalid parameters', function () { describe('Invalid parameters', () => {
Object.entries({ Object.entries({
'Invalid string': 'fail', 'Invalid string': 'fail',
'Invalid background option': { 'Invalid background option': {
@ -223,16 +223,16 @@ describe('Trim borders', function () {
'Invalid lineArt': { 'Invalid lineArt': {
lineArt: 'fail' lineArt: 'fail'
} }
}).forEach(function ([description, parameter]) { }).forEach(([description, parameter]) => {
it(description, function () { it(description, () => {
assert.throws(function () { assert.throws(() => {
sharp().trim(parameter); sharp().trim(parameter);
}); });
}); });
}); });
}); });
describe('Specific background colour', function () { describe('Specific background colour', () => {
it('Doesn\'t trim at all', async () => { it('Doesn\'t trim at all', async () => {
const { info } = await sharp(fixtures.inputPngTrimSpecificColour) const { info } = await sharp(fixtures.inputPngTrimSpecificColour)
.trim({ .trim({

View File

@ -7,24 +7,24 @@ const { describe, it } = require('node:test');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Unflatten', function () { describe('Unflatten', () => {
it('unflatten white background', function (_t, done) { it('unflatten white background', (_t, done) => {
sharp(fixtures.inputPng).unflatten() sharp(fixtures.inputPng).unflatten()
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.expected('unflatten-white-transparent.png'), data, { threshold: 0 }, done); fixtures.assertSimilar(fixtures.expected('unflatten-white-transparent.png'), data, { threshold: 0 }, done);
}); });
}); });
it('unflatten transparent image', function (_t, done) { it('unflatten transparent image', (_t, done) => {
sharp(fixtures.inputPngTrimSpecificColourIncludeAlpha).unflatten() sharp(fixtures.inputPngTrimSpecificColourIncludeAlpha).unflatten()
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.expected('unflatten-flag-white-transparent.png'), data, { threshold: 0 }, done); fixtures.assertSimilar(fixtures.expected('unflatten-flag-white-transparent.png'), data, { threshold: 0 }, done);
}); });
}); });
it('unflatten using threshold', function (_t, done) { it('unflatten using threshold', (_t, done) => {
sharp(fixtures.inputPngPalette).unflatten().threshold(128, { grayscale: false }) sharp(fixtures.inputPngPalette).unflatten().threshold(128, { grayscale: false })
.toBuffer(function (err, data) { .toBuffer((err, data) => {
if (err) throw err; if (err) throw err;
fixtures.assertSimilar(fixtures.expected('unflatten-swiss.png'), data, { threshold: 1 }, done); fixtures.assertSimilar(fixtures.expected('unflatten-swiss.png'), data, { threshold: 1 }, done);
}); });

View File

@ -8,9 +8,9 @@ const assert = require('node:assert');
const semver = require('semver'); const semver = require('semver');
const sharp = require('../../'); const sharp = require('../../');
describe('Utilities', function () { describe('Utilities', () => {
describe('Cache', function () { describe('Cache', () => {
it('Can be disabled', function (_t, done) { it('Can be disabled', (_t, done) => {
const check = setInterval(() => { const check = setInterval(() => {
const cache = sharp.cache(false); const cache = sharp.cache(false);
const empty = const empty =
@ -26,13 +26,13 @@ describe('Utilities', function () {
} }
}, 2000); }, 2000);
}); });
it('Can be enabled with defaults', function () { it('Can be enabled with defaults', () => {
const cache = sharp.cache(true); const cache = sharp.cache(true);
assert.strictEqual(cache.memory.max, 50); assert.strictEqual(cache.memory.max, 50);
assert.strictEqual(cache.files.max, 20); assert.strictEqual(cache.files.max, 20);
assert.strictEqual(cache.items.max, 100); assert.strictEqual(cache.items.max, 100);
}); });
it('Can be set to zero', function () { it('Can be set to zero', () => {
const cache = sharp.cache({ const cache = sharp.cache({
memory: 0, memory: 0,
files: 0, files: 0,
@ -42,7 +42,7 @@ describe('Utilities', function () {
assert.strictEqual(cache.files.max, 0); assert.strictEqual(cache.files.max, 0);
assert.strictEqual(cache.items.max, 0); assert.strictEqual(cache.items.max, 0);
}); });
it('Can be set to a maximum of 10MB, 100 files and 1000 items', function () { it('Can be set to a maximum of 10MB, 100 files and 1000 items', () => {
const cache = sharp.cache({ const cache = sharp.cache({
memory: 10, memory: 10,
files: 100, files: 100,
@ -52,7 +52,7 @@ describe('Utilities', function () {
assert.strictEqual(cache.files.max, 100); assert.strictEqual(cache.files.max, 100);
assert.strictEqual(cache.items.max, 1000); assert.strictEqual(cache.items.max, 1000);
}); });
it('Ignores invalid values', function () { it('Ignores invalid values', () => {
sharp.cache(true); sharp.cache(true);
const cache = sharp.cache('spoons'); const cache = sharp.cache('spoons');
assert.strictEqual(cache.memory.max, 50); assert.strictEqual(cache.memory.max, 50);
@ -61,23 +61,23 @@ describe('Utilities', function () {
}); });
}); });
describe('Concurrency', function () { describe('Concurrency', () => {
it('Can be set to use 16 threads', function () { it('Can be set to use 16 threads', () => {
sharp.concurrency(16); sharp.concurrency(16);
assert.strictEqual(16, sharp.concurrency()); assert.strictEqual(16, sharp.concurrency());
}); });
it('Can be reset to default', function () { it('Can be reset to default', () => {
sharp.concurrency(0); sharp.concurrency(0);
assert.strictEqual(true, sharp.concurrency() > 0); assert.strictEqual(true, sharp.concurrency() > 0);
}); });
it('Ignores invalid values', function () { it('Ignores invalid values', () => {
const defaultConcurrency = sharp.concurrency(); const defaultConcurrency = sharp.concurrency();
sharp.concurrency('spoons'); sharp.concurrency('spoons');
assert.strictEqual(defaultConcurrency, sharp.concurrency()); assert.strictEqual(defaultConcurrency, sharp.concurrency());
}); });
}); });
describe('Counters', function () { describe('Counters', () => {
it('Have zero value at rest', (_t, done) => { it('Have zero value at rest', (_t, done) => {
queueMicrotask(() => { queueMicrotask(() => {
const counters = sharp.counters(); const counters = sharp.counters();
@ -88,28 +88,28 @@ describe('Utilities', function () {
}); });
}); });
describe('SIMD', function () { describe('SIMD', () => {
it('Can get current state', function () { it('Can get current state', () => {
const simd = sharp.simd(); const simd = sharp.simd();
assert.strictEqual(typeof simd, 'boolean'); assert.strictEqual(typeof simd, 'boolean');
}); });
it('Can disable', function () { it('Can disable', () => {
const simd = sharp.simd(false); const simd = sharp.simd(false);
assert.strictEqual(simd, false); assert.strictEqual(simd, false);
}); });
it('Can attempt to enable', function () { it('Can attempt to enable', () => {
const simd = sharp.simd(true); const simd = sharp.simd(true);
assert.strictEqual(typeof simd, 'boolean'); assert.strictEqual(typeof simd, 'boolean');
}); });
}); });
describe('Format', function () { describe('Format', () => {
it('Contains expected attributes', function () { it('Contains expected attributes', () => {
assert.strictEqual('object', typeof sharp.format); assert.strictEqual('object', typeof sharp.format);
Object.keys(sharp.format).forEach(function (format) { Object.keys(sharp.format).forEach((format) => {
assert.strictEqual(true, 'id' in sharp.format[format]); assert.strictEqual(true, 'id' in sharp.format[format]);
assert.strictEqual(format, sharp.format[format].id); assert.strictEqual(format, sharp.format[format].id);
['input', 'output'].forEach(function (direction) { ['input', 'output'].forEach((direction) => {
assert.strictEqual(true, direction in sharp.format[format]); assert.strictEqual(true, direction in sharp.format[format]);
assert.strictEqual('object', typeof sharp.format[format][direction]); assert.strictEqual('object', typeof sharp.format[format][direction]);
assert.strictEqual(true, [3, 4].includes(Object.keys(sharp.format[format][direction]).length)); assert.strictEqual(true, [3, 4].includes(Object.keys(sharp.format[format][direction]).length));
@ -122,30 +122,30 @@ describe('Utilities', function () {
}); });
}); });
}); });
it('Raw file=false, buffer=true, stream=true', function () { it('Raw file=false, buffer=true, stream=true', () => {
['input', 'output'].forEach(function (direction) { ['input', 'output'].forEach((direction) => {
assert.strictEqual(false, sharp.format.raw[direction].file); assert.strictEqual(false, sharp.format.raw[direction].file);
assert.strictEqual(true, sharp.format.raw[direction].buffer); assert.strictEqual(true, sharp.format.raw[direction].buffer);
assert.strictEqual(true, sharp.format.raw[direction].stream); assert.strictEqual(true, sharp.format.raw[direction].stream);
}); });
}); });
it('vips format supports filesystem only', function () { it('vips format supports filesystem only', () => {
['input', 'output'].forEach(function (direction) { ['input', 'output'].forEach((direction) => {
assert.strictEqual(true, sharp.format.vips[direction].file); assert.strictEqual(true, sharp.format.vips[direction].file);
assert.strictEqual(false, sharp.format.vips[direction].buffer); assert.strictEqual(false, sharp.format.vips[direction].buffer);
assert.strictEqual(false, sharp.format.vips[direction].stream); assert.strictEqual(false, sharp.format.vips[direction].stream);
}); });
}); });
it('input fileSuffix', function () { it('input fileSuffix', () => {
assert.deepStrictEqual(['.jpg', '.jpeg', '.jpe', '.jfif'], sharp.format.jpeg.input.fileSuffix); assert.deepStrictEqual(['.jpg', '.jpeg', '.jpe', '.jfif'], sharp.format.jpeg.input.fileSuffix);
}); });
it('output alias', function () { it('output alias', () => {
assert.deepStrictEqual(['jpe', 'jpg'], sharp.format.jpeg.output.alias); assert.deepStrictEqual(['jpe', 'jpg'], sharp.format.jpeg.output.alias);
}); });
}); });
describe('Versions', function () { describe('Versions', () => {
it('Contains expected attributes', function () { it('Contains expected attributes', () => {
assert.strictEqual('object', typeof sharp.versions); assert.strictEqual('object', typeof sharp.versions);
assert(semver.valid(sharp.versions.vips)); assert(semver.valid(sharp.versions.vips));
assert(semver.valid(sharp.versions.sharp)); assert(semver.valid(sharp.versions.sharp));

View File

@ -10,12 +10,12 @@ const assert = require('node:assert');
const sharp = require('../../'); const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('WebP', function () { describe('WebP', () => {
it('WebP output', function (_t, done) { it('WebP output', (_t, done) => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(320, 240) .resize(320, 240)
.toFormat(sharp.format.webp) .toFormat(sharp.format.webp)
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
@ -25,22 +25,22 @@ describe('WebP', function () {
}); });
}); });
it('Invalid WebP quality throws error', function () { it('Invalid WebP quality throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().webp({ quality: 101 }); sharp().webp({ quality: 101 });
}); });
}); });
it('Invalid WebP alpha quality throws error', function () { it('Invalid WebP alpha quality throws error', () => {
assert.throws(function () { assert.throws(() => {
sharp().webp({ alphaQuality: 101 }); sharp().webp({ alphaQuality: 101 });
}); });
}); });
it('should work for webp alpha quality', function (_t, done) { it('should work for webp alpha quality', (_t, done) => {
sharp(fixtures.inputPngAlphaPremultiplicationSmall) sharp(fixtures.inputPngAlphaPremultiplicationSmall)
.webp({ alphaQuality: 80, effort: 0 }) .webp({ alphaQuality: 80, effort: 0 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
@ -48,10 +48,10 @@ describe('WebP', function () {
}); });
}); });
it('should work for webp lossless', function (_t, done) { it('should work for webp lossless', (_t, done) => {
sharp(fixtures.inputPngAlphaPremultiplicationSmall) sharp(fixtures.inputPngAlphaPremultiplicationSmall)
.webp({ lossless: true, effort: 0 }) .webp({ lossless: true, effort: 0 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
@ -59,10 +59,10 @@ describe('WebP', function () {
}); });
}); });
it('should work for webp near-lossless', function (_t, done) { it('should work for webp near-lossless', (_t, done) => {
sharp(fixtures.inputPngAlphaPremultiplicationSmall) sharp(fixtures.inputPngAlphaPremultiplicationSmall)
.webp({ nearLossless: true, quality: 50, effort: 0 }) .webp({ nearLossless: true, quality: 50, effort: 0 })
.toBuffer(function (err50, data50, info50) { .toBuffer((err50, data50, info50) => {
if (err50) throw err50; if (err50) throw err50;
assert.strictEqual(true, data50.length > 0); assert.strictEqual(true, data50.length > 0);
assert.strictEqual('webp', info50.format); assert.strictEqual('webp', info50.format);
@ -70,10 +70,10 @@ describe('WebP', function () {
}); });
}); });
it('should use near-lossless when both lossless and nearLossless are specified', function (_t, done) { it('should use near-lossless when both lossless and nearLossless are specified', (_t, done) => {
sharp(fixtures.inputPngAlphaPremultiplicationSmall) sharp(fixtures.inputPngAlphaPremultiplicationSmall)
.webp({ nearLossless: true, quality: 50, lossless: true, effort: 0 }) .webp({ nearLossless: true, quality: 50, lossless: true, effort: 0 })
.toBuffer(function (err50, data50, info50) { .toBuffer((err50, data50, info50) => {
if (err50) throw err50; if (err50) throw err50;
assert.strictEqual(true, data50.length > 0); assert.strictEqual(true, data50.length > 0);
assert.strictEqual('webp', info50.format); assert.strictEqual('webp', info50.format);
@ -272,11 +272,11 @@ describe('WebP', function () {
assert.deepStrictEqual(updated.delay, [120, 120, 90, 120, 120, 90, 120, 90, 30]); assert.deepStrictEqual(updated.delay, [120, 120, 90, 120, 120, 90, 120, 90, 30]);
}); });
it('should work with streams when only animated is set', function (_t, done) { it('should work with streams when only animated is set', (_t, done) => {
fs.createReadStream(fixtures.inputWebPAnimated) fs.createReadStream(fixtures.inputWebPAnimated)
.pipe(sharp({ animated: true })) .pipe(sharp({ animated: true }))
.webp({ lossless: true, effort: 0 }) .webp({ lossless: true, effort: 0 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
@ -284,11 +284,11 @@ describe('WebP', function () {
}); });
}); });
it('should work with streams when only pages is set', function (_t, done) { it('should work with streams when only pages is set', (_t, done) => {
fs.createReadStream(fixtures.inputWebPAnimated) fs.createReadStream(fixtures.inputWebPAnimated)
.pipe(sharp({ pages: -1 })) .pipe(sharp({ pages: -1 }))
.webp({ lossless: true, effort: 0 }) .webp({ lossless: true, effort: 0 })
.toBuffer(function (err, data, info) { .toBuffer((err, data, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);