Skip to content
Merged
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
4 changes: 3 additions & 1 deletion src/runtime/query/match/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ function createOperators (match: (...args: any[]) => boolean, operators: Record<
/**
* Match if item is in condition array
**/
$in: (item, condition) => ensureArray(condition).some(cond => match(item, cond)),
$in: (item, condition) => ensureArray(condition).some(
cond => Array.isArray(item) ? match(item, { $contains: cond }) : match(item, cond)
),

/**
* Match if item contains every condition or math every rule in condition array
Expand Down
49 changes: 38 additions & 11 deletions test/features/query/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@ describe('Match', () => {
})
})

test('$in', () => {
const item = { id: 1, name: 'a', to: 'a', category: 'c1', nested: { users: ['Mahatma', 'Steve', 'Woodrow'] } }

expect(match(item, { name: { $in: ['a', 'b'] } })).toBe(true)
expect(match(item, { category: { $in: 'c1' } })).toBe(true)
expect(match(item, { category: { $in: ['c1', 'c2'] } })).toBe(true)
expect(match(item, { category: { $in: ['c2', 'c3'] } })).toBe(false)

expect(match(item, { id: { $in: [1, 2] } })).toBe(true)
})

test('$eq', () => {
// string
expect(match(item, { name: { $eq: 'a' } })).toBe(true)
Expand Down Expand Up @@ -187,4 +176,42 @@ describe('Match', () => {
expect(match(item, { id: { $lte: 0 } })).toBe(false)
})
})

describe('$in ', () => {
test('string filed', () => {
const item = { id: 1, name: 'a', to: 'a', category: 'c1', nested: { users: ['Mahatma', 'Steve', 'Woodrow'] } }

expect(match(item, { name: { $in: ['a', 'b'] } })).toBe(true)
expect(match(item, { category: { $in: 'c1' } })).toBe(true)
expect(match(item, { category: { $in: ['c1', 'c2'] } })).toBe(true)
expect(match(item, { category: { $in: ['c2', 'c3'] } })).toBe(false)

expect(match(item, { id: { $in: [1, 2] } })).toBe(true)
})

test('array field', () => {
const data = [
{
name: 'post1',
tags: ['school', 'office']
},
{
name: 'post2',
tags: ['school', 'home']
},
{
item: 'Maps',
tags: ['office', 'storage']
}
]

const condition = { tags: { $in: ['school', 'home'] } }
data.forEach((item) => {
expect(match(item, condition)).toBe(
item.tags.includes(condition.tags.$in[0]) ||
item.tags.includes(condition.tags.$in[1])
)
})
})
})
})