Repository: ViacomInc/data-point
Pull request #88 (2017-12-31 13:37:56)

refactor: Move location of reducer resolve code
<!-- What changes are being made? (What feature/bug is being fixed here?) -->
**What**: Move the code for resolving reducers from `lib/transform-expression` to `lib/reducer`.

<!-- Why are these changes necessary? -->
**Why**: `lib/reducer/factory` aggregates the different reducer types, so it makes sense for the resolve function (which does the same thing) to be in the same directory.

<!-- Have you done all of these things?  -->
**Checklist**:
<!-- add "N/A" to the end of each line that's irrelevant to your changes -->
<!-- to check an item, place an "x" in the box like so: "- [x] Documentation" -->
- [ ] Documentation n/a
- [ ] Tests n/a
- [x] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->

coveralls: 
[![Coverage Status](https://coveralls.io/builds/14853741/badge)](https://coveralls.io/builds/14853741)

Coverage remained the same at 100.0% when pulling **5580ea12fd33013e5092d79e96ba995629029a40 on raingerber:move-reducer-resolve-code** into **6edaade64c5c6181016c5c0151ee1598a47de87c on ViacomInc:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/14854834/badge)](https://coveralls.io/builds/14854834)

Coverage remained the same at 100.0% when pulling **5ae3ba33ae3c3d485e65c31aca55967028d608a2 on raingerber:move-reducer-resolve-code** into **3cad5dac5c75c2c8aff355ed4f1522f0170ec193 on ViacomInc:master**.


Patch:
diff --git a/packages/data-point/lib/reducer/index.js b/packages/data-point/lib/reducer/index.js
index 8dd1848..8de7fdf 100644
--- a/packages/data-point/lib/reducer/index.js
+++ b/packages/data-point/lib/reducer/index.js
@@ -1,5 +1,6 @@
 /* eslint global-require: 0 */
 
 module.exports = {
-  create: require('./factory').create
+  create: require('./factory').create,
+  resolve: require('./resolve').resolve
 }
diff --git a/packages/data-point/lib/reducer/resolve.js b/packages/data-point/lib/reducer/resolve.js
new file mode 100644
index 0000000..16a9fbb
--- /dev/null
+++ b/packages/data-point/lib/reducer/resolve.js
@@ -0,0 +1,59 @@
+const partial = require('lodash/partial')
+const resolveReducerPath = require('../reducer-path').resolve
+const resolveReducerFunction = require('../reducer-function').resolve
+const resolveReducerObject = require('../reducer-object').resolve
+const resolveReducerEntity = require('../reducer-entity').resolve
+
+/**
+ * @param {Object} manager
+ * @param {Function} resolveTransform
+ * @param {string} reducerType
+ * @returns
+ */
+function getReducerFunction (manager, resolveTransform, reducerType) {
+  let resolver
+
+  /* eslint indent: ["error", 2, { "SwitchCase": 1 }] */
+  switch (reducerType) {
+    case 'ReducerPath':
+      resolver = resolveReducerPath
+      break
+    case 'ReducerFunction':
+      resolver = resolveReducerFunction
+      break
+    case 'ReducerObject':
+      // NOTE: recursive call
+      resolver = partial(resolveReducerObject, manager, resolveTransform)
+      break
+    case 'ReducerEntity':
+      // NOTE: recursive call
+      resolver = partial(resolveReducerEntity, manager, resolveTransform)
+      break
+    default:
+      throw new Error(`Reducer type '${reducerType}' was not recognized`)
+  }
+
+  return resolver
+}
+
+module.exports.getReducerFunction = getReducerFunction
+
+/**
+ * apply a Reducer to an accumulator
+ * @param {Object} manager
+ * @param {Function} resolveTransform
+ * @param {Accumulator} accumulator
+ * @param {Reducer} reducer
+ * @returns {Promise<Accumulator>}
+ */
+function resolve (manager, resolveTransform, accumulator, reducer) {
+  const reducerFunction = getReducerFunction(
+    manager,
+    resolveTransform,
+    reducer.type
+  )
+  const result = reducerFunction(accumulator, reducer)
+  return result
+}
+
+module.exports.resolve = resolve
diff --git a/packages/data-point/lib/transform-expression/factory.js b/packages/data-point/lib/transform-expression/factory.js
index 84469b8..055da80 100644
--- a/packages/data-point/lib/transform-expression/factory.js
+++ b/packages/data-point/lib/transform-expression/factory.js
@@ -82,14 +82,14 @@ function create (source = []) {
   validate(source)
   const tokens = parse(source)
 
-  const transformBase = new TransformExpression()
+  const transform = new TransformExpression()
 
-  transformBase.reducers = tokens.map(token => {
+  transform.reducers = tokens.map(token => {
     // NOTE: recursive call
     return reducerFactory.create(create, token)
   })
 
-  return Object.freeze(transformBase)
+  return Object.freeze(transform)
 }
 
 module.exports.create = create
diff --git a/packages/data-point/lib/transform-expression/resolve.js b/packages/data-point/lib/transform-expression/resolve.js
index 148d348..4e7b386 100644
--- a/packages/data-point/lib/transform-expression/resolve.js
+++ b/packages/data-point/lib/transform-expression/resolve.js
@@ -1,73 +1,15 @@
 'use strict'
 
-const _ = require('lodash')
 const Promise = require('bluebird')
-const resolveReducerPath = require('../reducer-path').resolve
-const resolveReducerFunction = require('../reducer-function').resolve
-const resolveReducerObject = require('../reducer-object').resolve
-const resolveReducerEntity = require('../reducer-entity').resolve
+const partial = require('lodash/partial')
 
-/**
- * @param {Object} store
- * @param {any} reducerType
- * @returns
- */
-function getReducerFunction (manager, reducerType) {
-  let reducerResolver
-
-  /* eslint indent: ["error", 2, { "SwitchCase": 1 }] */
-  switch (reducerType) {
-    case 'ReducerPath':
-      reducerResolver = resolveReducerPath
-      break
-    case 'ReducerFunction':
-      reducerResolver = resolveReducerFunction
-      break
-    case 'ReducerObject':
-      reducerResolver = _.partial(resolveReducerObject, manager, resolve)
-      break
-    case 'ReducerEntity':
-      /* eslint no-use-before-define: "off" */
-      // IMPORTANT: recursiveness here - watch out!
-      reducerResolver = _.partial(resolveReducerEntity, manager, resolve)
-      break
-    default:
-      throw new Error(`Reducer type '${reducerType}' was not recognized`)
-  }
-
-  return reducerResolver
-}
-
-module.exports.getReducerFunction = getReducerFunction
-
-/**
- * apply a Reducer to a accumulator
- *
- * @param {Object} manager
- * @param {Accumulator} accumulator
- * @param {Reducer} reducer
- * @returns {Promise<Accumulator>}
- */
-function resolveReducer (manager, accumulator, reducer) {
-  const reducerFunction = getReducerFunction(manager, reducer.type)
-  const result = reducerFunction(accumulator, reducer)
-  return result
-}
-
-module.exports.resolveReducer = resolveReducer
-
-const reduceContext = manager => (accumulator, reducer) => {
-  return resolveReducer(manager, accumulator, reducer)
-}
-
-module.exports.reduceContext = reduceContext
+const resolveReducer = require('../reducer').resolve
 
 /**
  * resolves a given transform
- *
  * @param {Object} manager
  * @param {Accumulator} accumulator
- * @param {any} transform
+ * @param {TransformExpression} transform
  * @returns {Promise<Accumulator>}
  */
 function resolve (manager, accumulator, transform) {
@@ -75,7 +17,8 @@ function resolve (manager, accumulator, transform) {
     return Promise.resolve(accumulator)
   }
 
-  const reduceTransformReducer = reduceContext(manager)
+  // NOTE: recursive call
+  const reduceTransformReducer = partial(resolveReducer, manager, resolve)
   const result = Promise.reduce(
     transform.reducers,
     reduceTransformReducer,


Test patch:
diff --git a/packages/data-point/lib/reducer/resolve.test.js b/packages/data-point/lib/reducer/resolve.test.js
new file mode 100644
index 0000000..c1e96d9
--- /dev/null
+++ b/packages/data-point/lib/reducer/resolve.test.js
@@ -0,0 +1,73 @@
+/* eslint-env jest */
+'use strict'
+
+const Factory = require('./factory')
+const Resolve = require('./resolve')
+
+const reducers = require('../../test/utils/reducers')
+const fixtureStore = require('../../test/utils/fixture-store')
+const testData = require('../../test/data.json')
+
+const AccumulatorFactory = require('../accumulator/factory')
+const TransformExpression = require('../transform-expression')
+
+let manager
+
+beforeAll(() => {
+  manager = fixtureStore.create()
+})
+
+describe('reducer.getReducerFunction', () => {
+  test('resolve to ReducerPath', () => {
+    const resolver = Resolve.getReducerFunction(
+      manager,
+      TransformExpression.resolve,
+      'ReducerPath'
+    )
+    expect(resolver).toBeInstanceOf(Function)
+  })
+  test('resolve to ReducerFunction', () => {
+    const resolver = Resolve.getReducerFunction(
+      manager,
+      TransformExpression.resolve,
+      'ReducerFunction'
+    )
+    expect(resolver).toBeInstanceOf(Function)
+  })
+  test('resolve to ReducerEntity', () => {
+    const resolver = Resolve.getReducerFunction(
+      manager,
+      TransformExpression.resolve,
+      'ReducerEntity'
+    )
+    expect(resolver).toBeInstanceOf(Function)
+  })
+  test('resolve to ReducerEntity', () => {
+    expect(() => {
+      Resolve.getReducerFunction(
+        manager,
+        TransformExpression.resolve,
+        'INVALID TYPE'
+      )
+    }).toThrow()
+  })
+})
+
+test('resolve#reducer.resolve', () => {
+  const accumulator = AccumulatorFactory.create({
+    value: testData.a.b.c
+  })
+
+  const reducer = Factory.create(
+    TransformExpression.create,
+    reducers.addCollectionValues()
+  )
+  return Resolve.resolve(
+    manager,
+    TransformExpression.resolve,
+    accumulator,
+    reducer
+  ).then(result => {
+    expect(result.value).toEqual(6)
+  })
+})
diff --git a/packages/data-point/lib/transform-expression/resolve.test.js b/packages/data-point/lib/transform-expression/resolve.test.js
index 12e748b..32910d0 100644
--- a/packages/data-point/lib/transform-expression/resolve.test.js
+++ b/packages/data-point/lib/transform-expression/resolve.test.js
@@ -4,11 +4,9 @@
 const nock = require('nock')
 
 const fixtureStore = require('../../test/utils/fixture-store')
-const reducers = require('../../test/utils/reducers')
 const testData = require('../../test/data.json')
 
 const AccumulatorFactory = require('../accumulator/factory')
-const reducerFactory = require('../reducer/factory')
 const TransformExpression = require('./factory')
 
 const ResolveTransform = require('./resolve')
@@ -19,48 +17,6 @@ beforeAll(() => {
   manager = fixtureStore.create()
 })
 
-describe('reducer.getReducerFunction', () => {
-  test('resolve to ReducerPath', () => {
-    const resolver = ResolveTransform.getReducerFunction(manager, 'ReducerPath')
-    expect(resolver).toBeInstanceOf(Function)
-  })
-  test('resolve to ReducerFunction', () => {
-    const resolver = ResolveTransform.getReducerFunction(
-      manager,
-      'ReducerFunction'
-    )
-    expect(resolver).toBeInstanceOf(Function)
-  })
-  test('resolve to ReducerEntity', () => {
-    const resolver = ResolveTransform.getReducerFunction(
-      manager,
-      'ReducerEntity'
-    )
-    expect(resolver).toBeInstanceOf(Function)
-  })
-  test('resolve to ReducerEntity', () => {
-    expect(() => {
-      ResolveTransform.getReducerFunction(manager, 'INVALID TYPE')
-    }).toThrow()
-  })
-})
-
-test('resolve#reducer.resolveReducer', () => {
-  const accumulator = AccumulatorFactory.create({
-    value: testData.a.b.c
-  })
-
-  const reducer = reducerFactory.create(
-    TransformExpression.create,
-    reducers.addCollectionValues()
-  )
-  return ResolveTransform.resolveReducer(manager, accumulator, reducer).then(
-    result => {
-      expect(result.value).toEqual(6)
-    }
-  )
-})
-
 test('resolve#reducer.resolve - reducer empty', () => {
   const accumulator = AccumulatorFactory.create({
     value: testData.a.g