Skip to content

Commit 0ff4e62

Browse files
committed
Refactor code and tests
1 parent d4f4c33 commit 0ff4e62

File tree

4 files changed

+16
-48
lines changed

4 files changed

+16
-48
lines changed

src/router.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module.exports = function(source) {
5252
// Result array
5353
var array
5454

55-
// Remove _start and _end from req.query to avoid filtering using those
55+
// Remove _start, _end and _limit from req.query to avoid filtering using those
5656
// parameters
5757
var _start = req.query._start
5858
var _end = req.query._end
@@ -118,13 +118,18 @@ module.exports = function(source) {
118118
}
119119

120120
// Slice result
121-
_start = _start || 0
122-
res.setHeader('X-Total-Count', array.length)
123-
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
121+
if (_end || _limit) {
122+
res.setHeader('X-Total-Count', array.length)
123+
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
124+
}
125+
126+
_start = parseInt(_start) || 0
127+
124128
if (_end) {
125-
array = array.slice(_start, _end)
129+
array = array.slice(_start, parseInt(_end))
126130
} else if (_limit) {
127-
array = utils.limitArray(array, _start, _limit)
131+
// Convert strings to int and sum to get end value
132+
array = array.slice(_start, parseInt(_start) + parseInt(_limit))
128133
}
129134

130135
res.jsonp(array)

src/utils.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,8 @@ function getRemovable(db) {
6666
return removable
6767
}
6868

69-
//Returns limited array
70-
function limitArray(array, start, limit) {
71-
var end = parseInt(start) + parseInt(limit)
72-
return array.slice(start, end)
73-
}
74-
7569
module.exports = {
7670
toNative: toNative,
7771
createId: createId,
78-
getRemovable: getRemovable,
79-
limitArray: limitArray
72+
getRemovable: getRemovable
8073
}

test/index.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ describe('Server', function() {
2727
{id: 2, published: false, postId: 1},
2828
{id: 3, published: false, postId: 2},
2929
{id: 4, published: false, postId: 2},
30-
{id: 5, published: false, postId: 2},
31-
{id: 6, published: false, postId: 2},
32-
{id: 7, published: false, postId: 2},
33-
{id: 8, published: false, postId: 2},
34-
{id: 9, published: false, postId: 2},
35-
{id: 10, published: false, postId: 2},
36-
{id: 11, published: false, postId: 2}
30+
{id: 5, published: false, postId: 2}
3731
]
3832

3933
db.refs = [
@@ -156,11 +150,11 @@ describe('Server', function() {
156150
describe('GET /:resource?_start=&_limit=', function() {
157151
it('should respond with a limited array', function(done) {
158152
request(server)
159-
.get('/comments?_start=5&_limit=3')
153+
.get('/comments?_start=1&_limit=1')
160154
.expect('Content-Type', /json/)
161155
.expect('x-total-count', db.comments.length.toString())
162156
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
163-
.expect(utils.limitArray(db.comments, 5, 3))
157+
.expect(db.comments.slice(1, 2))
164158
.expect(200, done)
165159
})
166160
})
@@ -298,7 +292,7 @@ describe('Server', function() {
298292
.end(function(err, res) {
299293
if (err) return done(err)
300294
assert.equal(db.posts.length, 1)
301-
assert.equal(db.comments.length, 9)
295+
assert.equal(db.comments.length, 3)
302296
done()
303297
})
304298
})

test/utils.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,4 @@ describe('utils', function() {
4646
})
4747

4848
})
49-
50-
describe('limitArray', function() {
51-
it('should return limited array', function() {
52-
var testArray = [
53-
{id: 2, postId: 2},
54-
{id: 3, postId: 4},
55-
{id: 4, postId: 6},
56-
{id: 5, postId: 8},
57-
{id: 6, postId: 9},
58-
{id: 7, postId: 10},
59-
{id: 8, postId: 11},
60-
{id: 9, postId: 12},
61-
{id: 10, postId: 13},
62-
{id: 11, postId: 14},
63-
{id: 12, postId: 15},
64-
{id: 13, postId: 16},
65-
{id: 14, postId: 17},
66-
{id: 15, postId: 18},
67-
{id: 16, postId: 19}
68-
]
69-
assert.deepEqual(utils.limitArray(testArray, 3, 3), testArray.slice(3, 6))
70-
assert.deepEqual(utils.limitArray(testArray, 5, 3), testArray.slice(5, 8))
71-
})
72-
})
7349
})

0 commit comments

Comments
 (0)