Repository: ViacomInc/data-point
Pull request #94 (2018-01-01 14:58:20)

Remove redundant validation from TransformExpression
<!-- What changes are being made? (What feature/bug is being fixed here?) -->
**What**: #90

<!-- Why are these changes necessary? -->
**Why**: This removes redundant code.

<!-- 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? -->

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

Coverage remained the same at 100.0% when pulling **58dd89b0392d398ddb897c28194f5d36aaffa498 on raingerber:remove-transform-expression-validation** into **ce851a75793d16daa4a25fe904295218e6b7245c on ViacomInc:master**.

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

Coverage remained the same at 100.0% when pulling **54c0ee52c7f1ecbd0c8ce685cfceecb067a8ff55 on raingerber:remove-transform-expression-validation** into **ce851a75793d16daa4a25fe904295218e6b7245c on ViacomInc:master**.

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

Coverage remained the same at 100.0% when pulling **dda1917b321832aafeac92132b6adaa7795a0f7a on raingerber:remove-transform-expression-validation** into **d5ab2c6d8634f291dac01a74bafb3ceaba5cbe01 on ViacomInc:master**.


Patch:
diff --git a/packages/data-point/lib/reducer/factory.js b/packages/data-point/lib/reducer/factory.js
index 7162ba8..47b589b 100644
--- a/packages/data-point/lib/reducer/factory.js
+++ b/packages/data-point/lib/reducer/factory.js
@@ -12,12 +12,13 @@ const reducerTypes = [ReducerPath, ReducerFunction, ReducerEntity]
 
 /**
  * parse reducer
- * @param  {string} source - reducer string representation
+ * @param {Function} createTransform
+ * @param {*} source
+ * @throws if source is not a valid type for creating a reducer
  * @return {reducer}
  */
 function create (createTransform, source) {
-  // ReducerObject requires an extra parameter, so
-  // it's not included in the reducerTypes array
+  // ReducerObject requires an extra parameter, so it's not included in the reducerTypes array
   if (ReducerObject.isType(source)) {
     return ReducerObject.create(createTransform, source)
   }
@@ -29,8 +30,8 @@ function create (createTransform, source) {
       'Invalid reducer type.',
       ' Could not find a matching reducer type while parsing the value:\n ',
       _.attempt(util.inspect, source),
-      '\nFor a list of supported types visit:\n',
-      'https://github.com/ViacomInc/data-point/tree/master/packages/data-point#reducers\n'
+      '\nTry using an Array, String, Object, or Function.\n',
+      'More info: https://github.com/ViacomInc/data-point/tree/master/packages/data-point#reducers\n'
     ].join('')
 
     throw new Error(message)
diff --git a/packages/data-point/lib/transform-expression/factory.js b/packages/data-point/lib/transform-expression/factory.js
index 055da80..31efd3c 100644
--- a/packages/data-point/lib/transform-expression/factory.js
+++ b/packages/data-point/lib/transform-expression/factory.js
@@ -1,7 +1,6 @@
 'use strict'
 
 const _ = require('lodash')
-const util = require('util')
 const reducerFactory = require('../reducer/factory')
 
 /**
@@ -16,6 +15,10 @@ function TransformExpression () {
 
 module.exports.TransformExpression = TransformExpression
 
+/**
+ * @param {string} source
+ * @returns {Array}
+ */
 function parseFromString (source) {
   const transformSource = _.defaultTo(source, '')
   const tokens = _.compact(transformSource.split(' | '))
@@ -23,11 +26,19 @@ function parseFromString (source) {
 }
 module.exports.parseFromString = parseFromString
 
+/**
+ * @param {*} source
+ * @returns {*}
+ */
 function parseTokenExpression (source) {
   return _.isString(source) ? parseFromString(source) : source
 }
 module.exports.parseTokenExpression = parseTokenExpression
 
+/**
+ * @param {Array} source
+ * @returns {Array}
+ */
 function parseFromArray (source) {
   return _.chain(source)
     .compact()
@@ -35,51 +46,25 @@ function parseFromArray (source) {
     .flatten()
     .value()
 }
+
 module.exports.parseFromArray = parseFromArray
 
-function parse (src) {
-  let source = _.defaultTo(src, [])
-  source = _.castArray(source)
-  return parseFromArray(source)
+/**
+ * @param {*} source
+ * @returns {Array}
+ */
+function parse (source) {
+  return parseFromArray(source ? _.castArray(source) : [])
 }
 
 module.exports.parse = parse
 
-function isValid (source) {
-  const type = typeof source
-  return (
-    type === 'string' ||
-    type === 'function' ||
-    source instanceof Array ||
-    _.isPlainObject(source)
-  )
-}
-
-module.exports.isValid = isValid
-
-function validate (source) {
-  const isValidTransform = isValid(source)
-  if (isValidTransform) {
-    return true
-  }
-  const message = [
-    `Could not parse a TransformExpression. The TransformExpression:\n `,
-    _.attempt(util.inspect, source),
-    '\nis not using a valid type, try using an Array, String, Object, or Function.',
-    '\nMore info: https://github.com/ViacomInc/data-point/tree/master/packages/data-point#transform-expression\n'
-  ].join('')
-  throw new Error(message)
-}
-
-module.exports.validate = validate
-
 /**
  * parses a raw transform
- * @param  {string} transformRaw raw value path to be parsed
+ * @param {string} source
  * @return {Transform}
  */
 function create (source = []) {
-  validate(source)
   const tokens = parse(source)
 
   const transform = new TransformExpression()


Test patch:
diff --git a/packages/data-point/lib/core/__snapshots__/transform.test.js.snap b/packages/data-point/lib/core/__snapshots__/transform.test.js.snap
index da2a3af..b081341 100644
--- a/packages/data-point/lib/core/__snapshots__/transform.test.js.snap
+++ b/packages/data-point/lib/core/__snapshots__/transform.test.js.snap
@@ -3,7 +3,7 @@
 exports[`transform - throw error in invalid id(promise) 1`] = `
 [Error: Invalid reducer type. Could not find a matching reducer type while parsing the value:
  'INVALID'
-For a list of supported types visit:
-https://github.com/ViacomInc/data-point/tree/master/packages/data-point#reducers
+Try using an Array, String, Object, or Function.
+More info: https://github.com/ViacomInc/data-point/tree/master/packages/data-point#reducers
 ]
 `;
diff --git a/packages/data-point/lib/reducer/__snapshots__/factory.test.js.snap b/packages/data-point/lib/reducer/__snapshots__/factory.test.js.snap
index 1baa580..3964ab1 100644
--- a/packages/data-point/lib/reducer/__snapshots__/factory.test.js.snap
+++ b/packages/data-point/lib/reducer/__snapshots__/factory.test.js.snap
@@ -3,7 +3,23 @@
 exports[`reducer#create detect invalid 1`] = `
 "Invalid reducer type. Could not find a matching reducer type while parsing the value:
  'a'
-For a list of supported types visit:
-https://github.com/ViacomInc/data-point/tree/master/packages/data-point#reducers
+Try using an Array, String, Object, or Function.
+More info: https://github.com/ViacomInc/data-point/tree/master/packages/data-point#reducers
+"
+`;
+
+exports[`reducer#create detect invalid 2`] = `
+"Invalid reducer type. Could not find a matching reducer type while parsing the value:
+ true
+Try using an Array, String, Object, or Function.
+More info: https://github.com/ViacomInc/data-point/tree/master/packages/data-point#reducers
+"
+`;
+
+exports[`reducer#create detect invalid 3`] = `
+"Invalid reducer type. Could not find a matching reducer type while parsing the value:
+ 1
+Try using an Array, String, Object, or Function.
+More info: https://github.com/ViacomInc/data-point/tree/master/packages/data-point#reducers
 "
 `;
diff --git a/packages/data-point/lib/reducer/factory.test.js b/packages/data-point/lib/reducer/factory.test.js
index d0431d8..3ceb661 100644
--- a/packages/data-point/lib/reducer/factory.test.js
+++ b/packages/data-point/lib/reducer/factory.test.js
@@ -39,5 +39,13 @@ describe('reducer#create', () => {
     expect(() => {
       Factory.create(createTransform, 'a')
     }).toThrowErrorMatchingSnapshot()
+
+    expect(() => {
+      Factory.create(createTransform, true)
+    }).toThrowErrorMatchingSnapshot()
+
+    expect(() => {
+      Factory.create(createTransform, 1)
+    }).toThrowErrorMatchingSnapshot()
   })
 })
diff --git a/packages/data-point/lib/transform-expression/__snapshots__/factory.test.js.snap b/packages/data-point/lib/transform-expression/__snapshots__/factory.test.js.snap
deleted file mode 100644
index a9cef92..0000000
--- a/packages/data-point/lib/transform-expression/__snapshots__/factory.test.js.snap
+++ /dev/null
@@ -1,9 +0,0 @@
-// Jest Snapshot v1, https://goo.gl/fbAQLP
-
-exports[`validate It should throw error if transform is invalid 1`] = `
-"Could not parse a TransformExpression. The TransformExpression:
- true
-is not using a valid type, try using an Array, String, Object, or Function.
-More info: https://github.com/ViacomInc/data-point/tree/master/packages/data-point#transform-expression
-"
-`;
diff --git a/packages/data-point/lib/transform-expression/factory.test.js b/packages/data-point/lib/transform-expression/factory.test.js
index 17aa38e..b65eaa5 100644
--- a/packages/data-point/lib/transform-expression/factory.test.js
+++ b/packages/data-point/lib/transform-expression/factory.test.js
@@ -4,32 +4,6 @@
 const _ = require('lodash')
 const TransformFactory = require('./factory')
 
-describe('isValid', () => {
-  test('It should return true if type is valid', () => {
-    expect(TransformFactory.isValid('string')).toEqual(true)
-    expect(TransformFactory.isValid(() => 1)).toEqual(true)
-    expect(TransformFactory.isValid([])).toEqual(true)
-    expect(TransformFactory.isValid({})).toEqual(true)
-  })
-
-  test('should return false if invalid', () => {
-    expect(TransformFactory.isValid(1)).toEqual(false)
-    expect(TransformFactory.isValid(true)).toEqual(false)
-  })
-})
-
-describe('validate', () => {
-  test('It should throw error if transform is invalid', () => {
-    expect(() => {
-      TransformFactory.validate(true)
-    }).toThrowErrorMatchingSnapshot()
-  })
-
-  test('it should return true if valid', () => {
-    expect(TransformFactory.validate('$.')).toEqual(true)
-  })
-})
-
 describe('TransformFactory#create', () => {
   test('TransformFactory#create default', () => {
     const result = TransformFactory.create()