forked from ambassify/url-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (115 loc) · 3.28 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
144
145
146
147
148
149
var URL = require('url');
var Path = require('path');
var mergeWith = require('lodash/mergeWith');
var extensions = Object.create(URL);
extensions.URL = URL;
extensions.qs = require('qs');
function merge(base /*, updates... */) {
var args = [], args_i = arguments.length;
while (args_i-- > 1) args[args_i - 1] = arguments[args_i];
args.unshift(base);
args.push(function(base, update) {
if (Array.isArray(base) || Array.isArray(update))
return update;
});
return mergeWith.apply(null, args);
}
function parseQuery(url) {
if (typeof url === 'string')
url = URL.parse(url);
return extensions.qs.parse((url.search || '').replace(/^\?/, ''));
}
function rebuildQuery(query) {
return extensions.qs.stringify(query);
}
function applyQuery(url, query) {
if (typeof query !== 'string')
query = rebuildQuery(query);
if (typeof url === 'string')
url = URL.parse(url);
url = merge({}, url, {
search: query ? '?' + query : ''
});
return URL.format(url);
}
function addQuery(url, additions, options) {
options = options || {};
var query = parseQuery(url);
if (options.preferNew || typeof options.preferNew === 'undefined') {
query = merge(query, additions);
} else {
query = merge({}, additions, query);
}
return applyQuery(url, query);
}
function removeQuery(url, deletions) {
var i = deletions.length;
var query = parseQuery(url);
while (i-- > 0)
delete query[deletions[i]];
return applyQuery(url, query);
}
var CONCAT_RE_START = /^\/+/;
var CONCAT_RE_END = /\/+$/;
var CONCAT_RE_BOTH = /^\/+|\/+$/g;
function concatPaths() {
var parts = new Array(arguments.length);
for (var i = parts.length - 1; i >= 0; --i) {
parts[i] = '' + arguments[i];
if (i === 0)
parts[i] = parts[i].replace(CONCAT_RE_END, '');
else if (i === parts.length - 1)
parts[i] = parts[i].replace(CONCAT_RE_START, '');
else
parts[i] = parts[i].replace(CONCAT_RE_BOTH, '');
if (i !== 0 && !parts[i])
parts.splice(i, 1);
}
return parts.join('/') || '/';
}
/**
* Use this as tagged template literals:
*
* pathEscape`/person/${id}/device/${deviceId}`
*
* will return a string in which `id` and `deviceId`
* have been url-encoded.
*/
function pathEscape(strings/* , ...args */) {
var args = [], args_i = arguments.length;
while (args_i-- > 1) args[args_i - 1] = arguments[args_i];
var out = [];
var len = strings.length;
for (var i = 0; i < len; i++) {
out.push(strings[i]);
if (typeof args[i] != 'undefined')
out.push(encodeURIComponent(args[i]));
}
return out.join('');
}
/**
* Remove all relative path navigation from a path
*
*/
function pathNormalize(path, relativeTo) {
if (!relativeTo)
relativeTo = '/';
if (path[0] != '/')
path = relativeTo + path;
return Path.normalize(path);
}
extensions.query = {
parse: parseQuery,
rebuild: rebuildQuery,
build: rebuildQuery,
add: addQuery,
remove: removeQuery,
omit: removeQuery,
set: applyQuery
};
extensions.path = {
concat: concatPaths,
escape: pathEscape,
normalize: pathNormalize,
};
module.exports = extensions;