Skip to content

Commit 5d642af

Browse files
committed
Add "options" argument to req.range
1 parent 7fcf1d7 commit 5d642af

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

History.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
unreleased
22
==========
33

4+
* Add `options` argument to `req.range`
5+
- Includes the `combine` option
46
* Fix Windows absolute path check using forward slashes
57
* Improve performance for `res.json`/`res.jsonp` in most cases
68
* deps: accepts@~1.3.3

lib/request.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,23 @@ req.acceptsLanguage = deprecate.function(req.acceptsLanguages,
182182
* range that is required (most commonly, "bytes"). Each array element is an object
183183
* with a "start" and "end" property for the portion of the range.
184184
*
185+
* The "combine" option can be set to `true` and overlapping & adjacent ranges
186+
* will be combined into a single range.
187+
*
185188
* NOTE: remember that ranges are inclusive, so for example "Range: users=0-3"
186189
* should respond with 4 users when available, not 3.
187190
*
188191
* @param {number} size
192+
* @param {object} [options]
193+
* @param {boolean} [options.combine=false]
189194
* @return {number|array}
190195
* @public
191196
*/
192197

193-
req.range = function range(size) {
198+
req.range = function range(size, options) {
194199
var range = this.get('Range');
195200
if (!range) return;
196-
return parseRange(size, range);
201+
return parseRange(size, range, options);
197202
};
198203

199204
/**

test/req.range.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ function req(ret) {
1212
describe('req', function(){
1313
describe('.range(size)', function(){
1414
it('should return parsed ranges', function(){
15-
var ret = [{ start: 0, end: 50 }, { start: 60, end: 100 }];
16-
ret.type = 'bytes';
17-
req('bytes=0-50,60-100').range(120).should.eql(ret);
15+
var ranges = [{ start: 0, end: 50 }, { start: 51, end: 100 }]
16+
ranges.type = 'bytes'
17+
assert.deepEqual(req('bytes=0-50,51-100').range(120), ranges)
1818
})
1919

2020
it('should cap to the given size', function(){
@@ -35,4 +35,14 @@ describe('req', function(){
3535
assert(req('').range(120) === undefined);
3636
})
3737
})
38+
39+
describe('.range(size, options)', function(){
40+
describe('with "combine: true" option', function(){
41+
it('should return combined ranges', function(){
42+
var ranges = [{ start: 0, end: 100 }]
43+
ranges.type = 'bytes'
44+
assert.deepEqual(req('bytes=0-50,51-100').range(120, { combine: true }), ranges)
45+
})
46+
})
47+
})
3848
})

0 commit comments

Comments
 (0)