bare-url 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +29 -1
- package/lib/serialize.js +49 -0
- package/package.json +4 -1
package/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
const path = require('path')
|
|
1
|
+
const path = require('bare-path')
|
|
2
2
|
const constants = require('./lib/constants')
|
|
3
3
|
const errors = require('./lib/errors')
|
|
4
4
|
const parse = require('./lib/parse')
|
|
5
|
+
const serialize = require('./lib/serialize')
|
|
5
6
|
|
|
6
7
|
const URL = exports.URL = class URL {
|
|
7
8
|
constructor (href, base) {
|
|
@@ -17,6 +18,16 @@ const URL = exports.URL = class URL {
|
|
|
17
18
|
this._url = parse(href, base ? base._url : null)
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
// https://url.spec.whatwg.org/#dom-url-href
|
|
22
|
+
|
|
23
|
+
get href () {
|
|
24
|
+
return serialize(this._url)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
set href (value) {
|
|
28
|
+
this._url = parse(value)
|
|
29
|
+
}
|
|
30
|
+
|
|
20
31
|
// https://url.spec.whatwg.org/#dom-url-protocol
|
|
21
32
|
|
|
22
33
|
get protocol () {
|
|
@@ -171,6 +182,23 @@ const URL = exports.URL = class URL {
|
|
|
171
182
|
|
|
172
183
|
parse(value, null, this._url, constants.STATE_FRAGMENT)
|
|
173
184
|
}
|
|
185
|
+
|
|
186
|
+
[Symbol.for('bare.inspect')] () {
|
|
187
|
+
return {
|
|
188
|
+
__proto__: { constructor: URL },
|
|
189
|
+
|
|
190
|
+
href: this.href,
|
|
191
|
+
protocol: this.protocol,
|
|
192
|
+
username: this.username,
|
|
193
|
+
password: this.password,
|
|
194
|
+
host: this.host,
|
|
195
|
+
hostname: this.hostname,
|
|
196
|
+
port: this.port,
|
|
197
|
+
pathname: this.pathname,
|
|
198
|
+
search: this.search,
|
|
199
|
+
hash: this.hash
|
|
200
|
+
}
|
|
201
|
+
}
|
|
174
202
|
}
|
|
175
203
|
|
|
176
204
|
exports.fileURLToPath = function fileURLToPath (url) {
|
package/lib/serialize.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// https://url.spec.whatwg.org/#url-serializing
|
|
2
|
+
module.exports = function serialize (url, excludeFragment = false) {
|
|
3
|
+
let output = url.scheme + ':'
|
|
4
|
+
|
|
5
|
+
if (url.host) {
|
|
6
|
+
output += '//'
|
|
7
|
+
|
|
8
|
+
if (url.username !== '' || url.password !== '') {
|
|
9
|
+
output += url.username
|
|
10
|
+
|
|
11
|
+
if (url.password !== '') {
|
|
12
|
+
output += ':' + url.password
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
output += '@'
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
output += url.host
|
|
19
|
+
|
|
20
|
+
if (url.port !== null) {
|
|
21
|
+
output += ':' + url.port.toString(10)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (
|
|
26
|
+
url.host === null &&
|
|
27
|
+
typeof url.path !== 'string' &&
|
|
28
|
+
url.path.length > 1 &&
|
|
29
|
+
url.path[0] === ''
|
|
30
|
+
) {
|
|
31
|
+
output += '/.'
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (typeof url.path === 'string') {
|
|
35
|
+
output += url.path
|
|
36
|
+
} else {
|
|
37
|
+
output += '/' + url.path.join('/')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (url.query !== null) {
|
|
41
|
+
output += '?' + url.query
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (url.fragment !== null && !excludeFragment) {
|
|
45
|
+
output += '#' + url.fragment
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return output
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-url",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "URL parser for JavaScript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -20,6 +20,9 @@
|
|
|
20
20
|
"url": "https://github.com/holepunchto/bare-url/issues"
|
|
21
21
|
},
|
|
22
22
|
"homepage": "https://github.com/holepunchto/bare-url",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"bare-path": "^1.1.2"
|
|
25
|
+
},
|
|
23
26
|
"devDependencies": {
|
|
24
27
|
"brittle": "^3.3.2",
|
|
25
28
|
"standard": "^17.1.0"
|