-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f52451a
Showing
6 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Range Specifier Parser | ||
|
||
A parser to handle [Range Pagination Headers](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). | ||
|
||
*Inspired by [range-parser](https://github.com/jshttp/range-parser)*. | ||
|
||
## Installation | ||
|
||
Choose your preferred method: | ||
|
||
* npm: `npm install --save range-specifier-parser` | ||
* Download: [range-specifier-parser](https://github.com/seegno/range-specifier-parser) | ||
|
||
## Usage | ||
|
||
The parser receives a `byte-ranges-specifier` as its only argument. | ||
|
||
```js | ||
var parser = require('range-specifier-parser'); | ||
|
||
parser('bytes=0-499'); | ||
|
||
``` | ||
|
||
## Output | ||
|
||
The parser outputs an object with the following properties according to the [Byte Ranges](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.1) spec: | ||
|
||
|
||
```js | ||
{ | ||
first: 0, // `first-byte-pos`. | ||
last: 499, // `last-byte-pos`. | ||
unit: 'bytes' // `bytes-unit`. | ||
} | ||
|
||
``` | ||
|
||
## Running tests | ||
|
||
```sh | ||
npm test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
/** | ||
* Export `parser`. | ||
*/ | ||
|
||
module.exports = function(range) { | ||
// Test `Range` format. | ||
if (null === range.match(/^\w+=\d+-\d+$/)) { | ||
return -2; | ||
} | ||
|
||
// Parse `range-set` from range. | ||
var rangeSet = range.replace(range.match(/^\w+=/g), '').split('-'); | ||
|
||
// Pick `first-byte-pos` and `last-byte-pos` from `range-set`. | ||
var first = parseInt(rangeSet[0], 10) | ||
var last = parseInt(rangeSet[1], 10); | ||
var unit = range.replace(range.match(/=\d+-\d+$/), ''); | ||
|
||
// `first-byte-pos` must not be greater than `last-byte-pos`. | ||
if (first > last) { | ||
return -1; | ||
} | ||
|
||
return { | ||
first: first, | ||
last: last, | ||
unit: unit | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "range-specifier-parser", | ||
"version": "0.1.0", | ||
"description": "Range Specifier Parser", | ||
"main": "index.js", | ||
"homepage": "https://github.com/seegno/range-specifier-parser", | ||
"devDependencies": { | ||
"co-mocha": "1.1.0", | ||
"mocha": "2.1.0", | ||
"should": "4.6.1" | ||
}, | ||
"scripts": { | ||
"test": "NODE_ENV=test ./node_modules/.bin/mocha test/*_test.js" | ||
}, | ||
"author": "Seegno", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "[email protected]:seegno/range-specifier-parser" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/seegno/range-specifier-parser/issues" | ||
}, | ||
"directories": { | ||
"test": "test" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--require co-mocha | ||
--require should | ||
--reporter spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var parser = require('../'); | ||
|
||
/** | ||
* Test `rangeSpecifierParser`. | ||
*/ | ||
|
||
describe('rangeSpecifierParser', function() { | ||
it('should return -2 range is missing `range-unit`', function(){ | ||
parser('=0-5').should.equal(-2); | ||
}); | ||
|
||
it('should return -2 range is missing `=`', function(){ | ||
parser('bytes0-5').should.equal(-2); | ||
}); | ||
|
||
it('should return -2 `byte-range-spec` is not totally defined', function(){ | ||
parser('bytes=-5').should.equal(-2); | ||
}); | ||
|
||
it('should return -2 `byte-range-spec` is not a numeric interval', function(){ | ||
parser('bytes=start-end').should.equal(-2); | ||
}); | ||
|
||
it('should return -1 for invalid `byte-range-spec`', function(){ | ||
parser('bytes=5-0').should.equal(-1); | ||
}); | ||
|
||
it('should parse range', function(){ | ||
var range = parser('bytes=0-499'); | ||
|
||
range.last.should.equal(499); | ||
range.first.should.equal(0); | ||
range.unit.should.equal('bytes'); | ||
}); | ||
}); |