forked from unshiftio/url-parse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzzy.js
More file actions
128 lines (115 loc) · 2.43 KB
/
fuzzy.js
File metadata and controls
128 lines (115 loc) · 2.43 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
'use strict';
var URL = require('./')
, url = new URL('');
/**
* A dictionary with all kind of different options that should generate a valid
* and parse-able URL.
*
* @type {Object}
* @api private
*/
var combinations = {};
combinations.protocol = [
'http:',
'https:',
'ws:',
'wss:',
'blob:'/*,
''*/
];
combinations.username = ['foo', 'bar'];
combinations.password = combinations.username;
combinations.hostname = [
'example.com',
'www.example.com',
'travel.travel',
'sub.sub.sub.domain.nl',
'xn--n3h.com',
'localhost',
'127.0.0.1',
'255.255.255.255',
'[3ffe:6a88:85a3:08d3:1319:8a2e:0370:7344]',
'[2001:2353::1428:57ab]',
'[2001:2353:0::0:1428:57ab]',
'[2001:2353:0:0:0:0:1428:57ab]',
'[2001:2353:0000:0000:0000::1428:57ab]',
'[2001:2353:0000:0000:0000:0000:1428:57ab]',
'[2001:2353:02de::0e13]',
'[2001:2353:2de::e13]',
'[::2]',
'[1::]'
];
combinations.port = ['8080', '844', '3340'];
combinations.pathname = [
'/',
'/bar',
'/bar/',
'/foo/bar',
'/foo.bar/foo',
'/fav.ico',
'/@3rd-Eden',
'/a/b/c/d/e/f/g/j/1/d/4/'
];
combinations.query = ['foo=bar',
'foo[]=bar&foo[]=foo',
'email=foo@bar.travel',
'q='
];
combinations.hash = [
'name',
'moo-with-longer-name',
'/what/about/slashes?querystring',
'?querystring',
'!/google/urls',
'use:foo@',
'http://'
];
/**
* Get a random item from the given array.
*
* @param {String} name Name of the array we want to have a random item returned.
* @returns {Mixed}
* @api private
*/
function get(name) {
var data = combinations[name];
return data[Math.floor(Math.random() * data.length)];
}
/**
* Return a random boolean.
*
* @returns {Boolean}
* @api private
*/
function yep() {
return !!Math.round(Math.random() * 1);
}
/**
* Generate the actual URL.
*
* @returns {Object} specification
* @api public
*/
module.exports = function generate() {
var spec = {}
, key;
spec.protocol = get('protocol');
spec.hostname = get('hostname');
spec.pathname = get('pathname');
if (yep()) spec.port = get('port');
if (yep()) spec.query = '?'+ get('query');
if (yep()) spec.hash = '#'+ get('hash');
if (yep()) {
spec.username = get('username');
spec.password = get('password');
}
spec.host = spec.port ? spec.hostname + ':' + spec.port : spec.hostname;
for (key in combinations) {
url[key] = '';
}
for (key in spec) {
url[key] = spec[key];
}
spec.href = url.toString();
return spec;
};