Skip to content

Commit

Permalink
feat(operator): prepare operator handling for mixed calc
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Apr 13, 2019
1 parent 1e22cce commit 967cf91
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 26 deletions.
70 changes: 56 additions & 14 deletions src/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
/* eslint no-param-reassign: 0 */
/* eslint getter-return: 0 */
/* eslint new-cap: 0 */
/* eslint no-prototype-builtins: 0 */

const X = 0;
const Y = 1;
const Z = 2;
const DEFAULT = 3;
const VECTOR_LENGTH = Symbol('vector length');

let inProgress = DEFAULT;
let inVector;
Expand All @@ -21,10 +23,21 @@ function handleProgess(progess, alg, resVec) {
return alg(resVec);
}

function getVectorLength(vec) {
return vec[VECTOR_LENGTH] !== 2 ? 3 : 2;
}

function maxVector(v1, v2) {
if (getVectorLength(v1) > getVectorLength(v2)) {
return v1;
}
return v2;
}

/**
* @param {() => number} alg
* @param {{ x: number, y: number, z: number }=} result
* @return {{ x: number, y: number, z: number } | number}
* @param {{ x: number, y: number, z: number } | { x: number, y: number }=} result
* @return {{ x: number, y: number, z: number } | { x: number, y: number } | number}
*/
export function operatorCalc(alg, result) {
if (typeof alg !== 'function') {
Expand All @@ -43,19 +56,39 @@ export function operatorCalc(alg, result) {
return x;
}

let calcZ = false;
if (inVector) {
calcZ = getVectorLength(inVector) !== 2;
} else {
calcZ = result.length !== 2;
}

const y = handleProgess(Y, alg, resVec);
const z = handleProgess(Z, alg, resVec);

if (noRes) {
return new inVector.constructor(x, y, z);
}
if (funRes) {
return result(x, y, z);
}
if (!calcZ) {
if (noRes) {
return new inVector.constructor(x, y);
}
if (funRes) {
return result(x, y);
}

result.x = x;
result.y = y;
result.z = z;
result.x = x;
result.y = y;
} else {
const z = handleProgess(Z, alg, resVec);

if (noRes) {
return new inVector.constructor(x, y, z);
}
if (funRes) {
return result(x, y, z);
}

result.x = x;
result.y = y;
result.z = z;
}
return result;
} finally {
inProgress = DEFAULT;
Expand All @@ -70,14 +103,17 @@ export function cachedValueOf(VectorClass) {

Vector[name] = function () {
if (inProgress === X) {
inVector = this;
inVector = inVector ? maxVector(inVector, this) : this;
return this.x;
}
if (inProgress === Y) {
return this.y;
}
if (inProgress === Z) {
return this.z;
if (this[VECTOR_LENGTH] !== 2) {
return this.z;
}
return 0;
}
return org.call(this);
};
Expand Down Expand Up @@ -129,3 +165,9 @@ export function cachedGetter(VectorClass, name) {
get: bindCache(org)
});
}

export function defineVectorLength(VectorClass, value) {
const Vector = VectorClass.prototype;

Object.defineProperty(Vector, VECTOR_LENGTH, { value });
}
35 changes: 28 additions & 7 deletions src/point.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {
cachedMethod, cachedGetter, cachedValueOf, operatorCalc
cachedMethod,
cachedGetter,
cachedValueOf,
operatorCalc,
defineVectorLength
} from './operator';
import formatNumber from './formatter';

Expand Down Expand Up @@ -49,8 +53,8 @@ class APoint {
*/
constructor(x, y) {
if (typeof x === 'function') {
operatorCalc(x, (...args) => {
this[AXES] = args;
operatorCalc(x, (nx, ny) => {
this[AXES] = [nx, ny];
});
} else {
this[AXES] = [x || 0, y || 0];
Expand All @@ -69,7 +73,7 @@ class APoint {
*/
normalize() {
const { length } = this;
return new this.constructor(this.x / length, this.y / length, this.z / length);
return new this.constructor(this.x / length, this.y / length);
}

/**
Expand Down Expand Up @@ -128,10 +132,10 @@ class APoint {

/**
*
* @returns {[number, number, number]}
* @returns {[number, number]}
*/
toArray() {
return [this.x, this.y, this.z];
return [this.x, this.y];
}

/**
Expand Down Expand Up @@ -190,14 +194,31 @@ class APoint {
get len() {
return this.length;
}

/**
*
* @throws GetNotImplementedError
*/
get z() {
throw new Error('get z() not implemented');
}

/**
*
* @throws SetNotImplementedError
*/
set z(_) {
throw new Error('set z() not implemented');
}
}

cachedValueOf(APoint);
defineVectorLength(APoint, 2);
cachedMethod(APoint, 'dot');
cachedMethod(APoint, 'toAngle');
cachedMethod(APoint, 'angleTo');
cachedMethod(APoint, 'distance');
cachedMethod(APoint, 'toArray');
cachedMethod(APoint, 'getRad');
cachedGetter(APoint, 'length');
cachedGetter(APoint, 'lengthSq');

Expand Down
11 changes: 8 additions & 3 deletions src/vector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {
cachedMethod, cachedGetter, cachedValueOf, operatorCalc
cachedMethod,
cachedGetter,
cachedValueOf,
operatorCalc,
defineVectorLength
} from './operator';
import formatNumber from './formatter';

Expand All @@ -26,8 +30,8 @@ class AVector {
*/
constructor(x, y, z) {
if (typeof x === 'function') {
operatorCalc(x, (...args) => {
this[AXES] = args;
operatorCalc(x, (nx, ny, nz) => {
this[AXES] = [nx, ny, nz];
});
} else {
this[AXES] = [x || 0, y || 0, z || 0];
Expand Down Expand Up @@ -239,6 +243,7 @@ class AVector {
}

cachedValueOf(AVector);
defineVectorLength(AVector, 3);
cachedMethod(AVector, 'dot');
cachedMethod(AVector, 'cross');
cachedMethod(AVector, 'crossNormalize');
Expand Down
4 changes: 2 additions & 2 deletions test/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert } from 'chai';
import { Point, IPoint, calc } from '../src/point';

const pointTest = (Vec2) => {
it('should create x y z values', () => {
it('should create x y values', () => {
const pos = new Vec2(5, 6);
assert.equal(pos.x, 5);
assert.equal(pos.y, 6);
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('special Point test.', () => {
assert.equal(pos.y, 28);
});

it('should change x y z values when calling local calc method', () => {
it('should change x y values when calling local calc method', () => {
const pos = new Point(5, 6);
const res = pos.calc(p => p * 25);

Expand Down

0 comments on commit 967cf91

Please sign in to comment.