Skip to content

Commit

Permalink
fix: allow empty string in $contains (#2179)
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes committed Jul 18, 2023
1 parent 8469ecd commit 2c76e2d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
8 changes: 8 additions & 0 deletions playground/basic/content/1.real-content/post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Post 1
authors: [ "alice", "bob" ]
---

# Post 1

This is the first post written by Alice and Bob used to try `$contains` filter.
9 changes: 9 additions & 0 deletions playground/basic/pages/custom-query.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts" setup>
const { data } = await useAsyncData('posts', () => queryContent('/real-content/post').where({ authors: { $contains: '' } }).find())
</script>

<template>
<div>
{{ data }}
</div>
</template>
5 changes: 3 additions & 2 deletions src/runtime/query/match/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@ export const assertArray = (value: any, message = 'Expected an array') => {
/**
* Ensure result is an array
*/
export const ensureArray = <T>(value: T) =>
(Array.isArray(value) ? value : value ? [value] : []) as T extends Array<any> ? T : T[]
export const ensureArray = <T>(value: T) => {
return (Array.isArray(value) ? value : [undefined, null].includes(value as any) ? [] : [value]) as T extends Array<any> ? T : T[]
}
9 changes: 9 additions & 0 deletions test/features/query/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ describe('Match', () => {

// match multiple conditions
expect(match(item, { 'nested.users.0': { $contains: ['Maha', 'tma'] } })).toBe(true)

expect(match({ values: [0, false, ''] }, { values: { $contains: '' } })).toBe(true)
expect(match({ values: [0, false, ''] }, { values: { $contains: 0 } })).toBe(true)
expect(match({ values: [0, false, ''] }, { values: { $contains: false } })).toBe(true)
})

test('$icontains string', () => {
Expand All @@ -38,6 +42,11 @@ describe('Match', () => {
expect(match(item, { 'nested.users': { $contains: ['Woodrow', 'Steve'] } })).toBe(true)
expect(match(item, { 'nested.users': { $contains: ['Woodrow', 'Steve', 'Mahatma'] } })).toBe(true)
expect(match(item, { 'nested.users': { $contains: ['Woodrow', 'Steve', 'Mahatma', 'John'] } })).toBe(false)
expect(match(item, { 'nested.users': { $contains: '' } })).toBe(false)
expect(match(item, { 'nested.users': { $contains: 0 } })).toBe(false)
expect(match(item, { 'nested.users': { $contains: false } })).toBe(false)
expect(match(item, { 'nested.users': { $contains: null } })).toBe(true)
expect(match(item, { 'nested.users': { $contains: undefined } })).toBe(true)
})

test('$containsAny', () => {
Expand Down

0 comments on commit 2c76e2d

Please sign in to comment.