Repository: ViacomInc/data-point
Pull request #82 (2017-12-27 18:01:12)

refactor: Reorganize code for creating entities
<!--
Thanks for your interest in the project. We appreciate bugs filed and PRs submitted!

Please make sure that you are familiar with and follow the Code of Conduct for
this project (found in the CODE_OF_CONDUCT.md file).

Also, please make sure you're familiar with and follow the instructions in the
contributing guidelines (found in the CONTRIBUTING.md file).

Please fill out the information below to expedite the review and (hopefully)
merge of your pull request!
-->

<!-- What changes are being made? (What feature/bug is being fixed here?) -->
**What**: Moves existing code into a new `base-entity` directory.

<!-- Why are these changes necessary? -->
**Why**: #81

<!-- 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**
- [x] Tests
- [x] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->

<!-- feel free to add additional comments -->


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

Coverage remained the same at 100.0% when pulling **b3170162ce25ef8ca76074e20cb5a3af9463d9a4 on raingerber:add-base-entity** into **28b6ed9766164eddbc455dff5f4a86c17d930bfc on ViacomInc:master**.

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

Coverage remained the same at 100.0% when pulling **18e13c0f0cf3e1b8b0afb232abea6b26f030a9ce on raingerber:add-base-entity** into **141de5e2ce8471736076e1918629257d72548715 on ViacomInc:master**.


Patch:
diff --git a/packages/data-point/lib/core/factory.js b/packages/data-point/lib/core/factory.js
index 6a3d7dd..9e7e95c 100644
--- a/packages/data-point/lib/core/factory.js
+++ b/packages/data-point/lib/core/factory.js
@@ -27,7 +27,7 @@ function addToStore (store, reducers) {
 /**
  * setup store
  */
-function addDefinitionsToStore (store, entities) {
+function addEntitiesToStore (store, entities) {
   const entitySpecs = _.defaultTo(entities, {})
   const specs = normalizeEntities.normalize(entitySpecs)
 
@@ -57,13 +57,13 @@ function create (spec) {
 
   manager.transform = _.partial(transform, manager)
 
-  // add single item (singlular)
+  // add single item (singular)
   manager.addValue = manager.values.add
   manager.addEntityTypes = manager.entityTypes.add
   manager.use = manager.middleware.use
 
   // add collection of items (plural)
-  manager.addEntities = _.partial(addDefinitionsToStore, manager.entities)
+  manager.addEntities = _.partial(addEntitiesToStore, manager.entities)
 
   // using options to initialize dataPoint
 
diff --git a/packages/data-point/lib/entity-types/base-entity/factory.js b/packages/data-point/lib/entity-types/base-entity/factory.js
new file mode 100644
index 0000000..6feec89
--- /dev/null
+++ b/packages/data-point/lib/entity-types/base-entity/factory.js
@@ -0,0 +1,24 @@
+const deepFreeze = require('deep-freeze')
+const defaultTo = require('lodash/defaultTo')
+
+const createTransform = require('../../transform-expression').create
+
+/**
+ * @param {function} Factory - factory function to create the entity
+ * @param {Object} spec - spec for the Entity
+ * @param {string} id - Entity's id
+ */
+function create (Factory, spec, id) {
+  const entity = new Factory(spec)
+
+  entity.id = id
+  entity.value = createTransform(spec.value)
+  entity.before = createTransform(spec.before)
+  entity.error = createTransform(spec.error)
+  entity.after = createTransform(spec.after)
+  entity.params = deepFreeze(defaultTo(spec.params, {}))
+
+  return entity
+}
+
+module.exports.create = create
diff --git a/packages/data-point/lib/entity-types/base-entity/index.js b/packages/data-point/lib/entity-types/base-entity/index.js
new file mode 100644
index 0000000..5e2c642
--- /dev/null
+++ b/packages/data-point/lib/entity-types/base-entity/index.js
@@ -0,0 +1,9 @@
+/*
+NOTE: exporting the resolve function here creates a circular dependency when it's
+imported by reducer-entity/resolve.js -- to avoid this problem, that file imports
+the resolve function directly from base-entity/resolve.js
+*/
+
+module.exports = {
+  create: require('./factory').create
+}
diff --git a/packages/data-point/lib/entity-types/entity-collection/factory.js b/packages/data-point/lib/entity-types/entity-collection/factory.js
index de78230..8d06ccd 100644
--- a/packages/data-point/lib/entity-types/entity-collection/factory.js
+++ b/packages/data-point/lib/entity-types/entity-collection/factory.js
@@ -3,7 +3,7 @@
 const _ = require('lodash')
 const createTransform = require('../../transform-expression').create
 const parseCompose = require('../parse-compose')
-const helpers = require('../../helpers')
+const createBaseEntity = require('../base-entity').create
 
 /**
  * @class
@@ -59,7 +59,7 @@ function validateComposeVsInlineModifiers (spec, invalidInlinesKeys) {
 function create (spec, id) {
   validateComposeVsInlineModifiers(spec, modifierKeys)
 
-  const entity = helpers.createEntity(EntityCollection, spec, id)
+  const entity = createBaseEntity(EntityCollection, spec, id)
 
   const compose = parseCompose.parse(spec, modifierKeys)
   entity.compose = createCompose(compose)
diff --git a/packages/data-point/lib/entity-types/entity-control/factory.js b/packages/data-point/lib/entity-types/entity-control/factory.js
index 0380600..18bb6f3 100644
--- a/packages/data-point/lib/entity-types/entity-control/factory.js
+++ b/packages/data-point/lib/entity-types/entity-control/factory.js
@@ -3,7 +3,7 @@
 const _ = require('lodash')
 
 const createTransform = require('../../transform-expression').create
-const helpers = require('../../helpers')
+const createBaseEntity = require('../base-entity').create
 
 /**
  * @class
@@ -75,7 +75,7 @@ module.exports.parseSwitch = parseSwitch
  * @return {EntityControl} Entity Object
  */
 function create (spec, id) {
-  const entity = helpers.createEntity(EntityControl, spec, id)
+  const entity = createBaseEntity(EntityControl, spec, id)
   entity.select = parseSwitch(spec)
   return Object.freeze(entity)
 }
diff --git a/packages/data-point/lib/entity-types/entity-entry/factory.js b/packages/data-point/lib/entity-types/entity-entry/factory.js
index 3b13906..d960fa7 100644
--- a/packages/data-point/lib/entity-types/entity-entry/factory.js
+++ b/packages/data-point/lib/entity-types/entity-entry/factory.js
@@ -1,6 +1,6 @@
 'use strict'
 
-const helpers = require('../../helpers')
+const createBaseEntity = require('../base-entity').create
 
 /**
  * @class
@@ -16,7 +16,7 @@ module.exports.EntityEntry = EntityEntry
  * @return {EntityEntry} Entity Object
  */
 function create (spec, id) {
-  const entity = helpers.createEntity(EntityEntry, spec, id)
+  const entity = createBaseEntity(EntityEntry, spec, id)
   return Object.freeze(entity)
 }
 
diff --git a/packages/data-point/lib/entity-types/entity-hash/factory.js b/packages/data-point/lib/entity-types/entity-hash/factory.js
index fae882b..2c1eeb3 100644
--- a/packages/data-point/lib/entity-types/entity-hash/factory.js
+++ b/packages/data-point/lib/entity-types/entity-hash/factory.js
@@ -5,7 +5,7 @@ const createTransform = require('../../transform-expression').create
 const createReducerObject = require('../../reducer-object').create
 const deepFreeze = require('deep-freeze')
 const parseCompose = require('../parse-compose')
-const helpers = require('../../helpers')
+const createBaseEntity = require('../base-entity').create
 
 /**
  * @class
@@ -89,7 +89,7 @@ function validateCompose (entityId, compose, validKeys) {
 function create (spec, id) {
   validateComposeVsInlineModifiers(spec, modifierKeys)
 
-  const entity = helpers.createEntity(EntityHash, spec, id)
+  const entity = createBaseEntity(EntityHash, spec, id)
 
   const compose = parseCompose.parse(spec, modifierKeys)
   validateCompose(entity.id, compose, modifierKeys)
diff --git a/packages/data-point/lib/entity-types/entity-request/factory.js b/packages/data-point/lib/entity-types/entity-request/factory.js
index 97c243e..3214198 100644
--- a/packages/data-point/lib/entity-types/entity-request/factory.js
+++ b/packages/data-point/lib/entity-types/entity-request/factory.js
@@ -3,7 +3,7 @@
 const _ = require('lodash')
 const fp = require('lodash/fp')
 const createTransform = require('../../transform-expression').create
-const helpers = require('../../helpers')
+const createBaseEntity = require('../base-entity').create
 const TransformKeys = require('./transform-keys')
 
 /**
@@ -41,7 +41,7 @@ module.exports.unsetTransformKeys = unsetTransformKeys
  * @return {EntityRequest} Entity Object
  */
 function create (spec, id) {
-  const entity = helpers.createEntity(EntityRequest, spec, id)
+  const entity = createBaseEntity(EntityRequest, spec, id)
   const options = _.defaultTo(spec.options, {})
   entity.url = _.defaultTo(spec.url, '')
   entity.beforeRequest = createTransform(spec.beforeRequest)
diff --git a/packages/data-point/lib/entity-types/entity-schema/factory.js b/packages/data-point/lib/entity-types/entity-schema/factory.js
index 58705bb..f08d689 100644
--- a/packages/data-point/lib/entity-types/entity-schema/factory.js
+++ b/packages/data-point/lib/entity-types/entity-schema/factory.js
@@ -2,7 +2,7 @@
 
 const _ = require('lodash')
 const deepFreeze = require('deep-freeze')
-const helpers = require('../../helpers')
+const createBaseEntity = require('../base-entity').create
 
 /**
  * @class
@@ -21,7 +21,7 @@ module.exports.EntitySchema = EntitySchema
  * @return {EntitySchema} Entity Object
  */
 function create (spec, id) {
-  const entity = helpers.createEntity(EntitySchema, spec, id)
+  const entity = createBaseEntity(EntitySchema, spec, id)
   entity.schema = deepFreeze(_.defaultTo(spec.schema, {}))
   entity.options = deepFreeze(_.defaultTo(spec.options, {}))
 
diff --git a/packages/data-point/lib/entity-types/entity-transform/factory.js b/packages/data-point/lib/entity-types/entity-transform/factory.js
index facda5b..404f6a9 100644
--- a/packages/data-point/lib/entity-types/entity-transform/factory.js
+++ b/packages/data-point/lib/entity-types/entity-transform/factory.js
@@ -1,6 +1,6 @@
 'use strict'
 
-const helpers = require('../../helpers')
+const createBaseEntity = require('../base-entity').create
 
 /**
  * @class
@@ -16,7 +16,7 @@ module.exports.EntityTransform = EntityTransform
  * @return {EntityTransform} Entity Object
  */
 function create (spec, id) {
-  const entity = helpers.createEntity(
+  const entity = createBaseEntity(
     EntityTransform,
     {
       value: spec
diff --git a/packages/data-point/lib/entity-types/resolve-entity/index.js b/packages/data-point/lib/entity-types/resolve-entity/index.js
deleted file mode 100644
index 63a9870..0000000
--- a/packages/data-point/lib/entity-types/resolve-entity/index.js
+++ /dev/null
@@ -1,1 +0,0 @@
-module.exports = require('./resolve-entity')
diff --git a/packages/data-point/lib/helpers/helpers.js b/packages/data-point/lib/helpers/helpers.js
index d1b2024..98f4c54 100644
--- a/packages/data-point/lib/helpers/helpers.js
+++ b/packages/data-point/lib/helpers/helpers.js
@@ -4,7 +4,6 @@ const _ = require('lodash')
 const Promise = require('bluebird')
 const resolveTransform = require('../transform-expression').resolve
 const AccumulatorFactory = require('../accumulator/factory')
-const deepFreeze = require('deep-freeze')
 
 function reducify (method) {
   return function () {
@@ -40,31 +39,17 @@ function mockReducer (reducer, acc) {
 module.exports.mockReducer = mockReducer
 
 const createTransform = require('../transform-expression').create
+
 module.exports.createTransform = createTransform
 
-const resolveEntity = require('../entity-types/resolve-entity')
-module.exports.resolveEntity = resolveEntity
-
-/**
- * @param {function} Factory - factory function to create the entity
- * @param {Object} spec - spec for the Entity
- * @param {string} id - Entity's id
- */
-function createEntity (Factory, spec, id) {
-  const entity = new Factory(spec)
-
-  entity.id = id
-  entity.value = createTransform(spec.value)
-  entity.before = createTransform(spec.before)
-  entity.error = createTransform(spec.error)
-  entity.after = createTransform(spec.after)
-  entity.params = deepFreeze(_.defaultTo(spec.params, {}))
-
-  return entity
-}
+const createEntity = require('../entity-types/base-entity').create
 
 module.exports.createEntity = createEntity
 
+const resolveEntity = require('../entity-types/base-entity/resolve').resolve
+
+module.exports.resolveEntity = resolveEntity
+
 function createAccumulator (value, options) {
   return AccumulatorFactory.create(
     Object.assign(
@@ -75,11 +60,13 @@ function createAccumulator (value, options) {
     )
   )
 }
+
 module.exports.createAccumulator = createAccumulator
 
 function createResolveTransform (dataPoint) {
   return resolveTransform.bind(null, dataPoint)
 }
+
 module.exports.createResolveTransform = createResolveTransform
 
 function isTransform (transform) {
@@ -87,4 +74,5 @@ function isTransform (transform) {
     _.isArray(transform.reducers) && transform.typeOf === 'TransformExpression'
   )
 }
+
 module.exports.isTransform = isTransform
diff --git a/packages/data-point/lib/reducer-entity/resolve.js b/packages/data-point/lib/reducer-entity/resolve.js
index ae28d4c..ddad3a4 100644
--- a/packages/data-point/lib/reducer-entity/resolve.js
+++ b/packages/data-point/lib/reducer-entity/resolve.js
@@ -1,6 +1,6 @@
 'use strict'
 
-const ResolveEntity = require('../entity-types/resolve-entity')
+const BaseEntity = require('../entity-types/base-entity/resolve')
 
 /**
  * Resolve an Entity Reducer, actual entity resolution is delegated
@@ -15,7 +15,7 @@ const ResolveEntity = require('../entity-types/resolve-entity')
 function resolve (store, resolveReducer, accumulator, reducerEntity) {
   const reducerEntityType = reducerEntity.entityType
   const EntityType = store.entityTypes.get(reducerEntityType)
-  return ResolveEntity.resolve(
+  return BaseEntity.resolve(
     store,
     resolveReducer,
     accumulator,
diff --git a/packages/data-point/lib/transform-expression/factory.js b/packages/data-point/lib/transform-expression/factory.js
index 85d79f5..84469b8 100644
--- a/packages/data-point/lib/transform-expression/factory.js
+++ b/packages/data-point/lib/transform-expression/factory.js
@@ -1,8 +1,8 @@
 'use strict'
 
 const _ = require('lodash')
-const reducerFactory = require('../reducer/factory')
 const util = require('util')
+const reducerFactory = require('../reducer/factory')
 
 /**
  * Describes the transform parts used to reduce a context
diff --git a/packages/data-point/lib/utils/index.js b/packages/data-point/lib/utils/index.js
index 728365c..9586b2b 100644
--- a/packages/data-point/lib/utils/index.js
+++ b/packages/data-point/lib/utils/index.js
@@ -14,11 +14,13 @@ function set (target, key, value) {
   obj[key] = value
   return Object.assign({}, target, obj)
 }
+
 module.exports.set = set
 
 function assign (target, toMerge) {
   return Object.assign({}, target, toMerge)
 }
+
 module.exports.assign = assign
 
 let uid = 0
@@ -26,6 +28,7 @@ function getUID () {
   uid++
   return uid
 }
+
 module.exports.getUID = getUID
 
 /**
@@ -41,8 +44,13 @@ function typeOf (value) {
     .slice(0, -1)
     .toLowerCase()
 }
+
 module.exports.typeOf = typeOf
 
+/**
+ * @param {Accumulator} acc
+ * @param {Object} data
+ */
 function inspect (acc, data) {
   const log = []
   log.push('\n\x1b[33minspect\x1b[0m:', _.get(acc, 'reducer.spec.id'))
@@ -58,4 +66,5 @@ function inspect (acc, data) {
     _.attempt(acc.params.inspect, acc)
   }
 }
+
 module.exports.inspect = inspect


Test patch:
diff --git a/packages/data-point/lib/entity-types/base-entity/factory.test.js b/packages/data-point/lib/entity-types/base-entity/factory.test.js
new file mode 100644
index 0000000..a2aae39
--- /dev/null
+++ b/packages/data-point/lib/entity-types/base-entity/factory.test.js
@@ -0,0 +1,26 @@
+/* eslint-env jest */
+'use strict'
+
+const createBaseEntity = require('./factory').create
+
+describe('factory.createBaseEntity', () => {
+  test('It should create entity defaults', () => {
+    function FooEntity () {}
+    const entity = createBaseEntity(
+      FooEntity,
+      {
+        before: '$.',
+        value: '$.',
+        error: '$.',
+        after: '$.'
+      },
+      'foo'
+    )
+
+    expect(entity).toHaveProperty('id', 'foo')
+    expect(entity).toHaveProperty('value.typeOf', 'TransformExpression')
+    expect(entity).toHaveProperty('error.typeOf', 'TransformExpression')
+    expect(entity).toHaveProperty('after.typeOf', 'TransformExpression')
+    expect(entity).toHaveProperty('params', {})
+  })
+})
diff --git a/packages/data-point/lib/entity-types/resolve-entity/resolve-entity.js b/packages/data-point/lib/entity-types/base-entity/resolve.js
similarity index 100%
rename from packages/data-point/lib/entity-types/resolve-entity/resolve-entity.js
rename to packages/data-point/lib/entity-types/base-entity/resolve.js
diff --git a/packages/data-point/lib/entity-types/resolve-entity/resolve-entity.test.js b/packages/data-point/lib/entity-types/base-entity/resolve.test.js
similarity index 99%
rename from packages/data-point/lib/entity-types/resolve-entity/resolve-entity.test.js
rename to packages/data-point/lib/entity-types/base-entity/resolve.test.js
index dc0ebe5..05dfe43 100644
--- a/packages/data-point/lib/entity-types/resolve-entity/resolve-entity.test.js
+++ b/packages/data-point/lib/entity-types/base-entity/resolve.test.js
@@ -1,7 +1,7 @@
 /* eslint-env jest */
 'use strict'
 
-const ResolveEntity = require('./resolve-entity')
+const ResolveEntity = require('./resolve')
 const ReducerEntity = require('../../reducer-entity')
 
 const FixtureStore = require('../../../test/utils/fixture-store')
diff --git a/packages/data-point/lib/entity-types/entity-request/reducer.test.js b/packages/data-point/lib/entity-types/entity-request/reducer.test.js
index a0064e8..2c546cc 100644
--- a/packages/data-point/lib/entity-types/entity-request/reducer.test.js
+++ b/packages/data-point/lib/entity-types/entity-request/reducer.test.js
@@ -10,7 +10,7 @@ const ReducerFactory = require('../../reducer/factory')
 const createTransform = require('../../transform-expression/factory').create
 const LocalsFactory = require('../../locals/factory')
 
-const ResolveEntity = require('../resolve-entity')
+const ResolveEntity = require('../base-entity/resolve')
 
 const FixtureStore = require('../../../test/utils/fixture-store')
 
diff --git a/packages/data-point/lib/helpers/helpers.test.js b/packages/data-point/lib/helpers/helpers.test.js
index 40b6ec8..58d3f9c 100644
--- a/packages/data-point/lib/helpers/helpers.test.js
+++ b/packages/data-point/lib/helpers/helpers.test.js
@@ -74,28 +74,6 @@ describe('helpers.mockReducer', () => {
   })
 })
 
-describe('helpers.createEntity', () => {
-  test('It should create entity defaults', () => {
-    function FooEntity () {}
-    const entity = helpers.createEntity(
-      FooEntity,
-      {
-        before: '$.',
-        value: '$.',
-        error: '$.',
-        after: '$.'
-      },
-      'foo'
-    )
-
-    expect(entity).toHaveProperty('id', 'foo')
-    expect(entity).toHaveProperty('value.typeOf', 'TransformExpression')
-    expect(entity).toHaveProperty('error.typeOf', 'TransformExpression')
-    expect(entity).toHaveProperty('after.typeOf', 'TransformExpression')
-    expect(entity).toHaveProperty('params', {})
-  })
-})
-
 describe('helpers.createAccumulator', () => {
   test('It should assign value', () => {
     const acc = helpers.createAccumulator('foo')