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(findSurround): allow before and after to be 0 #1922

Merged
merged 2 commits into from
Mar 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/runtime/query/match/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export function createPipelineFetcher<T> (getContentsList: () => Promise<T[]>) {
// Find matched item index
const index = data.findIndex(item => match(item, matchQuery))

before = before || 1
after = after || 1
before = before ?? 1
after = after ?? 1
const slice = new Array(before + after).fill(null, 0)

return index === -1 ? slice : slice.map((_, i) => data[index - before! + i + Number(i >= before!)] || null)
Expand Down
18 changes: 18 additions & 0 deletions test/features/query/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ describe('Database Provider', () => {
assert(result[2]._path === '/c')
})

test('Surround with 0 item before', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))
const result = await createQuery(fetcher)
.findSurround('/b', { before: 0, after: 1 })

assert((result as Array<any>).length === 1)
assert(result[0]._path === '/c')
})

test('Surround with 0 item after', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))
const result = await createQuery(fetcher)
.findSurround('/b', { before: 1, after: 0 })

assert((result as Array<any>).length === 1)
assert(result[0]._path === '/a')
})

test('Surround with object', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))
const result = await createQuery(fetcher)
Expand Down