Skip to content

Commit

Permalink
fix(operator): fix handling with number wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed May 13, 2021
1 parent 1d20dd4 commit 6cd5be3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ function getVectorValue(vec, index) {

const getSource = vec[GET_SOURCE];
if (getSource) {
return getSource(vec)[index];
return 1.0 * getSource(vec)[index];
}
if (index === X) {
return vec.x;
return 1.0 * vec.x;
}
if (index === Y) {
return vec.y;
return 1.0 * vec.y;
}
if (index === Z) {
return vec.z;
return 1.0 * vec.z;
}
if (index === W) {
return vec.w;
return 1.0 * vec.w;
}
// really?
return undefined;
Expand Down
29 changes: 27 additions & 2 deletions test/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,41 @@ import { operatorCalc, cachedFactory } from '../src/operator';
import { victor, vector } from '../src';

describe('operatorCalc with Victor test.', () => {
it('should throw error with assgined Victor', () => {
it('should throw error with assingned Victor', () => {
const pos = victor(5, 6, 7);
const dir = victor(1, 0, 0);

assert.throws(() => operatorCalc(() => dir * pos, victor(1, 0, 0)));
});

it('should work fine with number wrapper class', () => {
class Num {
constructor(nr) {
this.nr = nr;
}

valueOf() {
return this.nr;
}

toString() {
return `${this.nr}`;
}
}

const pos = vector(new Num(5), 6, 7);
const dir = vector(1, new Num(0), 0);

const scale = operatorCalc(() => dir * pos, vector(1, 0.9, 0));

assert.equal(scale.x, 5);
assert.equal(scale.y, 0);
assert.equal(scale.z, 0);
});
});

describe('operatorCalc with Vector test.', () => {
it('should throw error with assgined Victor', () => {
it('should work fine with assigning result vector', () => {
const pos = vector(5, 6, 7);
const dir = vector(1, 0, 0);

Expand Down

0 comments on commit 6cd5be3

Please sign in to comment.