/
index.js
185 lines (167 loc) · 4.94 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*!
* async-simple-iterator <https://github.com/tunnckoCore/async-simple-iterator>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var utils = require('./utils')
var AppBase = require('app-base').AppBase
/**
* > Initialize `AsyncSimpleIterator` with `options`.
*
* **Example**
*
* ```js
* var ctrl = require('async')
* var AsyncSimpleIterator = require('async-simple-iterator').AsyncSimpleIterator
*
* var fs = require('fs')
* var base = new AsyncSimpleIterator({
* settle: true,
* beforeEach: function (val) {
* console.log('before each:', val)
* },
* error: function (err, res, val) {
* console.log('on error:', val)
* }
* })
* var iterator = base.wrapIterator(fs.stat, {
* afterEach: function (err, res, val) {
* console.log('after each:', val)
* }
* })
*
* ctrl.map([
* 'path/to/existing/file.js',
* 'filepath/not/exist',
* 'path/to/file'
* ], iterator, function (err, results) {
* // => `err` will always be null, if `settle:true`
* // => `results` is now an array of stats for each file
* })
* ```
*
* @param {Object=} `options` Pass `beforeEach`, `afterEach` and `error` hooks or `settle:true`.
* @api public
*/
function AsyncSimpleIterator (options) {
if (!(this instanceof AsyncSimpleIterator)) {
return new AsyncSimpleIterator(options)
}
this.defaultOptions(options)
AppBase.call(this)
this.on = utils.emitter.compose.call(this, 'on', this.options)
this.off = utils.emitter.compose.call(this, 'off', this.options)
this.once = utils.emitter.compose.call(this, 'once', this.options)
this.emit = utils.emitter.compose.call(this, 'emit', this.options)
}
AppBase.extend(AsyncSimpleIterator)
/**
* > Setting default options. Default `settle` option is `false`.
*
* @param {Object=} `options` Pass `beforeEach`, `afterEach` and `error` hooks or `settle:true`.
* @return {Object} `AsyncSimpleIterator` instance for chaining.
* @api private
*/
AppBase.define(AsyncSimpleIterator.prototype, 'defaultOptions', function defaultOptions (opts) {
var options = this.options || null
var context = opts
? utils.extend({}, options && options.context, opts.context)
: (options && options.context || null)
opts = opts ? utils.extend(options, opts) : opts
opts = utils.extend({
emitter: new utils.EventEmitter(),
settle: false
}, options, opts)
opts.context = context
this.options = opts
return this
})
/**
* > Wraps `iterator` function which then can be passed to [async][] lib.
* You can pass returned iterator function to **every** [async][] method that you want.
*
* **Example**
*
* ```js
* var ctrl = require('async')
* var base = require('async-simple-iterator')
*
* base
* .on('afterEach', function (err, res, value, key, next) {
* console.log('after each:', err, res, value, key)
* })
* .on('error', function (err, res, value, key, next) {
* console.log('on error:', err, res, value, key)
* })
*
* var iterator = base.wrapIterator(function (value, key, next) {
* console.log('actual:', value, key)
*
* if (key === 'dev') {
* var err = new Error('err:' + key)
* err.value = value
* next(err)
* return
* }
* next(null, 123 + key + 456)
*
* }, {
* settle: true,
* beforeEach: function (value, key, next) {
* console.log('before each:', value, key)
* }
* })
*
* ctrl.forEachOf({
* dev: './dev.json',
* test: './test.json',
* prod: './prod.json'
* }, iterator, function done (err) {
* // if settle:false, `err`
* // if settle:true, `null`
* console.log('end:', err)
* })
* ```
*
* @emit `beforeEach` with signature `val[, value], next`
* @emit `afterEach` with signature `err, res, val[, value], next`
* @emit `error` with signature `err, res, val[, value], next`
*
* @name .wrapIterator
* @param {Function} `iterator` Iterator to pass to [async][] lib.
* @param {Object=} `options` Pass `beforeEach`, `afterEach` and `error` hooks or `settle` option.
* @return {Function} Wrapped `iterator` function which can be passed to every [async][] method.
* @api public
*/
AppBase.define(AsyncSimpleIterator.prototype, 'wrapIterator', function wrapIterator (iterator, options) {
if (typeof iterator !== 'function') {
throw new TypeError('async-simple-iterator: expect `iterator` to be function')
}
this.defaultOptions(options)
var hooks = ['beforeEach', 'afterEach', 'error']
var len = hooks.length
var i = 0
while (i < len) {
var name = hooks[i++]
if (typeof this.options[name] === 'function') {
this.on(name, this.options[name])
}
}
return utils.iteratorFactory(this, iterator)
})
/**
* Expose `AsyncSimpleIterator` instance
*
* @type {Object}
* @api private
*/
module.exports = new AsyncSimpleIterator()
/**
* Expose `AsyncSimpleIterator` constructor
*
* @type {Function}
* @api private
*/
module.exports.AsyncSimpleIterator = AsyncSimpleIterator