Skip to content

Commit

Permalink
Implement a class Link method setUnique.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydenseric committed Nov 8, 2022
1 parent 53d4c8a commit 67bc8b5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,40 @@ link.rel( 'alternate' )
### Setting references

```js
link.set({ rel: 'next', uri: 'http://example.com/next' })
link.set({ uri: 'https://example.com/next', rel: 'next' })
> Link {
refs: [
{ uri: 'example.com', rel: 'example', title: 'Example Website' },
{ uri: 'example-01.com', rel: 'alternate', title: 'Alternate Example Domain' },
{ rel: 'next', uri: 'http://example.com/next' }
{ rel: 'next', uri: 'https://example.com/next' }
]
}
```

### 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' }
]
}
```
Expand Down
29 changes: 29 additions & 0 deletions lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ function needsQuotes( value ) {
!TOKEN_PATTERN.test( value )
}

/**
* 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 {

/**
Expand Down Expand Up @@ -101,11 +116,25 @@ class Link {

}

/** Sets a reference. */
set( link ) {
this.refs.push( link )
return this
}

/**
* 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 ) {

attr = attr.toLowerCase()
Expand Down
27 changes: 27 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,33 @@ context( 'API', function() {
})
})

test( 'setUnique()', function() {
var link = new Link()
link.setUnique( { uri: 'https://example.com/a', rel: 'preconnect' } )
assert.deepEqual(
link.refs,
[
{ uri: 'https://example.com/a', rel: 'preconnect' }
]
)
link.setUnique( { uri: 'https://example.com/b', rel: 'preconnect' } )
assert.deepEqual(
link.refs,
[
{ uri: 'https://example.com/a', rel: 'preconnect' },
{ uri: 'https://example.com/b', rel: 'preconnect' }
]
)
link.setUnique( { uri: 'https://example.com/a', rel: 'preconnect' } )
assert.deepEqual(
link.refs,
[
{ uri: 'https://example.com/a', rel: 'preconnect' },
{ uri: 'https://example.com/b', rel: 'preconnect' }
]
)
})

test( 'parse() multiple', function() {

var links = new Link()
Expand Down

0 comments on commit 67bc8b5

Please sign in to comment.