UNPKG

2.95 kBJavaScriptView Raw
1/**
2 * Lo-Dash 2.2.1 (Custom Build) <http://lodash.com/>
3 * Build: `lodash modularize modern exports="npm" -o ./npm/`
4 * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
5 * Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
6 * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
7 * Available under MIT license <http://lodash.com/license>
8 */
9var createCallback = require('lodash.createcallback'),
10 slice = require('lodash._slice');
11
12/* Native method shortcuts for methods with the same name as other `lodash` methods */
13var nativeMax = Math.max;
14
15/**
16 * Gets the last element or last `n` elements of an array. If a callback is
17 * provided elements at the end of the array are returned as long as the
18 * callback returns truey. The callback is bound to `thisArg` and invoked
19 * with three arguments; (value, index, array).
20 *
21 * If a property name is provided for `callback` the created "_.pluck" style
22 * callback will return the property value of the given element.
23 *
24 * If an object is provided for `callback` the created "_.where" style callback
25 * will return `true` for elements that have the properties of the given object,
26 * else `false`.
27 *
28 * @static
29 * @memberOf _
30 * @category Arrays
31 * @param {Array} array The array to query.
32 * @param {Function|Object|number|string} [callback] The function called
33 * per element or the number of elements to return. If a property name or
34 * object is provided it will be used to create a "_.pluck" or "_.where"
35 * style callback, respectively.
36 * @param {*} [thisArg] The `this` binding of `callback`.
37 * @returns {*} Returns the last element(s) of `array`.
38 * @example
39 *
40 * _.last([1, 2, 3]);
41 * // => 3
42 *
43 * _.last([1, 2, 3], 2);
44 * // => [2, 3]
45 *
46 * _.last([1, 2, 3], function(num) {
47 * return num > 1;
48 * });
49 * // => [2, 3]
50 *
51 * var food = [
52 * { 'name': 'beet', 'organic': false },
53 * { 'name': 'carrot', 'organic': true }
54 * ];
55 *
56 * // using "_.pluck" callback shorthand
57 * _.last(food, 'organic');
58 * // => [{ 'name': 'carrot', 'organic': true }]
59 *
60 * var food = [
61 * { 'name': 'banana', 'type': 'fruit' },
62 * { 'name': 'beet', 'type': 'vegetable' },
63 * { 'name': 'carrot', 'type': 'vegetable' }
64 * ];
65 *
66 * // using "_.where" callback shorthand
67 * _.last(food, { 'type': 'vegetable' });
68 * // => [{ 'name': 'beet', 'type': 'vegetable' }, { 'name': 'carrot', 'type': 'vegetable' }]
69 */
70function last(array, callback, thisArg) {
71 var n = 0,
72 length = array ? array.length : 0;
73
74 if (typeof callback != 'number' && callback != null) {
75 var index = length;
76 callback = createCallback(callback, thisArg, 3);
77 while (index-- && callback(array[index], index, array)) {
78 n++;
79 }
80 } else {
81 n = callback;
82 if (n == null || thisArg) {
83 return array ? array[length - 1] : undefined;
84 }
85 }
86 return slice(array, nativeMax(0, length - n));
87}
88
89module.exports = last;