Skip to content

Commit

Permalink
arrayConnection: return all elements if cursors are on the outside
Browse files Browse the repository at this point in the history
Fixes #51
  • Loading branch information
IvanGoncharov committed Jun 28, 2021
1 parent c5670c6 commit e4f4516
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
32 changes: 19 additions & 13 deletions src/connection/__tests__/arrayConnection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, it } from 'mocha';
import { expect } from 'chai';

import {
offsetToCursor,
connectionFromArray,
connectionFromArraySlice,
connectionFromPromisedArray,
Expand Down Expand Up @@ -289,19 +290,24 @@ describe('connectionFromArray()', () => {
});

it('returns all elements if cursors are on the outside', () => {
const c = connectionFromArray(arrayABCDE, {
before: 'YXJyYXljb25uZWN0aW9uOjY=' /* offsetToCursor(6) */,
after: 'YXJyYXljb25uZWN0aW9uOi0x' /* offsetToCursor(-1) */,
});
expect(c).to.deep.equal({
edges: [edgeA, edgeB, edgeC, edgeD, edgeE],
pageInfo: {
startCursor: cursorA,
endCursor: cursorE,
hasPreviousPage: false,
hasNextPage: false,
},
});
function expectAllEdges(args: { before?: string, after?: string }) {
const c = connectionFromArray(arrayABCDE, args);
expect(c).to.deep.equal({
edges: [edgeA, edgeB, edgeC, edgeD, edgeE],
pageInfo: {
startCursor: cursorA,
endCursor: cursorE,
hasPreviousPage: false,
hasNextPage: false,
},
});
}

expectAllEdges({ before: offsetToCursor(6) });
expectAllEdges({ before: offsetToCursor(-1) });

expectAllEdges({ after: offsetToCursor(-1) });
expectAllEdges({ after: offsetToCursor(6) });
});

it('returns no elements if cursors cross', () => {
Expand Down
20 changes: 15 additions & 5 deletions src/connection/arrayConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,21 @@ export function connectionFromArraySlice<T>(
const { after, before, first, last } = args;
const { sliceStart, arrayLength } = meta;
const sliceEnd = sliceStart + arraySlice.length;
const beforeOffset = getOffsetWithDefault(before, arrayLength);

let startOffset = Math.max(sliceStart, 0);
let endOffset = Math.min(sliceEnd, arrayLength);
console.log(startOffset, endOffset);

const afterOffset = getOffsetWithDefault(after, -1);
if (0 <= afterOffset && afterOffset < arrayLength) {
startOffset = Math.max(startOffset, afterOffset + 1);
}

const beforeOffset = getOffsetWithDefault(before, endOffset);
if (0 <= beforeOffset && beforeOffset < arrayLength) {
endOffset = Math.min(endOffset, beforeOffset);
}

let startOffset = Math.max(sliceStart - 1, afterOffset, -1) + 1;
let endOffset = Math.min(sliceEnd, beforeOffset, arrayLength);
if (typeof first === 'number') {
if (first < 0) {
throw new Error('Argument "first" must be a non-negative integer');
Expand All @@ -76,8 +86,8 @@ export function connectionFromArraySlice<T>(

// If supplied slice is too large, trim it down before mapping over it.
const slice = arraySlice.slice(
Math.max(startOffset - sliceStart, 0),
arraySlice.length - (sliceEnd - endOffset),
startOffset - sliceStart,
endOffset - sliceStart,
);

const edges = slice.map((value, index) => ({
Expand Down

0 comments on commit e4f4516

Please sign in to comment.