git ssb

1+

Matt McKegg / mutant



Commit 6a2488358b3153546476aba65e4cc9dd5bb5663f

add dict-to-collection, mapped-array and mapped-dict

Matt McKegg committed on 9/10/2016, 6:21:54 AM
Parent: ec5f24a6f4ba05019584fb51d27668e5766a4963

Files changed

dict-to-collection.jsadded
mapped-array.jsadded
mapped-dict.jsadded
dict-to-collection.jsView
@@ -1,0 +1,58 @@
1+var addCollectionMethods = require('./lib/add-collection-methods')
2+var computed = require('./computed')
3+var KeyValue = require('./lib/key-value')
4+var resolve = require('./resolve')
5+var isObservable = require('./is-observable')
6+
7+module.exports = function DictToCollection (obs) {
8+ var value = []
9+ var raw = []
10+
11+ var instance = computed.extended(obs, function () {
12+ var keys = getKeys(obs)
13+ var length = keys.length
14+
15+ for (var i = 0; i < length; i++) {
16+ var key = keys[i]
17+ var item = obs.get(key)
18+ if (shouldUpdate(item, raw[i])) {
19+ if (raw[i].value !== item) {
20+ raw[i].value.set(item)
21+ }
22+ } else {
23+ raw[i] = KeyValue(key, item)
24+ }
25+ value[i] = resolve(raw[i])
26+ }
27+
28+ raw.length = value.length = length
29+ return value
30+ })
31+
32+ var result = function MutantDictToCollection (listener) {
33+ return instance(listener)
34+ }
35+
36+ // getLength, get, indexOf, etc
37+ addCollectionMethods(result, raw, instance.checkUpdated)
38+
39+ return result
40+}
41+
42+function shouldUpdate (newItem, keyValue) {
43+ if (!keyValue) {
44+ return false
45+ } else if (isObservable(newItem) && keyValue.value === newItem) {
46+ return true
47+ } else {
48+ return !keyValue.isBound
49+ }
50+}
51+
52+function getKeys (value) {
53+ if (isObservable(value) && value.keys) {
54+ return resolve(value.keys)
55+ } else {
56+ return Object.keys(resolve(value))
57+ }
58+}
mapped-array.jsView
@@ -1,0 +1,50 @@
1+var MutantMap = require('./map')
2+var MutantArray = require('./array')
3+
4+module.exports = MutantMappedArray
5+
6+function MutantMappedArray (defaultItems, lambda, opts) {
7+ opts = opts || {}
8+
9+ var list = MutantArray(defaultItems, {
10+ comparer: opts.comparer,
11+ fixedIndexing: true
12+ })
13+
14+ var obs = MutantMap(list, lambda, opts)
15+ obs.set = list.set
16+
17+ obs.push = function (item) {
18+ list.push(item)
19+ return obs.get(obs.getLength() - 1)
20+ }
21+
22+ obs.insert = function (item, at) {
23+ list.insert(item, at)
24+ return obs.get(at)
25+ }
26+
27+ obs.remove = function (mappedItem) {
28+ var index = obs.indexOf(mappedItem)
29+ if (~index) {
30+ list.deleteAt(index)
31+ return true
32+ }
33+ }
34+
35+ obs.move = function (mappedItem, targetIndex) {
36+ var currentIndex = obs.indexOf(mappedItem)
37+ if (~currentIndex) {
38+ var item = list.get(currentIndex)
39+ if (currentIndex < targetIndex) {
40+ list.insert(item, targetIndex + 1)
41+ list.deleteAt(currentIndex)
42+ } else {
43+ list.insert(item, targetIndex)
44+ list.deleteAt(currentIndex)
45+ }
46+ }
47+ }
48+
49+ return obs
50+}
mapped-dict.jsView
@@ -1,0 +1,35 @@
1+var MutantLookup = require('./lookup')
2+var MutantDict = require('./dict')
3+var DictToCollection = require('./dict-to-collection')
4+
5+module.exports = MutantMappedDict
6+
7+function MutantMappedDict (defaultItems, lambda, opts) {
8+ opts = opts || {}
9+
10+ var list = MutantDict(defaultItems, {
11+ comparer: opts.comparer
12+ })
13+
14+ var observable = MutantLookup(DictToCollection(list), function (item, invalidateOn) {
15+ var value = lambda(item.key, item.value, invalidateOn)
16+ if (value[0] === item.key && value[1] === item.value) {
17+ return item // passthru
18+ } else {
19+ return value
20+ }
21+ }, opts)
22+
23+ observable.set = list.set
24+
25+ observable.put = function (key, item) {
26+ list.put(key, item)
27+ return observable.get(key)
28+ }
29+
30+ observable.delete = function (key) {
31+ list.remove(key)
32+ }
33+
34+ return observable
35+}

Built with git-ssb-web