Skip to content

Commit 8fa6d3c

Browse files
committed
dop.collect set the collector in index 0 rather than collectors.length
1 parent 10d5a92 commit 8fa6d3c

File tree

3 files changed

+132
-124
lines changed

3 files changed

+132
-124
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dop",
3-
"version": "0.41.0",
3+
"version": "0.42.0",
44
"main": "./dist/dop.nodejs.js",
55
"browser": "./dist/dop.js",
66
"unpkg": "./dist/dop.min.js",

src/api/collect.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
dop.collect = function(index_function) {
2+
var is_function = isFunction(index_function)
23
dop.util.invariant(
3-
arguments.length == 0 ||
4-
(arguments.length == 1 && isFunction(index_function)),
4+
arguments.length == 0 || (arguments.length == 1 && is_function),
55
'dop.collect only accept one argument as function'
66
)
77
var collectors = dop.data.collectors
8-
var index = index_function ? index_function(collectors) : collectors.length
8+
var index = is_function ? index_function(collectors) : 0
99
return dop.core.createCollector(collectors, index)
1010
}

test/collectors.js

Lines changed: 128 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,145 @@
1-
2-
var test = require('tape');
1+
var test = require('tape')
32
// require('tabe').createStream( test );
4-
var dop = require('./.proxy').create();
5-
6-
7-
8-
var object = dop.register({});
3+
var dop = require('./.proxy').create()
94

5+
var object = dop.register({})
106

117
test('Collect as default', function(t) {
12-
var collector = dop.collect();
13-
dop.set(object, 'array', []);
14-
t.equal(collector.mutations.length, 1);
15-
t.equal(collector.mutations[0].object.array, object.array);
16-
collector.destroy();
17-
t.end();
18-
});
19-
8+
var collector = dop.collect()
9+
dop.set(object, 'array', [])
10+
t.equal(collector.mutations.length, 1)
11+
t.equal(collector.mutations[0].object.array, object.array)
12+
collector.destroy()
13+
t.end()
14+
})
2015

2116
test('Multiple collectors order', function(t) {
22-
var collector1 = dop.collect();
23-
var collector2 = dop.collect();
24-
object.array.push(Math.random());
25-
t.equal(collector1.mutations.length, 1);
26-
t.equal(collector2.mutations.length, 0);
27-
collector1.destroy();
28-
collector2.destroy();
29-
t.end();
30-
});
31-
32-
33-
test('Using collectFirst', function(t) {
34-
var collector1 = dop.collect();
35-
var collector2 = dop.collect(function(){ return 0 });
36-
object.array.push(Math.random());
37-
var collector3 = dop.collect();
38-
object.array.push(Math.random());
39-
var collector4 = dop.collect(function(){ return 0 });
40-
object.array.push(Math.random());
41-
t.equal(collector1.mutations.length, 0);
42-
t.equal(collector2.mutations.length, 2);
43-
t.equal(collector3.mutations.length, 0);
44-
t.equal(collector4.mutations.length, 1);
45-
collector1.destroy();
46-
collector2.destroy();
47-
collector3.destroy();
48-
collector4.destroy();
49-
t.end();
50-
});
51-
52-
17+
var collector1 = dop.collect()
18+
var collector2 = dop.collect()
19+
object.array.push(Math.random())
20+
t.equal(collector1.mutations.length, 0)
21+
t.equal(collector2.mutations.length, 1)
22+
collector1.destroy()
23+
collector2.destroy()
24+
t.end()
25+
})
26+
27+
test('Using index_function', function(t) {
28+
var collector1 = dop.collect()
29+
var collector2 = dop.collect(function() {
30+
return 0
31+
})
32+
object.array.push(Math.random())
33+
var collector3 = dop.collect()
34+
object.array.push(Math.random())
35+
var collector4 = dop.collect(function() {
36+
return 0
37+
})
38+
object.array.push(Math.random())
39+
t.equal(collector1.mutations.length, 0)
40+
t.equal(collector2.mutations.length, 1)
41+
t.equal(collector3.mutations.length, 1)
42+
t.equal(collector4.mutations.length, 1)
43+
collector1.destroy()
44+
collector2.destroy()
45+
collector3.destroy()
46+
collector4.destroy()
47+
t.end()
48+
})
49+
50+
test('Using index_function 2', function(t) {
51+
var collector2 = dop.collect(function() {
52+
return 0
53+
})
54+
object.array.push(Math.random())
55+
var collector3 = dop.collect()
56+
object.array.push(Math.random())
57+
var collector4 = dop.collect(function(collectors) {
58+
return collectors.length
59+
})
60+
object.array.push(Math.random())
61+
t.equal(collector2.mutations.length, 1)
62+
t.equal(collector3.mutations.length, 2)
63+
t.equal(collector4.mutations.length, 0)
64+
collector2.destroy()
65+
collector3.destroy()
66+
collector4.destroy()
67+
t.end()
68+
})
5369

5470
test('Active and inactive collectores', function(t) {
55-
var collector1 = dop.collect();
56-
object.array.push(Math.random());
57-
var collector2 = dop.collect();
58-
object.array.push(Math.random());
59-
collector1.active = false;
60-
object.array.push(Math.random());
61-
collector1.active = true;
62-
object.array.push(Math.random());
63-
t.equal(collector1.mutations.length, 3);
64-
t.equal(collector2.mutations.length, 1);
65-
collector1.destroy();
66-
collector2.destroy();
67-
t.end();
68-
});
69-
70-
71+
var collector1 = dop.collect()
72+
object.array.push(Math.random())
73+
var collector2 = dop.collect()
74+
object.array.push(Math.random())
75+
collector1.active = false
76+
object.array.push(Math.random())
77+
collector1.active = true
78+
object.array.push(Math.random())
79+
t.equal(collector1.mutations.length, 1)
80+
t.equal(collector2.mutations.length, 3)
81+
collector1.destroy()
82+
collector2.destroy()
83+
t.end()
84+
})
7185

7286
test('Destroying', function(t) {
73-
var collector1 = dop.collect();
74-
object.array.push(Math.random());
75-
var collector2 = dop.collect();
76-
object.array.push(Math.random());
77-
collector1.destroy();
78-
object.array.push(Math.random());
79-
t.equal(collector1.mutations.length, 2);
80-
t.equal(collector2.mutations.length, 1);
81-
collector1.destroy();
82-
collector2.destroy();
83-
t.end();
84-
});
85-
87+
var collector1 = dop.collect()
88+
object.array.push(Math.random())
89+
var collector2 = dop.collect()
90+
object.array.push(Math.random())
91+
collector1.destroy()
92+
object.array.push(Math.random())
93+
t.equal(collector1.mutations.length, 1)
94+
t.equal(collector2.mutations.length, 2)
95+
collector1.destroy()
96+
collector2.destroy()
97+
t.end()
98+
})
8699

87100
test('Filtering', function(t) {
88-
var totalArray = object.array.length;
89-
var collector1 = dop.collect();
90-
collector1.filter = function(mutation){
91-
return (mutation.object.length===totalArray+1);
92-
};
93-
object.array.push(Math.random());
94-
var collector2 = dop.collect();
95-
object.array.push(Math.random());
96-
t.equal(collector1.mutations.length, 1);
97-
t.equal(collector2.mutations.length, 1);
98-
object.array.push(Math.random());
99-
t.equal(collector1.mutations.length, 1);
100-
t.equal(collector2.mutations.length, 2);
101+
var totalArray = object.array.length
102+
var collector1 = dop.collect()
103+
collector1.filter = function(mutation) {
104+
return mutation.object.length === totalArray + 1
105+
}
106+
object.array.push(Math.random())
107+
var collector2 = dop.collect()
108+
object.array.push(Math.random())
109+
t.equal(collector1.mutations.length, 1)
110+
t.equal(collector2.mutations.length, 1)
111+
object.array.push(Math.random())
112+
t.equal(collector1.mutations.length, 1)
113+
t.equal(collector2.mutations.length, 2)
101114
delete collector1.filter
102-
object.array.push(Math.random());
103-
t.equal(collector1.mutations.length, 2);
104-
t.equal(collector2.mutations.length, 2);
105-
collector1.destroy();
106-
collector2.destroy();
107-
t.end();
108-
});
109-
110-
115+
object.array.push(Math.random())
116+
t.equal(collector1.mutations.length, 1)
117+
t.equal(collector2.mutations.length, 3)
118+
collector1.destroy()
119+
collector2.destroy()
120+
t.end()
121+
})
111122

112123
test('Emit', function(t) {
113-
var collector = dop.collect();
114-
object.array.push(Math.random());
115-
t.equal(collector.mutations.length, 1);
116-
collector.emitWithoutDestroy();
117-
t.equal(collector.mutations.length, 0);
118-
object.array.push(Math.random());
119-
t.equal(collector.mutations.length, 1);
120-
collector.emit();
121-
object.array.push(Math.random());
122-
t.equal(collector.mutations.length, 0);
123-
t.end();
124-
});
125-
126-
127-
124+
var collector = dop.collect()
125+
object.array.push(Math.random())
126+
t.equal(collector.mutations.length, 1)
127+
collector.emitWithoutDestroy()
128+
t.equal(collector.mutations.length, 0)
129+
object.array.push(Math.random())
130+
t.equal(collector.mutations.length, 1)
131+
collector.emit()
132+
object.array.push(Math.random())
133+
t.equal(collector.mutations.length, 0)
134+
t.end()
135+
})
128136

129137
test('Testing shadow option on set', function(t) {
130-
var collector = dop.collect();
131-
dop.set(object, 'noshadow', [2,3,4])
132-
dop.set(object, 'shadow', [2,3,4], {shadow:true})
133-
t.equal(collector.mutations.length, 1);
134-
t.deepEqual(object.noshadow, object.shadow);
135-
collector.destroy();
136-
t.end();
137-
});
138+
var collector = dop.collect()
139+
dop.set(object, 'noshadow', [2, 3, 4])
140+
dop.set(object, 'shadow', [2, 3, 4], { shadow: true })
141+
t.equal(collector.mutations.length, 1)
142+
t.deepEqual(object.noshadow, object.shadow)
143+
collector.destroy()
144+
t.end()
145+
})

0 commit comments

Comments
 (0)