Skip to content

Commit d161e45

Browse files
committed
TypeScript: Ensure 'FormatEnum' keys match reality #4475
Renames format.jp2k as format.jp2 for consistency
1 parent 006d37b commit d161e45

File tree

7 files changed

+47
-15
lines changed

7 files changed

+47
-15
lines changed

docs/src/content/docs/changelog/v0.35.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ slug: changelog/v0.35.0
1616

1717
* Breaking: Remove deprecated properties from `sharpen` operation.
1818

19+
* Breaking: Rename `format.jp2k` as `format.jp2` for API consistency.
20+
1921
* Upgrade to libvips v8.18.0 for upstream bug fixes.
2022

2123
* Deprecate Windows 32-bit (win32-ia32) prebuilt binaries.
@@ -29,4 +31,7 @@ slug: changelog/v0.35.0
2931
* Add `toUint8Array` for output image as a `TypedArray` backed by a transferable `ArrayBuffer`.
3032
[#4355](https://github.com/lovell/sharp/issues/4355)
3133

34+
* TypeScript: Ensure `FormatEnum` keys match reality.
35+
[#4475](https://github.com/lovell/sharp/issues/4475)
36+
3237
* Add WebP `exact` option for control over transparent pixel colour values.

lib/index.d.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ declare namespace sharp {
840840
* @returns A sharp instance that can be used to chain operations
841841
*/
842842
toFormat(
843-
format: keyof FormatEnum | AvailableFormatInfo,
843+
format: keyof FormatEnum | AvailableFormatInfo | "avif",
844844
options?:
845845
| OutputOptions
846846
| JpegOptions
@@ -1911,16 +1911,13 @@ declare namespace sharp {
19111911
}
19121912

19131913
interface FormatEnum {
1914-
avif: AvailableFormatInfo;
19151914
dcraw: AvailableFormatInfo;
19161915
dz: AvailableFormatInfo;
19171916
exr: AvailableFormatInfo;
19181917
fits: AvailableFormatInfo;
19191918
gif: AvailableFormatInfo;
19201919
heif: AvailableFormatInfo;
1921-
input: AvailableFormatInfo;
19221920
jpeg: AvailableFormatInfo;
1923-
jpg: AvailableFormatInfo;
19241921
jp2: AvailableFormatInfo;
19251922
jxl: AvailableFormatInfo;
19261923
magick: AvailableFormatInfo;
@@ -1932,8 +1929,7 @@ declare namespace sharp {
19321929
raw: AvailableFormatInfo;
19331930
svg: AvailableFormatInfo;
19341931
tiff: AvailableFormatInfo;
1935-
tif: AvailableFormatInfo;
1936-
v: AvailableFormatInfo;
1932+
vips: AvailableFormatInfo;
19371933
webp: AvailableFormatInfo;
19381934
}
19391935

lib/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function toFile (fileOut, callback) {
7676
err = new Error('Missing output file path');
7777
} else if (is.string(this.options.input.file) && path.resolve(this.options.input.file) === path.resolve(fileOut)) {
7878
err = new Error('Cannot use same file for input and output');
79-
} else if (jp2Regex.test(path.extname(fileOut)) && !this.constructor.format.jp2k.output.file) {
79+
} else if (jp2Regex.test(path.extname(fileOut)) && !this.constructor.format.jp2.output.file) {
8080
err = errJp2Save();
8181
}
8282
if (err) {
@@ -950,7 +950,7 @@ function gif (options) {
950950
*/
951951
function jp2 (options) {
952952
/* node:coverage ignore next 41 */
953-
if (!this.constructor.format.jp2k.output.buffer) {
953+
if (!this.constructor.format.jp2.output.buffer) {
954954
throw errJp2Save();
955955
}
956956
if (is.object(options)) {

lib/utility.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const format = sharp.format();
2424
format.heif.output.alias = ['avif', 'heic'];
2525
format.jpeg.output.alias = ['jpe', 'jpg'];
2626
format.tiff.output.alias = ['tif'];
27-
format.jp2k.output.alias = ['j2c', 'j2k', 'jp2', 'jpx'];
27+
format.jp2.output.alias = ['j2c', 'j2k', 'jp2', 'jpx'];
2828

2929
/**
3030
* An Object containing the available interpolators and their proper values

src/utilities.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Napi::Value format(const Napi::CallbackInfo& info) {
123123
"jpeg", "png", "webp", "tiff", "magick", "openslide", "dz",
124124
"ppm", "fits", "gif", "svg", "heif", "pdf", "vips", "jp2k", "jxl", "rad", "dcraw"
125125
}) {
126+
std::string id = f == "jp2k" ? "jp2" : f;
126127
// Input
127128
const VipsObjectClass *oc = vips_class_find("VipsOperation", (f + "load").c_str());
128129
Napi::Boolean hasInputFile = Napi::Boolean::New(env, oc);
@@ -154,11 +155,11 @@ Napi::Value format(const Napi::CallbackInfo& info) {
154155
output.Set("stream", hasOutputBuffer);
155156
// Other attributes
156157
Napi::Object container = Napi::Object::New(env);
157-
container.Set("id", f);
158+
container.Set("id", id);
158159
container.Set("input", input);
159160
container.Set("output", output);
160161
// Add to set of formats
161-
format.Set(f, container);
162+
format.Set(id, container);
162163
}
163164

164165
// Raw, uncompressed data

test/types/sharp.test-d.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ sharp(input)
234234

235235
sharp(input)
236236
.resize(100, 100)
237-
.toFormat('jpg')
237+
.toFormat('avif')
238238
.toBuffer({ resolveWithObject: false })
239239
.then((outputBuffer: Buffer) => {
240240
// Resolves with a Buffer object when resolveWithObject is false
@@ -267,9 +267,7 @@ sharp(input)
267267
// Output to tif
268268
sharp(input)
269269
.resize(100, 100)
270-
.toFormat('tif')
271270
.toFormat('tiff')
272-
.toFormat(sharp.format.tif)
273271
.toFormat(sharp.format.tiff)
274272
.toBuffer();
275273

@@ -774,3 +772,35 @@ sharp().erode();
774772
sharp().erode(1);
775773
sharp().dilate();
776774
sharp().dilate(1);
775+
776+
sharp.format.dcraw;
777+
sharp.format.dz;
778+
sharp.format.fits;
779+
sharp.format.gif;
780+
sharp.format.heif;
781+
sharp.format.jp2;
782+
sharp.format.jpeg;
783+
sharp.format.jxl;
784+
sharp.format.magick;
785+
sharp.format.openslide;
786+
sharp.format.pdf;
787+
sharp.format.png;
788+
sharp.format.ppm;
789+
sharp.format.rad;
790+
sharp.format.raw;
791+
sharp.format.svg;
792+
sharp.format.tiff;
793+
sharp.format.vips;
794+
sharp.format.webp;
795+
// @ts-expect-error
796+
sharp.format.avif;
797+
// @ts-expect-error
798+
sharp.format.input;
799+
// @ts-expect-error
800+
sharp.format.jp2k;
801+
// @ts-expect-error
802+
sharp.format.jpg;
803+
// @ts-expect-error
804+
sharp.format.tif;
805+
// @ts-expect-error
806+
sharp.format.v;

test/unit/jp2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const sharp = require('../../');
1111
const fixtures = require('../fixtures');
1212

1313
describe('JP2 output', () => {
14-
if (!sharp.format.jp2k.input.buffer) {
14+
if (!sharp.format.jp2.input.buffer) {
1515
it('JP2 output should fail due to missing OpenJPEG', () =>
1616
assert.rejects(async () =>
1717
sharp(fixtures.inputJpg)

0 commit comments

Comments
 (0)