-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathutils.js
More file actions
183 lines (169 loc) · 4.57 KB
/
utils.js
File metadata and controls
183 lines (169 loc) · 4.57 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var WHATWGURL = require('url').URL
, namespaces = require('./namespaces')
;
exports.has = require('lodash.has');
exports.assign = require('lodash.assign');
exports.uniq = require('lodash.uniq');
var _get = require('lodash.get');
/**
* lodash.get, but wrapped to provide a default subkey (a/k/a path) of "#"
* and defaultValue of "null"
*
* var obj = { '#': 'foo', 'bar': 'baz' };
*
* get(obj);
* // => 'foo'
*
* get(obj, 'bar');
* // => 'baz'
*
* @param {Object} obj
* @param {String} [subkey="#"] By default, use the '#' key, but you may pass any key you like
* @return Returns the value of the selected key or 'null' if undefined.
* @private
*/
function get(obj, subkey, defaultValue) {
if (!subkey) {
subkey = '#';
}
if (!defaultValue) {
defaultValue = null;
}
if (Array.isArray(obj)) {
return _get(obj[0], subkey, defaultValue);
}
else {
return _get(obj, subkey, defaultValue);
}
}
exports.get = get;
/**
* Safely trim a value if it's a String
* @private
*/
function safeTrim (val) {
if (typeof val === 'string') {
return val.trim();
}
return val;
}
exports.safeTrim = safeTrim;
/*
* Resolve a URL against a base URL, returning the original pathUrl if
* either parameter isn't provided or if the URL is not resolvable (e.g.
* tag: URIs and other non-http schemes that the URL constructor rejects).
* @private
*/
function resolve (baseUrl, pathUrl) {
if (!baseUrl || !pathUrl) return pathUrl;
try {
return new WHATWGURL(pathUrl, baseUrl).href;
} catch (e) {
return pathUrl;
}
}
exports.resolve = resolve;
/*
* Check whether a given uri is an absolute URL
* @param {String} uri
* @private
*/
function isAbsoluteUrl (uri) {
if (!uri || typeof uri !== 'string') return false;
try {
return Boolean(new WHATWGURL(uri).host);
} catch (e) {
return false;
}
}
exports.isAbsoluteUrl = isAbsoluteUrl;
/*
* Check whether a given namespace URI matches the given default
*
* @param {String} URI
* @param {String} default, e.g., 'atom'
* @return {Boolean}
* @private
*/
function nslookup (uri, def) {
return namespaces[uri] === def;
}
exports.nslookup = nslookup;
/*
* Return the "default" namespace prefix for a given namespace URI
*
* @param {String} URI
* @return {String}
* @private
*/
function nsprefix (uri) {
return namespaces[uri];
}
exports.nsprefix = nsprefix;
/*
* Walk a node and re-resolve the urls using the given baseurl
*
* @param {Object} node
* @param {String} baseurl
* @return {Object} modified node
* @private
*/
function reresolve (node, baseurl) {
if (!node || !baseurl) {
return false; // Nothing to do.
}
function resolveLevel (level) {
var els = Object.keys(level);
els.forEach(function(el){
if (Array.isArray(level[el])) {
// The shape of the array of element items is different than if the element is not an array.
// We need it to be the same shape to enable using the same function for recursion.
var levelFromArray = {};
level[el].forEach(function (attrs) {
levelFromArray[el] = attrs;
resolveLevel(levelFromArray);
});
} else {
if (level[el].constructor.name === 'Object') {
if (el == 'logo' || el == 'icon' || el == 'link') {
if ('#' in level[el]) {
level[el]['#'] = resolve(baseurl, level[el]['#']);
}
} else if (el == 'image') {
if ('url' in level[el] && level[el]['url'].constructor.name === 'Object' && '#' in level[el]['url']) {
level[el]['url']['#'] = resolve(baseurl, level[el]['url']['#']);
}
if ('link' in level[el] && level[el]['link'].constructor.name === 'Object' && '#' in level[el]['link']) {
level[el]['link']['#'] = resolve(baseurl, level[el]['link']['#']);
}
}
if ('@' in level[el]) {
var attrs = Object.keys(level[el]['@']);
attrs.forEach(function (name) {
if (name == 'href' || name == 'src' || name == 'uri') {
if ('string' === typeof level[el]['@'][name]) {
level[el]['@'][name] = resolve(baseurl, level[el]['@'][name]);
}
}
});
}
}
}
});
return level;
}
return resolveLevel(node);
}
exports.reresolve = reresolve;
/*
* Aggressivly strip HTML tags
* Pulled out of node-resanitize because it was all that was being used
* and it's way lighter...
*
* @param {String} str
* @private
*/
function stripHtml (str) {
return str.replace(/<.*?>/g, '');
}
exports.stripHtml = stripHtml;