diff --git a/AUTHORS b/AUTHORS index 145279e..634deb2 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,3 +1,4 @@ Jonas Hermsmeier +Dale Hui Matteo Brancaleoni Yamagishi Kazutoshi diff --git a/CHANGELOG.md b/CHANGELOG.md index b9238a5..93cd208 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## v1.0.2 + +- Make `.get()` have a consistent return type ([@dhui]) +- Improve `.has()` best-case performance ([@dhui]) +- Fix `.has()` test and add a test for the negative case ([@dhui]) + +## v1.0.1 + +- Fix `link.has()` always returning `true` + ## v1.0.0 **Breaking Changes:** @@ -14,4 +24,7 @@ - Fixed handling of backslash-escaped characters - Fixed missing cardinality handling for `type`, `media` and `title` attributes - Fixed handling of leading / trailing whitespace around delimiters -- Replace `querystring.*()` with `xxcodeURIComponent()` ([@ykzts](https://github.com/ykzts)) +- Replace `querystring.*()` with `xxcodeURIComponent()` ([@ykzts]) + +[@ykzts]: https://github.com/ykzts +[@dhui]: https://github.com/dhui diff --git a/lib/link.js b/lib/link.js index f46fc2b..bb5ef86 100644 --- a/lib/link.js +++ b/lib/link.js @@ -74,7 +74,7 @@ class Link { * Get refs where given attribute has a given value * @param {String} attr * @param {String} value - * @returns {Array|null} + * @returns {Array} */ get( attr, value ) { @@ -88,7 +88,7 @@ class Link { } } - return links.length ? links : null + return links } @@ -98,7 +98,17 @@ class Link { } has( attr, value ) { - return this.get( attr, value ) != null + + attr = attr.toLowerCase() + + for( var i = 0; i < this.refs.length; i++ ) { + if( this.refs[ i ][ attr ] === value ) { + return true + } + } + + return false + } parse( value, offset ) { diff --git a/package.json b/package.json index a926bf3..c26e76d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "http-link-header", - "version": "1.0.1", + "version": "1.0.2", "description": "Parse & format HTTP link headers according to RFC 8288", "author": "Jonas Hermsmeier (https://jhermsmeier.de)", "license": "MIT", diff --git a/test/api.js b/test/api.js index 651ed7c..318e02f 100644 --- a/test/api.js +++ b/test/api.js @@ -25,7 +25,13 @@ suite( 'API', function() { test( 'has("rel", "next")', function() { var link = Link.parse( ';rel="next", ;rel="terms-of-service"' ) // console.log( inspect( link.has( 'rel', 'next' ) ) ) - assert.deepEqual( link.has( 'next' ), true ) + assert.deepEqual( link.has( 'rel', 'next' ), true ) + }) + + test( 'has("rel", "prev")', function() { + var link = Link.parse( ';rel="next", ;rel="terms-of-service"' ) + // console.log( inspect( link.has( 'rel', 'prev' ) ) ) + assert.deepEqual( link.has( 'rel', 'prev' ), false ) }) test( 'toString()', function() {