Skip to content
This repository has been archived by the owner on Aug 26, 2020. It is now read-only.

Commit

Permalink
Build link helper (#69)
Browse files Browse the repository at this point in the history
* initial buildLink helper
 🐿 v2.9.0

* add unit tests
 🐿 v2.9.0

* bump circle testing to node 8
 🐿 v2.9.0

* add default param
 🐿 v2.9.0
  • Loading branch information
alexnaish authored Jul 3, 2018
1 parent 05a8c39 commit 0a1c921
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
machine:
node:
version: 6.11.0
version: 8.11.3

deployment:
release:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"handlebars": "^4.0.0"
},
"devDependencies": {
"@financial-times/n-gage": "^1.19.4",
"@financial-times/n-gage": "^1.19.14",
"chai": "^3.3.0",
"eslint": "^3.2.2",
"express": "^4.12.3",
Expand Down
1 change: 1 addition & 0 deletions src/extend-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = function (helpers) {
helpers.debug = require('./helpers/debug');
helpers.array = require('./helpers/array');
helpers.inline = require('./helpers/inline');
helpers.buildLink = require('./helpers/buildLink');
helpers.nImagePresenter = require('@financial-times/n-image/src/handlebars-helpers/nImagePresenter');
helpers.concept = require('@financial-times/n-topic-card/handlebars-helpers/concept');

Expand Down
10 changes: 10 additions & 0 deletions src/helpers/buildLink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { URL } = require('url');

module.exports = function (url, queryParams = {}) {
if (!url) return '';
const urlObject = new URL(url);
Object.keys(queryParams).forEach(key => {
urlObject.searchParams.set(key, queryParams[key]);
});
return urlObject.href;
};
28 changes: 28 additions & 0 deletions test/buildLink.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { expect } = require('chai');
const buildLink = require('../src/helpers/buildLink');

describe('Build Link Helper', () => {

const url = 'https://ft.com/somePath';
const queryParams = {
cpccampaign: 'test',
something: 'else'
};

it('should correctly generate links', () => {
expect(buildLink(url, queryParams)).to.equal('https://ft.com/somePath?cpccampaign=test&something=else');
});

it('should return an empty string if no URL specified', () => {
expect(buildLink()).to.equal('');
});

it('should not break if no query params are passed', () => {
expect(buildLink(url)).to.equal(url);
});

it('should not duplicate query params', () => {
const urlWithParam = `${url}?something=new`;
expect(buildLink(urlWithParam, queryParams)).to.equal('https://ft.com/somePath?something=else&cpccampaign=test');
});
});

0 comments on commit 0a1c921

Please sign in to comment.