Files: 1802ce94b0eb0903cc5f9dd7e00aed245b4f124b / lib / util.js
1274 bytesRaw
1 | var ref = require('ssb-ref') |
2 | |
3 | function isObject(o) { |
4 | return o && 'object' === typeof o |
5 | } |
6 | var DEFAULT_PORT = 8008 |
7 | |
8 | var isArray = Array.isArray |
9 | |
10 | var isInteger = Number.isInteger |
11 | |
12 | function isString (s) { |
13 | return 'string' === typeof s |
14 | } |
15 | |
16 | var 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 | |
21 | var 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 | |
37 | var 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 | |
50 | exports.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