Skip to content

Commit 011d9a5

Browse files
author
Nathan Booker
committed
Move SafeString unwrapping to common.js
1 parent 0ffd5a1 commit 011d9a5

4 files changed

Lines changed: 16 additions & 17 deletions

File tree

helpers/json.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
'use strict';
2-
const SafeString = require('handlebars').SafeString;
2+
const common = require('./lib/common.js');
33

44
const factory = () => {
55
return function(data) {
6-
if (data instanceof SafeString) {
7-
data = data.toString();
8-
}
6+
data = common.unwrapIfSafeString(data);
97
return JSON.stringify(data);
108
};
119
};

helpers/lib/common.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const URL = require('url').URL;
3+
const SafeString = require('handlebars').SafeString;
34

45
function isValidURL(val) {
56
try {
@@ -10,6 +11,14 @@ function isValidURL(val) {
1011
}
1112
}
1213

14+
function unwrapIfSafeString(val) {
15+
if (val instanceof SafeString) {
16+
val = val.toString();
17+
}
18+
return val;
19+
}
20+
1321
module.exports = {
1422
isValidURL,
23+
unwrapIfSafeString
1524
};

helpers/setURLQueryParam.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@ const SafeString = require('handlebars').SafeString;
66

77
const factory = () => {
88
return function(string, key, value) {
9-
if (string instanceof SafeString) {
10-
string = string.toString();
11-
}
12-
if (key instanceof SafeString) {
13-
key = key.toString();
14-
}
15-
if (value instanceof SafeString) {
16-
value = value.toString();
17-
}
9+
string = common.unwrapIfSafeString(string);
10+
key = common.unwrapIfSafeString(key);
11+
value = common.unwrapIfSafeString(value);
1812
if (!utils.isString(string) || !common.isValidURL(string)){
1913
throw new TypeError("Invalid URL passed to setURLQueryParam");
2014
} else if (!utils.isString(key)){

helpers/stripQuerystring.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
'use strict';
2-
const SafeString = require('handlebars').SafeString;
32
const utils = require('handlebars-utils');
3+
const common = require('./lib/common.js');
44

55
const factory = () => {
66
return function(url) {
7-
if (url instanceof SafeString) {
8-
url = url.toString();
9-
}
7+
url = common.unwrapIfSafeString(url);
108
if (utils.isString(url)) {
119
return url.split('?')[0];
1210
}

0 commit comments

Comments
 (0)