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", 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..9355137 100644 --- a/src/private/get.js +++ b/src/private/get.js @@ -1,9 +1,17 @@ const get = (object, keys) => { - keys = Array.isArray(keys) ? keys : keys.replace(/\[(['"]?)([\w\d-]+)(['"]?)\]/g, '.$2').split('.') + if (!object) { object = {} } + + 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 a093ede..8f9de02 100644 --- a/src/private/get.spec.js +++ b/src/private/get.spec.js @@ -61,4 +61,49 @@ 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, + null, + () => {}, + true, + false, + 1337 + ] + + let actual + + const expected = undefined + redArr.forEach((x) => { + actual = get(x, '@foo') + expect(actual).toEqual(expected) + }) + }) })