Skip to content

Commit

Permalink
feat(operator): cachedFactory function
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed May 15, 2019
1 parent 53b55c0 commit ec4515c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,7 @@ export function defineVectorLength(VectorClass, value) {

Object.defineProperty(Vector, VECTOR_LENGTH, { value });
}

export function cachedFactory(VectorClass) {
return bindCache((...args) => new VectorClass(...args));
}
35 changes: 26 additions & 9 deletions test/operator.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
// @ts-nocheck
import { assert } from 'chai';
import { operatorCalc } from '../src/operator';
import { Victor, Vector } from '../src';
import { operatorCalc, cachedFactory } from '../src/operator';
import { victor, vector } from '../src';

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

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

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

const scale = operatorCalc(() => dir * pos, new Vector(1, 0.9, 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 class using cachedFactory (Vector or Victor or Point or Ipoint) test.', () => {
let counter = 0;
class V {
constructor() {
counter++;
}
}
const vFactory = cachedFactory(V);

it('factory call should be cached', () => {
const pos = vector(5, 6, 7);
const dir = vector(1, 0, 0);
operatorCalc(() => dir * vFactory() + 1);

assert.equal(counter, 1);
});
});

0 comments on commit ec4515c

Please sign in to comment.