-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.js
352 lines (316 loc) · 10.1 KB
/
funcs.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*global define: false */
/**
* Function wrappers and utilities for enhanced behavior.
*
* @name funcs
* @namespace funcs
* @author Sagie Gur-Ari
*/
/**
* Initializes the funcs API.
*
* @function
* @memberof! funcs
* @alias funcs.initFuncs
* @private
* @param {Object} global - The root context (window/global/...)
* @param {function} factory - Returns a new instance of the API
* @returns {Object} New instance of the API
*/
(function initFuncs(global, factory) {
'use strict';
const funcs = factory();
/*istanbul ignore next*/
/**
* Initializes the funcs API (only used for testing).
*
* @function
* @memberof! funcs
* @alias funcs.initFuncsFromContext
* @private
* @param {Object} context - The root context (window/global/...)
* @returns {Object} New instance of the API
*/
funcs.initFuncsFromContext = function (context) {
return initFuncs(context, factory);
};
/*istanbul ignore next*/
if ((typeof define === 'function') && define.amd) {
define(function defineLib() {
return funcs;
});
} else if ((typeof module === 'object') && module.exports) {
module.exports = funcs;
} else {
global.funcs = funcs;
}
return funcs;
}(this, function initFuncs() {
'use strict';
const funcs = {};
/**
* Adds chaining support for the provided function.
*
* @function
* @memberof! funcs
* @alias funcs.addChaining
* @private
* @param {function} fn - Adds the funcs APIs to the provided function with this function as a context
*/
const addChaining = function (fn) {
/**
* Chain function.
*
* @function
* @memberof! funcs
* @alias funcs.maxTimesChain
* @private
* @param {Number} times - The max times the provided function will be invoked
* @returns {function} The new wrapper function
*/
fn.maxTimes = function (times) {
return funcs.maxTimes(fn, times);
};
/**
* Chain function.
*
* @function
* @memberof! funcs
* @alias funcs.onceChain
* @private
* @returns {function} The new wrapper function
*/
fn.once = function () {
return funcs.once(fn);
};
/**
* Chain function.
*
* @function
* @memberof! funcs
* @alias funcs.delayChain
* @private
* @param {Number} delay - The invocation delay in millies
* @returns {function} The new wrapper function
*/
fn.delay = function (delay) {
return funcs.delay(fn, delay);
};
/**
* Chain function.
*
* @function
* @memberof! funcs
* @alias funcs.asyncChain
* @private
* @returns {function} The new wrapper function
*/
fn.async = function () {
return funcs.async(fn);
};
};
/**
* Empty function.
*
* @function
* @memberof! funcs
* @alias funcs.noop
* @public
* @returns {undefined} Undefined
*/
funcs.noop = function () {
return undefined;
};
/**
* Returns true if the provided argument is a function.
*
* @function
* @memberof! funcs
* @alias funcs.isFunction
* @public
* @param {function} [fn] - The function to check
* @returns {Boolean} True if the provided argument is a function
* @example
* ````js
* const isFn = funcs.isFunction(myFunction);
*
* funcs.isFunction(function () {}); //true
* funcs.isFunction(); //false
* funcs.isFunction(5); //false
* funcs.isFunction(true); //false
* ````
*/
funcs.isFunction = function (fn) {
return (fn && (typeof fn === 'function')) || false;
};
/**
* Ensures a return function.<br>
* If a function is provided, it will be returned, otherwise a noop function will be returned.
*
* @function
* @memberof! funcs
* @alias funcs.ensure
* @public
* @param {function} [fn] - The function to check
* @returns {function} The original function if provided, or a noop
* @example
* ````js
* const handler = funcs.ensure(maybeHandler);
* ````
*/
funcs.ensure = function (fn) {
if (!this.isFunction(fn)) {
return funcs.noop;
}
return fn;
};
/**
* Wraps the provided function and ensures it is invoked no more than the provided amount.<br>
* This function output can be chained with other funcs apis.
*
* @function
* @memberof! funcs
* @alias funcs.maxTimes
* @public
* @param {function} fn - The function to wrap
* @param {Number} times - The max times the provided function will be invoked
* @param {Object} [options] - see details
* @param {Boolean} [options.callbackStyle=false] - If true, the provided function will only get the first 2 arguments (will improve runtime performance)
* @returns {function} The new wrapper function
* @example
* ````js
* const onlyOnceCallback = funcs.maxTimes(callback, 1);
*
* //can also chain multiple modifications (chained functions do not require original function as argument)
* const delayedMaxTimesCallback = funcs.maxTimes(callback, 5).delay(500);
* ````
*/
funcs.maxTimes = function (fn, times, options) {
if ((!this.isFunction(fn)) || (!times) || (typeof times !== 'number') || (times < 0)) {
return this.noop;
}
let callbackStyle = false;
if (options && options.callbackStyle) {
callbackStyle = true;
}
let counter = 0;
const fnMaxTimesWrapper = function (arg1, arg2) {
if (counter < times) {
counter++;
if (callbackStyle) {
return fn(arg1, arg2);
}
if (!arguments.length) {
return fn();
}
return fn.apply(null, arguments);
}
};
//add chaining support
addChaining(fnMaxTimesWrapper);
return fnMaxTimesWrapper;
};
/**
* Ensures the provided function is invoked only once.<br>
* This is the same as calling funcs.maxTimes(fn, 1)<br>
* This function output can be chained with other funcs apis.
*
* @function
* @memberof! funcs
* @alias funcs.once
* @public
* @param {function} fn - The function to wrap
* @param {Object} [options] - see details
* @param {Boolean} [options.callbackStyle=false] - If true, the provided function will only get the first 2 arguments (will improve runtime performance)
* @returns {function} The new wrapper function
* @example
* ````js
* const onlyOnceCallback = funcs.once(callback);
*
* //can also chain multiple modifications (chained functions do not require original function as argument)
* const asyncOnceCallback = funcs.once(callback).async();
* ````
*/
funcs.once = function (fn, options) {
return this.maxTimes(fn, 1, options);
};
/**
* Trigger the actual function only after the provided delay.<br>
* This function output can be chained with other funcs apis.
*
* @function
* @memberof! funcs
* @alias funcs.delay
* @public
* @param {function} fn - The function to wrap
* @param {Number} [delay=0] - The invocation delay in millies
* @param {Object} [options] - see details
* @param {Boolean} [options.callbackStyle=false] - If true, the provided function will only get the first 2 arguments (will improve runtime performance)
* @returns {function} The new wrapper function
* @example
* ````js
* const delayedCallback = funcs.delay(callback, 500);
*
* //can also chain multiple modifications (chained functions do not require original function as argument)
* const delayedMaxTimesCallback = funcs.delay(callback, 500).maxTimes(5);
* ````
*/
funcs.delay = function (fn, delay, options) {
if (!this.isFunction(fn)) {
return this.noop;
}
//if delay not provided, but options were provided
if (delay && (typeof delay === 'object')) {
options = delay;
delay = 0;
}
delay = delay || 0;
if (delay < 0) {
return fn;
}
let callbackStyle = false;
if (options && options.callbackStyle) {
callbackStyle = true;
}
const fnDelayWrapper = function (arg1, arg2) {
const fnArguments = arguments;
setTimeout(function postDelay() {
if (callbackStyle) {
fn(arg1, arg2);
} else if (!fnArguments.length) {
fn();
} else {
fn.apply(null, fnArguments);
}
}, delay);
};
//add chaining support
addChaining(fnDelayWrapper);
return fnDelayWrapper;
};
/**
* Ensures the function is invoked only in the next cycle.<br>
* This is the same as calling funcs.delay(fn, 0)<br>
* This function output can be chained with other funcs apis.
*
* @function
* @memberof! funcs
* @alias funcs.async
* @public
* @param {function} fn - The function to wrap
* @param {Object} [options] - see details
* @param {Boolean} [options.callbackStyle=false] - If true, the provided function will only get the first 2 arguments (will improve runtime performance)
* @returns {function} The new wrapper function
* @example
* ````js
* const asyncCallback = funcs.async(callback);
*
* //can also chain multiple modifications (chained functions do not require original function as argument)
* const asyncOnceCallback = funcs.async(callback).once();
* ````
*/
funcs.async = function (fn, options) {
return this.delay(fn, 0, options);
};
return funcs;
}));