-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshlyce.js
52 lines (46 loc) · 993 Bytes
/
shlyce.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
/**
Constructor
Args:
arr {Array} - the backing Array
lo {uint} - inclusive lower bound
hi {unit} - exclusive upper bound
*/
class Shlyce {
constructor (arr, lo, hi) {
this.arr = arr;
this.lo = lo || 0; // inclusive
this.hi = hi || arr.length;
this.length = this.hi - this.lo;
const shlyce = new Proxy(this, {
get: function(target, name) {
if(/[0-9]+/.test(name)) {
return target.get(Number(name));
}
return target[name];
},
set: function(target, name, value) {
if(/[0-9]+/.test(name)) {
return target.set(Number(name), value);
}
target[name] = value;
}
});
return shlyce;
}
get(n) {
return this.arr[this.lo + n];
}
set(n, x) {
this.arr[this.lo + n] = x;
}
slice(lo, hi) {
if(hi == null) {
hi = this.hi;
} else {
hi += this.lo;
}
lo += this.lo;
return new Shlyce(this.arr, lo, hi);
}
}
module.exports = Shlyce;