Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Misc. cleanup & adding type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
nateabele committed Aug 21, 2015
1 parent b48cdb7 commit 3c606fd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "geometry.js",
"version": "0.1.0",
"version": "0.1.1",
"description": "A collection of ES6 classes for simple geometric objects and calculations",
"main": "dist/geometry.min.js",
"scripts": {
Expand All @@ -23,13 +23,12 @@
},
"homepage": "https://github.com/radify/geometry.js",
"devDependencies": {
"babel": "^5.1.10",
"gulp-babel": "^5.1.0",
"babel": "^5.8.21",
"gulp-babel": "^5.2.1",
"gulp-concat": "^2.5.2",
"gulp-jasmine": "^2.0.1",
"gulp-sourcemaps": "^1.5.2",
"gulp-uglify": "^1.2.0",
"yargs": "^3.7.2"
},
"dependencies": {}
}
}
6 changes: 3 additions & 3 deletions spec/BoxSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ describe("Box", () => {
expect((new Box(new Point(1, 1), new Size(100, 100))).corners()).toEqual([
new Point(1, 1),
new Point(101, 1),
new Point(101, 101),
new Point(1, 101),
new Point(101, 101)
]);
});
});
Expand Down Expand Up @@ -50,9 +50,9 @@ describe("Box", () => {
var box = new Box(new Point(1, 1), new Size(100, 100));
expect(box.edges().map(line => line.toPairs())).toEqual([
[1, 1, 101, 1],
[1, 1, 1, 101],
[1, 1, 101, 101],
[101, 101, 1, 1]
[1, 1, 1, 101],
[1, 101, 1, 1]
]);
});
});
Expand Down
4 changes: 1 addition & 3 deletions src/Box.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import Point from "./Point";
import Line from "./Line";
import Size from "./Size";

var prop = (name) => {
return (obj) => obj[name];
};
var prop = (name) => (obj) => obj[name];

export default class Box {

Expand Down
2 changes: 1 addition & 1 deletion src/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class Line {
}

add(offset: Point): Line {
return
return new Line(this.origin.add(offset), this.destination.add(offset));
}

crosses(line: Line): boolean {
Expand Down
37 changes: 18 additions & 19 deletions src/Point.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import Box from "./Box";
export default class Point {

class Point {

constructor(x: number = 0, y: number = 0) {
var [_x, _y] = [x, y];
Object.assign(this, { _x, _y });
constructor(_x: number = 0, _y: number = 0) {
Object.freeze(Object.assign(this, { _x, _y }));
}

add(point: Point): Point {
return new Point(this._x + point.x, this._y + point.y);
add(point): Point {
var adding = Point.instance(point);
return new Point(this._x + adding.x, this._y + adding.y);
}

subtract(point: Point): Point {
Expand All @@ -27,10 +25,6 @@ class Point {
return new Point(-this._x, -this._y);
}

isIn(box: Box): boolean {
return box.contains(this);
}

distance(point: Point): number {
return Math.sqrt(Math.pow(point.x - this.x, 2) + Math.pow(point.y - this.y, 2));
}
Expand All @@ -54,15 +48,15 @@ class Point {
return 180 * ((rad < 0) ? (2 * Math.PI + rad) : rad) / Math.PI;
}

get x() {
get x(): number {
return this._x;
}

get y() {
get y(): number {
return this._y;
}

get transform() {
get transform(): string {
return `translate(${this._x}, ${this._y})`;
}

Expand All @@ -74,9 +68,14 @@ class Point {
return `${this._x}, ${this._y}`;
}

static fromString(str) {
return new Point(...(str.split(',').map(val => parseInt(val, 10))));
static instance(x, y): Point {
if (x instanceof Point) return x;
if (x && x.hasOwnProperty('x')) return new Point(x.x, x.y);
if (x && x.length && x.length >= 2) return new Point(x[0], x[1]);
return new Point(x, y);
}
}

export default Point;
static fromString(str): Point {
return new Point(...(str.split(',').map(val => parseInt(val, 10))));
}
}
14 changes: 10 additions & 4 deletions src/Size.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@ export default class Size {
return new Size(this._w + size.width, this._h + size.height);
}

get width() {
get width(): number {
return this._w;
}

get height() {
get height(): number {
return this._h;
}

get transform() {
get transform(): string {
return `scale(${this._w}, ${this._h})`;
}

toJSON() {
return { width: this._w, height: this._h };
}
}

static instance(width, height) {
if (width && width.hasOwnProperty('width')) return new Size(width.width, width.height);
if (width && width.length && width.length >= 2) return new Size(...width);
return new Size(width, height);
}
}

0 comments on commit 3c606fd

Please sign in to comment.