Skip to content
Open
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
31 changes: 31 additions & 0 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@ const Point = require("./point");

class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
if(!(point1 instanceof Point)){
throw new Error("The entered argument point1 is not valid!")
}
if (!(point2 instanceof Point)){
throw new Error("The entered argument point2 is not valid!")
}


if(typeof(slope)!=="undefined" && typeof(slope)!=="number"){
throw new Error("slop should get a number")
}
if(typeof(n)!=="undefined" && typeof(n)!=="number"){
throw new Error("n should get a number")
}
this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
}

calculateSlope = () => {
// if(this.point1.x - this.point2.x==0){
// throw new Error("it is impossible to divide by 0")
// }
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)

}

calculateNOfLineFunction = () => {
Expand All @@ -18,6 +36,7 @@ class Line {

getPointOnXAsis() {
return this.getPointByY(0)

}

getPointOnYAsis() {
Expand All @@ -26,11 +45,23 @@ class Line {


getPointByX(x) {
if(x===undefined){
throw new Error("the function should get number")
}
if(typeof(x)!="number"){
throw new Error("the function should get number")
}
let y = this.slope * x + this.n
return new Point({ x, y })
}

getPointByY(y) {
if(y===undefined){
throw new Error("the function should get number")
}
if(typeof(y)!="number"){
throw new Error("the function should get number")
}
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
Expand Down
18 changes: 18 additions & 0 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
class Point {
constructor({x=0, y=0}={}) {
if(typeof(x)!=="number"){
throw new Error("x should be of type number")
}
if(typeof(y)!=="number"){
throw new Error("y should be of type number")
}
this.x = x;
this.y = y;
}
moveVertical(value) {
if(value===undefined){
throw new Error("the function should get number")
}
if(typeof(value)!="number"){
throw new Error("the function should get number")
}
this.y += value;
}
moveHorizontal(value) {
if(value===undefined){
throw new Error("the function should get number")
}
if(typeof(value)!="number"){
throw new Error("the function should get number")
}
this.x += value;
}
}
Expand Down
89 changes: 83 additions & 6 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,65 @@
const Line = require('./ecs6-class/line')
const Point = require('./ecs6-class/point')

const calculateDistance = (point1, point2) => {
let distanceX = (point2.x - point1.x) ** 2;
let distanceY = (point2.y - point2.y) ** 2;
const distance = Math.sqrt(distanceX + distanceY);
return distance;
if(point1===undefined||point2===undefined){
throw new Error('the function must get an arguments')

}
if((!(point1 instanceof Point)) || (!(point2 instanceof Point))){
throw new Error("the arguments is not instance of 'Point'")
}
let distanceX = (point2.x - point1.x) ** 2
let distanceY = (point2.y - point1.y) ** 2
const distance = Math.sqrt(distanceX + distanceY)
return distance
}

const calculateJunctionPoint = (line1, line2) => {

if(line1===undefined||line2===undefined){
throw new Error('the function must get line1 and line2')

}
if((!(line1 instanceof Line)) && (!(line2 instanceof Line))){
throw new Error("line1 and line2 should be of type 'Line'")
}
if((!(line1 instanceof Line))){
throw new Error("line1 should be of type 'Line'")
}
if((!(line2 instanceof Line))){
throw new Error("line2 should be of type 'Line'")
}




if(line1.slope===undefined){
line1.calculateSlope()
}
if(line2.slope===undefined){
line2.calculateSlope()
}
if(line1.n===undefined){
line1.calculateNOfLineFunction()

}
if(line2.n===undefined){
line2.calculateNOfLineFunction()
}


// if(line1.n===undefined){

// }
// if(line2.n===undefined){

// }


if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
return true
}
else {
return false
Expand All @@ -22,10 +71,38 @@ const calculateJunctionPoint = (line1, line2) => {
return junctionPoint
}
}

const isPointOnLine = (line, point) => {

if(line===undefined && point===undefined){
throw new Error('the function must get an two arguments')
}

if(line===undefined){
throw new Error('the function must accept a line')
}

if(point===undefined){
throw new Error('the function must accept a point')

}

if(!(line instanceof Line)){
throw new Error("the arguments is not instance of 'line'")

}
if(!(point instanceof Point)){
throw new Error("the arguments is not instance of 'point'")

}

if(line.slope === undefined){
line.calculateSlope()
}


const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if the line.slope is still undefined?


if (line.slope === proxyLine.slope) {
proxyLine.calculateNOfLineFunction()
if (line.n === proxyLine.n) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"test": "jest"
},
"dependencies": {
"jest": "^29.7.0"
"jest": "^29.7.0",
"coverage":"npm run test -- --coverage"
},
"repository": {
"type": "git",
Expand Down
134 changes: 134 additions & 0 deletions tests/modules/esc-6-class/line.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const Line = require('../../../modules/ecs6-class/line')
const Point = require('../../../modules/ecs6-class/point')
const point1 = new Point({ x: 1, y: 2 })
const point2 = new Point({ x: 3, y: 4 })
const line = new Line({ point1, point2,n:2,slope:4 })
const line1 = new Line({})
let lineTest=new Line({})

describe('CONSTRUCTOR',()=>{
it('should return n after is built the constructor',()=>{
expect(line.n).toBe(2)
})
it('should return slope after is built the constructor',()=>{
expect(line.slope).toBe(4)
})
it('should return point1 after is built the constructor',()=>{
expect(line.point1).toEqual({x:1,y:2})
})
it('should return point2 after is built the constructor',()=>{
expect(line.point2).toEqual({x:3,y:4})
})


})

describe ('ERRORS',()=>{
it('Error checker for constructor',()=>{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are the good tests for the constructor?

expect(()=>new Line({point1:new Line({})})).toThrow('The entered argument point1 is not valid!')
expect(()=>new Line({n:"abc"})).toThrow('n should get a number')
expect(()=>new Line({point2:true})).toThrow('The entered argument point2 is not valid!')
expect(()=>new Line({point2:new Point['hello']})).toThrow('Point.hello is not a constructor')
expect(()=>new Line({slope:"sss"})).toThrow('slop should get a number')

})
})

describe('CALCULATE_SLOP', () => {
it('should calculate the slope', () => {
line.calculateSlope()
expect(line.slope).toBe(1)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are the errors for the function?

})
})

describe('Error',()=>{
it("Error checker for function calculateSlope",()=>{
const line={point1:new Point,point2:new Point,slope:undefined,n:5}
console.log(line);
expect(()=>line.calculateSlope()).toThrow('line.calculateSlope is not a function')
})
})


describe('CALCULATE_N_OF_LINE_FUNCTION', () => {
it('should calculate n of line', () => {
line.calculateNOfLineFunction()
expect(line.slope).toBe(1)

})

})
describe('GET_POINT_ON_Y_ASIS',()=>{
it('should return getPointOnYAsis as per 0',()=>{
expect(line.getPointOnYAsis()).toEqual({x:0,y:1})
})

it('mocking getPointByX function in getPointOnYAxis method', () => {
const line = new Line({});
line.getPointByX = jest.fn().mockReturnValue({ x: 0, y: 5 });

const result = line.getPointOnYAsis();

expect(result).toEqual({ x: 0, y: 5 });
})

})
describe('GET_POINT_ON_X_ASIS',()=>{
it('should return getPointOnXAsis as per 0',()=>{
expect(line.getPointOnXAsis()).toEqual({x:-1,y:0})
})


it('mocking getPointByY function in getPointOnXAxis method', () => {
const line = new Line({});
getPointByY = jest.fn().mockReturnValue({ x: 5, y: 0 });

const result = line.getPointOnXAsis();

expect(result).toEqual({ x: NaN, y:0});
})

})


describe('GET_POINT_BY_X',()=>{
it('should retutn y by x',()=>{
expect(line.getPointByX(3)).toEqual({x:3,y:4})
})
})

describe('ERRORS',()=>{
it('Error checker for function getPointByX',()=>{
expect(()=>lineTest.getPointByX('a')).toThrow('the function should get number')
expect(()=>lineTest.getPointByX(['acc','add'])).toThrow('the function should get number')
expect(()=>lineTest.getPointByX(true)).toThrow('the function should get number')
expect(()=>lineTest.getPointByX()).toThrow('the function should get number')
expect(()=>lineTest.getPointByX((v)=>v)).toThrow('the function should get number')

})


})

describe ('GET_POINT_BY_Y',()=>{
it('should return x by y',()=>{
expect(line.getPointByY(5)).toEqual({x:4,y:5})
})


})

describe('ERRORS',()=>{
it('Error checker for function getPointByY',()=>{
expect(()=>lineTest.getPointByY('a')).toThrow('the function should get number')
expect(()=>lineTest.getPointByY(['acc','add'])).toThrow('the function should get number')
expect(()=>lineTest.getPointByY(true)).toThrow('the function should get number')
expect(()=>lineTest.getPointByY()).toThrow('the function should get number')
expect(()=>lineTest.getPointByY((v)=>v)).toThrow('the function should get number')

})

})


Loading