-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.js
More file actions
187 lines (164 loc) · 7.11 KB
/
string.js
File metadata and controls
187 lines (164 loc) · 7.11 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
@fileOverview
@toc
1. parseUrl
1.5. parseUrlParams
1.6. stripUrlParams
*/
'use strict';
angular.module('jackrabbitsgroup.angular-string', [])
.factory('jrgString', [ function () {
//methods and properties (some of which may be private - explicitly state which methods and properties to expose/return at bottom)
var privateObj ={
/**
Parses the url to retrieve GET params BEFORE Angular $location.url() is available..
Handles hash (#) for non HTML5 History support (so '#/' will be stripped off too - though this may be an AngularJS specific routing solution??)
@toc 1.
@method parseUrl
@param {Object} params
@param {String} url The full url to parse
@param {String} [rootPath =''] The part to strip off the beginning of the url (i.e. 'static/public/')
@return {Object} A parsed section of the current url, i.e. for a url of: 'http://localhost/static/public/home?p1=yes&p2=no#hash1=h1&hash2=h2'
@param {String} page The current url WITHOUT url GET params and WITHOUT hash params and WITHOUT the root path, i.e. 'home'
@param {String} queryParams The GET params, i.e. 'p1=yes&p2=no'
@param {Object} queryParamsObj An object version of the GET params, i.e. {p1:'yes', p2:'no'}
@param {String} hashParams The hash params, i.e. 'hash1=h1&hash2=h2'
@param {Object} hashParamsObj An object version of the hash params, i.e. {hash1:'h1', hash2:'h2'}
@usage
var parsedUrl =jrgString.parseUrl({url:'http://localhost/static/public/home?p1=yes&p2=no#hash1=h1&hash2=h2', rootPath:'static/public/'});
*/
parseUrl: function(params) {
var ret ={page: '', queryParams: '', hashParams: ''};
var defaults ={rootPath: ''};
var xx;
for(xx in defaults) {
if(params[xx] ===undefined) {
params[xx] =defaults[xx];
}
}
var appPath =params.rootPath;
var curUrl =params.url;
//strip off host info (in case rootPath is just '/', don't want to match the slash in the host/protocol info)
var posSlashes =curUrl.indexOf('://');
if(posSlashes >-1) {
curUrl =curUrl.slice(posSlashes+3, curUrl.length);
}
var pos1 =curUrl.indexOf(appPath);
var curPage =curUrl.slice((pos1+appPath.length), curUrl.length);
//handle Angular non HTML5 history by stripping off leading '#/'
var posHash =curPage.indexOf("#/");
if(posHash >-1) {
curPage =curPage.slice((posHash+2), curPage.length);
}
//hash url params - this assumes '#' is AFTER '?', which is why we do it FIRST
posHash =curPage.indexOf("#");
var hashParams ='';
if(posHash >-1) {
hashParams =curPage.slice((posHash+1), curPage.length);
curPage =curPage.slice(0, posHash);
}
//query / GET url params
var posQuery =curPage.indexOf("?");
var queryParams ='';
if(posQuery >-1) {
queryParams =curPage.slice((posQuery+1), curPage.length);
curPage =curPage.slice(0, posQuery);
}
ret.page =curPage;
ret.queryParams =queryParams;
ret.queryParamsObj =this.parseUrlParams(queryParams, {});
ret.hashParams =hashParams;
ret.hashParamsObj =this.parseUrlParams(hashParams, {});
return ret;
},
/**
Turns a query (or hash) string (i.e. '?yes=no&maybe=so') into an object for easier reference
@toc 1.5.
@param {String} urlParams The query string (i.e. '?yes=no&maybe=so' or '#yes=no&maybe=so')
@param {Object} [params]
@return {Object} Key-value pairs for each parameter; i.e. {'yes':'no', 'maybe':'so'}
@usage
var parsedParams =jrgString.parseUrlParams('?yes=no&maybe=so', {});
*/
parseUrlParams: function(urlParams, params) {
//strip out leading question mark, if present
var questionMark =urlParams.indexOf("?");
if(questionMark >-1) {
urlParams =urlParams.slice((questionMark+1), urlParams.length);
}
//strip out leading hash, if present
var hashTag =urlParams.indexOf("#");
if(hashTag >-1) {
urlParams =urlParams.slice((hashTag+1), urlParams.length);
}
var urlParamsObj ={};
var parts =urlParams.split('&');
var ii, subParts;
for(ii =0; ii<parts.length; ii++) {
subParts =parts[ii].split('=');
urlParamsObj[subParts[0]] =subParts[1];
}
return urlParamsObj;
},
/**
Takes a url and removes one or more parameters and adds a trailing '?' or '&' so more can be added
@toc 1.6.
@method stripUrlParams
@param {String} url The original url (i.e. 'http://domain.com?p1=yes&p2=no&p3=maybe' )
@param {Array} stripKeys The params to remove from the url (i.e. ['p2'] )
@param {Object} [params]
@param {Boolean} [returnParamsOnly] True to return JUST url params (i.e. cut out the domain - i.e. 'http://domain.com' would NOT be present in the returned url)
@return {String} newUrl (i.e. 'http://domain.com?p1=yes&p3=maybe&' )
@usage
var newUrl =jrgString.stripUrlParams('http://domain.com?p1=yes&p2=no&p3=maybe', ['p2'], {});
*/
stripUrlParams: function(url, stripKeys, params) {
var newUrl =url;
var ii, patt1, patt2, patt3, patt4;
//strip out host (everything before leading question mark) since need to add back in question mark later since need to search WITH a leading '?' and '&' otherwise can get improper matches (i.e. 'page' will improperly replace '&editpage=yes' if don't search with the leading character first)
var host ='';
var questionMark =newUrl.indexOf("?");
if(questionMark >-1) {
host =newUrl.slice(0, questionMark);
newUrl =newUrl.slice((questionMark+0), newUrl.length);
}
for(ii =0; ii<stripKeys.length; ii++) {
//note: order matters here - the last two will match the ENTIRE rest of the string so must only replace AFTER have searched for and replaced it earlier (i.e. before a '&') if it exists there!
//must do these first
patt1 =new RegExp('\\?'+stripKeys[ii]+'=.*&', 'i'); //for leading (first) parameter with non-ending parameter
patt2 =new RegExp('&'+stripKeys[ii]+'=.*&', 'i'); //for all other (non-first) parameters with non-ending parameter
//must do these last
patt3 =new RegExp('\\?'+stripKeys[ii]+'=.*', 'i'); //for leading (first) parameter
patt4 =new RegExp('&'+stripKeys[ii]+'=.*', 'i'); //for all other (non-first) parameters
newUrl =newUrl.replace(patt1, '?').replace(patt2, '&').replace(patt3, '?').replace(patt4, '&');
}
//re-add leading question mark
if(newUrl.length >0) { //if have something left
if(newUrl.indexOf('?') <0) { //if no question mark, replace leading character (must be an '&') with a question mark
newUrl ='?'+newUrl.slice(1, newUrl.length);
}
}
//add appropriate trailing character so returned url can be added to without having to figure out if it should be a '?' or a '&'
if(newUrl.indexOf('?') <0) {
newUrl +='?';
}
else {
var lastChar =newUrl[(newUrl.length-1)];
if(lastChar !=='&' && lastChar !=='?') { //don't have duplicate '&&' or '?&'
newUrl +='&';
}
}
if(params.returnParamsOnly ===undefined || !params.returnParamsOnly) {
//add back in host
newUrl =host+newUrl;
}
return newUrl;
},
};
//select which methods/functions (and potentially properties) to expose
return {
parseUrl: privateObj.parseUrl,
parseUrlParams: privateObj.parseUrlParams,
stripUrlParams: privateObj.stripUrlParams
};
}]);