forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.js
132 lines (113 loc) · 2.65 KB
/
url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict';
var url = require('url');
var util = require('handlebars-utils');
var querystring = require('querystring');
var helpers = module.exports;
/**
* Encodes a Uniform Resource Identifier (URI) component
* by replacing each instance of certain characters by
* one, two, three, or four escape sequences representing
* the UTF-8 encoding of the character.
*
* @param {String} `str` The un-encoded string
* @return {String} The endcoded string
* @api public
*/
helpers.encodeURI = function(str) {
if (util.isString(str)) {
return encodeURIComponent(str);
}
};
/**
* Escape the given string by replacing characters with escape sequences.
* Useful for allowing the string to be used in a URL, etc.
*
* @param {String} `str`
* @return {String} Escaped string.
* @api public
*/
helpers.escape = function(str) {
if (util.isString(str)) {
return querystring.escape(str);
}
};
/**
* Decode a Uniform Resource Identifier (URI) component.
*
* @param {String} `str`
* @return {String}
* @api public
*/
helpers.decodeURI = function(str) {
if (util.isString(str)) {
return decodeURIComponent(str);
}
};
/**
* Alias for [encodeURI](#encodeuri).
* @api public
*/
helpers.url_encode = function() {
return helpers.encodeURI.apply(this, arguments);
};
/**
* Alias for [decodeURI](#decodeuri).
* @api public
*/
helpers.url_decode = function(val) {
return helpers.decodeURI.apply(this, arguments);
};
/**
* Take a base URL, and a href URL, and resolve them as a
* browser would for an anchor tag.
*
* @param {String} `base`
* @param {String} `href`
* @return {String}
* @api public
*/
helpers.urlResolve = function(base, href) {
return url.resolve(base, href);
};
/**
* Parses a `url` string into an object.
*
* @param {String} `str` URL string
* @return {String} Returns stringified JSON
* @api public
*/
helpers.urlParse = function(str) {
return url.parse(str);
};
/**
* Strip the query string from the given `url`.
*
* @param {String} `url`
* @return {String} the url without the queryString
* @api public
*/
helpers.stripQuerystring = function(str) {
if (util.isString(str)) {
return str.split('?')[0];
}
};
/**
* Strip protocol from a `url`. Useful for displaying media that
* may have an 'http' protocol on secure connections.
*
* ```handlebars
* <!-- url = 'http://foo.bar' -->
* {{stripProtocol url}}
* <!-- results in: '//foo.bar' -->
* ```
* @param {String} `str`
* @return {String} the url with http protocol stripped
* @api public
*/
helpers.stripProtocol = function(str) {
if (util.isString(str)) {
var parsed = url.parse(str);
parsed.protocol = '';
return parsed.format();
}
};