@@ -14,43 +14,55 @@ var _ = require('lodash');
14
14
* @param {Boolean } options.semanticTypes enable semantic type detection (default: false)
15
15
* @param {Boolean } options.storeValues enable storing of values (default: true)
16
16
*
17
- * @param {Function } fn Callback which will be passed `(err, schema)`
17
+ * @param {Function } callback Callback which will be passed `(err, schema)`
18
+ * @return {Promise } You can await promise, or use callback if provided.
18
19
*/
19
- module . exports = function ( docs , options , fn ) {
20
- // shift parameters if no options are specified
21
- if ( _ . isUndefined ( options ) || _ . isFunction ( options ) && _ . isUndefined ( fn ) ) {
22
- fn = options ;
23
- options = { } ;
24
- }
20
+ module . exports = function ( docs , options , callback ) {
21
+ const promise = new Promise ( ( resolve , reject ) => {
22
+ // shift parameters if no options are specified
23
+ if ( _ . isUndefined ( options ) || ( _ . isFunction ( options ) && _ . isUndefined ( callback ) ) ) {
24
+ callback = options ;
25
+ options = { } ;
26
+ }
27
+
28
+ var src ;
29
+ // MongoDB Cursors
30
+ if ( docs . stream && typeof docs . stream === 'function' ) {
31
+ src = docs . stream ( ) ;
32
+ // Streams
33
+ } else if ( docs . pipe && typeof docs . pipe === 'function' ) {
34
+ src = docs ;
35
+ // Arrays
36
+ } else if ( _ . isArray ( docs ) ) {
37
+ src = es . readArray ( docs ) ;
38
+ } else {
39
+ reject ( new Error (
40
+ 'Unknown input type for `docs`. Must be an array, ' +
41
+ 'stream or MongoDB Cursor.'
42
+ ) ) ;
43
+ return ;
44
+ }
45
+
46
+ var result ;
47
+
48
+ src
49
+ . pipe ( stream ( options ) )
50
+ . on ( 'data' , function ( data ) {
51
+ result = data ;
52
+ } )
53
+ . on ( 'error' , function ( err ) {
54
+ reject ( err ) ;
55
+ } )
56
+ . on ( 'end' , function ( ) {
57
+ resolve ( result ) ;
58
+ } ) ;
59
+ } ) ;
25
60
26
- var src ;
27
- // MongoDB Cursors
28
- if ( docs . stream && typeof docs . stream === 'function' ) {
29
- src = docs . stream ( ) ;
30
- // Streams
31
- } else if ( docs . pipe && typeof docs . pipe === 'function' ) {
32
- src = docs ;
33
- // Arrays
34
- } else if ( _ . isArray ( docs ) ) {
35
- src = es . readArray ( docs ) ;
36
- } else {
37
- fn ( new Error ( 'Unknown input type for `docs`. Must be an array, '
38
- + 'stream or MongoDB Cursor.' ) ) ;
39
- return ;
61
+ if ( callback && typeof callback === 'function' ) {
62
+ promise . then ( callback . bind ( null , null ) , callback ) ;
40
63
}
41
64
42
- var result ;
43
-
44
- src . pipe ( stream ( options ) )
45
- . on ( 'data' , function ( data ) {
46
- result = data ;
47
- } )
48
- . on ( 'error' , function ( err ) {
49
- fn ( err ) ;
50
- } )
51
- . on ( 'end' , function ( ) {
52
- fn ( null , result ) ;
53
- } ) ;
65
+ return promise ;
54
66
} ;
55
67
56
68
module . exports . stream = stream ;
0 commit comments