http-link-header
Advanced tools
Comparing version 1.0.5 to 1.1.0
@@ -43,2 +43,17 @@ 'use strict' | ||
/** | ||
* Shallow compares two objects to check if their properties match. | ||
* @param {object} object1 First object to compare. | ||
* @param {object} object2 Second object to compare. | ||
* @returns {boolean} Do the objects have matching properties. | ||
*/ | ||
function shallowCompareObjects( object1, object2 ) { | ||
return ( | ||
Object.keys( object1 ).length === Object.keys( object2 ).length && | ||
Object.keys( object1 ).every( | ||
( key ) => key in object2 && object1[ key ] === object2[ key ] | ||
) | ||
); | ||
} | ||
class Link { | ||
@@ -105,2 +120,3 @@ | ||
/** Sets a reference. */ | ||
set( link ) { | ||
@@ -111,2 +127,15 @@ this.refs.push( link ) | ||
/** | ||
* Sets a reference if a reference with similar properties isn’t already set. | ||
*/ | ||
setUnique( link ) { | ||
if( !this.refs.some(( ref ) => shallowCompareObjects( ref, link )) ) { | ||
this.refs.push( link ) | ||
} | ||
return this | ||
} | ||
has( attr, value ) { | ||
@@ -113,0 +142,0 @@ |
{ | ||
"name": "http-link-header", | ||
"version": "1.0.5", | ||
"version": "1.1.0", | ||
"description": "Parse & format HTTP link headers according to RFC 8288", | ||
@@ -5,0 +5,0 @@ "author": "Jonas Hermsmeier <jhermsmeier@gmail.com> (https://jhermsmeier.de)", |
@@ -74,3 +74,3 @@ # HTTP Link Header | ||
```js | ||
link.set({ rel: 'next', uri: 'http://example.com/next' }) | ||
link.set({ uri: 'https://example.com/next', rel: 'next' }) | ||
> Link { | ||
@@ -80,3 +80,3 @@ refs: [ | ||
{ uri: 'example-01.com', rel: 'alternate', title: 'Alternate Example Domain' }, | ||
{ rel: 'next', uri: 'http://example.com/next' } | ||
{ rel: 'next', uri: 'https://example.com/next' } | ||
] | ||
@@ -86,2 +86,30 @@ } | ||
### Setting a unique reference | ||
```js | ||
link.setUnique({ | ||
uri: 'https://example.com/image.png', | ||
rel: 'preload', | ||
as: 'image', | ||
type: 'image/png' | ||
}) | ||
> Link { | ||
refs: [ | ||
{ uri: 'https://example.com/image.png', rel: 'preload', as: 'image', type: 'image/png' } | ||
] | ||
} | ||
link.setUnique({ | ||
uri: 'https://example.com/image.png', | ||
rel: 'preload', | ||
as: 'image', | ||
type: 'image/png' | ||
}) | ||
> Link { | ||
refs: [ | ||
{ uri: 'https://example.com/image.png', rel: 'preload', as: 'image', type: 'image/png' } | ||
] | ||
} | ||
``` | ||
### Parsing multiple headers | ||
@@ -88,0 +116,0 @@ |
Sorry, the diff of this file is not supported yet
16808
337
176