-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathapi-actions.js
132 lines (119 loc) · 3.37 KB
/
api-actions.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* eslint camelcase: "off" */
import { _ } from './utils';
/** @const */ var SET_ACTION = '$set';
/** @const */ var SET_ONCE_ACTION = '$set_once';
/** @const */ var UNSET_ACTION = '$unset';
/** @const */ var ADD_ACTION = '$add';
/** @const */ var APPEND_ACTION = '$append';
/** @const */ var UNION_ACTION = '$union';
/** @const */ var REMOVE_ACTION = '$remove';
/** @const */ var DELETE_ACTION = '$delete';
// Common internal methods for mixpanel.people and mixpanel.group APIs.
// These methods shouldn't involve network I/O.
var apiActions = {
set_action: function(prop, to) {
var data = {};
var $set = {};
if (_.isObject(prop)) {
_.each(prop, function(v, k) {
if (!this._is_reserved_property(k)) {
$set[k] = v;
}
}, this);
} else {
$set[prop] = to;
}
data[SET_ACTION] = $set;
return data;
},
unset_action: function(prop) {
var data = {};
var $unset = [];
if (!_.isArray(prop)) {
prop = [prop];
}
_.each(prop, function(k) {
if (!this._is_reserved_property(k)) {
$unset.push(k);
}
}, this);
data[UNSET_ACTION] = $unset;
return data;
},
set_once_action: function(prop, to) {
var data = {};
var $set_once = {};
if (_.isObject(prop)) {
_.each(prop, function(v, k) {
if (!this._is_reserved_property(k)) {
$set_once[k] = v;
}
}, this);
} else {
$set_once[prop] = to;
}
data[SET_ONCE_ACTION] = $set_once;
return data;
},
union_action: function(list_name, values) {
var data = {};
var $union = {};
if (_.isObject(list_name)) {
_.each(list_name, function(v, k) {
if (!this._is_reserved_property(k)) {
$union[k] = _.isArray(v) ? v : [v];
}
}, this);
} else {
$union[list_name] = _.isArray(values) ? values : [values];
}
data[UNION_ACTION] = $union;
return data;
},
append_action: function(list_name, value) {
var data = {};
var $append = {};
if (_.isObject(list_name)) {
_.each(list_name, function(v, k) {
if (!this._is_reserved_property(k)) {
$append[k] = v;
}
}, this);
} else {
$append[list_name] = value;
}
data[APPEND_ACTION] = $append;
return data;
},
remove_action: function(list_name, value) {
var data = {};
var $remove = {};
if (_.isObject(list_name)) {
_.each(list_name, function(v, k) {
if (!this._is_reserved_property(k)) {
$remove[k] = v;
}
}, this);
} else {
$remove[list_name] = value;
}
data[REMOVE_ACTION] = $remove;
return data;
},
delete_action: function() {
var data = {};
data[DELETE_ACTION] = '';
return data;
}
};
export {
SET_ACTION,
SET_ONCE_ACTION,
UNSET_ACTION,
ADD_ACTION,
APPEND_ACTION,
UNION_ACTION,
REMOVE_ACTION,
DELETE_ACTION,
apiActions
};