mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
class Vector2 {
|
||||
constructor (x, y) {
|
||||
constructor(x, y) {
|
||||
this.x = x
|
||||
this.y = y
|
||||
}
|
||||
@@ -17,7 +17,7 @@ class Vector2 {
|
||||
* @param vector The vector to compare to.
|
||||
* @returns Whether they are exactly equal or not.
|
||||
*/
|
||||
equalsExactly (vector) {
|
||||
equalsExactly(vector) {
|
||||
return this.x === vector.x && this.y === vector.y
|
||||
}
|
||||
|
||||
@@ -28,8 +28,11 @@ class Vector2 {
|
||||
* @param epsilon The allowed discrepancy for the x-values and the y-values.
|
||||
* @returns Whether they are approximately equal or not.
|
||||
*/
|
||||
equalsApproximately (vector, epsilon) {
|
||||
return (Math.abs(this.x - vector.x) < epsilon && Math.abs(this.y - vector.y) < epsilon)
|
||||
equalsApproximately(vector, epsilon) {
|
||||
return (
|
||||
Math.abs(this.x - vector.x) < epsilon &&
|
||||
Math.abs(this.y - vector.y) < epsilon
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,7 +40,7 @@ class Vector2 {
|
||||
*
|
||||
* @returns The length of the vector.
|
||||
*/
|
||||
length () {
|
||||
length() {
|
||||
return Math.sqrt(this.x * this.x + this.y * this.y)
|
||||
}
|
||||
|
||||
@@ -46,7 +49,7 @@ class Vector2 {
|
||||
*
|
||||
* @returns The normalized vector.
|
||||
*/
|
||||
normalize () {
|
||||
normalize() {
|
||||
const length = this.length()
|
||||
if (length === 0) {
|
||||
throw new Error('Cannot normalize vectors of length 0')
|
||||
@@ -60,7 +63,7 @@ class Vector2 {
|
||||
* @param vector The vector to be added.
|
||||
* @returns The sum-vector.
|
||||
*/
|
||||
add (vector) {
|
||||
add(vector) {
|
||||
const x = this.x + vector.x
|
||||
const y = this.y + vector.y
|
||||
return new Vector2(x, y)
|
||||
@@ -72,7 +75,7 @@ class Vector2 {
|
||||
* @param vector The vector to be subtracted.
|
||||
* @returns The difference-vector.
|
||||
*/
|
||||
subtract (vector) {
|
||||
subtract(vector) {
|
||||
const x = this.x - vector.x
|
||||
const y = this.y - vector.y
|
||||
return new Vector2(x, y)
|
||||
@@ -84,7 +87,7 @@ class Vector2 {
|
||||
* @param scalar The factor by which to multiply the vector.
|
||||
* @returns The scaled vector.
|
||||
*/
|
||||
multiply (scalar) {
|
||||
multiply(scalar) {
|
||||
const x = this.x * scalar
|
||||
const y = this.y * scalar
|
||||
return new Vector2(x, y)
|
||||
@@ -96,7 +99,7 @@ class Vector2 {
|
||||
* @param vector The vector to which to calculate the distance.
|
||||
* @returns The distance.
|
||||
*/
|
||||
distance (vector) {
|
||||
distance(vector) {
|
||||
const difference = vector.subtract(this)
|
||||
return difference.length()
|
||||
}
|
||||
@@ -107,7 +110,7 @@ class Vector2 {
|
||||
* @param vector The vector used for the multiplication.
|
||||
* @returns The resulting dot product.
|
||||
*/
|
||||
dotProduct (vector) {
|
||||
dotProduct(vector) {
|
||||
return this.x * vector.x + this.y * vector.y
|
||||
}
|
||||
|
||||
@@ -117,7 +120,7 @@ class Vector2 {
|
||||
* @param angleInRadians The angle in radians by which to rotate the vector.
|
||||
* @returns The rotated vector.
|
||||
*/
|
||||
rotate (angleInRadians) {
|
||||
rotate(angleInRadians) {
|
||||
const ca = Math.cos(angleInRadians)
|
||||
const sa = Math.sin(angleInRadians)
|
||||
const x = ca * this.x - sa * this.y
|
||||
@@ -131,7 +134,7 @@ class Vector2 {
|
||||
* @param vector The 2nd vector for the measurement.
|
||||
* @returns The angle in radians.
|
||||
*/
|
||||
angleBetween (vector) {
|
||||
angleBetween(vector) {
|
||||
return Math.atan2(vector.y, vector.x) - Math.atan2(this.y, this.x)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,52 +5,80 @@ describe('Vector2', () => {
|
||||
it('should compare equality correctly', () => {
|
||||
expect(new Vector2(1, 0).equalsExactly(new Vector2(1, 0))).toBe(true)
|
||||
|
||||
expect(new Vector2(1.23, 4.56).equalsExactly(new Vector2(0, 0))).toBe(false)
|
||||
expect(new Vector2(1.23, 4.56).equalsExactly(new Vector2(0, 0))).toBe(
|
||||
false
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#equalsApproximately', () => {
|
||||
it('should compare equality (approximately) correctly', () => {
|
||||
expect(new Vector2(1, 0).equalsApproximately(new Vector2(1, 0.0000001), 0.000001))
|
||||
.toBe(true)
|
||||
expect(
|
||||
new Vector2(1, 0).equalsApproximately(
|
||||
new Vector2(1, 0.0000001),
|
||||
0.000001
|
||||
)
|
||||
).toBe(true)
|
||||
|
||||
expect(new Vector2(1.23, 4.56).equalsApproximately(new Vector2(1.24, 4.56), 0.000001))
|
||||
.toBe(false)
|
||||
expect(
|
||||
new Vector2(1.23, 4.56).equalsApproximately(
|
||||
new Vector2(1.24, 4.56),
|
||||
0.000001
|
||||
)
|
||||
).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#add', () => {
|
||||
it('should add two vectors correctly', () => {
|
||||
expect(new Vector2(1, 0).add(new Vector2(0, 1)).equalsApproximately(new Vector2(1, 1), 0.000001))
|
||||
.toBe(true)
|
||||
expect(
|
||||
new Vector2(1, 0)
|
||||
.add(new Vector2(0, 1))
|
||||
.equalsApproximately(new Vector2(1, 1), 0.000001)
|
||||
).toBe(true)
|
||||
|
||||
expect(new Vector2(-3.3, -9).add(new Vector2(-2.2, 3)).equalsApproximately(new Vector2(-5.5, -6), 0.000001))
|
||||
.toBe(true)
|
||||
expect(
|
||||
new Vector2(-3.3, -9)
|
||||
.add(new Vector2(-2.2, 3))
|
||||
.equalsApproximately(new Vector2(-5.5, -6), 0.000001)
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#subtract', () => {
|
||||
it('should subtract two vectors correctly', () => {
|
||||
expect(new Vector2(1, 0).subtract(new Vector2(0, 1)).equalsApproximately(new Vector2(1, -1), 0.000001))
|
||||
.toBe(true)
|
||||
expect(
|
||||
new Vector2(1, 0)
|
||||
.subtract(new Vector2(0, 1))
|
||||
.equalsApproximately(new Vector2(1, -1), 0.000001)
|
||||
).toBe(true)
|
||||
|
||||
expect(new Vector2(234.5, 1.7).subtract(new Vector2(3.3, 2.7)).equalsApproximately(new Vector2(231.2, -1), 0.000001))
|
||||
.toBe(true)
|
||||
expect(
|
||||
new Vector2(234.5, 1.7)
|
||||
.subtract(new Vector2(3.3, 2.7))
|
||||
.equalsApproximately(new Vector2(231.2, -1), 0.000001)
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#multiply', () => {
|
||||
it('should multiply two vectors correctly', () => {
|
||||
expect(new Vector2(1, 0).multiply(5).equalsApproximately(new Vector2(5, 0), 0.000001))
|
||||
.toBe(true)
|
||||
expect(
|
||||
new Vector2(1, 0)
|
||||
.multiply(5)
|
||||
.equalsApproximately(new Vector2(5, 0), 0.000001)
|
||||
).toBe(true)
|
||||
|
||||
expect(new Vector2(3.41, -7.12).multiply(-3.1).equalsApproximately(new Vector2(-10.571, 22.072), 0.000001))
|
||||
.toBe(true)
|
||||
expect(
|
||||
new Vector2(3.41, -7.12)
|
||||
.multiply(-3.1)
|
||||
.equalsApproximately(new Vector2(-10.571, 22.072), 0.000001)
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#length', () => {
|
||||
it('should calculate it\'s length correctly', () => {
|
||||
it("should calculate it's length correctly", () => {
|
||||
expect(new Vector2(1, 0).length()).toBe(1)
|
||||
|
||||
expect(new Vector2(-1, 1).length()).toBe(Math.sqrt(2))
|
||||
@@ -59,11 +87,20 @@ describe('Vector2', () => {
|
||||
|
||||
describe('#normalize', () => {
|
||||
it('should normalize vectors correctly', () => {
|
||||
expect(new Vector2(1, 0).normalize().equalsApproximately(new Vector2(1, 0), 0.000001))
|
||||
.toBe(true)
|
||||
expect(
|
||||
new Vector2(1, 0)
|
||||
.normalize()
|
||||
.equalsApproximately(new Vector2(1, 0), 0.000001)
|
||||
).toBe(true)
|
||||
|
||||
expect(new Vector2(1, -1).normalize().equalsApproximately(new Vector2(Math.sqrt(2) / 2, -Math.sqrt(2) / 2), 0.000001))
|
||||
.toBe(true)
|
||||
expect(
|
||||
new Vector2(1, -1)
|
||||
.normalize()
|
||||
.equalsApproximately(
|
||||
new Vector2(Math.sqrt(2) / 2, -Math.sqrt(2) / 2),
|
||||
0.000001
|
||||
)
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -85,19 +122,29 @@ describe('Vector2', () => {
|
||||
|
||||
describe('#rotate', () => {
|
||||
it('should rotate a vector correctly', () => {
|
||||
expect(new Vector2(0, -1).rotate(Math.PI / 2).equalsApproximately(new Vector2(1, 0), 0.000001))
|
||||
.toBe(true)
|
||||
expect(
|
||||
new Vector2(0, -1)
|
||||
.rotate(Math.PI / 2)
|
||||
.equalsApproximately(new Vector2(1, 0), 0.000001)
|
||||
).toBe(true)
|
||||
|
||||
expect(new Vector2(1.23, -4.56).rotate(Math.PI).equalsApproximately(new Vector2(-1.23, 4.56), 0.000001))
|
||||
.toBe(true)
|
||||
expect(
|
||||
new Vector2(1.23, -4.56)
|
||||
.rotate(Math.PI)
|
||||
.equalsApproximately(new Vector2(-1.23, 4.56), 0.000001)
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#angleBetween', () => {
|
||||
it('should calculate the angle between two vectors correctly', () => {
|
||||
expect(new Vector2(1, 0).angleBetween(new Vector2(0, 1))).toBe(Math.PI / 2)
|
||||
expect(new Vector2(1, 0).angleBetween(new Vector2(0, 1))).toBe(
|
||||
Math.PI / 2
|
||||
)
|
||||
|
||||
expect(new Vector2(1, 0).angleBetween(new Vector2(1, -1))).toBe(-Math.PI / 4)
|
||||
expect(new Vector2(1, 0).angleBetween(new Vector2(1, -1))).toBe(
|
||||
-Math.PI / 4
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user