1 | var identity = require('./identity');
|
2 | var prop = require('./prop');
|
3 | var deepMatches = require('../object/deepMatches');
|
4 |
|
5 | /**
|
6 | * Converts argument into a valid iterator.
|
7 | * Used internally on most array/object/collection methods that receives a
|
8 | * callback/iterator providing a shortcut syntax.
|
9 | */
|
10 | function makeIterator(src, thisObj){
|
11 | if (src == null) {
|
12 | return identity;
|
13 | }
|
14 | switch(typeof src) {
|
15 | case 'function':
|
16 | // function is the first to improve perf (most common case)
|
17 | // also avoid using `Function#call` if not needed, which boosts
|
18 | // perf a lot in some cases
|
19 | return (typeof thisObj !== 'undefined')? function(val, i, arr){
|
20 | return src.call(thisObj, val, i, arr);
|
21 | } : src;
|
22 | case 'object':
|
23 | return function(val){
|
24 | return deepMatches(val, src);
|
25 | };
|
26 | case 'string':
|
27 | case 'number':
|
28 | return prop(src);
|
29 | }
|
30 | }
|
31 |
|
32 | module.exports = makeIterator;
|
33 |
|
34 |
|