-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
279 lines (243 loc) · 7.27 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var tape = _interopDefault(require('tape'));
/**
* This class overloads operator[] and array methods to given object.
* For example you have object with vector of points `let V = {points: [{x: 1, y: 2}, {x: 10, y: 20}, {x: -1, y: -2}]}`.
* You can create pseudo-array of x points by `let X = new ObjectHandler(V, x => x.points, x => x.x, (x, v) => x.x = v`.
* And access the elements of an pseudo-array like it ordinary array `(X[0] + X[1]) / X.length` and `X.filter(func)`
*/
class ObjectHandler {
/**
* @constructor ObjectHandler constructor
* @param object Target object with data
* @param container Function that return iterable container
* @param getter Getter inside container
* @param setter Setter indide container
*/
constructor(object, container, getter, setter = null) {
this.proxy = new Proxy(object, this.handler(container, getter, setter));
//this.proxy.toString = Function.prototype.bind(object);
return this.proxy;
}
handler(container, getter, setter) {
return {
get: (object, key) => {
if (key === 'length') {
return container(object).length;
} else if (typeof Array.prototype[key] == 'function') { //array methods
if (typeof this[key] == 'function') {
return this[key]();
} else {
return this.emulateArrayMethod(object, key, container, getter);
}
} else {
try { //access array by index
if(key === parseInt(key).toString()) {
if(0 <= key && key < this.length) {
return getter(container(object)[key]);
} else {
throw "index out of bondary";
}
} else {
throw "float index";
}
} catch (err) { //access to object by literal
return Reflect.get(object, key);
}
}
},
set: (object, key, value) => {
setter(container(object)[key], value);
return true;
}
}
}
emulateArrayMethod(object, key, container, getter) {
return (...args) => {
try {
console.log("Deprecated emulation. Better to define method " + key + "() by hands.");
return Reflect.apply([][key], container(object), args).map(x => getter(x));
} catch (err) {
throw "Array method can not be emulated!";
}
}
}
get length() {
return this.proxy.length;
}
slice() {
return (start, end = this.length) => {
let result = [];
for (let i = start; i < end; i++) {
result.push(this.proxy[i]);
}
return result;
}
}
forEach() {
return (func) => {
for(let i = 0; i < this.length; i++) {
func(this.proxy[i], i, this.proxy);
}
return this.proxy;
}
}
map() {
return (op) => {
let result = [];
for (let i = 0; i < this.length; i++) {
result.push(op(this.proxy[i], i, this.proxy));
}
return result;
}
}
filter() {
return (op) => {
let result = [];
for (let i = 0; i < this.length; i++) {
if (op(this.proxy[i], i, this.proxy)) {
result.push(this.proxy[i]);
}
}
return result;
}
}
reduce() {
return (op, init) => {
let total = init;
for (let i = 0; i < this.length; i++) {
total = op(total, this.proxy[i], i, this.proxy);
}
return total;
}
}
every() {
return (op,thisArg=undefined) => {
let isEvery = true;
for(let i = 0; i < this.length; i++) {
if (op.call(thisArg, this.proxy[i], i, this.proxy)) {
continue;
} else {
isEvery = false;
break;
}
}
return isEvery;
}
}
reverse() {
return () => {
for (let i = 0; i < Math.floor(this.length / 2); i++) {
let temp = this.proxy[i];
this.proxy[i] = this.proxy[this.length - i - 1];
this.proxy[this.length - i - 1] = temp;
}
return this.proxy;
}
}
join() {
return (separator = ",") => {
let result = "";
for (let i = 0; i < this.length - 1; i++) {
result += this.proxy[i] + separator;
}
return result + this.proxy[this.length - 1];
}
}
toString() {
return () => {
let result = "[ ";
for (let i = 0; i < this.length - 1; i++) {
result += this.proxy[i] + ", ";
}
return result + this.proxy[this.length - 1] + " ]";
}
}
get [Symbol.toStringTag]() {
return "ObjectHandler";
}
}
let obj = { "arr": [{ x: 1, d: {y: '100'}},
{ x: 2, d: {y: '200'}},
{ x: 3, d: {y: '300'}}],
"literal": 123 };
let test = new ObjectHandler(obj, p => p.arr, p => p.d.y, (p, val) => { p.d.y = val; });
let test2 = new ObjectHandler(obj, p => p.arr, p => p.x, (p, val) => { p.x = val; });
tape('Square bracket getter', function(t) {
t.equal(test[0] == 100, true);
t.end();
});
tape('Length property', function(t) {
t.equal(test.length == obj.arr.length, true);
t.end();
});
tape('Setter accessor', function(t) {
let temp = test[test.length - 1];
test[test.length - 1] = "set";
t.equal(test[test.length - 1] == "set", true);
test[test.length - 1] = temp;
t.end();
});
tape('Access to ordinary property', function(t) {
t.equal(test['literal'] == 123, true);
t.end();
});
tape('Method slice()', function(t) {
t.deepEqual(test.slice(0, 2), ['100', '200']);
t.end();
});
tape('Method forEach()', function(t) {
let temp = [];
test.forEach((item, i) => { temp[i] = item / 100;});
t.deepEqual(temp, test2.slice(0));
t.end();
});
tape('Method map()', function(t) {
let a = test2.map((item, i) => item * 100 / (i + 1));
console.log(a, test2);
t.deepEqual(a.slice(0), [100,100,100]);
test2.map((a, i) => i + 1);
t.end();
});
tape('Method filter()', function(t) {
let p = test.filter((item, i) => parseInt(item) > 150).length / 2;
let q = test2.filter((item, i) => (item / (i + 1)) == 1).map(x => (x * 100 * p).toString());
t.deepEqual(q.slice(0), test.slice(0));
t.end();
});
tape('Method reduce()', function(t) {
let p = test2.reduce((sum, item, i) => sum += item * i, 0);
t.equal(p, 8);
t.end();
});
tape('Method every()', function(t) {
let p = test2.every((item, i) => 1 <= item && item <= 3 && 0 <= i && i < 3);
let q = test2.every((item, i) => 100 <= item && item <= 300 && 0 <= i && i < 3);
t.deepEqual([p, q], [true, false]);
t.end();
});
tape('Method every() thisarg', function(t) {
let that = {'foo':42};
let fn = function(item, i) { return this && this.foo === 42 && 1 <= item && item <= 3 && 0 <= i && i < 3 };
let p = test2.every( fn, that );
let p2 = test2.every( fn );
let p3 = test2.every((item, i) => 1 <= item && item <= 3 && 0 <= i && i < 3);
t.ok(p);
t.notOk(p2);
t.ok(p3);
t.end();
});
tape('Method reverse()', function(t) {
t.deepEqual(test2.reverse().slice(0), [3, 2, 1]);
test2.reverse();
t.end();
});
tape('Method join()', function(t) {
t.deepEqual(test2.join('*'), "1*2*3");
t.end();
});
tape('Method toString()', function(t) {
t.deepEqual(test2.toString(), '[ 1, 2, 3 ]');
t.end();
});