-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (71 loc) · 1.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*!
* hybridify-all <https://github.com/hybridables/hybridify-all>
*
* Copyright (c) 2015 Charlike Mike Reagent, contributors.
* Released under the MIT license.
*/
'use strict'
var reduce = require('object.reduce')
var hybridify = require('hybridify')
/**
* > Hybridifies all the selected functions in an object.
*
* **Example:**
*
* ```js
* var hybridifyAll = require('hybridify-all')
* var fs = require('fs')
*
* fs = hybridifyAll(fs)
* fs.readFile(__filename, 'utf8', function(err, res) {
* //=> err, res
* })
* .then(function(res) {
* //=> res
* return fs.stat(__filename)
* })
* .then(function(stat) {
* assert.strictEqual(stat.size, fs.statSync(__filename).size)
* })
* ```
*
* @name hybridifyAll
* @param {Object|Function} `<source>` the source object for the async functions
* @param {Object|Function} `[dest]` the destination to set all the hybridified methods
* @return {Object|Function}
* @api public
*/
module.exports = function hybridifyAll (source, dest) {
if (!source) {
throw new Error('hybridify-all: should have at least 1 arguments')
}
if (typeOf(source) !== 'function' && typeOf(source) !== 'object') {
throw new TypeError('hybridify-all: expect `source` be object|function')
}
dest = dest || {}
if (typeof source === 'function') {
dest = hybridify(source)
}
return Object.keys(source).length ? reduce(source, function (dest, fn, key) {
if (typeof fn === 'function') {
dest[key] = hybridify(fn)
}
return dest
}, dest) : dest
}
/**
* Get correct type of value
*
* @param {*} `val`
* @return {String}
* @api private
*/
function typeOf (val) {
if (Array.isArray(val)) {
return 'array'
}
if (typeof val !== 'object') {
return typeof val
}
return Object.prototype.toString(val).slice(8, -1).toLowerCase()
}