Skip to content

Commit

Permalink
feat(vector): remove valueOf support outside of calc
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed May 17, 2020
1 parent dbd4bea commit 3da0f51
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 33 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ import { calc, vector, victor, point, ipoint } from "@js-basics/vector";

[![code preview](https://us-central1-code-snippet-to-svg.cloudfunctions.net/default/basics/vector/blob/master/examples/example.js?theme=atom_one_light&range=12-17)](https://github.com/basics/vector/blob/master/examples/example.js#L12)

### compare lengths

[![code preview](https://us-central1-code-snippet-to-svg.cloudfunctions.net/default/basics/vector/blob/master/examples/example.js?theme=atom_one_light&range=17-25)](https://github.com/basics/vector/blob/master/examples/example.js#L17)

### calculate cross product

[![code preview](https://us-central1-code-snippet-to-svg.cloudfunctions.net/default/basics/vector/blob/master/examples/example.js?theme=atom_one_light&range=25-33)](https://github.com/basics/vector/blob/master/examples/example.js#L25)
Expand Down
10 changes: 5 additions & 5 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ const offsetA = vector(() => dir * 30 + pos);
console.log(debug`offsetA: ${offsetA}`);
// offsetA: { x: 35, y: 6, z: 7 }

// compare length
let way = offsetA;
if (way > 1) {
way = way.normalize();
try {
way = offsetA + 1;
} catch (e) {
console.log('calculate outside of calc throw an error');
}
console.log(debug`way: ${way}`);
// way: { x: 0.967, y: 0.1658, z: 0.1934 }
// calculate outside of calc throw an error

// calculate cross product
const dir1 = vector(0, 1, 0);
Expand Down
4 changes: 2 additions & 2 deletions src/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class APoint {
}

/**
* @returns {number}
* @throws NotImplementedError
*/
valueOf() {
return this.length;
throw new Error('valueOf() not implemented, looks like you try to calculate outside of calc');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class AVector {
}

/**
* @returns {number}
* @throws NotImplementedError
*/
valueOf() {
return this.length;
throw new Error('valueOf() not implemented, looks like you try to calculate outside of calc');
}

/**
Expand Down
17 changes: 7 additions & 10 deletions test/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ const pointTest = (vec2, Vec2) => {
assert.equal(vec.y, 7);
});

it('should compares lengths of point.', () => {
const pos = vec2(1, 1);
const dir = vec2(2, 2);

assert.isTrue(dir > pos, `${dir} should be longer than ${pos}`);
assert.isTrue(dir.len > pos.len, `${dir} should be longer than ${pos}`);
assert.isFalse(dir < pos, `${dir} should be longer than ${pos}`);
assert.isFalse(dir.len < pos.len, `${dir} should be longer than ${pos}`);
});

it('should change length to 1 when calling normalize', () => {
const pos = vec2(5, 6);
const dir = pos.normalize();
Expand Down Expand Up @@ -172,6 +162,13 @@ const pointTest = (vec2, Vec2) => {
assert.equal(dir0.x, 4);
assert.equal(dir0.y, 4.5);
});

it("calculate directly outside of calc should throw an error.", () => {
const pos = vec2(1, 1, 1);
const dir = vec2(2, 2, 2);

assert.throws(() => (pos + dir));
});
};

describe('standard Point test.', () => {
Expand Down
18 changes: 8 additions & 10 deletions test/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@ const vectorTest = (vec3, Vec3) => {
assert.equal(vec.z, 7);
});

it("should compares lengths of vector.", () => {
const pos = vec3(1, 1, 1);
const dir = vec3(2, 2, 2);

assert.isTrue(dir > pos, `${dir} should be longer than ${pos}`);
assert.isTrue(dir.len > pos.len, `${dir} should be longer than ${pos}`);
assert.isFalse(dir < pos, `${dir} should be longer than ${pos}`);
assert.isFalse(dir.len < pos.len, `${dir} should be longer than ${pos}`);
});

it("should change length to 1 when calling normalize", () => {
const pos = vec3(5, 6, 7);
const dir = pos.normalize();
Expand Down Expand Up @@ -260,6 +250,14 @@ const vectorTest = (vec3, Vec3) => {
assert.equal(dir0.y, 5);
assert.equal(dir0.z, 4.5);
});

it("calculate directly outside of calc should throw an error.", () => {
const pos = vec3(1, 1, 1);
const dir = vec3(2, 2, 2);

assert.throws(() => (pos + dir));
});

};

describe("standard Vector test.", () => {
Expand Down

0 comments on commit 3da0f51

Please sign in to comment.