-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
178 lines (159 loc) · 4.54 KB
/
index.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
'use strict';
/**
* 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";
}
}
module.exports = ObjectHandler;