range-parser
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,6 @@ | ||
1.2.0 / 2016-06-01 | ||
================== | ||
* Add `combine` option to combine overlapping ranges | ||
1.1.0 / 2016-05-13 | ||
@@ -2,0 +7,0 @@ ================== |
90
index.js
/*! | ||
* range-parser | ||
* Copyright(c) 2012-2014 TJ Holowaychuk | ||
* Copyright(c) 2015-2016 Douglas Christopher Wilson | ||
* MIT Licensed | ||
@@ -21,2 +22,3 @@ */ | ||
* @param {String} str | ||
* @param {Object} [options] | ||
* @return {Array} | ||
@@ -26,3 +28,3 @@ * @public | ||
function rangeParser (size, str) { | ||
function rangeParser (size, str, options) { | ||
var index = str.indexOf('=') | ||
@@ -73,3 +75,87 @@ | ||
return ranges.length ? ranges : -1 | ||
if (ranges.length < 1) { | ||
// unsatisifiable | ||
return -1 | ||
} | ||
return options && options.combine | ||
? combineRanges(ranges) | ||
: ranges | ||
} | ||
/** | ||
* Combine overlapping & adjacent ranges. | ||
* @private | ||
*/ | ||
function combineRanges (ranges) { | ||
var ordered = ranges.map(mapWithIndex).sort(sortByRangeStart) | ||
for (var j = 0, i = 1; i < ordered.length; i++) { | ||
var range = ordered[i] | ||
var current = ordered[j] | ||
if (range.start > current.end + 1) { | ||
// next range | ||
ordered[++j] = range | ||
} else if (range.end > current.end) { | ||
// extend range | ||
current.end = range.end | ||
current.index = Math.min(current.index, range.index) | ||
} | ||
} | ||
// trim ordered array | ||
ordered.length = j + 1 | ||
// generate combined range | ||
var combined = ordered.sort(sortByRangeIndex).map(mapWithoutIndex) | ||
// copy ranges type | ||
combined.type = ranges.type | ||
return combined | ||
} | ||
/** | ||
* Map function to add index value to ranges. | ||
* @private | ||
*/ | ||
function mapWithIndex (range, index) { | ||
return { | ||
start: range.start, | ||
end: range.end, | ||
index: index | ||
} | ||
} | ||
/** | ||
* Map function to remove index value from ranges. | ||
* @private | ||
*/ | ||
function mapWithoutIndex (range) { | ||
return { | ||
start: range.start, | ||
end: range.end | ||
} | ||
} | ||
/** | ||
* Sort function to sort ranges by index. | ||
* @private | ||
*/ | ||
function sortByRangeIndex (a, b) { | ||
return a.index - b.index | ||
} | ||
/** | ||
* Sort function to sort ranges by start position. | ||
* @private | ||
*/ | ||
function sortByRangeStart (a, b) { | ||
return a.start - b.start | ||
} |
@@ -5,5 +5,6 @@ { | ||
"description": "Range header field string parser", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"contributors": [ | ||
"Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
"James Wyatt Cready <wyatt.cready@lanetix.com>", | ||
"Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)" | ||
@@ -19,3 +20,3 @@ ], | ||
"devDependencies": { | ||
"eslint": "2.9.0", | ||
"eslint": "2.11.1", | ||
"eslint-config-standard": "5.3.1", | ||
@@ -22,0 +23,0 @@ "eslint-plugin-promise": "1.1.0", |
@@ -23,3 +23,3 @@ # range-parser | ||
### parseRange(size, header) | ||
### parseRange(size, header, options) | ||
@@ -34,3 +34,3 @@ Parse the given `header` string where `size` is the maximum size of the resource. | ||
// parse header from request | ||
var range = parseRange(req.headers.range) | ||
var range = parseRange(size, req.headers.range) | ||
@@ -46,2 +46,20 @@ // the type of the range | ||
#### Options | ||
These properties are accepted in the options object. | ||
##### combine | ||
Specifies if overlapping & adjacent ranges should be combined, defaults to `false`. | ||
When `true`, ranges will be combined and returned as if they were specified that | ||
way in the header. | ||
```js | ||
parseRange(100, 'bytes=50-55,0-10,5-10,56-60', { combine: true }) | ||
// => [ | ||
// { start: 0, end: 10 }, | ||
// { start: 50, end: 60 } | ||
// ] | ||
``` | ||
## License | ||
@@ -48,0 +66,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7906
127
76