UNPKG

1.34 kBJavaScriptView Raw
1import LazyWrapper from './_LazyWrapper.js';
2import LodashWrapper from './_LodashWrapper.js';
3import baseAt from './_baseAt.js';
4import flatRest from './_flatRest.js';
5import isIndex from './_isIndex.js';
6import thru from './thru.js';
7
8/**
9 * This method is the wrapper version of `_.at`.
10 *
11 * @name at
12 * @memberOf _
13 * @since 1.0.0
14 * @category Seq
15 * @param {...(string|string[])} [paths] The property paths to pick.
16 * @returns {Object} Returns the new `lodash` wrapper instance.
17 * @example
18 *
19 * var object = { 'a': [{ 'b': { 'c': 3 } }, 4] };
20 *
21 * _(object).at(['a[0].b.c', 'a[1]']).value();
22 * // => [3, 4]
23 */
24var wrapperAt = flatRest(function(paths) {
25 var length = paths.length,
26 start = length ? paths[0] : 0,
27 value = this.__wrapped__,
28 interceptor = function(object) { return baseAt(object, paths); };
29
30 if (length > 1 || this.__actions__.length ||
31 !(value instanceof LazyWrapper) || !isIndex(start)) {
32 return this.thru(interceptor);
33 }
34 value = value.slice(start, +start + (length ? 1 : 0));
35 value.__actions__.push({
36 'func': thru,
37 'args': [interceptor],
38 'thisArg': undefined
39 });
40 return new LodashWrapper(value, this.__chain__).thru(function(array) {
41 if (length && !array.length) {
42 array.push(undefined);
43 }
44 return array;
45 });
46});
47
48export default wrapperAt;