const Long = require('long')

module.exports = (consumerOffsets, topicOffsets) => {
  const indexPartitions = (obj, { partition, offset }) =>
    Object.assign(obj, { [partition]: offset })

  const indexTopics = (obj, { topic, partitions }) =>
    Object.assign(obj, { [topic]: partitions.reduce(indexPartitions, {}) })

  const shouldInitializeOffset = offset => Long.fromValue(offset).equals(-1)

  const indexedConsumerOffsets = consumerOffsets.reduce(indexTopics, {})
  const indexedTopicOffsets = topicOffsets.reduce(indexTopics, {})

  return Object.keys(indexedConsumerOffsets).map(topic => {
    return {
      topic,
      partitions: Object.keys(indexedConsumerOffsets[topic]).map(partition => {
        const offset = indexedConsumerOffsets[topic][partition]
        const resolvedOffset = shouldInitializeOffset(offset)
          ? indexedTopicOffsets[topic][partition]
          : offset

        return { partition: Number(partition), offset: resolvedOffset }
      }),
    }
  })
}

@@ -26,65 +26,41 @@
 g')%0A
-%0Amodule.exports = (consumerOffsets, topicOffsets) =%3E %7B%0A  
+const %7B keys, assign %7D = Object%0A%0A
 cons
@@ -106,36 +106,25 @@
 offset %7D) =%3E
-%0A    Object.
+ 
 assign(obj, 
@@ -144,24 +144,153 @@
 : offset %7D)%0A
+const shouldInitializeOffset = offset =%3E Long.fromValue(offset).equals(-1)%0A%0Amodule.exports = (consumerOffsets, topicOffsets) =%3E %7B
 %0A  const ind
@@ -336,23 +336,16 @@
  =%3E%0A    
-Object.
 assign(o
@@ -406,86 +406,8 @@
 %7D)%0A%0A
-  const shouldInitializeOffset = offset =%3E Long.fromValue(offset).equals(-1)%0A%0A
   co
@@ -552,23 +552,16 @@
  return 
-Object.
 keys(ind
@@ -604,89 +604,116 @@
     
-return %7B%0A      topic,%0A      partitions: Object.keys(indexedConsumerOffsets%5Btopic%5D
+const partitions = indexedConsumerOffsets%5Btopic%5D%0A    return %7B%0A      topic,%0A      partitions: keys(partitions
 ).ma
@@ -756,37 +756,18 @@
 t = 
-indexedConsumerOffsets%5Btopic%5D
+partitions
 %5Bpar

Refactor OffsetManager/initializeConsumerOffsets