mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-09 05:35:45 +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:
@ -5,7 +5,7 @@
|
||||
* @param {number} radius - The radius of the circle.
|
||||
*/
|
||||
export default class Circle {
|
||||
constructor (radius) {
|
||||
constructor(radius) {
|
||||
this.radius = radius
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
* @param {number} height - The height of the cone
|
||||
*/
|
||||
export default class Cone {
|
||||
constructor (baseRadius, height) {
|
||||
constructor(baseRadius, height) {
|
||||
this.baseRadius = baseRadius
|
||||
this.height = height
|
||||
}
|
||||
@ -16,10 +16,15 @@ export default class Cone {
|
||||
}
|
||||
|
||||
volume = () => {
|
||||
return this.baseArea() * this.height * 1 / 3
|
||||
return (this.baseArea() * this.height * 1) / 3
|
||||
}
|
||||
|
||||
surfaceArea = () => {
|
||||
return this.baseArea() + Math.PI * this.baseRadius * Math.sqrt(Math.pow(this.baseRadius, 2) + Math.pow(this.height, 2))
|
||||
return (
|
||||
this.baseArea() +
|
||||
Math.PI *
|
||||
this.baseRadius *
|
||||
Math.sqrt(Math.pow(this.baseRadius, 2) + Math.pow(this.height, 2))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -6,13 +6,13 @@
|
||||
* convex polygon that contains all the points of it.
|
||||
*/
|
||||
|
||||
function compare (a, b) {
|
||||
function compare(a, b) {
|
||||
// Compare Function to Sort the points, a and b are points to compare
|
||||
if (a.x < b.x) return -1
|
||||
if (a.x === b.x && a.y < b.y) return -1
|
||||
return 1
|
||||
}
|
||||
function orientation (a, b, c) {
|
||||
function orientation(a, b, c) {
|
||||
// Check orientation of Line(a,b) and Line(b,c)
|
||||
const alpha = (b.y - a.y) / (b.x - a.x)
|
||||
const beta = (c.y - b.y) / (c.x - b.x)
|
||||
@ -25,17 +25,19 @@ function orientation (a, b, c) {
|
||||
return 0
|
||||
}
|
||||
|
||||
function convexHull (points) {
|
||||
function convexHull(points) {
|
||||
const pointsLen = points.length
|
||||
if (pointsLen <= 2) {
|
||||
throw new Error('Minimum of 3 points is required to form closed polygon!')
|
||||
}
|
||||
|
||||
points.sort(compare)
|
||||
const p1 = points[0]; const p2 = points[pointsLen - 1]
|
||||
const p1 = points[0]
|
||||
const p2 = points[pointsLen - 1]
|
||||
|
||||
// Divide Hull in two halves
|
||||
const upperPoints = []; const lowerPoints = []
|
||||
const upperPoints = []
|
||||
const lowerPoints = []
|
||||
|
||||
upperPoints.push(p1)
|
||||
lowerPoints.push(p1)
|
||||
@ -44,7 +46,14 @@ function convexHull (points) {
|
||||
if (i === pointsLen - 1 || orientation(p1, points[i], p2) !== -1) {
|
||||
let upLen = upperPoints.length
|
||||
|
||||
while (upLen >= 2 && orientation(upperPoints[upLen - 2], upperPoints[upLen - 1], points[i]) === -1) {
|
||||
while (
|
||||
upLen >= 2 &&
|
||||
orientation(
|
||||
upperPoints[upLen - 2],
|
||||
upperPoints[upLen - 1],
|
||||
points[i]
|
||||
) === -1
|
||||
) {
|
||||
upperPoints.pop()
|
||||
upLen = upperPoints.length
|
||||
}
|
||||
@ -52,7 +61,14 @@ function convexHull (points) {
|
||||
}
|
||||
if (i === pointsLen - 1 || orientation(p1, points[i], p2) !== 1) {
|
||||
let lowLen = lowerPoints.length
|
||||
while (lowLen >= 2 && orientation(lowerPoints[lowLen - 2], lowerPoints[lowLen - 1], points[i]) === 1) {
|
||||
while (
|
||||
lowLen >= 2 &&
|
||||
orientation(
|
||||
lowerPoints[lowLen - 2],
|
||||
lowerPoints[lowLen - 1],
|
||||
points[i]
|
||||
) === 1
|
||||
) {
|
||||
lowerPoints.pop()
|
||||
lowLen = lowerPoints.length
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
* @param {number} height - The height of the pyramid
|
||||
*/
|
||||
export default class Pyramid {
|
||||
constructor (bsl, height) {
|
||||
constructor(bsl, height) {
|
||||
this.bsl = bsl
|
||||
this.height = height
|
||||
}
|
||||
@ -16,10 +16,14 @@ export default class Pyramid {
|
||||
}
|
||||
|
||||
volume = () => {
|
||||
return this.baseArea() * this.height / 3
|
||||
return (this.baseArea() * this.height) / 3
|
||||
}
|
||||
|
||||
surfaceArea = () => {
|
||||
return this.baseArea() + this.bsl * 4 / 2 * Math.sqrt(Math.pow(this.bsl / 2, 2) + Math.pow(this.height, 2))
|
||||
return (
|
||||
this.baseArea() +
|
||||
((this.bsl * 4) / 2) *
|
||||
Math.sqrt(Math.pow(this.bsl / 2, 2) + Math.pow(this.height, 2))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -5,12 +5,12 @@
|
||||
* @see https://en.wikipedia.org/wiki/Sphere
|
||||
*/
|
||||
export default class Sphere {
|
||||
constructor (radius) {
|
||||
constructor(radius) {
|
||||
this.radius = radius
|
||||
}
|
||||
|
||||
volume = () => {
|
||||
return Math.pow(this.radius, 3) * Math.PI * 4 / 3
|
||||
return (Math.pow(this.radius, 3) * Math.PI * 4) / 3
|
||||
}
|
||||
|
||||
surfaceArea = () => {
|
||||
|
@ -1,29 +1,41 @@
|
||||
import { convexHull } from '../ConvexHullGraham'
|
||||
|
||||
test('The ConvexHull of the following points is [{x: 0, y: 3}, {x: 4, y: 4}, {x: 3, y: 1}, {x: 0, y: 0}]', () => {
|
||||
const points = [
|
||||
{ x: 0, y: 3 },
|
||||
{ x: 1, y: 1 },
|
||||
{ x: 2, y: 2 },
|
||||
{ x: 4, y: 4 },
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 1, y: 2 },
|
||||
{ x: 3, y: 1 },
|
||||
{ x: 3, y: 3 }]
|
||||
const res = convexHull(points)
|
||||
expect(res).toEqual([{ x: 0, y: 3 }, { x: 4, y: 4 }, { x: 3, y: 1 }, { x: 0, y: 0 }])
|
||||
})
|
||||
|
||||
test('The ConvexHull of the following points is [{x: 1, y: 4}, {x: 9, y: 6}, {x: 7, y: 0}, {x: 0, y: 0}]', () => {
|
||||
const points = [
|
||||
{ x: 4, y: 3 },
|
||||
{ x: 1, y: 4 },
|
||||
{ x: 2, y: 4 },
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 9, y: 6 },
|
||||
{ x: 1, y: 3 },
|
||||
{ x: 4, y: 1 },
|
||||
{ x: 7, y: 0 }]
|
||||
const res = convexHull(points)
|
||||
expect(res).toEqual([{ x: 1, y: 4 }, { x: 9, y: 6 }, { x: 7, y: 0 }, { x: 0, y: 0 }])
|
||||
})
|
||||
import { convexHull } from '../ConvexHullGraham'
|
||||
|
||||
test('The ConvexHull of the following points is [{x: 0, y: 3}, {x: 4, y: 4}, {x: 3, y: 1}, {x: 0, y: 0}]', () => {
|
||||
const points = [
|
||||
{ x: 0, y: 3 },
|
||||
{ x: 1, y: 1 },
|
||||
{ x: 2, y: 2 },
|
||||
{ x: 4, y: 4 },
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 1, y: 2 },
|
||||
{ x: 3, y: 1 },
|
||||
{ x: 3, y: 3 }
|
||||
]
|
||||
const res = convexHull(points)
|
||||
expect(res).toEqual([
|
||||
{ x: 0, y: 3 },
|
||||
{ x: 4, y: 4 },
|
||||
{ x: 3, y: 1 },
|
||||
{ x: 0, y: 0 }
|
||||
])
|
||||
})
|
||||
|
||||
test('The ConvexHull of the following points is [{x: 1, y: 4}, {x: 9, y: 6}, {x: 7, y: 0}, {x: 0, y: 0}]', () => {
|
||||
const points = [
|
||||
{ x: 4, y: 3 },
|
||||
{ x: 1, y: 4 },
|
||||
{ x: 2, y: 4 },
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 9, y: 6 },
|
||||
{ x: 1, y: 3 },
|
||||
{ x: 4, y: 1 },
|
||||
{ x: 7, y: 0 }
|
||||
]
|
||||
const res = convexHull(points)
|
||||
expect(res).toEqual([
|
||||
{ x: 1, y: 4 },
|
||||
{ x: 9, y: 6 },
|
||||
{ x: 7, y: 0 },
|
||||
{ x: 0, y: 0 }
|
||||
])
|
||||
})
|
||||
|
Reference in New Issue
Block a user