Skip to content

Commit

Permalink
Fix bug where lookup did not work on object
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed May 18, 2024
1 parent 5a940d6 commit bf90f7f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/operations/lookup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ test('should match several matches when `matchSeveral` is true', async (t) => {
})

test('should force the value at array path to an array', async (t) => {
const props = { arrayPath: '^^related.users', propPath: 'id' } // No array brackets in path
const data = {
content: { author: 'user2' },
related: {
Expand Down
9 changes: 8 additions & 1 deletion src/operations/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../utils/stateHelpers.js'
import { defToOperation } from '../utils/definitionHelpers.js'
import { noopNext } from '../utils/stateHelpers.js'
import { isObject } from '../utils/is.js'

export interface Props extends TransformerProps {
arrayPath: Path
Expand Down Expand Up @@ -49,7 +50,13 @@ const matchInArray =
const getFn = getArray({})(noopNext)
return async (value: unknown) => {
const { value: arr } = await getFn(goForward(state))
return Array.isArray(arr) ? match(value, state, arr, getProp) : undefined
if (Array.isArray(arr)) {
return match(value, state, arr, getProp)
} else if (isObject(arr)) {
return match(value, state, [arr], getProp)
} else {
return undefined
}
}
}

Expand Down

0 comments on commit bf90f7f

Please sign in to comment.