From c9c84dd5f9468a877120bc80be7f1e07e0f45baf Mon Sep 17 00:00:00 2001 From: Ivan Eisenberg Date: Tue, 10 Mar 2020 09:26:16 -0500 Subject: [PATCH 1/3] fix bad data bug in get function --- src/functions/equals.spec.js | 19 +++++++++++++++++++ src/functions/find.spec.js | 16 ++++++++++++++++ src/private/get.js | 3 ++- src/private/get.spec.js | 19 +++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/functions/equals.spec.js b/src/functions/equals.spec.js index 16e38a4..0aa7d2d 100644 --- a/src/functions/equals.spec.js +++ b/src/functions/equals.spec.js @@ -61,4 +61,23 @@ describe('equals', () => { expect(actual).toEqual(expected) }) + + it('should return false if called with bad data', () => { + const redArr = [ + undefined, + null, + () => {}, + true, + false, + 1337 + ] + + let actual + + const expected = false + redArr.forEach((x) => { + actual = equals('@foo[0].bar', 'bar')(x) + expect(actual).toEqual(expected) + }) + }) }) diff --git a/src/functions/find.spec.js b/src/functions/find.spec.js index 49029e2..2ae9061 100644 --- a/src/functions/find.spec.js +++ b/src/functions/find.spec.js @@ -67,4 +67,20 @@ describe('find', () => { const expected = 'This is somewhere else!' expect(actual).toEqual(expected) }) + + it('should return undefined when called with no data', () => { + const logic = [{ foo: 'hello', rule: equals('@foo', 'foo') }] + const actual = find(logic) + const expected = undefined + + expect(actual).toEqual(expected) + }) + + it('should return undefined when called with null', () => { + const logic = [{ foo: 'hello', rule: equals('@foo', 'foo') }] + const actual = find(logic, null) + const expected = undefined + + expect(actual).toEqual(expected) + }) }) diff --git a/src/private/get.js b/src/private/get.js index 1dc3fe2..74eca4c 100644 --- a/src/private/get.js +++ b/src/private/get.js @@ -1,4 +1,5 @@ -const get = (object, keys) => { +const get = (object, keys = '') => { + if (!object) { object = {} } keys = Array.isArray(keys) ? keys : keys.replace(/\[(['"]?)([\w\d-]+)(['"]?)\]/g, '.$2').split('.') object = object[keys[0]] if (object && keys.length > 1) { diff --git a/src/private/get.spec.js b/src/private/get.spec.js index a093ede..e2e633b 100644 --- a/src/private/get.spec.js +++ b/src/private/get.spec.js @@ -61,4 +61,23 @@ describe('get', () => { expect(actual).toEqual(expected) }) + + it('should return undefined when called with bad data', () => { + const redArr = [ + undefined, + null, + () => {}, + true, + false, + 1337 + ] + + let actual + + const expected = undefined + redArr.forEach((x) => { + actual = get(x, '@foo') + expect(actual).toEqual(expected) + }) + }) }) From ccbb0fc7814f978b27d464697193f3b50f316659 Mon Sep 17 00:00:00 2001 From: Ivan Eisenberg Date: Tue, 10 Mar 2020 09:26:23 -0500 Subject: [PATCH 2/3] 3.0.4 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 89fa81c..6bc5248 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "regent", - "version": "3.0.3", + "version": "3.0.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 2455da3..d728629 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "regent", - "version": "3.0.3", + "version": "3.0.4", "description": "Javascript rules engine", "repository": { "type": "git", From 0201dc74d774e54864c1e05b07d1fc624cec3819 Mon Sep 17 00:00:00 2001 From: Ivan Eisenberg Date: Tue, 10 Mar 2020 10:18:44 -0500 Subject: [PATCH 3/3] support array lookup syntax, better test coverage --- src/private/get.js | 11 +++++++++-- src/private/get.spec.js | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/private/get.js b/src/private/get.js index 74eca4c..9355137 100644 --- a/src/private/get.js +++ b/src/private/get.js @@ -1,10 +1,17 @@ -const get = (object, keys = '') => { +const get = (object, keys) => { if (!object) { object = {} } - keys = Array.isArray(keys) ? keys : keys.replace(/\[(['"]?)([\w\d-]+)(['"]?)\]/g, '.$2').split('.') + + keys = Array.isArray(keys) ? keys : `${keys}` + .replace(/\[['"]?([\w\d-]+)['"]?\]/g, '.$1') + .split('.') + .filter(x => x) + object = object[keys[0]] + if (object && keys.length > 1) { return get(object, keys.slice(1)) } + return object } diff --git a/src/private/get.spec.js b/src/private/get.spec.js index e2e633b..8f9de02 100644 --- a/src/private/get.spec.js +++ b/src/private/get.spec.js @@ -62,6 +62,32 @@ describe('get', () => { expect(actual).toEqual(expected) }) + it('should return undefined if not provided a selector', () => { + const data = { + foo: { 'ba-ar': 'bars' } + } + const actual = get(data) + const expected = undefined + + expect(actual).toEqual(expected) + }) + + it('should allow lookups in arrays', () => { + const greenArr = [ + '[1]', + '1', + 1 + ] + + const data = ['foo', 'bar'] + const expected = 'bar' + + greenArr.forEach((x) => { + const actual = get(data, x) + expect(actual).toEqual(expected) + }) + }) + it('should return undefined when called with bad data', () => { const redArr = [ undefined,