-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,051 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import test from 'ava' | ||
|
||
import createPaging from './createPaging' | ||
|
||
// Helpers | ||
|
||
const prepareData = (data) => data.map((item) => ({...item, _id: `${item.type}:${item.id}`})) | ||
|
||
// Tests | ||
|
||
test('should return next: null when no data', (t) => { | ||
const data = [] | ||
const params = {type: 'entry', pageSize: 2} | ||
const expected = {next: null} | ||
|
||
const ret = createPaging(data, params) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should return paging for first page', (t) => { | ||
const data = prepareData([{id: 'ent1', type: 'entry'}, {id: 'ent2', type: 'entry'}]) | ||
const params = { | ||
type: 'entry', | ||
pageSize: 2 | ||
} | ||
const expected = { | ||
next: { | ||
type: 'entry', | ||
query: {_id: {$gte: 'entry:ent2'}}, | ||
pageAfter: 'entry:ent2', | ||
pageSize: 2 | ||
} | ||
} | ||
|
||
const ret = createPaging(data, params) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should return paging for second page', (t) => { | ||
const data = prepareData([{id: 'ent3', type: 'entry'}, {id: 'ent4', type: 'entry'}]) | ||
const params = { | ||
type: 'entry', | ||
query: {_id: {$gte: 'entry:ent2'}}, | ||
pageAfter: 'entry:ent2', | ||
pageSize: 2 | ||
} | ||
const expected = { | ||
next: { | ||
type: 'entry', | ||
query: {_id: {$gte: 'entry:ent4'}}, | ||
pageAfter: 'entry:ent4', | ||
pageSize: 2 | ||
} | ||
} | ||
|
||
const ret = createPaging(data, params) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should return paging when sorting', (t) => { | ||
const data = prepareData([ | ||
{id: 'ent2', type: 'entry', attributes: {index: 1}}, | ||
{id: 'ent3', type: 'entry', attributes: {index: 2}} | ||
]) | ||
const params = { | ||
type: 'entry', | ||
pageSize: 2 | ||
} | ||
const sort = { | ||
'attributes.index': 1 | ||
} | ||
const expected = { | ||
next: { | ||
type: 'entry', | ||
query: {'attributes.index': {$gte: 2}}, | ||
pageAfter: 'entry:ent3', | ||
pageSize: 2 | ||
} | ||
} | ||
|
||
const ret = createPaging(data, params, sort) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should return paging when sorting descending', (t) => { | ||
const data = prepareData([ | ||
{id: 'ent3', type: 'entry', attributes: {index: 2}}, | ||
{id: 'ent2', type: 'entry', attributes: {index: 1}} | ||
]) | ||
const params = { | ||
type: 'entry', | ||
pageSize: 2 | ||
} | ||
const sort = { | ||
'attributes.index': -1 | ||
} | ||
const expected = { | ||
next: { | ||
type: 'entry', | ||
query: {'attributes.index': {$lte: 1}}, | ||
pageAfter: 'entry:ent2', | ||
pageSize: 2 | ||
} | ||
} | ||
|
||
const ret = createPaging(data, params, sort) | ||
|
||
t.deepEqual(ret, expected) | ||
}) | ||
|
||
test('should return paging when sorting ascending and descending', (t) => { | ||
const data = prepareData([ | ||
{id: 'ent3', type: 'entry', attributes: {index: 2}}, | ||
{id: 'ent2', type: 'entry', attributes: {index: 1}} | ||
]) | ||
const params = { | ||
type: 'entry', | ||
pageSize: 2 | ||
} | ||
const sort = { | ||
'attributes.index': -1, | ||
id: 1 | ||
} | ||
const expected = { | ||
next: { | ||
type: 'entry', | ||
query: { | ||
'attributes.index': {$lte: 1}, | ||
id: {$gte: 'ent2'} | ||
}, | ||
pageAfter: 'entry:ent2', | ||
pageSize: 2 | ||
} | ||
} | ||
|
||
const ret = createPaging(data, params, sort) | ||
|
||
t.deepEqual(ret, expected) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const dotprop = require('dot-prop') | ||
|
||
const createQuery = (lastItem, sort) => { | ||
if (sort) { | ||
return Object.keys(sort).reduce((query, key) => { | ||
const value = dotprop.get(lastItem, key) | ||
const operator = (sort[key] > 0) ? '$gte' : '$lte' | ||
return {...query, [key]: {[operator]: value}} | ||
}, {}) | ||
} else { | ||
return {_id: {$gte: lastItem._id}} | ||
} | ||
} | ||
|
||
const createPaging = (data, params, sort) => { | ||
if (data.length === 0) { | ||
return {next: null} | ||
} | ||
const lastItem = data[data.length - 1] | ||
|
||
const {typePlural, ...nextParams} = params | ||
|
||
const query = createQuery(lastItem, sort) | ||
|
||
return { | ||
next: { | ||
...nextParams, | ||
pageSize: params.pageSize, | ||
pageAfter: lastItem._id, | ||
query | ||
} | ||
} | ||
} | ||
|
||
module.exports = createPaging |
Oops, something went wrong.