createObjectMapper.js 736 B

1234567891011121314151617181920212223242526
  1. var baseCallback = require('./baseCallback'),
  2. baseForOwn = require('./baseForOwn');
  3. /**
  4. * Creates a function for `_.mapKeys` or `_.mapValues`.
  5. *
  6. * @private
  7. * @param {boolean} [isMapKeys] Specify mapping keys instead of values.
  8. * @returns {Function} Returns the new map function.
  9. */
  10. function createObjectMapper(isMapKeys) {
  11. return function(object, iteratee, thisArg) {
  12. var result = {};
  13. iteratee = baseCallback(iteratee, thisArg, 3);
  14. baseForOwn(object, function(value, key, object) {
  15. var mapped = iteratee(value, key, object);
  16. key = isMapKeys ? mapped : key;
  17. value = isMapKeys ? value : mapped;
  18. result[key] = value;
  19. });
  20. return result;
  21. };
  22. }
  23. module.exports = createObjectMapper;