Skip to content

Commit 63e8b43

Browse files
authored
Merge pull request #60 from vslinko-forks/master
First and last arguments must be positive integers
2 parents 97ec9a6 + 5804cb4 commit 63e8b43

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/connection/__tests__/arrayconnection.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -470,20 +470,22 @@ describe('connectionFromArray()', () => {
470470
});
471471

472472
describe('cursor edge cases', () => {
473-
it('returns no elements if first is 0', () => {
474-
var c = connectionFromArray(
475-
letters,
476-
{first: 0}
477-
);
478-
return expect(c).to.deep.equal({
479-
edges: [],
480-
pageInfo: {
481-
startCursor: null,
482-
endCursor: null,
483-
hasPreviousPage: false,
484-
hasNextPage: true,
485-
}
486-
});
473+
it('throws an error if first < 0', () => {
474+
expect(() => {
475+
connectionFromArray(
476+
letters,
477+
{first: -1}
478+
);
479+
}).to.throw('Argument "first" must be a non-negative integer');
480+
});
481+
482+
it('throws an error if last < 0', () => {
483+
expect(() => {
484+
connectionFromArray(
485+
letters,
486+
{last: -1}
487+
);
488+
}).to.throw('Argument "last" must be a non-negative integer');
487489
});
488490

489491
it('returns all elements if cursors are invalid', () => {

src/connection/arrayconnection.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,20 @@ export function connectionFromArraySlice<T>(
8585
arrayLength
8686
);
8787
if (typeof first === 'number') {
88+
if (first < 0) {
89+
throw new Error('Argument "first" must be a non-negative integer');
90+
}
91+
8892
endOffset = Math.min(
8993
endOffset,
9094
startOffset + first
9195
);
9296
}
9397
if (typeof last === 'number') {
98+
if (last < 0) {
99+
throw new Error('Argument "last" must be a non-negative integer');
100+
}
101+
94102
startOffset = Math.max(
95103
startOffset,
96104
endOffset - last

0 commit comments

Comments
 (0)