Repository: ViacomInc/data-point
Pull request #209 (2018-02-06 17:34:21)

Feature/add reducer alias for transform 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!

BREAKING CHANGES:

If your PR includes a breaking change, please submit it with a codemod under
data-point-codemods that will help users upgrade their codebase.

Breaking changes without a codemod will not be accepted unless a codemod is not
viable or does not apply to the specific situation.
-->

<!-- What changes are being made? (What feature/bug is being fixed here?) -->
**What**: We want 'reducer' to be used as an alias for 'transform'

<!-- Why are these changes necessary? -->
**Why**:  Because a transform entity is just a cached reducer

<!-- How were these changes implemented? -->
**How**:

` manager.addEntityType('reducer', EntityTransform)
`

Was added to the built in entity types.

<!-- 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" -->
- [ ] Has Breaking changes
- [x] Documentation
- [x] Tests
- [ ] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
- [ ] Added username to **all-contributors** list

<!-- feel free to add additional comments -->


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

Coverage remained the same at 100.0% when pulling **d72fd4ed6f6e8b5b7181218db2f1a8d7ad2bfad4 on victorsingh:feature/add-reducer-alias-for-transform-entities** into **fdfdc49b250ec08588b827b87a2f61ed02c99882 on ViacomInc:master**.

raingerber: @paulmolluzzo Has victor addressed the changes that you requested?
paulmolluzzo: @raingerber yup, all good!

Thanks @victorsingh! 🙌 

Patch:
diff --git a/.all-contributorsrc b/.all-contributorsrc
index cd74f0c..00322ff 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -82,6 +82,9 @@
       "avatar_url": "https://avatars2.githubusercontent.com/u/16357592?v=4",
       "profile": "https://github.com/victorsingh",
       "contributions": [
+        "code",
+        "doc",
+        "test",
         "bug"
       ]
     }
diff --git a/packages/data-point/README.md b/packages/data-point/README.md
index 220e163..d347af2 100644
--- a/packages/data-point/README.md
+++ b/packages/data-point/README.md
@@ -42,7 +42,7 @@ npm install --save data-point
 - [Entities](#entities)
   - [dataPoint.addEntities](#api-data-point-add-entities)
   - [Built-in entities](#built-in-entities)
-    - [Transform](#transform-entity)
+    - [Reducer / Transform](#reducer-entity)
     - [Model](#model-entity)
     - [Entry](#entry-entity)
     - [Request](#request-entity)
@@ -350,7 +350,7 @@ The `Accumulator.value` property is the data source from which you want to apply
 | *value*  | `Object` | Value to be transformed. |
 | *initialValue*  | `Object` | Initial value passed to the an entity. You can use this value as a reference to the initial value passed to your Entity before any reducer was applied. |
 | *values*  | `Object` | Access to the values stored via [dataPoint.addValue](#api-data-point-add-value). |
-| *params*  | `Object` | Value of the current Entity's params property. (for all entites except transform) |
+| *params*  | `Object` | Value of the current Entity's params property. (for all entities except [Reducer](#reducer-entity)) |
 | *locals*  | `Object` | Value passed from the `options` _argument_ when executing [dataPoint.transform](#api-data-point-transform). |
 | *reducer*  | `Object` | Information relative to the current [Reducer](#reducers) being executed. |
 
@@ -1329,7 +1329,7 @@ dataPoint.addEntities({
 
 DataPoint comes with the following built-in entities: 
 
-- [Transform](#transform-entity)
+- [Reducer / Transform](#reducer-entity)
 - [Model](#model-entity)
 - [Entry](#entry-entity)
 - [Request](#request-entity)
@@ -1340,7 +1340,7 @@ DataPoint comes with the following built-in entities:
 
 #### <a name="entity-base-api">Entity Base API</a>
 
-All entities share a common API (except for [Transform](#transform-entity)).
+All entities share a common API (except for [Reducer](#reducer-entity)).
 
 ```js
 {
@@ -1482,16 +1482,22 @@ To customize type checking you may use a [Reducer](#reducers). If the reducer th
 </details>
 
 
-#### <a name="transform-entity">Transform Entity</a>
+#### <a name="reducer-entity">Reducer Entity</a>
 
-A Transform entity is meant to be used as a 'snippet' entity that you can re-use in other entities. It does not expose the before/after/error/params API that other entities have.
+A Reducer entity is a 'snippet' that you can re-use in other entities. It does not expose the before/after/error/params API that other entities have.
 
-The value of a Transform entity is a [Reducer](#reducers).
-
-IMPORTANT: Transform Entities **do not support** [extension](#extending-entities).
+IMPORTANT: Reducer Entities **do not support** [extension](#extending-entities).
 
 **SYNOPSIS**
 
+```js
+dataPoint.addEntities({
+  'reducer:<entityId>': Reducer
+})
+```
+
+For backwards compatibility, the keyword **transform** can be used in place of **reducer**:
+
 ```js
 dataPoint.addEntities({
   'transform:<entityId>': Reducer
@@ -1499,7 +1505,7 @@ dataPoint.addEntities({
 ```
 
 <details>
-  <summary>Transform Entity Example</summary>
+  <summary>Reducer Entity Example</summary>
   
   ```js
   const input = {
@@ -1519,11 +1525,11 @@ dataPoint.addEntities({
   }
   
   dataPoint.addEntities({
-    'transform:foo': ['$a.b.c', getMax, multiplyBy(10)]
+    'reducer:foo': ['$a.b.c', getMax, multiplyBy(10)]
   })
   
   dataPoint
-    .resolve('transform:foo', input)
+    .resolve('reducer:foo', input)
     .then((output) => {
       assert.equal(output, 30)
     })
diff --git a/packages/data-point/lib/core/core.js b/packages/data-point/lib/core/core.js
index c5f17ae..4f0c133 100644
--- a/packages/data-point/lib/core/core.js
+++ b/packages/data-point/lib/core/core.js
@@ -72,7 +72,7 @@ function create (spec) {
   manager.addEntities = _.partial(addEntitiesToStore, manager.entities)
 
   // built-in entity types
-  manager.addEntityType('transform', EntityTransform)
+  manager.addEntityType('reducer', EntityTransform)
   manager.addEntityType('entry', EntityEntry)
   // alias to entry, may be used to process any object type
   manager.addEntityType('model', EntityModel)
@@ -80,6 +80,7 @@ function create (spec) {
   manager.addEntityType('collection', EntityCollection)
   manager.addEntityType('request', EntityRequest)
   // for backwards compatibility
+  manager.addEntityType('transform', EntityTransform)
   manager.addEntityType('source', EntityRequest)
   manager.addEntityType('control', EntityControl)
   manager.addEntityType('schema', EntitySchema)


Test patch:
diff --git a/packages/data-point/lib/entity-types/entity-transform/resolve.test.js b/packages/data-point/lib/entity-types/entity-transform/resolve.test.js
index 73540e3..e6df2d5 100644
--- a/packages/data-point/lib/entity-types/entity-transform/resolve.test.js
+++ b/packages/data-point/lib/entity-types/entity-transform/resolve.test.js
@@ -45,3 +45,21 @@ describe('entity.transform.value', () => {
     })
   })
 })
+
+describe('entity.reducer.value', () => {
+  test('should resolve value Transform', () => {
+    return transform('reducer:a0', {
+      message: 'hello world'
+    }).then(acc => {
+      expect(acc.value).toEqual('hello world')
+    })
+  })
+
+  test('should resolve context Transform', () => {
+    return transform('reducer:a1', {
+      message: 'hello world'
+    }).then(acc => {
+      expect(acc.value).toEqual('HELLO WORLD')
+    })
+  })
+})
diff --git a/packages/data-point/test/definitions/entities.js b/packages/data-point/test/definitions/entities.js
index a8e2a0b..e9f28cc 100644
--- a/packages/data-point/test/definitions/entities.js
+++ b/packages/data-point/test/definitions/entities.js
@@ -8,5 +8,6 @@ module.exports = Object.assign(
   require('./collection'),
   require('./requests'),
   require('./control'),
-  require('./integrations')
+  require('./integrations'),
+  require('./reducer')
 )
diff --git a/packages/data-point/test/definitions/reducer.js b/packages/data-point/test/definitions/reducer.js
new file mode 100644
index 0000000..c56df8a
--- /dev/null
+++ b/packages/data-point/test/definitions/reducer.js
@@ -0,0 +1,4 @@
+module.exports = {
+  'reducer:a0': '$message',
+  'reducer:a1': ['reducer:a0', value => value.toUpperCase()]
+}