Repository: ViacomInc/data-point
Pull request #38 (2017-12-07 03:45:54)

Improve regex for detecting entity ids
This addresses #37, I'm just not sure how to treat hyphens in entity names. I see names like `transform:my-test-query` in the code, so I made it possible to include hyphens in the substring after the colon (including trailing hyphens like in `aaa:aaa-`). Hyphens are still not allowed before the colon though. Please let me know if I should change any of that.


Patch:
diff --git a/packages/data-point/lib/reducer-entity/factory.js b/packages/data-point/lib/reducer-entity/factory.js
index 29a9682..4376066 100644
--- a/packages/data-point/lib/reducer-entity/factory.js
+++ b/packages/data-point/lib/reducer-entity/factory.js
@@ -20,7 +20,8 @@ module.exports.ReducerEntity = ReducerEntity
 
 function isEntity (source) {
   return (
-    _.isString(source) && source.match(/^[^$]([\w.]+):?([\w.]+)(\[])?/) !== null
+    _.isString(source) &&
+    source.match(/^([^$][\w.]*):([\w.-]+)(\[])?$/) !== null
   )
 }
 


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
new file mode 100644
index 0000000..da2a3af
--- /dev/null
+++ b/packages/data-point/lib/core/__snapshots__/transform.test.js.snap
@@ -0,0 +1,9 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+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
+]
+`;
diff --git a/packages/data-point/lib/core/transform.test.js b/packages/data-point/lib/core/transform.test.js
index c966648..61047e1 100644
--- a/packages/data-point/lib/core/transform.test.js
+++ b/packages/data-point/lib/core/transform.test.js
@@ -24,9 +24,12 @@ beforeAll(() => {
 })
 
 test('transform - throw error in invalid id(promise)', () => {
-  return transform(dataPoint, 'INVALID', TestData, {}).catch(res => {
-    expect(res.name).toBe('InvalidId')
-  })
+  return transform(dataPoint, 'INVALID', TestData, {})
+    .catch(err => err)
+    .then(res => {
+      expect(res).toBeInstanceOf(Error)
+      expect(res).toMatchSnapshot()
+    })
 })
 
 test('transform - single reducer', () => {
diff --git a/packages/data-point/lib/reducer-entity/factory.test.js b/packages/data-point/lib/reducer-entity/factory.test.js
index 6fdceb8..6c76514 100644
--- a/packages/data-point/lib/reducer-entity/factory.test.js
+++ b/packages/data-point/lib/reducer-entity/factory.test.js
@@ -4,13 +4,23 @@
 const factory = require('./factory')
 
 test('reducer/reducer-entity#isEntity', () => {
-  expect(factory.isEntity('a')).not.toBe('is not Entity')
-  expect(factory.isEntity('#a')).not.toBe('is not Entity')
-  expect(factory.isEntity('#ab')).not.toBe('is not Entity')
-  expect(factory.isEntity('$ab:abc')).not.toBe('is not Entity')
-  expect(factory.isEntity('abc:abc')).toBeTruthy()
-  expect(factory.isEntity('#abc:abc')).toBeTruthy()
-  expect(factory.isEntity('abc:abc[]')).toBeTruthy()
+  expect(factory.isEntity('a')).toBe(false)
+  expect(factory.isEntity('ab')).toBe(false)
+  expect(factory.isEntity('#ab')).toBe(false)
+  expect(factory.isEntity('abc')).toBe(false)
+  expect(factory.isEntity('ab:')).toBe(false)
+  expect(factory.isEntity(':ab')).toBe(false)
+  expect(factory.isEntity('$a:abc')).toBe(false)
+  expect(factory.isEntity('a-bc:abc')).toBe(false)
+  expect(factory.isEntity('a:abc')).toBe(true)
+  expect(factory.isEntity('abc:a')).toBe(true)
+  expect(factory.isEntity('abc:abc')).toBe(true)
+  expect(factory.isEntity('abc:ab-c-')).toBe(true)
+  expect(factory.isEntity('_abc:abc')).toBe(true)
+  expect(factory.isEntity('#abc:abc')).toBe(true)
+  expect(factory.isEntity('abc:abc[]')).toBe(true)
+  expect(factory.isEntity('abc:abc[][]')).toBe(false)
+  expect(factory.isEntity('abc:abc[]d')).toBe(false)
 })
 
 test('reducer/reducer-entity#create', () => {