forked from vuejs/vuex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
72 lines (67 loc) · 1.68 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Create a actual callable action function.
*
* @param {String|Function} action
* @param {Vuex} store
* @return {Function} [description]
*/
export function createAction (action, store) {
if (typeof action === 'string') {
// simple action string shorthand
return (...payload) => store.dispatch(action, ...payload)
} else if (typeof action === 'function') {
// normal action
return (...payload) => action(store, ...payload)
}
}
/**
* Merge an array of objects into one.
*
* @param {Array<Object>} arr
* @param {Boolean} allowDuplicate
* @return {Object}
*/
export function mergeObjects (arr, allowDuplicate) {
return arr.reduce((prev, obj) => {
Object.keys(obj).forEach(key => {
const existing = prev[key]
if (existing) {
// allow multiple mutation objects to contain duplicate
// handlers for the same mutation type
if (allowDuplicate) {
if (Array.isArray(existing)) {
existing.push(obj[key])
} else {
prev[key] = [prev[key], obj[key]]
}
} else {
console.warn(`[vuex] Duplicate action: ${ key }`)
}
} else {
prev[key] = obj[key]
}
})
return prev
}, {})
}
/**
* Deep clone an object. Faster than JSON.parse(JSON.stringify()).
*
* @param {*} obj
* @return {*}
*/
export function deepClone (obj) {
if (Array.isArray(obj)) {
return obj.map(deepClone)
} else if (obj && typeof obj === 'object') {
var cloned = {}
var keys = Object.keys(obj)
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i]
cloned[key] = deepClone(obj[key])
}
return cloned
} else {
return obj
}
}