-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
143 lines (112 loc) · 3.35 KB
/
index.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
133
134
135
136
137
138
139
140
141
142
143
/***************************
* Modules
***************************/
var url = require('url');
/***************************
* Yuri API
***************************/
var methods = [
'protocol', // The request protocol, lowercased.
'slashes', // The protocol requires slashes after the colon
'host', // The full lowercased host portion of the URL, including port information.
'auth', // The authentication information portion of a URL.
'hostname', // Just the lowercased hostname portion of the host.
'port', // The port number portion of the host.
'pathname', // The path section of the URL, that comes after the host and before the query, including the initial slash if present.
'search', // The 'query string' portion of the URL, including the leading question mark.
'path', // Concatenation of pathname and search.
'query', // Either the 'params' portion of the query string, or a querystring-parsed object.
'hash' // The 'fragment' portion of the URL including the pound-sign.
];
/***************************
* Yuri Constuctor
***************************/
var Yuri = module.exports = function Yuri(config) {
if (!(this instanceof Yuri)) {
return new Yuri(config);
}
if (typeof config === 'string') {
this.url = url.parse(config);
} else {
this.url = config || {};
}
};
/********************************************************
* Yuri Factory
* Create a static method on the Yuri constuctor for
* each protype, which acts as a factory for creating
* a Yuri instance. This allows one to use Yuri directly
* without initializing yuri.
* example -
* var yo = yuri
* .protocol('http')
* .hostname('www.yo.com')
* .format();
********************************************************/
function yuriFactory(method) {
return function (arg) {
var yuri = Yuri({});
return yuri[method].call(yuri, arg);
}
}
methods.forEach(function (method) {
Yuri[method] = yuriFactory(method);
});
/***************************
* Yuri Prototypes
***************************/
Yuri.prototype.protocol = function (protocol) {
this.url.protocol = protocol;
return this;
};
Yuri.prototype.slashes = function (slashes) {
this.url.slashes = slashes;
return this;
};
Yuri.prototype.host = function (host) {
this.url.host = host;
return this;
};
Yuri.prototype.auth = function (auth) {
this.url.auth = auth;
return this;
};
Yuri.prototype.hostname = function (hostname) {
this.url.hostname = hostname;
return this;
};
Yuri.prototype.port = function (port) {
this.url.port = port;
return this;
};
Yuri.prototype.pathname = function (pathname) {
if (Array.isArray(pathname)) {
this.url.pathname = pathname.map(function (p, i) {
if (i === pathname.length - 1) return p.toString().replace(/^\//, '');
return p.toString().replace(/^\/|\/$/g, '');
}).join('/');
} else {
this.url.pathname = pathname;
}
return this;
};
Yuri.prototype.search = function (search) {
this.url.search = search;
return this;
};
Yuri.prototype.path = function (path) {
this.url.path = path;
return this;
};
Yuri.prototype.query = function (query) {
this.url.query = query;
return this;
};
Yuri.prototype.hash = function (hash) {
this.url.hash = hash;
return this;
};
Yuri.prototype.format = function () {
return url.format(this.url);
};
Yuri.prototype.toString = Yuri.prototype.format;