Skip to content

Commit

Permalink
Allow Vector2 to be instantiated with one argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyd committed Jan 26, 2024
1 parent b1270d2 commit 3117de4
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/vector2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { jitter, random } from './random.js'
export class Vector2 {
/**
* @param {number} x coordinate
* @param {number} y coordinate
* @param {number} [y] coordinate defaults to `x` if omitted.
*/
constructor(x, y) {
if (typeof x !== 'number') {
throw new Error(`Vector2 constructor requires a number for x, got ${typeof x}`)
}
this.x = x
this.y = y
this.y = y ?? x
}

/**
Expand Down Expand Up @@ -154,9 +157,9 @@ export class Vector2 {

/**
* @param {number} x
* @param {number} y
* @param {number} [y] defaults to `x` if omitted.
* @returns Vector2
*/
export function vec2(x, y) {
return new Vector2(x, y)
return new Vector2(x, y ?? x)
}

0 comments on commit 3117de4

Please sign in to comment.