format-link-header
Advanced tools
Comparing version
{ | ||
"name": "format-link-header", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Format a link header object, to the original RFC 5988", | ||
@@ -28,7 +28,8 @@ "main": "src/index.js", | ||
"devDependencies": { | ||
"chai": "^3.4.1", | ||
"eslint": "^1.10.1", | ||
"eslint-config-airbnb": "^1.0.2", | ||
"mocha": "^2.3.4" | ||
"chai": "^3.5.0", | ||
"eslint": "^3.8.1", | ||
"eslint-config-airbnb-base": "^9.0.0", | ||
"eslint-plugin-import": "^2.0.1", | ||
"mocha": "^3.1.0" | ||
} | ||
} |
@@ -16,3 +16,3 @@ [](https://travis-ci.org/jonathansamines/format-link-header) | ||
```js | ||
var link = { next: | ||
const link = { next: | ||
{ page: '3', | ||
@@ -38,3 +38,3 @@ per_page: '100', | ||
```js | ||
var formatter = require('format-link-header'); | ||
const formatter = require('format-link-header'); | ||
formatter(link); | ||
@@ -41,0 +41,0 @@ ``` |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
/** | ||
@@ -14,8 +16,8 @@ * Stringify every linkProperty attribute in the format specified in the weblinks specs | ||
.map(function stringifyAttribute(linkAttributeName, index, array) { | ||
var linkAttribute = linkProperty[linkAttributeName]; | ||
var isLastElement = index === array.length - 1; | ||
const linkAttribute = linkProperty[linkAttributeName]; | ||
const isLastElement = index === array.length - 1; | ||
return linkAttributeName + '="' + linkAttribute + (isLastElement ? '"' : '";'); | ||
return `${linkAttributeName}="${linkAttribute}${isLastElement ? '"' : '";'}`; | ||
}) | ||
.join(' '); | ||
}; |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
/** | ||
@@ -8,18 +10,16 @@ * Get an array with unique link object rel attributes | ||
module.exports = function groupRelAttributes(linkObject) { | ||
var linkPropertyName; | ||
var linkProperty; | ||
var uniqueProperties = {}; | ||
const uniqueProperties = {}; | ||
for (linkPropertyName in linkObject) { | ||
if (!linkObject.hasOwnProperty(linkPropertyName)) continue; | ||
Object | ||
.keys(linkObject) | ||
.forEach(function evalGroup(linkPropertyName) { | ||
const linkProperty = linkObject[linkPropertyName]; | ||
linkProperty = linkObject[linkPropertyName]; | ||
if (uniqueProperties[linkProperty.url] === undefined) { | ||
uniqueProperties[linkProperty.url] = linkProperty; | ||
} else { | ||
uniqueProperties[linkProperty.url].rel += ` ${linkProperty.rel}`; | ||
} | ||
}); | ||
if (uniqueProperties[linkProperty.url] === undefined) { | ||
uniqueProperties[linkProperty.url] = linkProperty; | ||
} else { | ||
uniqueProperties[linkProperty.url].rel += ' ' + linkProperty.rel; | ||
} | ||
} | ||
return Object | ||
@@ -26,0 +26,0 @@ .keys(uniqueProperties) |
@@ -1,5 +0,7 @@ | ||
var groupRelAttributes = require('./group-link-rels'); | ||
var transformLinkProperty = require('./transform-link-property'); | ||
var formatLinkAttributes = require('./format-link-attributes'); | ||
'use strict'; | ||
const groupRelAttributes = require('./group-link-rels'); | ||
const transformLinkProperty = require('./transform-link-property'); | ||
const formatLinkAttributes = require('./format-link-attributes'); | ||
/** | ||
@@ -15,8 +17,8 @@ * Format a link properties Object, to the format described in RFC 5988 | ||
.map(function formatProperties(linkProperty) { | ||
var linkTransformation = transformLinkProperty(linkProperty); | ||
var linkAttributes = formatLinkAttributes(linkTransformation); | ||
const linkTransformation = transformLinkProperty(linkProperty); | ||
const linkAttributes = formatLinkAttributes(linkTransformation); | ||
return '<' + linkTransformation.url + '>; ' + linkAttributes; | ||
return `<${linkTransformation.url}>; ${linkAttributes}`; | ||
}) | ||
.join(', '); | ||
}; |
@@ -1,3 +0,5 @@ | ||
var url = require('url'); | ||
'use strict'; | ||
const url = require('url'); | ||
/** | ||
@@ -8,4 +10,4 @@ * Transform the current link header object, to an object easy to transform to the link format | ||
module.exports = function transformLinkProperty(linkProperty) { | ||
var queryParams = url.parse(linkProperty.url, true).query; | ||
var transformation = {}; | ||
const queryParams = url.parse(linkProperty.url, true).query; | ||
const transformation = {}; | ||
@@ -15,3 +17,3 @@ Object | ||
.forEach(function mapReferenceProperties(navProperty) { | ||
var navPropertyValue = linkProperty[navProperty]; | ||
const navPropertyValue = linkProperty[navProperty]; | ||
@@ -18,0 +20,0 @@ // if it´s not a querystring param, then generate an attribute for it |
@@ -1,5 +0,7 @@ | ||
var formatLinkAttributes = require('../src/format-link-attributes'); | ||
var expect = require('chai').expect; | ||
'use strict'; | ||
var linkAttributes = { | ||
const formatLinkAttributes = require('../src/format-link-attributes'); | ||
const expect = require('chai').expect; | ||
const linkAttributes = { | ||
rel: 'last', | ||
@@ -11,7 +13,7 @@ hreflang: 'es', | ||
describe('+ format-link-attributes', function() { | ||
describe('#call', function() { | ||
var attributes = formatLinkAttributes(linkAttributes); | ||
describe('+ format-link-attributes', function () { | ||
describe('#call', function () { | ||
const attributes = formatLinkAttributes(linkAttributes); | ||
it('it should stringify the link attributes as a list of key="value" separated by semicolons, excluding the url attribute', function() { | ||
it('it should stringify the link attributes as a list of key="value" separated by semicolons, excluding the url attribute', function () { | ||
expect(attributes).to.be.equals('rel="last"; hreflang="es"; content="boom"'); | ||
@@ -18,0 +20,0 @@ }); |
@@ -1,5 +0,7 @@ | ||
var groupRelAttributes = require('../src/group-link-rels'); | ||
var expect = require('chai').expect; | ||
'use strict'; | ||
var expectedProperties = [ | ||
const groupRelAttributes = require('../src/group-link-rels'); | ||
const expect = require('chai').expect; | ||
const expectedProperties = [ | ||
{ | ||
@@ -23,3 +25,3 @@ client_id: '1', | ||
var linkObject = { | ||
const linkObject = { | ||
current: { | ||
@@ -51,11 +53,11 @@ client_id: '1', | ||
describe('+ group-link-rels', function() { | ||
describe('#call', function() { | ||
var uniqueProperties = groupRelAttributes(linkObject); | ||
describe('+ group-link-rels', function () { | ||
describe('#call', function () { | ||
const uniqueProperties = groupRelAttributes(linkObject); | ||
it('should return an array of unique properties', function() { | ||
it('should return an array of unique properties', function () { | ||
expect(uniqueProperties).to.be.an.array; | ||
}); | ||
it('should group related rel attributes toguether by dropping duplicated entries on the link object', function() { | ||
it('should group related rel attributes toguether by dropping duplicated entries on the link object', function () { | ||
expect(uniqueProperties).to.be.deep.equals(expectedProperties); | ||
@@ -62,0 +64,0 @@ }); |
@@ -1,12 +0,14 @@ | ||
var formatter = require('../src/index'); | ||
var expect = require('chai').expect; | ||
'use strict'; | ||
describe('+ link-formatter', function() { | ||
describe('#call', function() { | ||
it('should format a proper link header with rel attributes', function() { | ||
var linkHeader = | ||
const formatter = require('../src/index'); | ||
const expect = require('chai').expect; | ||
describe('+ link-formatter', function () { | ||
describe('#call', function () { | ||
it('should format a proper link header with rel attributes', function () { | ||
const linkHeader = | ||
'<https://api.github.com/user/9287/repos?client_id=1&client_secret=2&page=2&per_page=100>; rel="next", ' + | ||
'<https://api.github.com/user/9287/repos?client_id=1&client_secret=2&page=3&per_page=100>; rel="last"'; | ||
var linkObject = { | ||
const linkObject = { | ||
next: { | ||
@@ -33,3 +35,3 @@ client_id: '1', | ||
it('should return a blank string if the link object is nullable', function() { | ||
it('should return a blank string if the link object is nullable', function () { | ||
expect(formatter(null)).to.be.equal(''); | ||
@@ -39,5 +41,5 @@ expect(formatter(undefined)).to.be.equal(''); | ||
it('should group together properties with related rel attribute values', function() { | ||
var linkHeader = '<https://imaginary.url.notreal?name=value>; rel="next page"; hreflang="es"'; | ||
var linkObject = { | ||
it('should group together properties with related rel attribute values', function () { | ||
const linkHeader = '<https://imaginary.url.notreal?name=value>; rel="next page"; hreflang="es"'; | ||
const linkObject = { | ||
next: { | ||
@@ -44,0 +46,0 @@ rel: 'next', |
@@ -1,5 +0,7 @@ | ||
var transformProperty = require('../src/transform-link-property'); | ||
var expect = require('chai').expect; | ||
'use strict'; | ||
var expectedTransformation = { | ||
const transformProperty = require('../src/transform-link-property'); | ||
const expect = require('chai').expect; | ||
const expectedTransformation = { | ||
rel: 'last', | ||
@@ -10,3 +12,3 @@ hreflang: 'es', | ||
var linkProperty = { | ||
const linkProperty = { | ||
client_id: '1', | ||
@@ -21,7 +23,7 @@ client_secret: '2', | ||
describe('+ transform-link-property', function() { | ||
describe('#call', function() { | ||
var transformedLink = transformProperty(linkProperty); | ||
describe('+ transform-link-property', function () { | ||
describe('#call', function () { | ||
const transformedLink = transformProperty(linkProperty); | ||
it('should return an object with only the link attributes, excluding the attributes generated by queryParams', function() { | ||
it('should return an object with only the link attributes, excluding the attributes generated by queryParams', function () { | ||
expect(transformedLink).to.be.an.object; | ||
@@ -28,0 +30,0 @@ expect(transformedLink).to.be.deep.equal(expectedTransformation); |
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
12297
4.77%15
7.14%258
11.69%5
25%