git ssb

1+

Matt McKegg / mutant



Commit 784a83eb521d4e284478ac2ee4160e2e85a617bc

add concat

Matt McKegg committed on 7/15/2016, 4:39:41 AM
Parent: 3f950d344b2e9e7cd202b465ec4e3ca15225155b

Files changed

test/test-concat.jsadded
concat.jsadded
test/test-concat.jsView
@@ -1,0 +1,32 @@
1 +var Array = require('../array')
2 +var concat = require('../concat')
3 +var Map = require('../map')
4 +var Value = require('../value')
5 +var computed = require('../computed')
6 +
7 +var value = Value('human')
8 +var array = Array(['cat'])
9 +var array2 = Array(['cat'])
10 +var result = concat([array, array2])
11 +
12 +var map = Map(result, function (obj) {
13 + if (typeof obj === 'function') {
14 + console.log('mapping => obs')
15 + return computed([obj], (x) => x + ' [dynamic]')
16 + } else {
17 + console.log('mapping => ', obj)
18 + return obj + ' [static]'
19 + }
20 +})
21 +
22 +result(x => console.log(x))
23 +map(x => console.log('[map]', x))
24 +
25 +array.push('dog')
26 +array2.push('cow')
27 +array.push(value)
28 +array2.push('chicken')
29 +array.push('wolf')
30 +array.delete('cat')
31 +array2.insert('sheep', 0)
32 +value.set('monkey')
concat.jsView
@@ -1,0 +1,52 @@
1 +var computed = require('./computed')
2 +
3 +module.exports = function Concat (observables) {
4 + var values = []
5 + var rawValues = []
6 +
7 + var result = computed(observables, function (args) {
8 + var index = 0
9 + for (var i = 0; i < arguments.length; i++) {
10 + for (var x = 0; x < arguments[i].length; x++) {
11 + var value = arguments[i][x]
12 + var raw = get(observables[i], x)
13 + values[index] = value
14 + rawValues[index] = raw
15 + index += 1
16 + }
17 + }
18 + values.length = index
19 + rawValues.length = index
20 + return values
21 + })
22 +
23 + result.get = function (index) {
24 + return rawValues[index]
25 + }
26 +
27 + result.getLength = function (index) {
28 + return rawValues.length
29 + }
30 +
31 + result.includes = function (valueOrObs) {
32 + return !!~rawValues.indexOf(valueOrObs)
33 + }
34 +
35 + result.indexOf = function (valueOrObs) {
36 + return rawValues.indexOf(valueOrObs)
37 + }
38 +
39 + return result
40 +}
41 +
42 +function get (target, index) {
43 + if (typeof target === 'function' && !target.get) {
44 + target = target()
45 + }
46 +
47 + if (Array.isArray(target)) {
48 + return target[index]
49 + } else if (target && target.get) {
50 + return target.get(index)
51 + }
52 +}

Built with git-ssb-web