Skip to content

Commit abddb31

Browse files
committed
Add the parse() method
1 parent 2cc43c9 commit abddb31

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Once the lib is downloaded,
2525
'ngUrlParser'
2626
])
2727
.controller('demoCtrl', function(urlParser) {
28+
this.parts = urlParser.parse();
2829
this.protocol = urlParser.getProtocol();
2930
this.host = urlParser.getHost();
3031
this.hostname = urlParser.getHostname();
@@ -50,7 +51,22 @@ Once the lib is downloaded,
5051
- `getQuerystring([string] url): string` : Return the part of the given url with options
5152
- `getOption([string] param, [string] url): object|string` : Return a specific option in the url's options, or all options in an object
5253
- `getHash([string] url): string` : Return the hash of the given url
53-
54+
- `parse([string] url): object` : Return parsed url in an object:
55+
```json
56+
// urlParser.parse('http://localhost:3000/models?sort=asc#quantiles')
57+
58+
{
59+
"protocol": "http:",
60+
"host": "localhost:3000",
61+
"hostname": "localhost",
62+
"port": "3000",
63+
"pathname": "/models"
64+
"search": "?sort=asc",
65+
"searchObject": { "sort": "asc" },
66+
"hash": "quantiles"
67+
}
68+
```
69+
5470
# Contributing
5571

5672
1. Fork it

angular-url-parser.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Angular service to get simple methods to manipulate url parts
3+
*
4+
* @version 0.0.2 - 2016-07-27
5+
* @link https://github.com/yllieth/angular-url-parser
6+
* @license MIT License, http://www.opensource.org/licenses/MIT
7+
*/
18
angular
29
.module('ngUrlParser', [])
310
.factory('urlParser', function () {
@@ -30,6 +37,17 @@ angular
3037
}
3138

3239
return {
40+
/**
41+
* Returns an object with url parts placed in different properties
42+
*
43+
* @param {string} [url] - location.href if not specified
44+
* @returns {{protocol, host, hostname, port, pathname, search, searchObject, hash}}
45+
*/
46+
parse: function (url) {
47+
url = url || window.location.href;
48+
return urlParser(url);
49+
},
50+
3351
/**
3452
* Returns the protocol part of the given url
3553
*
@@ -42,7 +60,7 @@ angular
4260
*/
4361
getProtocol: function (url) {
4462
url = url || window.location.href;
45-
return urlParser(url).protocol;
63+
return this.parse(url).protocol;
4664
},
4765

4866
/**
@@ -57,7 +75,7 @@ angular
5775
*/
5876
getHost: function (url) {
5977
url = url || window.location.href;
60-
return urlParser(url).host;
78+
return this.parse(url).host;
6179
},
6280

6381
/**
@@ -72,7 +90,7 @@ angular
7290
*/
7391
getHostname: function (url) {
7492
url = url || window.location.href;
75-
return urlParser(url).hostname;
93+
return this.parse(url).hostname;
7694
},
7795

7896
/**
@@ -87,7 +105,7 @@ angular
87105
*/
88106
getPort: function (url) {
89107
url = url || window.location.href;
90-
return urlParser(url).port;
108+
return this.parse(url).port;
91109
},
92110

93111
/**
@@ -102,7 +120,7 @@ angular
102120
*/
103121
getRoute: function (url) {
104122
url = url || window.location.href;
105-
return urlParser(url).pathname;
123+
return this.parse(url).pathname;
106124
},
107125

108126
/**
@@ -131,7 +149,7 @@ angular
131149
*/
132150
getQuerystring: function (url) {
133151
url = url || window.location.href;
134-
return urlParser(url).search;
152+
return this.parse(url).search;
135153
},
136154

137155
/**
@@ -152,7 +170,7 @@ angular
152170
getOption: function (param, url) {
153171
url = url || window.location.href;
154172

155-
var searchOject = urlParser(url).searchObject;
173+
var searchOject = this.parse(url).searchObject;
156174

157175
if (typeof param === 'string') {
158176
return (searchOject.hasOwnProperty(param) === true)
@@ -175,7 +193,7 @@ angular
175193
*/
176194
getHash: function (url) {
177195
url = url || window.location.href;
178-
return urlParser(url).hash;
196+
return this.parse(url).hash;
179197
}
180198
};
181199
});

bower.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-url-parser",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"homepage": "https://github.com/yllieth/angular-url-parser",
55
"authors": [
66
"Sylvain RAGOT <sylvnimes@hotmail.com>"
@@ -17,7 +17,6 @@
1717
"license": "MIT",
1818
"ignore": [
1919
"**/.*",
20-
"node_modules",
2120
"bower_components"
2221
],
2322
"dependencies": {

0 commit comments

Comments
 (0)