-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3-adder.js
More file actions
56 lines (45 loc) · 1.2 KB
/
3-adder.js
File metadata and controls
56 lines (45 loc) · 1.2 KB
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
'use strict';
class Adder {
constructor(type, reducer) {
this.type = type;
this.reducer = reducer;
this.next = null;
}
}
class Chain {
constructor() {
this.first = null;
this.last = null;
}
add(adder) {
if (!this.first) this.first = adder;
else this.last.next = adder;
this.last = adder;
return this;
}
process(collection) {
let adder = this.first;
do {
if (collection instanceof adder.type) {
return adder.reducer(collection);
}
adder = adder.next;
} while (adder);
throw new Error('Unsupported collection type');
}
}
// Usage
const sum = (a, b) => a + b;
const chain = new Chain()
.add(new Adder(Array, (array) => array.reduce(sum)))
.add(new Adder(Set, (set) => [...set].reduce(sum)))
.add(new Adder(Uint8Array, (u8a) => Array.from(u8a).reduce(sum)))
.add(new Adder(Object, (obj) => `Not supported ${obj.constructor.name}`));
const sum1 = chain.process([1, 2, 3]);
console.dir({ sum1 });
const sum2 = chain.process(new Set([1, 2, 3]));
console.dir({ sum2 });
const sum3 = chain.process(new Uint8Array([1, 2, 3]));
console.dir({ sum3 });
const sum4 = chain.process(new Int32Array([1, 2, 3]));
console.dir({ sum4 });