Skip to content

Commit edd908f

Browse files
committed
check enum options with oneOf
1 parent 7818414 commit edd908f

3 files changed

Lines changed: 17 additions & 17 deletions

File tree

‎src/animation.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as jxl from './codecs/jxl.ts';
66
import { availableThreads } from './util/threads.ts';
77
import { BinaryData, view } from './util/mem.ts';
88
import { GainMap } from './gainmap.ts';
9-
import { Image, DecodeOptions, EncodeOptions, GIFEncodeOptions, ResizeOptions, ResizeToOptions, RotateOptions, TransformOptions, AffineTransform, ShadowOptions, OutlineOptions, TrimOptions, resizeArguments, insideSize, alignedPixels, PNGEncodeOptions, WebPEncodeOptions, FlipDirection, FLIP_DIRECTIONS } from './image.ts';
9+
import { Image, DecodeOptions, EncodeOptions, GIFEncodeOptions, ResizeOptions, ResizeToOptions, RotateOptions, TransformOptions, AffineTransform, ShadowOptions, OutlineOptions, TrimOptions, resizeArguments, insideSize, alignedPixels, PNGEncodeOptions, WebPEncodeOptions, FlipDirection, FLIP_DIRECTIONS, TONE_MAPPINGS } from './image.ts';
1010
import { Frame } from './frame.ts';
1111
import { ImageType } from './image-type.ts';
1212
import { optimize, OptimizeOptions, OptimizedFrame } from './util/optimize.ts';
@@ -656,7 +656,7 @@ export class Animation {
656656
if (!Number.isInteger(threads) || threads < 1) throw new RangeError(`threads must be a positive integer, not ${threads}`);
657657
if (!(Number(maxPixels) > 0)) throw new RangeError(`maxPixels must be a positive number, not ${maxPixels}`);
658658
const { toneMapping = 'reinhard' } = options;
659-
if (toneMapping !== 'reinhard' && toneMapping !== 'clamp') throw new TypeError(`toneMapping must be 'reinhard' or 'clamp', not ${String(toneMapping)}`);
659+
oneOf(toneMapping, TONE_MAPPINGS, 'toneMapping');
660660
return decoding(() => Animation.__decode__(input, options));
661661
}
662662

‎src/image.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ export type TransformMode = 'nearest-neighbor' | 'bilinear' | 'bicubic';
180180
export type AffineTransform = readonly [a: number, b: number, c: number, d: number, e: number, f: number];
181181

182182
// the sampling modes of Image.transform()
183-
const TRANSFORM_MODES: readonly string[] = ['nearest-neighbor', 'bilinear', 'bicubic'];
183+
const TRANSFORM_MODES: readonly TransformMode[] = ['nearest-neighbor', 'bilinear', 'bicubic'];
184+
const RESIZE_FITS: readonly ResizeFit[] = ['fill', 'inside', 'contain', 'cover'];
185+
const SUBSAMPLINGS: readonly string[] = ['4:4:4', '4:2:0'];
186+
/** @internal */
187+
export const TONE_MAPPINGS: readonly ToneMapping[] = ['reinhard', 'clamp'];
184188
/** @internal */
185189
export const FLIP_DIRECTIONS: readonly FlipDirection[] = ['horizontal', 'vertical'];
186190

@@ -1751,7 +1755,6 @@ export class Image {
17511755
*/
17521756
quantize(options: QuantizeOptions = {}): this {
17531757
checkOptions(options, 'image.quantize({ colors: 16 })');
1754-
if (typeof options !== 'object' || options === null) throw new TypeError('quantize takes an options object');
17551758
// (JavaScript callers can give both: checked below)
17561759
const { colors, palette, dither = 'floyd-steinberg', ditherStrength = 0.85 } = options as DitherOptions & { colors?: number, palette?: Color[] };
17571760

@@ -1844,9 +1847,8 @@ export class Image {
18441847
if (matrix === null || typeof matrix !== 'object' || matrix.length !== 6) throw new RangeError('A transform has 6 values: [a, b, c, d, e, f]');
18451848
// (numbers only: a string is a mistake in data like this)
18461849
const values = Array.from({ length: 6 }, (_, i) => finite(typeof matrix[i] === 'number' ? matrix[i] : NaN, 'transform value'));
1847-
if (typeof options !== 'object' || options === null) throw new TypeError('transform takes an options object');
18481850
const { origin = {}, expand = true, mode = 'bilinear' } = options;
1849-
if (!TRANSFORM_MODES.includes(mode)) throw new TypeError(`transform mode must be one of ${TRANSFORM_MODES.map(name => `'${name}'`).join(', ')}, not ${String(mode)}`);
1851+
oneOf(mode, TRANSFORM_MODES, 'transform mode');
18501852
const { x: pivotX = this.width / 2, y: pivotY = this.height / 2 } = origin;
18511853
finite(pivotX, 'origin x');
18521854
finite(pivotY, 'origin y');
@@ -2205,7 +2207,7 @@ export class Image {
22052207
rotate(angle: number, options: RotateOptions = {}): this {
22062208
checkOptions(options, 'rotate(angle, { expand: false })');
22072209
const { expand = true, mode = 'bilinear' } = options;
2208-
if (!TRANSFORM_MODES.includes(mode)) throw new TypeError(`rotate mode must be one of ${TRANSFORM_MODES.map(name => `'${name}'`).join(', ')}, not ${String(mode)}`);
2210+
oneOf(mode, TRANSFORM_MODES, 'rotate mode');
22092211
// (counterclockwise from here on: the image's y axis points down, so this turns it clockwise on screen)
22102212
angle = ((-finite(angle, 'angle') % 360) + 360) % 360;
22112213
if (angle === 0) return this;
@@ -2448,7 +2450,7 @@ export class Image {
24482450
checkOptions(options, 'encodeJPEG({ quality: 80 })');
24492451
const { quality: qualityOption = 90, subsampling, background = 0x000000ff, metadata = {}, gainMap: gainMapOption = true } = options;
24502452
const quality = checkQuality(qualityOption);
2451-
if (subsampling !== undefined && subsampling !== '4:4:4' && subsampling !== '4:2:0') throw new TypeError(`subsampling must be '4:4:4' or '4:2:0', not ${String(subsampling)}`);
2453+
if (subsampling !== undefined) oneOf(subsampling, SUBSAMPLINGS, 'subsampling');
24522454
const backgroundColor = checkColor(background, 'background');
24532455
const m = resolveMetadata(metadata);
24542456
const base = { exif: exifData(m), comment: m.comment ? utf8(m.comment) : undefined, subsampling };
@@ -2554,7 +2556,7 @@ export class Image {
25542556
const { quality = 60, alphaQuality = 100, subsampling = '4:4:4', effort = 1, threads = defaultThreads(), metadata = {}, gainMap = true } = options;
25552557
checkEffort(effort, 2);
25562558
checkThreads(threads);
2557-
if (subsampling !== '4:4:4' && subsampling !== '4:2:0') throw new TypeError(`subsampling must be '4:4:4' or '4:2:0', not ${String(subsampling)}`);
2559+
oneOf(subsampling, SUBSAMPLINGS, 'subsampling');
25582560
const exif = exifData(resolveMetadata(metadata));
25592561
return avifEnc.encode(this.__bytes__(), this.width, this.height, {
25602562
quality: checkQuality(quality), alphaQuality: checkQuality(alphaQuality, 'alphaQuality'), subsampling, effort, exif, threads, gainMap: this.__gain_map_pixels__(gainMap)
@@ -2628,7 +2630,7 @@ export class Image {
26282630
const { applyOrientation = true, applyColorProfile = true, maxPixels = MAX_PIXELS, toneMapping = 'reinhard', threads = defaultThreads(), gainMap = true } = options;
26292631
checkThreads(threads);
26302632
if (!(Number(maxPixels) > 0)) throw new RangeError(`maxPixels must be a positive number, not ${maxPixels}`);
2631-
if (toneMapping !== 'reinhard' && toneMapping !== 'clamp') throw new TypeError(`toneMapping must be 'reinhard' or 'clamp', not ${String(toneMapping)}`);
2633+
oneOf(toneMapping, TONE_MAPPINGS, 'toneMapping');
26322634
const data = view(input);
26332635
const dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
26342636

@@ -2973,7 +2975,7 @@ export function resizeArguments(widthOrOptions: number | ResizeToOptions, height
29732975
}
29742976
const { width, height: optionsHeight, fit = 'fill', ...rest } = widthOrOptions;
29752977
if (width === undefined && optionsHeight === undefined) throw new TypeError('resize() needs a width, a height or both');
2976-
if (!['fill', 'inside', 'contain', 'cover'].includes(fit)) throw new TypeError(`fit must be 'fill', 'inside', 'contain' or 'cover', not ${String(fit)}`);
2978+
oneOf(fit, RESIZE_FITS, 'fit');
29772979
return [width === undefined ? undefined : finite(width, 'width'), optionsHeight === undefined ? undefined : finite(optionsHeight, 'height'), rest, fit];
29782980
}
29792981

‎src/text-layout.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export interface TextLayoutOptions {
3434
verticalAlign?: VerticalAlign;
3535
}
3636

37-
const HORIZONTAL: readonly string[] = ['left', 'center', 'right'];
38-
const VERTICAL: readonly string[] = ['top', 'middle', 'bottom'];
37+
const HORIZONTAL: readonly HorizontalAlign[] = ['left', 'center', 'right'];
38+
const VERTICAL: readonly VerticalAlign[] = ['top', 'middle', 'bottom'];
3939

4040
/** A size option: undefined, or a whole number of pixels from 1 */
4141
function boxSide(value: number | undefined, name: string): number | undefined {
@@ -51,13 +51,11 @@ function boxSide(value: number | undefined, name: string): number | undefined {
5151
*/
5252
export function textLayout(options: TextLayoutOptions): Layout {
5353
const { width, height, wrapStyle, verticalAlign, horizontalAlign } = options;
54-
if (horizontalAlign !== undefined && !HORIZONTAL.includes(horizontalAlign)) throw new TypeError(`horizontalAlign must be 'left', 'center' or 'right', not '${horizontalAlign}'`);
55-
if (verticalAlign !== undefined && !VERTICAL.includes(verticalAlign)) throw new TypeError(`verticalAlign must be 'top', 'middle' or 'bottom', not '${verticalAlign}'`);
5654
return {
5755
width: boxSide(width, 'width'),
5856
height: boxSide(height, 'height'),
5957
wrapStyle: wrapStyle === undefined ? 'word' : oneOf(wrapStyle, ['word', 'char'], 'wrapStyle'),
60-
horizontalAlign: horizontalAlign ?? 'left',
61-
verticalAlign: verticalAlign ?? 'top'
58+
horizontalAlign: horizontalAlign === undefined ? 'left' : oneOf(horizontalAlign, HORIZONTAL, 'horizontalAlign'),
59+
verticalAlign: verticalAlign === undefined ? 'top' : oneOf(verticalAlign, VERTICAL, 'verticalAlign')
6260
};
6361
}

0 commit comments

Comments
 (0)