destructuring.js 707 B

1234567891011121314151617181920212223242526272829
  1. var keys = require('./keys')
  2. var slice = require('./slice')
  3. var includes = require('./includes')
  4. var arrayEach = require('./arrayEach')
  5. var assign = require('./assign')
  6. /**
  7. * 将一个或者多个对象值解构到目标对象
  8. *
  9. * @param {Object} destination 目标对象
  10. * @param {...Object}
  11. * @return {Boolean}
  12. */
  13. function destructuring (destination, sources) {
  14. if (destination && sources) {
  15. var rest = assign.apply(this, [{}].concat(slice(arguments, 1)))
  16. var restKeys = keys(rest)
  17. arrayEach(keys(destination), function (key) {
  18. if (includes(restKeys, key)) {
  19. destination[key] = rest[key]
  20. }
  21. })
  22. }
  23. return destination
  24. }
  25. module.exports = destructuring