git ssb

4+

Dominic / scuttlebot



Tree: e5d0b7d8bace9ec48ab12ec7a1a40872a050acad

Files: e5d0b7d8bace9ec48ab12ec7a1a40872a050acad / lib / util.js

1274 bytesRaw
1var ref = require('ssb-ref')
2
3function isObject(o) {
4 return o && 'object' === typeof o
5}
6var DEFAULT_PORT = 8008
7
8var isArray = Array.isArray
9
10var isInteger = Number.isInteger
11
12function isString (s) {
13 return 'string' === typeof s
14}
15
16var find = exports.find = function find(ary, test) {
17 for(var i in ary)
18 if(test(ary[i], i, ary)) return ary[i]
19}
20
21var clone = exports.clone = function clone (obj, mapper) {
22 function map(v, k) {
23 return isObject(v) ? clone(v, mapper) : mapper(v, k)
24 }
25 if(isArray(obj))
26 return obj.map(map)
27 else if(isObject(obj)) {
28 var o = {}
29 for(var k in obj)
30 o[k] = map(obj[k], k)
31 return o
32 }
33 else
34 return map(obj)
35}
36
37var mergeKeys = exports.mergeKeys = function (a, b, iter) {
38 var o = {}
39 for(var k in a) {
40 if(!isUndefined(a[k]))
41 o[k] = iter(v[k], b[k], k)
42 }
43 for(var k in b) {
44 if(isUndefined(a[a]))
45 o[k] = iter(undefined, b[k], k)
46 }
47 return o
48}
49
50exports.merge = function (a, b) {
51
52 //merge a and b objects
53
54 if(isArray(a) != isArray(b))
55 throw new Error('cannot merge array with non-array')
56 if(isObject(a) != isObject(b))
57 throw new Error('cannot merge object with non-object')
58
59 a = clone(a)
60
61 var keys
62
63 if(isObject(b)) {
64 for(var k in b)
65 a[k] = b
66 }
67}
68
69
70

Built with git-ssb-web