Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit 7e22b9c

Browse files
committed
feat: make encode types easier to use
1 parent 2aa4b63 commit 7e22b9c

File tree

2 files changed

+7
-42
lines changed

2 files changed

+7
-42
lines changed

src/save/encode.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,9 @@ export interface EncodeOptionsJpeg {
2323
const defaultPng: EncodeOptionsPng = { format: 'png' };
2424

2525
/**
26-
* Encodes the image to an output format.
27-
* Defaults to PNG.
26+
* Encodes the image to the specified format
2827
* @param image - Image to encode.
29-
* @returns The encoded image.
30-
*/
31-
export function encode(image: Image): Uint8Array;
32-
/**
33-
* Encodes the image to PNG.
34-
* @param image - Image to encode.
35-
* @param options - Format and options passed to the PNG encoder.
36-
* @returns The encoded image.
37-
*/
38-
export function encode(image: Image, options: EncodeOptionsPng): Uint8Array;
39-
/**
40-
* Encodes the image to JPEG.
41-
* @param image - Image to encode.
42-
* @param options - Format and options passed to the JPEG encoder.
43-
* @returns The encoded image.
44-
*/
45-
export function encode(image: Image, options: EncodeOptionsJpeg): Uint8Array;
46-
/**
47-
* Encode an image in JPEG or PNG format.
48-
* @param image - Image to encode.
49-
* @param options - Encoding options.
28+
* @param options - Format and options passed to the encoder.
5029
* @returns The encoded image.
5130
*/
5231
export function encode(

src/save/write.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ import fs from 'node:fs';
22
import nodePath from 'node:path';
33
import url from 'node:url';
44

5-
import { Mask, Image } from '..';
5+
import { Image, Mask } from '..';
66

7-
import {
8-
encode,
9-
ImageFormat,
10-
EncodeOptionsPng,
11-
EncodeOptionsJpeg,
12-
} from './encode';
7+
import { encode, EncodeOptionsJpeg, EncodeOptionsPng } from './encode';
138

149
export interface WriteOptions {
1510
/**
@@ -123,25 +118,16 @@ function getDataToWrite(
123118
image: Image,
124119
options?: WriteOptionsPng | WriteOptionsJpeg | WriteOptions,
125120
): Uint8Array {
126-
let format: ImageFormat;
127121
if (!options || !('format' in options)) {
128122
const extension = nodePath.extname(destinationPath).slice(1).toLowerCase();
129-
if (extension === 'png') {
130-
format = 'png';
131-
return encode(image, { format });
132-
} else if (extension === 'jpg' || extension === 'jpeg') {
133-
format = 'jpg';
134-
return encode(image, { format });
123+
if (extension === 'png' || extension === 'jpg' || extension === 'jpeg') {
124+
return encode(image, { format: extension });
135125
} else {
136126
throw new RangeError(
137127
'image format could not be determined from file extension. Use a supported extension or specify the format option',
138128
);
139129
}
140-
} else if (options.format === 'png') {
141-
return encode(image, options);
142-
} else if (options.format === 'jpg' || options.format === 'jpeg') {
143-
return encode(image, options);
144130
} else {
145-
throw new RangeError(`invalid format: ${options.format}`);
131+
return encode(image, options);
146132
}
147133
}

0 commit comments

Comments
 (0)