Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: empty array does not throw error when traversing #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions __tests__/lib/evaluator/Evaluator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ describe('Evaluator', () => {
const e = new Evaluator(grammar, context)
return expect(e.eval(toTree('foo.bar.tek.hello'))).resolves.toBe('world')
})
it('empty array does not throw error when traversing', async () => {
const context = {
foo: {
bar: []
}
}
const e = new Evaluator(grammar, context)
return expect(e.eval(toTree('foo.bar.tek.hello'))).resolves.toBe(undefined)
})
it('makes array elements addressable by index', async () => {
const context = {
foo: {
Expand Down
6 changes: 3 additions & 3 deletions lib/evaluator/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ exports.Identifier = function (ast) {
return ast.relative ? this._relContext[ast.value] : this._context[ast.value]
}
return this.eval(ast.from).then((context) => {
if (context === undefined || context === null) {
return undefined
}
if (Array.isArray(context)) {
context = context[0]
}
if (context === undefined || context === null) {
return undefined
}
return context[ast.value]
})
}
Expand Down