object.js 765 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict'
  2. module.exports = {
  3. prop: prop => obj => obj[prop],
  4. defaults: (obj, values) => {
  5. Object.keys(values).forEach(key => {
  6. if (!obj.hasOwnProperty(key)) {
  7. obj[key] = values[key]
  8. }
  9. })
  10. },
  11. forEach: (obj, callback) => {
  12. Object.keys(obj).forEach(key => {
  13. callback(key, obj[key])
  14. })
  15. },
  16. reduce: (obj, fnReduce, startValue) => {
  17. return Object.keys(obj).reduce((acc, key) => {
  18. return fnReduce.call(null, acc, key, obj[key])
  19. }, startValue)
  20. },
  21. map: (obj, fnMap) => {
  22. return Object.keys(obj).reduce((mapped, key) => {
  23. mapped[key] = fnMap(obj[key], key)
  24. return mapped
  25. }, {})
  26. }
  27. }