/** * Parse & merge multiple rank results from redis. * @param {array} ranks In this format: [['foo', '39', 'bar', '13'], ['foo', '11']] * @return {object} In this format: [{ foo: 50 }, { bar: 13 }] */ const createRankTotalParser = (direction, startingAt, limit) => ranks => { return _.chain(ranks) .flatten() .chunk(2) .reduce((acc, [key, count]) => _.tap(acc, acc => { acc[key] = (acc[key] || 0) + Number(count); }), { }) .toPairs() .sortBy(([, count]) => direction === 'asc' ? count : -count) .drop(startingAt) .takeWhile((value, index) => limit <= 0 || index < limit) .map(([key, count]) => ({ [key]: count })) .value(); }