Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fallback values when options object contains explicit undefined values #772

Merged
merged 2 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/signature_pad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,15 @@ export default class SignaturePad extends SignatureEventTarget {
this.velocityFilterWeight = options.velocityFilterWeight || 0.7;
this.minWidth = options.minWidth || 0.5;
this.maxWidth = options.maxWidth || 2.5;
this.throttle = ('throttle' in options ? options.throttle : 16) as number; // in milliseconds
this.minDistance = (
'minDistance' in options ? options.minDistance : 5
) as number; // in pixels

// We need to handle 0 value, so use `??` instead of `||`
this.throttle = options.throttle ?? 16; // in milliseconds
this.minDistance = options.minDistance ?? 5; // in pixels
this.dotSize = options.dotSize || 0;
this.penColor = options.penColor || 'black';
this.backgroundColor = options.backgroundColor || 'rgba(0,0,0,0)';
this.compositeOperation = options.compositeOperation || 'source-over';
this.canvasContextOptions = (
'canvasContextOptions' in options ? options.canvasContextOptions : {}
) as CanvasRenderingContext2DSettings;
this.canvasContextOptions = options.canvasContextOptions ?? {};

this._strokeMoveUpdate = this.throttle
? throttle(SignaturePad.prototype._strokeUpdate, this.throttle)
Expand Down
46 changes: 46 additions & 0 deletions tests/signature_pad.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SignaturePad from '../src/signature_pad';
import type { Options } from '../src/signature_pad';
import { face } from './fixtures/face';
import { square } from './fixtures/square';
import './utils/pointer-event-polyfill';
Expand Down Expand Up @@ -37,6 +38,51 @@ describe('#constructor', () => {

expect(pad.minDistance).toBe(0);
});

it("uses fallback values for options with explicit 'undefined'", () => {
const opts: Options = {
dotSize: undefined,
minWidth: undefined,
maxWidth: undefined,
penColor: undefined,
velocityFilterWeight: undefined,
compositeOperation: undefined,
minDistance: undefined,
backgroundColor: undefined,
throttle: undefined,
canvasContextOptions: undefined,
};

const exp: Options = {
dotSize: 0,
minWidth: 0.5,
maxWidth: 2.5,
penColor: 'black',
velocityFilterWeight: 0.7,
compositeOperation: 'source-over',
minDistance: 5,
backgroundColor: 'rgba(0,0,0,0)',
throttle: 16,
canvasContextOptions: {},
};

const pad = new SignaturePad(canvas, opts);

const actual = {
dotSize: pad.dotSize,
minWidth: pad.minWidth,
maxWidth: pad.maxWidth,
penColor: pad.penColor,
velocityFilterWeight: pad.velocityFilterWeight,
compositeOperation: pad.compositeOperation,
minDistance: pad.minDistance,
backgroundColor: pad.backgroundColor,
throttle: pad.throttle,
canvasContextOptions: pad.canvasContextOptions,
};

expect(actual).toStrictEqual(exp);
});
});

describe('#clear', () => {
Expand Down
Loading