forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathurl.js
123 lines (108 loc) · 2.94 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
'use strict';
var url = require('url');
var util = {
isString: require('./utils/isString')
};
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
* @example {{ encodeURI 'https://myurl?Hello There' }} -> https%3A%2F%2Fmyurl%3FHello%20There
*/
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
* @example {{ escape 'https://myurl?Hello+There' }} -> https%3A%2F%2Fmyurl%3FHello%2BThere
*/
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
* @example {{ decodeURI 'https://myurl?Hello%20There' }} -> https://myurl?Hello There
*/
helpers.decodeURI = function(str) {
if (util.isString(str)) {
return decodeURIComponent(str);
}
};
/**
* 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
* @example {{ urlResolve 'https://myurl' '/api/test' }} -> https://myurl/api/test
*/
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
* @example {{ urlParse 'https://myurl/api/test' }}
*/
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
* @example {{ stripQuerystring 'https://myurl/api/test?foo=bar' }} -> 'https://myurl/api/test'
*/
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
* @example {{ stripProtocol 'https://myurl/api/test' }} -> '//myurl/api/test'
*/
helpers.stripProtocol = function(str) {
if (util.isString(str)) {
var parsed = url.parse(str);
parsed.protocol = '';
return parsed.format();
}
};