This repository has been archived by the owner on Jul 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
162 lines (119 loc) · 3.25 KB
/
main.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
/**/ "use strict" /**/
var Detacher = require('detacher'),
isRul = '3Vvw4Kb8NwR1Y74',
volatile = Symbol(),
consumable = Symbol(),
consumers = Symbol(),
length = Symbol(),
list = Symbol();
class Rul{
static is(obj){
return !!(obj && obj[isRul]);
}
constructor(vol){
this[volatile] = !!vol;
this[consumable] = true;
this[length] = 0;
this[list] = [];
this[consumers] = new Set();
}
add(elem,index){
add.apply(this,arguments);
}
move(){
move.apply(this,arguments);
}
remove(index,num){
remove.apply(this,arguments);
}
indexOf(elem){
if(!this[consumable]) return -1;
return this[list].indexOf(elem);
}
lastIndexOf(elem){
if(!this[consumable]) return -1;
return this[list].lastIndexOf(elem);
}
get(index){
if(!this[consumable]) return;
return this[list][index];
}
[Symbol.iterator](){
if(!this[consumable]) return noop();
return this[list][Symbol.iterator]();
}
append(it){
var elem;
for(elem of it) this.add(elem);
}
replace(index,elem){
if(index < this[length] && index >= 0) this.remove(index);
this.add(elem,index);
}
clear(){
this.remove(0,this[length]);
}
swap(from,to){
var first = from < to ? from : to,
second = to > from ? to : from;
first = Math.round(Math.max(0,Math.min(this[length] - 1,first)));
second = Math.round(Math.max(0,Math.min(this[length] - 1,second)));
if(second <= first) return;
this.move(first,second);
this.move(second - 1,first);
}
consume(onAdd,onRemove,onMove,thisArg){
var i;
thisArg = thisArg || this;
if(!this[consumable]) return new Detacher();
if(this[volatile]) setTimeout(evaporate,0,this);
this[consumers].add(arguments);
for(i = 0;i < this[list].length;i++) arguments[0].call(thisArg,this[list][i],i);
return new Detacher(detach,[arguments,this]);
}
get consumable(){ return this[consumable]; }
get volatile(){ return this[volatile]; }
get length(){ return this[length]; }
get [isRul](){ return true; }
}
// - utils
// -- main methods
function add(elem,index){
var c;
if(arguments.length == 1) index = this[length];
else index = Math.round(Math.max(0,Math.min(this[length],index)));
this[length]++;
if(this[consumable]) this[list].splice(index,0,elem);
for(c of this[consumers]) c[0].call(c[3],elem,index);
}
function remove(index,num){
var c;
index = Math.round(Math.max(0,Math.min(this[length] - 1,index)));
if(arguments.length == 1) num = 1;
else num = Math.round(Math.max(0,Math.min(this[length] - index,num)));
if(!num) return;
this[length] -= num;
if(this[consumable]) this[list].splice(index,num);
for(c of this[consumers]) c[1].call(c[3],index,num);
}
function move(from,to){
var c,elem;
from = Math.round(Math.max(0,Math.min(this[length] - 1,from)));
to = Math.round(Math.max(0,Math.min(this[length] - 1,to)));
if(this[consumable]){
elem = this[list].splice(from,1)[0];
this[list].splice(to,0,elem);
}
for(c of this[consumers]) c[2].call(c[3],from,to);
}
// -- helpers
function* noop(){}
function evaporate(rul){
delete rul[list];
rul[consumable] = false;
}
function detach(args,rul){
rul[consumers].delete(args);
}
/*/ exports /*/
module.exports = Rul;