Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #51 from nhsuk/bug/trim-location
Browse files Browse the repository at this point in the history
✂️ trim leading and trailing space on location
  • Loading branch information
st3v3nhunt authored Apr 18, 2018
2 parents a01a62e + c3cb59a commit 9c186d8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Do not use name of place in map link
- Display address when there is no postcode
- Add formatted opening times for pharmacies
- Trim leading and trailing spaces on postcode

0.5.0 / 2018-04-11
==================
Expand Down
5 changes: 5 additions & 0 deletions app/lib/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ function getResultsInternalUrl(query, location) {
return `${constants.siteRoot}/results?location=${location}&type=${getAlternativeTypeFor(query.type)}&origin=${query.origin}`;
}

function trim(string) {
return string && string.trim();
}

module.exports = {
areEqual,
getResultsInternalUrl,
isProfessionalChoice,
joinTruthyValues,
trim,
};
3 changes: 2 additions & 1 deletion app/middleware/locals.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const queryMapper = require('../lib/utils/queryMapper');
const trim = require('../lib/utils/utils').trim;

module.exports = config =>
(req, res, next) => {
Expand All @@ -13,7 +14,7 @@ module.exports = config =>
res.locals.choices = req.query.choices;
res.locals.type = queryMapper.mapServiceType(req.query);
res.locals.origin = queryMapper.mapServiceChoice(req.query);
res.locals.location = req.query.location;
res.locals.location = trim(req.query.location);
res.locals.locationHeading = queryMapper.getLocationHeading(req.query);
res.locals.correctLocationParams = res.locals.locationHeading;
const resultsInfo = queryMapper.getResultsInfo(req.query, res.locals.location);
Expand Down
11 changes: 11 additions & 0 deletions test/unit/lib/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,15 @@ describe('utils', () => {
expect(output).to.equal('a, 1, true');
});
});

describe('trim', () => {
it('should remove leading and trailing whitespace', () => {
const result = utils.trim(' test ');
expect(result).to.equal('test');
});
it('should gracefully handled undefined values', () => {
const result = utils.trim(undefined);
expect(result).to.equal(undefined);
});
});
});

0 comments on commit 9c186d8

Please sign in to comment.