3 var _
= require('lodash');
5 var exports
= module
.exports
;
7 function parseLookups(response
, fill
) {
9 response contains {lookups: {}, items: []}
11 fill is a map of attribute: lookupName where attribute is the result
12 attribute that holds the id (fromUser) and lookupName is the name of a key
13 inside lookups that holds the array of values (users).
15 This function will change response.items in place so that each lookup
16 attribute in there will get the full value from lookups.
18 Do not use this directly, but rather make/use one function that parses each
19 API response type. ie parseChatResults(response)
22 // map the lookups by id
23 // ie. users: [] becomes users: {}
24 var lookupNames
= Object
.keys(response
.lookups
);
26 _
.each(lookupNames
, function(lookupName
) {
27 var map
= _
.indexBy(response
.lookups
[lookupName
], 'id');
28 maps
[lookupName
] = map
;
31 // fill the lookup values in place
32 // ie. result.fromUser which is an id becomes an object
33 _
.each(response
.items
, function(item
) {
34 _
.each(fill
, function(lookupName
, attribute
) {
35 item
[attribute
] = maps
[lookupName
][item
[attribute
]];
39 return response
.items
;
41 exports
.parseLookups
= parseLookups
;
43 function parseChats(response
) {
44 return parseLookups(response
, { fromUser
: 'users' });
46 exports
.parseChats
= parseChats
;