* Gets the first element or first `n` elements of an array. If a callback * is provided elements at the beginning of the array are returned as long * as the callback returns truey. The callback is bound to `thisArg` and * invoked with three arguments; (value, index, array). *
(array, callback, thisArg)
| 39434 | * // => ['barney', 'fred'] |
| 39435 | */ |
| 39436 | function first(array, callback, thisArg) { |
| 39437 | var n = 0, |
| 39438 | length = array ? array.length : 0; |
| 39439 | |
| 39440 | if (typeof callback != 'number' && callback != null) { |
| 39441 | var index = -1; |
| 39442 | callback = lodash.createCallback(callback, thisArg, 3); |
| 39443 | while (++index < length && callback(array[index], index, array)) { |
| 39444 | n++; |
| 39445 | } |
| 39446 | } else { |
| 39447 | n = callback; |
| 39448 | if (n == null || thisArg) { |
| 39449 | return array ? array[0] : undefined; |
| 39450 | } |
| 39451 | } |
| 39452 | return slice(array, 0, nativeMin(nativeMax(0, n), length)); |
| 39453 | } |
| 39454 | |
| 39455 | /** |
| 39456 | * Flattens a nested array (the nesting can be to any depth). If `isShallow` |