wrapperAt.js 1.3 KB

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