-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.js
181 lines (168 loc) · 3.91 KB
/
utils.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
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
/**
* Module dependencies.
*/
var URL = require('url')
, NS = require('./namespaces')
;
/**
* Safe hasOwnProperty
* See: http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/
*/
function has (obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
exports.has = has;
/**
* Merge object b with object a.
*
* var a = { foo: 'bar' }
* , b = { foo: 'quux', bar: 'baz' };
*
* merge(a, b);
* // => { foo: 'quux', bar: 'baz' }
*
* merge(a, b, true);
* // => { foo: 'bar', bar: 'baz' }
*
* @param {Object} a
* @param {Object} b
* @param {Boolean} [noforce] Optionally, don't overwrite any existing keys in a found in b
* @return {Object}
*/
function merge (a, b, noforce) {
if (a && b && a === Object(a) && b === Object(b)) {
for (var key in b) {
if (has(b, key)) {
if (noforce) {
if (!a.hasOwnProperty(key)) a[key] = b[key];
} else {
a[key] = b[key];
}
}
}
}
return a;
}
exports.merge = merge;
/**
* Create an array containing the unique members of an array.
*
* var array = ['a', 'b', 1, 2, 'a', 3 ];
*
* unique(array);
* // => ['b', 1, 2, 'a', 3 ]
*
* @param {Array} array
* @return {Array}
*/
function unique (array) {
var a = [];
var l = array.length;
for (var i=0; i<l; i++) {
for (var j=i+1; j<l; j++) {
// If array[i] is found later in the array
if (array[i] === array[j])
j = ++i;
}
a.push(array[i]);
}
return a;
}
exports.unique = unique;
/**
* Utility function to test for and extract a subkey.
*
* 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.
*/
function get(obj, subkey) {
if (!subkey)
subkey = '#';
if (Array.isArray(obj))
obj = obj[0];
if (obj && obj[subkey])
return obj[subkey];
else
return null;
}
exports.get = get;
/*
* Expose require('url').resolve
*/
function resolve (baseUrl, pathUrl) {
return URL.resolve(baseUrl, pathUrl);
}
exports.resolve = resolve;
/*
* Check whether a given namespace URI matches the given default
*
* @param {String} URI
* @param {String} default, e.g., 'atom'
* @return {Boolean}
*/
function nslookup (uri, def) {
return NS[uri] === def;
}
exports.nslookup = nslookup;
/*
* Return the "default" namespace prefix for a given namespace URI
*
* @param {String} URI
* @return {String}
*/
function nsprefix (uri) {
return NS[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
*/
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])) {
level[el].forEach(resolveLevel);
} else {
if (level[el].constructor.name === 'Object') {
if (el == 'logo' || el == 'icon') {
if ('#' in level[el]) {
level[el]['#'] = URL.resolve(baseurl, level[el]['#']);
}
} else {
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] = URL.resolve(baseurl, level[el][name]);
}
else if ('#' in level[el][name]) {
level[el][name]['#'] = URL.resolve(baseurl, level[el][name]['#']);
}
}
});
}
}
}
});
return level;
}
return resolveLevel(node);
}
exports.reresolve = reresolve;