Skip to content

Commit

Permalink
Merge pull request #35 from northwesternmutual/release/3.0.4
Browse files Browse the repository at this point in the history
Release/3.0.4
  • Loading branch information
eisenivan authored Mar 10, 2020
2 parents 3eaf2cd + 0201dc7 commit b0141c8
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "regent",
"version": "3.0.3",
"version": "3.0.4",
"description": "Javascript rules engine",
"repository": {
"type": "git",
Expand Down
19 changes: 19 additions & 0 deletions src/functions/equals.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
16 changes: 16 additions & 0 deletions src/functions/find.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
10 changes: 9 additions & 1 deletion src/private/get.js
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
45 changes: 45 additions & 0 deletions src/private/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

0 comments on commit b0141c8

Please sign in to comment.