mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 12:43:18 +08:00
Refactor code and tests
This commit is contained in:
@ -52,7 +52,7 @@ module.exports = function(source) {
|
|||||||
// Result array
|
// Result array
|
||||||
var array
|
var array
|
||||||
|
|
||||||
// Remove _start and _end from req.query to avoid filtering using those
|
// Remove _start, _end and _limit from req.query to avoid filtering using those
|
||||||
// parameters
|
// parameters
|
||||||
var _start = req.query._start
|
var _start = req.query._start
|
||||||
var _end = req.query._end
|
var _end = req.query._end
|
||||||
@ -118,13 +118,18 @@ module.exports = function(source) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Slice result
|
// Slice result
|
||||||
_start = _start || 0
|
if (_end || _limit) {
|
||||||
res.setHeader('X-Total-Count', array.length)
|
res.setHeader('X-Total-Count', array.length)
|
||||||
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
|
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
|
||||||
|
}
|
||||||
|
|
||||||
|
_start = parseInt(_start) || 0
|
||||||
|
|
||||||
if (_end) {
|
if (_end) {
|
||||||
array = array.slice(_start, _end)
|
array = array.slice(_start, parseInt(_end))
|
||||||
} else if (_limit) {
|
} else if (_limit) {
|
||||||
array = utils.limitArray(array, _start, _limit)
|
// Convert strings to int and sum to get end value
|
||||||
|
array = array.slice(_start, parseInt(_start) + parseInt(_limit))
|
||||||
}
|
}
|
||||||
|
|
||||||
res.jsonp(array)
|
res.jsonp(array)
|
||||||
|
@ -66,15 +66,8 @@ function getRemovable(db) {
|
|||||||
return removable
|
return removable
|
||||||
}
|
}
|
||||||
|
|
||||||
//Returns limited array
|
|
||||||
function limitArray(array, start, limit) {
|
|
||||||
var end = parseInt(start) + parseInt(limit)
|
|
||||||
return array.slice(start, end)
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
toNative: toNative,
|
toNative: toNative,
|
||||||
createId: createId,
|
createId: createId,
|
||||||
getRemovable: getRemovable,
|
getRemovable: getRemovable
|
||||||
limitArray: limitArray
|
|
||||||
}
|
}
|
@ -27,13 +27,7 @@ describe('Server', function() {
|
|||||||
{id: 2, published: false, postId: 1},
|
{id: 2, published: false, postId: 1},
|
||||||
{id: 3, published: false, postId: 2},
|
{id: 3, published: false, postId: 2},
|
||||||
{id: 4, published: false, postId: 2},
|
{id: 4, published: false, postId: 2},
|
||||||
{id: 5, published: false, postId: 2},
|
{id: 5, published: false, postId: 2}
|
||||||
{id: 6, published: false, postId: 2},
|
|
||||||
{id: 7, published: false, postId: 2},
|
|
||||||
{id: 8, published: false, postId: 2},
|
|
||||||
{id: 9, published: false, postId: 2},
|
|
||||||
{id: 10, published: false, postId: 2},
|
|
||||||
{id: 11, published: false, postId: 2}
|
|
||||||
]
|
]
|
||||||
|
|
||||||
db.refs = [
|
db.refs = [
|
||||||
@ -156,11 +150,11 @@ describe('Server', function() {
|
|||||||
describe('GET /:resource?_start=&_limit=', function() {
|
describe('GET /:resource?_start=&_limit=', function() {
|
||||||
it('should respond with a limited array', function(done) {
|
it('should respond with a limited array', function(done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/comments?_start=5&_limit=3')
|
.get('/comments?_start=1&_limit=1')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect('x-total-count', db.comments.length.toString())
|
.expect('x-total-count', db.comments.length.toString())
|
||||||
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
|
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
|
||||||
.expect(utils.limitArray(db.comments, 5, 3))
|
.expect(db.comments.slice(1, 2))
|
||||||
.expect(200, done)
|
.expect(200, done)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -298,7 +292,7 @@ describe('Server', function() {
|
|||||||
.end(function(err, res) {
|
.end(function(err, res) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
assert.equal(db.posts.length, 1)
|
assert.equal(db.posts.length, 1)
|
||||||
assert.equal(db.comments.length, 9)
|
assert.equal(db.comments.length, 3)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -46,28 +46,4 @@ describe('utils', function() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('limitArray', function() {
|
|
||||||
it('should return limited array', function() {
|
|
||||||
var testArray = [
|
|
||||||
{id: 2, postId: 2},
|
|
||||||
{id: 3, postId: 4},
|
|
||||||
{id: 4, postId: 6},
|
|
||||||
{id: 5, postId: 8},
|
|
||||||
{id: 6, postId: 9},
|
|
||||||
{id: 7, postId: 10},
|
|
||||||
{id: 8, postId: 11},
|
|
||||||
{id: 9, postId: 12},
|
|
||||||
{id: 10, postId: 13},
|
|
||||||
{id: 11, postId: 14},
|
|
||||||
{id: 12, postId: 15},
|
|
||||||
{id: 13, postId: 16},
|
|
||||||
{id: 14, postId: 17},
|
|
||||||
{id: 15, postId: 18},
|
|
||||||
{id: 16, postId: 19}
|
|
||||||
]
|
|
||||||
assert.deepEqual(utils.limitArray(testArray, 3, 3), testArray.slice(3, 6))
|
|
||||||
assert.deepEqual(utils.limitArray(testArray, 5, 3), testArray.slice(5, 8))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
Reference in New Issue
Block a user