Skip to content

Commit 8bdf2d8

Browse files
committed
feat(vector): calc flexible for different vector implementations
1 parent eab990c commit 8bdf2d8

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/index.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
/* eslint class-methods-use-this: 0 */
2+
/* eslint no-param-reassign: 0 */
3+
24
const X = Symbol.for('x');
35
const Y = Symbol.for('y');
46
const Z = Symbol.for('z');
57
const DEFAULT = Symbol.for('default');
68

79
let inProgress = DEFAULT;
10+
let inVector;
811

912
const v3ValueOf = new Map();
10-
v3ValueOf[X] = function getX() {
11-
return this[inProgress];
12-
};
13-
v3ValueOf[Y] = function getY() {
14-
return this[inProgress];
15-
};
16-
v3ValueOf[Z] = function getZ() {
13+
v3ValueOf[X] = function getValueOf() {
14+
if (!inVector) {
15+
inVector = this;
16+
}
1717
return this[inProgress];
1818
};
19+
v3ValueOf[Y] = v3ValueOf[X];
20+
v3ValueOf[Z] = v3ValueOf[X];
1921
v3ValueOf[DEFAULT] = function getDefault() {
2022
return this.length;
2123
};
@@ -28,18 +30,27 @@ function innerCalc(alg, result) {
2830
throw new Error('something wrong');
2931
}
3032
try {
31-
const res = result;
32-
3333
inProgress = X;
34-
res[inProgress] = alg();
34+
const x = alg();
35+
36+
if (!result && !inVector) {
37+
return x;
38+
}
3539
inProgress = Y;
36-
res[inProgress] = alg();
40+
const y = alg();
3741
inProgress = Z;
38-
res[inProgress] = alg();
42+
const z = alg();
3943

40-
return res;
44+
if (!result) {
45+
return inVector.createVector(x, y, z);
46+
}
47+
result[X] = x;
48+
result[Y] = y;
49+
result[Z] = z;
50+
return result;
4151
} finally {
4252
inProgress = DEFAULT;
53+
inVector = undefined;
4354
}
4455
}
4556

@@ -200,7 +211,7 @@ export class Victor extends AVector {
200211
}
201212

202213
export function calc(alg) {
203-
return innerCalc(alg, new Vector());
214+
return innerCalc(alg);
204215
}
205216

206217
export default Vector;

0 commit comments

Comments
 (0)