diff --git a/index.js b/index.js index 41e1cd3..ad2b6cc 100644 --- a/index.js +++ b/index.js @@ -57,7 +57,7 @@ function rangeParser (size, str, options) { var end = parsePos(endStr) if (startStr.length === 0) { - start = size - end + start = Math.max(size - end, 0) end = size - 1 } else if (endStr.length === 0) { end = size - 1 @@ -74,7 +74,7 @@ function rangeParser (size, str, options) { } // skip unsatisfiable ranges - if (start > end || start < 0) { + if (start > end) { valid = true continue } diff --git a/test/range-parser.js b/test/range-parser.js index 9f2c483..5147754 100644 --- a/test/range-parser.js +++ b/test/range-parser.js @@ -109,6 +109,18 @@ describe('parseRange(len, str)', function () { deepEqual(range[0], { start: 600, end: 999 }) }) + it('should be unsatisfiable asking for -0', function () { + var range = parse(1000, 'bytes=-0') + assert.strictEqual(range, -1) + }) + + it('should use the whole representation when the suffix-length is larger', function () { + var range = parse(100, 'bytes=-101') + assert.strictEqual(range.type, 'bytes') + assert.strictEqual(range.length, 1) + deepEqual(range[0], { start: 0, end: 99 }) + }) + it('should parse str with only start', function () { var range = parse(1000, 'bytes=400-') assert.strictEqual(range.type, 'bytes')