Skip to content

Commit

Permalink
feat: make encode types easier to use
Browse files Browse the repository at this point in the history
  • Loading branch information
stropitek committed Feb 21, 2024
1 parent 2aa4b63 commit 0446468
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 42 deletions.
25 changes: 2 additions & 23 deletions src/save/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,9 @@ export interface EncodeOptionsJpeg {
const defaultPng: EncodeOptionsPng = { format: 'png' };

/**
* Encodes the image to an output format.
* Defaults to PNG.
* Encodes the image to the specified format
* @param image - Image to encode.
* @returns The encoded image.
*/
export function encode(image: Image): Uint8Array;
/**
* Encodes the image to PNG.
* @param image - Image to encode.
* @param options - Format and options passed to the PNG encoder.
* @returns The encoded image.
*/
export function encode(image: Image, options: EncodeOptionsPng): Uint8Array;
/**
* Encodes the image to JPEG.
* @param image - Image to encode.
* @param options - Format and options passed to the JPEG encoder.
* @returns The encoded image.
*/
export function encode(image: Image, options: EncodeOptionsJpeg): Uint8Array;
/**
* Encode an image in JPEG or PNG format.
* @param image - Image to encode.
* @param options - Encoding options.
* @param options - Format and options passed to the encoder.
* @returns The encoded image.
*/
export function encode(
Expand Down
24 changes: 5 additions & 19 deletions src/save/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import fs from 'node:fs';
import nodePath from 'node:path';
import url from 'node:url';

import { Mask, Image } from '..';
import { Image, Mask } from '..';

import {
encode,
ImageFormat,
EncodeOptionsPng,
EncodeOptionsJpeg,
} from './encode';
import { encode, EncodeOptionsJpeg, EncodeOptionsPng } from './encode';

export interface WriteOptions {
/**
Expand Down Expand Up @@ -123,25 +118,16 @@ function getDataToWrite(
image: Image,
options?: WriteOptionsPng | WriteOptionsJpeg | WriteOptions,
): Uint8Array {
let format: ImageFormat;
if (!options || !('format' in options)) {
const extension = nodePath.extname(destinationPath).slice(1).toLowerCase();
if (extension === 'png') {
format = 'png';
return encode(image, { format });
} else if (extension === 'jpg' || extension === 'jpeg') {
format = 'jpg';
return encode(image, { format });
if (extension === 'png' || extension === 'jpg' || extension === 'jpeg') {
return encode(image, { format: extension });
} else {
throw new RangeError(
'image format could not be determined from file extension. Use a supported extension or specify the format option',
);
}
} else if (options.format === 'png') {
return encode(image, options);
} else if (options.format === 'jpg' || options.format === 'jpeg') {
return encode(image, options);
} else {
throw new RangeError(`invalid format: ${options.format}`);
return encode(image, options);
}
}

0 comments on commit 0446468

Please sign in to comment.