Files: 2225e7077f112ee10318157443adbbdb5748a50e / transforms / uniq.js
869 bytesRaw
1 | var Value = require('./value') |
2 | |
3 | module.exports = Uniq |
4 | |
5 | function Uniq (obs) { |
6 | var object = [] |
7 | update() |
8 | |
9 | var observable = Value(object) |
10 | var broadcast = observable.set |
11 | obs(refresh) |
12 | |
13 | observable.has = function (valueOrObs) { |
14 | return !!~object.indexOf(valueOrObs) |
15 | } |
16 | |
17 | return observable |
18 | |
19 | function update () { |
20 | var currentValues = object.map(get) |
21 | var newValues = obs() |
22 | currentValues.filter(notIncluded, newValues).forEach(removeFrom, object) |
23 | newValues.filter(notIncluded, currentValues).forEach(addTo, object) |
24 | } |
25 | |
26 | function refresh () { |
27 | update() |
28 | broadcast(object) |
29 | } |
30 | } |
31 | |
32 | function get (value) { |
33 | return value |
34 | } |
35 | |
36 | function notIncluded (value) { |
37 | return !~this.indexOf(value) |
38 | } |
39 | |
40 | function removeFrom (item) { |
41 | var index = this.indexOf(item) |
42 | if (~index) { |
43 | this.splice(index, 1) |
44 | } |
45 | } |
46 | |
47 | function addTo (item) { |
48 | this.push(item) |
49 | } |
50 |
Built with git-ssb-web