util.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Merge an array of objects into one.
  3. *
  4. * @param {Array<Object>} arr
  5. * @return {Object}
  6. */
  7. export function mergeObjects (arr) {
  8. return arr.reduce((prev, obj) => {
  9. Object.keys(obj).forEach(key => {
  10. const existing = prev[key]
  11. if (existing) {
  12. // allow multiple mutation objects to contain duplicate
  13. // handlers for the same mutation type
  14. if (Array.isArray(existing)) {
  15. existing.push(obj[key])
  16. } else {
  17. prev[key] = [prev[key], obj[key]]
  18. }
  19. } else {
  20. prev[key] = obj[key]
  21. }
  22. })
  23. return prev
  24. }, {})
  25. }
  26. /**
  27. * Deep clone an object. Faster than JSON.parse(JSON.stringify()).
  28. *
  29. * @param {*} obj
  30. * @return {*}
  31. */
  32. export function deepClone (obj) {
  33. if (Array.isArray(obj)) {
  34. return obj.map(deepClone)
  35. } else if (obj && typeof obj === 'object') {
  36. var cloned = {}
  37. var keys = Object.keys(obj)
  38. for (var i = 0, l = keys.length; i < l; i++) {
  39. var key = keys[i]
  40. cloned[key] = deepClone(obj[key])
  41. }
  42. return cloned
  43. } else {
  44. return obj
  45. }
  46. }
  47. /**
  48. * Hacks to get access to Vue internals.
  49. * Maybe we should expose these...
  50. */
  51. let Watcher
  52. export function getWatcher (vm) {
  53. if (!Watcher) {
  54. const unwatch = vm.$watch('__vuex__', a => a)
  55. Watcher = vm._watchers[0].constructor
  56. unwatch()
  57. }
  58. return Watcher
  59. }
  60. let Dep
  61. export function getDep (vm) {
  62. if (!Dep) {
  63. Dep = vm._data.__ob__.dep.constructor
  64. }
  65. return Dep
  66. }