Repository: ViacomInc/data-point
Pull request #102 (2018-01-02 20:44:10)

Remove empty lifecycle transforms
<!--
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**: This will introduce a change that omits the lifecycle reducers from entities if they're not defined in the spec during their creation.

<!-- Why are these changes necessary? -->
**Why**: This reduces the noise when debugging entities by removing unused keys. It could also improve the memory use by reducing the size of the objects being created.

<!-- How were these changes implemented? -->
**How**: Modifications to the `base-entity/factory#create` that checks for the existence of the `spec.before`/`spec.after`/`spec.error`.

<!-- 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" -->
- [N/A] Documentation
- [X] Tests
- [X] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
- [N/A] Added username to **all-contributors** list

<!-- feel free to add additional comments -->


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

Coverage remained the same at 100.0% when pulling **c67d59b03cfd2226fc11b53b16f12592a4300267 on paulmolluzzo:remove-empty-lifecycle-transforms** into **9a884ddb354272e27062797e046ed91d4c7c5afa on ViacomInc:master**.

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

Coverage remained the same at 100.0% when pulling **66a50679e8647ffc70b031549e2a70982bb674bd on paulmolluzzo:remove-empty-lifecycle-transforms** into **b5d848a88e21d21352f1a2a56540d0fcbbc82ccb on ViacomInc:master**.

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

Coverage remained the same at 100.0% when pulling **576e535b1bad72ac3baeffd91d5be7257ce093ca on paulmolluzzo:remove-empty-lifecycle-transforms** into **b5d848a88e21d21352f1a2a56540d0fcbbc82ccb on ViacomInc:master**.

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

Coverage remained the same at 100.0% when pulling **e4ca1ae2abbef2b5c0ab8dbaf1ef623033c2f873 on paulmolluzzo:remove-empty-lifecycle-transforms** into **9177b7c10c930b8339ae6aada76654061e0f8fc7 on ViacomInc:master**.


Patch:
diff --git a/packages/data-point/lib/entity-types/base-entity/factory.js b/packages/data-point/lib/entity-types/base-entity/factory.js
index 6feec89..687637a 100644
--- a/packages/data-point/lib/entity-types/base-entity/factory.js
+++ b/packages/data-point/lib/entity-types/base-entity/factory.js
@@ -12,10 +12,23 @@ 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)
+
+  if (spec.before) {
+    entity.before = createTransform(spec.before)
+  }
+
+  if (spec.value) {
+    entity.value = createTransform(spec.value)
+  }
+
+  if (spec.after) {
+    entity.after = createTransform(spec.after)
+  }
+
+  if (spec.error) {
+    entity.error = createTransform(spec.error)
+  }
+
   entity.params = deepFreeze(defaultTo(spec.params, {}))
 
   return entity
diff --git a/packages/data-point/lib/entity-types/base-entity/resolve.js b/packages/data-point/lib/entity-types/base-entity/resolve.js
index 41fd5fa..19d2eaf 100644
--- a/packages/data-point/lib/entity-types/base-entity/resolve.js
+++ b/packages/data-point/lib/entity-types/base-entity/resolve.js
@@ -10,7 +10,7 @@ const utils = require('../../utils')
 function resolveErrorReducers (error, accumulator, resolveReducer) {
   const errorTransform = accumulator.reducer.spec.error
 
-  if (errorTransform.reducers.length === 0) {
+  if (!errorTransform || errorTransform.reducers.length === 0) {
     return Promise.reject(error)
   }
 
diff --git a/packages/data-point/lib/transform-expression/resolve.js b/packages/data-point/lib/transform-expression/resolve.js
index 4e7b386..2841e9e 100644
--- a/packages/data-point/lib/transform-expression/resolve.js
+++ b/packages/data-point/lib/transform-expression/resolve.js
@@ -13,7 +13,7 @@ const resolveReducer = require('../reducer').resolve
  * @returns {Promise<Accumulator>}
  */
 function resolve (manager, accumulator, transform) {
-  if (transform.reducers.length === 0) {
+  if (!transform || transform.reducers.length === 0) {
     return Promise.resolve(accumulator)
   }
 


Test patch:
diff --git a/packages/data-point/lib/entity-types/entity-collection/factory.test.js b/packages/data-point/lib/entity-types/entity-collection/factory.test.js
index aa8117a..bbb32fc 100644
--- a/packages/data-point/lib/entity-types/entity-collection/factory.test.js
+++ b/packages/data-point/lib/entity-types/entity-collection/factory.test.js
@@ -6,12 +6,12 @@ const modelFactory = require('./factory')
 test('modelFactory#create default', () => {
   const result = modelFactory.create({})
 
-  expect(result.error).toHaveProperty('typeOf', 'TransformExpression')
-  expect(result.before).toHaveProperty('typeOf', 'TransformExpression')
-  expect(result.after).toHaveProperty('typeOf', 'TransformExpression')
+  expect(result).not.toHaveProperty('error')
+  expect(result).not.toHaveProperty('before')
+  expect(result).not.toHaveProperty('after')
   expect(result.params).toEqual({})
 
-  expect(result.value).toHaveProperty('typeOf', 'TransformExpression')
+  expect(result).not.toHaveProperty('value')
   expect(result.compose).toBeInstanceOf(Array)
 })
 
diff --git a/packages/data-point/lib/entity-types/entity-control/factory.test.js b/packages/data-point/lib/entity-types/entity-control/factory.test.js
index dbc4165..7fd096f 100644
--- a/packages/data-point/lib/entity-types/entity-control/factory.test.js
+++ b/packages/data-point/lib/entity-types/entity-control/factory.test.js
@@ -12,9 +12,9 @@ test('Factory#create', () => {
     ]
   })
 
-  expect(control).toHaveProperty('before')
-  expect(control).toHaveProperty('after')
-  expect(control).toHaveProperty('error')
+  expect(control).not.toHaveProperty('before')
+  expect(control).not.toHaveProperty('after')
+  expect(control).not.toHaveProperty('error')
   expect(control).toHaveProperty('params')
   expect(control).toHaveProperty('select')
 })
diff --git a/packages/data-point/lib/entity-types/entity-entry/factory.test.js b/packages/data-point/lib/entity-types/entity-entry/factory.test.js
index 45efe67..6277268 100644
--- a/packages/data-point/lib/entity-types/entity-entry/factory.test.js
+++ b/packages/data-point/lib/entity-types/entity-entry/factory.test.js
@@ -8,9 +8,9 @@ test('Factory#create', () => {
     value: ['$foo', (acc, done) => {}]
   })
 
-  expect(obj).toHaveProperty('before')
-  expect(obj).toHaveProperty('after')
-  expect(obj).toHaveProperty('error')
+  expect(obj).not.toHaveProperty('before')
+  expect(obj).not.toHaveProperty('after')
+  expect(obj).not.toHaveProperty('error')
   expect(obj).toHaveProperty('params')
   expect(obj).toHaveProperty('value')
 })
diff --git a/packages/data-point/lib/entity-types/entity-hash/factory.test.js b/packages/data-point/lib/entity-types/entity-hash/factory.test.js
index 39dab68..fe570ad 100644
--- a/packages/data-point/lib/entity-types/entity-hash/factory.test.js
+++ b/packages/data-point/lib/entity-types/entity-hash/factory.test.js
@@ -6,10 +6,10 @@ const factory = require('./factory')
 test('factory#create default', () => {
   const result = factory.create({})
 
-  expect(result.error).toHaveProperty('typeOf', 'TransformExpression')
-  expect(result.before).toHaveProperty('typeOf', 'TransformExpression')
-  expect(result.after).toHaveProperty('typeOf', 'TransformExpression')
-  expect(result.value).toHaveProperty('typeOf', 'TransformExpression')
+  expect(result).not.toHaveProperty('error')
+  expect(result).not.toHaveProperty('before')
+  expect(result).not.toHaveProperty('after')
+  expect(result).not.toHaveProperty('value')
   expect(result.params).toEqual({})
 
   expect(result.compose).toBeInstanceOf(Array)
diff --git a/packages/data-point/lib/entity-types/entity-request/factory.test.js b/packages/data-point/lib/entity-types/entity-request/factory.test.js
index e988664..a1421c1 100644
--- a/packages/data-point/lib/entity-types/entity-request/factory.test.js
+++ b/packages/data-point/lib/entity-types/entity-request/factory.test.js
@@ -14,9 +14,9 @@ describe('create', () => {
   })
   test('It should have defaults', () => {
     expect(request).toHaveProperty('id')
-    expect(request).toHaveProperty('before')
-    expect(request).toHaveProperty('after')
-    expect(request).toHaveProperty('error')
+    expect(request).not.toHaveProperty('before')
+    expect(request).not.toHaveProperty('after')
+    expect(request).not.toHaveProperty('error')
     expect(request).toHaveProperty('params')
   })
   test('It should have url', () => {
diff --git a/packages/data-point/lib/entity-types/entity-schema/factory.test.js b/packages/data-point/lib/entity-types/entity-schema/factory.test.js
index c9071ba..c03c762 100644
--- a/packages/data-point/lib/entity-types/entity-schema/factory.test.js
+++ b/packages/data-point/lib/entity-types/entity-schema/factory.test.js
@@ -18,8 +18,8 @@ test('Factory#create', () => {
 
   expect(obj).toHaveProperty('schema')
   expect(obj).toHaveProperty('options')
-  expect(obj).toHaveProperty('before')
-  expect(obj).toHaveProperty('after')
-  expect(obj).toHaveProperty('error')
+  expect(obj).not.toHaveProperty('before')
+  expect(obj).not.toHaveProperty('after')
+  expect(obj).not.toHaveProperty('error')
   expect(obj).toHaveProperty('params')
 })