git ssb

1+

Matt McKegg / mutant



Tree: 955ee4ba0914a6c2c9a1158bdfa7ea7a186ca900

Files: 955ee4ba0914a6c2c9a1158bdfa7ea7a186ca900 / transforms / uniq.js

869 bytesRaw
1var Value = require('./value')
2
3module.exports = Uniq
4
5function 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
32function get (value) {
33 return value
34}
35
36function notIncluded (value) {
37 return !~this.indexOf(value)
38}
39
40function removeFrom (item) {
41 var index = this.indexOf(item)
42 if (~index) {
43 this.splice(index, 1)
44 }
45}
46
47function addTo (item) {
48 this.push(item)
49}
50

Built with git-ssb-web