Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

changed: ret -1 for unsatisfiable and -2 when invalid

  • Loading branch information...
commit b4e2023fa31fabbec1280cdf53babd9b0218c048 1 parent 1b7e34b
@tj tj authored
Showing with 18 additions and 10 deletions.
  1. +13 −8 Readme.md
  2. +3 −1 index.js
  3. +2 −1  test/range-parser.js
View
21 Readme.md
@@ -6,14 +6,19 @@
## Example:
```js
-parse(1000, 'bytes=0-499').should.eql([{ start: 0, end: 499 }]);
-parse(1000, 'bytes=40-80').should.eql([{ start: 40, end: 80 }]);
-parse(1000, 'bytes=-500').should.eql([{ start: 500, end: 999 }]);
-parse(1000, 'bytes=-400').should.eql([{ start: 600, end: 999 }]);
-parse(1000, 'bytes=500-').should.eql([{ start: 500, end: 999 }]);
-parse(1000, 'bytes=400-').should.eql([{ start: 400, end: 999 }]);
-parse(1000, 'bytes=0-0').should.eql([{ start: 0, end: 0 }]);
-parse(1000, 'bytes=-1').should.eql([{ start: 999, end: 999 }]);
+assert(-1 == parse(200, 'bytes=500-20'));
+assert(-2 == parse(200, 'bytes=malformed'));
+parse(200, 'bytes=0-499').should.eql(arr('bytes', [{ start: 0, end: 199 }]));
+parse(1000, 'bytes=0-499').should.eql(arr('bytes', [{ start: 0, end: 499 }]));
+parse(1000, 'bytes=40-80').should.eql(arr('bytes', [{ start: 40, end: 80 }]));
+parse(1000, 'bytes=-500').should.eql(arr('bytes', [{ start: 500, end: 999 }]));
+parse(1000, 'bytes=-400').should.eql(arr('bytes', [{ start: 600, end: 999 }]));
+parse(1000, 'bytes=500-').should.eql(arr('bytes', [{ start: 500, end: 999 }]));
+parse(1000, 'bytes=400-').should.eql(arr('bytes', [{ start: 400, end: 999 }]));
+parse(1000, 'bytes=0-0').should.eql(arr('bytes', [{ start: 0, end: 0 }]));
+parse(1000, 'bytes=-1').should.eql(arr('bytes', [{ start: 999, end: 999 }]));
+parse(1000, 'items=0-5').should.eql(arr('items', [{ start: 0, end: 5 }]));
+parse(1000, 'bytes=40-80,-1').should.eql(arr('bytes', [{ start: 40, end: 80 }, { start: 999, end: 999 }]));
```
## Installation
View
4 index.js
@@ -12,6 +12,8 @@ module.exports = function(size, str){
var valid = true;
var i = str.indexOf('=');
+ if (-1 == i) return -2;
+
var arr = str.slice(i + 1).split(',').map(function(range){
var range = range.split('-')
, start = parseInt(range[0], 10)
@@ -43,5 +45,5 @@ module.exports = function(size, str){
arr.type = str.slice(0, i);
- return valid ? arr : null;
+ return valid ? arr : -1;
};
View
3  test/range-parser.js
@@ -9,7 +9,8 @@ function arr(type, arr) {
describe('parseRange(len, str)', function(){
it('should parse range strings', function(){
- assert(null == parse(200, 'bytes=500-20'));
+ assert(-1 == parse(200, 'bytes=500-20'));
+ assert(-2 == parse(200, 'malformed'));
parse(200, 'bytes=0-499').should.eql(arr('bytes', [{ start: 0, end: 199 }]));
parse(1000, 'bytes=0-499').should.eql(arr('bytes', [{ start: 0, end: 499 }]));
parse(1000, 'bytes=40-80').should.eql(arr('bytes', [{ start: 40, end: 80 }]));
Please sign in to comment.
Something went wrong with that request. Please try again.