Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Cardoso authored and fixe committed Jan 28, 2015
0 parents commit f52451a
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
43 changes: 43 additions & 0 deletions README.md
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
```
30 changes: 30 additions & 0 deletions index.js
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
};
};
27 changes: 27 additions & 0 deletions package.json
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"
}
}
3 changes: 3 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--require co-mocha
--require should
--reporter spec
40 changes: 40 additions & 0 deletions test/range-specifier-parser_test.js
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');
});
});

0 comments on commit f52451a

Please sign in to comment.