-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js.html
More file actions
437 lines (384 loc) · 16.6 KB
/
utils.js.html
File metadata and controls
437 lines (384 loc) · 16.6 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: utils.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: utils.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import 'isomorphic-fetch';
/**
* @module opensearch/utils
*/
export function parseURLQuery(url) {
const search = (url.indexOf('?') === -1) ? url : url.substring(url.indexOf('?'));
const vars = search.split('&');
const parsed = {};
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=');
parsed[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return parsed;
}
export function parseXml(xmlStr) {
if (typeof DOMParser !== 'undefined') {
return (new DOMParser()).parseFromString(xmlStr, 'text/xml');
} else if (typeof ActiveXObject !== 'undefined') {
const xmlDoc = new ActiveXObject('Microsoft.XMLDOM'); // eslint-disable-line no-undef
xmlDoc.async = 'false';
xmlDoc.loadXML(xmlStr);
return xmlDoc;
}
throw new Error('Could not parse XML document.');
}
/*
* Some common namespace definitions
*/
export const namespaces = {
os: 'http://a9.com/-/spec/opensearch/1.1/',
parameters: 'http://a9.com/-/spec/opensearch/extensions/parameters/1.0/',
atom: 'http://www.w3.org/2005/Atom',
georss: 'http://www.georss.org/georss',
dc: 'http://purl.org/dc/elements/1.1/',
media: 'http://search.yahoo.com/mrss/',
// EOP and OM related namespaces
opt: 'http://www.opengis.net/opt/2.1',
om: 'http://www.opengis.net/om/2.0',
eop: 'http://www.opengis.net/eop/2.0',
};
/*
* Get an array of all child elements
*/
function getChildren(element) {
if (element.children) {
return Array.from(element.children);
}
return Array.from(element.childNodes).filter(node => node.nodeType === 1); // Node.ELEMENT_NODE
}
/*
* Get an array of all *direct* descendants (in contrast to getElementsByTagName)
* of an element with a certain namespace URI and tag name.
*/
export function getElements(element, namespace, tagName) {
if (!element) {
return [];
}
const namespaceURI = namespaces[namespace] || namespace;
const children = getChildren(element);
if (tagName && namespaceURI) {
return children.filter(
child => child.localName === tagName && child.namespaceURI === namespaceURI
);
} else if (tagName) {
return children.filter(child => child.localName === tagName);
}
return children;
}
/*
* Get the first direct descendant element with the given namespace URI and tag name.
*/
export function getFirstElement(element, namespace, tagName) {
// use shortcut; when available
if (!namespace && !tagName && element.firstElementChild) {
return element.firstElementChild;
}
const elements = getElements(element, namespace, tagName);
if (elements.length) {
return elements[0];
}
return null;
}
/*
* Get the text of the first direct descendant element with the given namespace
* URI and tag name.
*/
export function getText(element, namespace, tagName) {
const first = getFirstElement(element, namespace, tagName);
return first ? first.textContent : null;
}
/*
* Get the value of the namespaced attribute or return a default.
*/
export function getAttributeNS(node, namespace, name, defaultValue) {
const namespaceURI = namespaces[namespace] || namespace;
if (node.hasAttributeNS(namespaceURI, name)) {
return node.getAttributeNS(namespaceURI, name);
}
return defaultValue;
}
function splitNamespace(name) {
return (name.indexOf(':') !== -1) ? name.split(':') : [null, name];
}
/**
* Resolves an xPath like query with the given element as basis. All parts of
* the path must be specified, none may be omitted. Allows to select attributes
* using the `@attrName` postfix or the text of an element using the `text()`
* as the last path part.
* @param {object} element The root element to start the query on. Must be a DOM
* compliant object.
* @param {string} path The search path: parts are separated by the `/` character
* and may contain a supported namespace prefix (separated
* by the color character).
* Examples: `os:Url@type`, `atom:entry/atom:title/text()`,
* `channel/item/georss:box/text()`
* @param {boolean} [single=false] Whether multiple elements are expected. When
* false, an array is returned, otherwise single
* values.
* @returns {object|string|object[]|string[]} Depending on the query and the
* single parameter, either a DOM Node
* or a string, or arrays thereof.
*/
export function simplePath(element, path, single = false) {
// split path and discard empty parts
const parts = path.split('/').filter(part => part.length);
let current = single ? element : [element];
for (let i = 0; i < parts.length; ++i) {
const part = parts[i];
// single values are treated differently
if (single) {
if (part === 'text()') {
return current.textContent;
} else if (part.indexOf('@') !== -1) {
const [nodePart, attrPart] = part.split('@');
const [namespace, tagName] = splitNamespace(nodePart);
const [attrNamespace, attrName] = splitNamespace(attrPart);
current = getFirstElement(current, namespace, tagName);
return getAttributeNS(current, attrNamespace, attrName);
}
const [namespace, tagName] = splitNamespace(part);
current = getFirstElement(current, namespace, tagName);
if (!current) {
return null;
}
} else if (part === 'text()') {
return current.map(currentElement => currentElement.textContent);
} else if (part.indexOf('@') !== -1) {
const [nodePart, attrPart] = part.split('@');
const [namespace, tagName] = splitNamespace(nodePart);
const [attrNamespace, attrName] = splitNamespace(attrPart);
return current.map(currentElement => getElements(currentElement, namespace, tagName))
.reduce((acc, value) => acc.concat(value), [])
.map(finalElement => getAttributeNS(finalElement, attrNamespace, attrName));
} else {
const [namespace, tagName] = splitNamespace(part);
current = current.map(currentElement => getElements(currentElement, namespace, tagName))
.reduce((acc, value) => acc.concat(value), []);
}
}
return current;
}
// adapted from https://developer.mozilla.org/en-US/Add-ons/Code_snippets/LookupPrefix
// Private function for lookupPrefix only
// eslint-disable-next-line no-underscore-dangle
function _lookupNamespacePrefix(namespaceURI, originalElement) {
const xmlnsPattern = /^xmlns:(.*)$/;
if (originalElement.namespaceURI && originalElement.namespaceURI === namespaceURI
&& originalElement.lookupNamespaceURI(originalElement.prefix) === namespaceURI) {
return originalElement.prefix;
}
if (originalElement.attributes && originalElement.attributes.length) {
for (let i = 0; i < originalElement.attributes.length; i++) {
const att = originalElement.attributes[i];
xmlnsPattern.lastIndex = 0;
let localName = att.localName || att.name.substr(att.name.indexOf(':') + 1); // latter test for IE which doesn't support localName
if (localName.indexOf(':') !== -1) { // For Firefox when in HTML mode
localName = localName.substr(att.name.indexOf(':') + 1);
}
if (xmlnsPattern.test(att.name) && att.value === namespaceURI) {
return localName;
}
}
}
if (originalElement.parentNode) {
// EntityReferences may have to be skipped to get to it
return _lookupNamespacePrefix(namespaceURI, originalElement.parentNode);
}
return null;
}
/**
* Looks up the the namespace prefix on the given DOM Node and the given namespace
* @param {DOMNode} node The node to look up the namespace prefix
* @param {String} namespaceURI The namespace URI to look up the namespace definition
* @returns {String} The namespace prefix
*/
export function lookupPrefix(node, namespaceURI) {
// Depends on private function _lookupNamespacePrefix() below and on https://developer.mozilla.org/En/Code_snippets/LookupNamespaceURI
// http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespacePrefix
// http://www.w3.org/TR/DOM-Level-3-Core/namespaces-algorithms.html#lookupNamespacePrefixAlgo
// (The above had a few apparent 'bugs' in the pseudo-code which were corrected here)
if (node.lookupPrefix) { // Shouldn't use this in text/html for Mozilla as will return null
return node.lookupPrefix(namespaceURI);
}
if (namespaceURI === null || namespaceURI === '') {
return null;
}
switch (node.nodeType) {
case 1: // Node.ELEMENT_NODE
return _lookupNamespacePrefix(namespaceURI, node);
case 9: // Node.DOCUMENT_NODE
return _lookupNamespacePrefix(namespaceURI, node.documentElement);
case 6: // Node.ENTITY_NODE
case 12: // Node.NOTATION_NODE
case 11: // Node.DOCUMENT_FRAGMENT_NODE
case 10: // Node.DOCUMENT_TYPE_NODE
return null; // type is unknown
case 2: // Node.ATTRIBUTE_NODE
if (node.ownerElement) {
return _lookupNamespacePrefix(namespaceURI, node.ownerElement);
}
return null;
default:
if (node.parentNode) {
// EntityReferences may have to be skipped to get to it
return _lookupNamespacePrefix(namespaceURI, node.parentNode);
}
return null;
}
}
export function fetchAndCheck(...args) {
return fetch(...args).then((response) => {
if (response.status >= 400) {
throw new Error('Bad response from server');
}
return response;
});
}
export function isNullOrUndefined(value) {
return typeof value === 'undefined' || value === null;
}
/*
* Forked from https://github.com/mapbox/wellknown/blob/87965f6f46ee38355e7e1f82107aa832ea29bc6c/wellknown.js
* Removed whitespaces after geometry type to be more robust with some (FedEO)
* services.
*/
/* eslint-disable no-param-reassign, prefer-template */
export function toWKT(gj) {
if (gj.type === 'Feature') {
gj = gj.geometry;
}
function wrapParens(s) { return '(' + s + ')'; }
function pairWKT(c) {
return c.join(' ');
}
function ringWKT(r) {
return r.map(pairWKT).join(', ');
}
function ringsWKT(r) {
return r.map(ringWKT).map(wrapParens).join(', ');
}
function multiRingsWKT(r) {
return r.map(ringsWKT).map(wrapParens).join(', ');
}
switch (gj.type) {
case 'Point':
return 'POINT(' + pairWKT(gj.coordinates) + ')';
case 'LineString':
return 'LINESTRING(' + ringWKT(gj.coordinates) + ')';
case 'Polygon':
return 'POLYGON(' + ringsWKT(gj.coordinates) + ')';
case 'MultiPoint':
return 'MULTIPOINT(' + ringWKT(gj.coordinates) + ')';
case 'MultiPolygon':
return 'MULTIPOLYGON(' + multiRingsWKT(gj.coordinates) + ')';
case 'MultiLineString':
return 'MULTILINESTRING(' + ringsWKT(gj.coordinates) + ')';
case 'GeometryCollection':
return 'GEOMETRYCOLLECTION(' + gj.geometries.map(toWKT).join(', ') + ')';
default:
throw new Error('stringify requires a valid GeoJSON Feature or geometry object as input');
}
}
/* eslint-enable no-param-reassign, prefer-template */
/**
* Returns a [Request]{@link https://developer.mozilla.org/en-US/docs/Web/API/Request}
* object for the fetch API.
* @param {string} url The request URL
* @param {object} [baseRequest] the baseRequest
* @returns {Request} The constructed request.
*/
export function createRequest(url, baseRequest) {
return new Request(url, baseRequest);
}
/**
* Creates (and sends) an [XMLHttpRequest]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest}.
* @param {string} url The request URL
* @param {object} [baseRequest] the baseRequest
* @returns {XMLHttpRequest} The constructed request.
*/
export function createXHR(url, baseRequest = {}) {
const xhr = new XMLHttpRequest();
xhr.open(baseRequest.method || 'GET', url);
if (baseRequest.headers) {
Object.keys(baseRequest.headers).forEach((key) => {
xhr.setRequestHeader(key, baseRequest.headers[key]);
});
}
xhr.send(baseRequest.body ? baseRequest.body : null);
return xhr;
}
/**
* Sort of polyfill for [Array.prototype.find]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find}
* @param {Array} arr the array to find the entry on.
* @param {function} predicate the callback to find the value.
* @param {*} thisArg the `this` for the predicate function.
* @returns {*} the found item or undefined
*/
export function find(arr, predicate, thisArg) {
if (Array.prototype.find) {
return arr.find(predicate, thisArg);
}
for (let i = 0; i < arr.length; ++i) {
const v = arr[i];
if (predicate(v, i, arr)) {
return v;
}
}
return undefined;
}
/**
* Sort of polyfill for [Object.assign]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign}
* @param {object} target the target to set the properties on.
* @param {...object} sources the source objects to copy properties from.
* @returns {object} the target
*/
export function assign(target, ...sources) {
if (Object.assign) {
return Object.assign(target, ...sources);
}
for (let i = 0; i < sources.length; ++i) {
const source = sources[i];
if (source) {
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key]; // eslint-disable-line no-param-reassign
}
}
}
}
return target;
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-opensearch.html">opensearch</a></li><li><a href="module-opensearch_config.html">opensearch/config</a></li><li><a href="module-opensearch_error.html">opensearch/error</a></li><li><a href="module-opensearch_formats.html">opensearch/formats</a></li><li><a href="module-opensearch_formats_atom.html">opensearch/formats/atom</a></li><li><a href="module-opensearch_formats_geojson.html">opensearch/formats/geojson</a></li><li><a href="module-opensearch_formats_rss.html">opensearch/formats/rss</a></li><li><a href="module-opensearch_formats_suggestions-json.html">opensearch/formats/suggestions-json</a></li><li><a href="module-opensearch_paginator.html">opensearch/paginator</a></li><li><a href="module-opensearch_parameter.html">opensearch/parameter</a></li><li><a href="module-opensearch_search.html">opensearch/search</a></li><li><a href="module-opensearch_url.html">opensearch/url</a></li><li><a href="module-opensearch_utils.html">opensearch/utils</a></li></ul><h3>Classes</h3><ul><li><a href="module-opensearch_formats_atom-AtomFormat.html">AtomFormat</a></li><li><a href="module-opensearch_formats_geojson-GeoJSONFormat.html">GeoJSONFormat</a></li><li><a href="module-opensearch_formats_rss-RSSFormat.html">RSSFormat</a></li><li><a href="module-opensearch_formats_suggestions-json-SuggestionsJSONFormat.html">SuggestionsJSONFormat</a></li><li><a href="module-opensearch_paginator.OpenSearchPaginator.html">OpenSearchPaginator</a></li><li><a href="module-opensearch_paginator-PagedSearchProgressEmitter.html">PagedSearchProgressEmitter</a></li><li><a href="module-opensearch_parameter.OpenSearchParameter.html">OpenSearchParameter</a></li><li><a href="module-opensearch_url.OpenSearchUrl.html">OpenSearchUrl</a></li><li><a href="OpenSearchDescription.html">OpenSearchDescription</a></li><li><a href="OpenSearchService.html">OpenSearchService</a></li></ul><h3>Events</h3><ul><li><a href="module-opensearch_paginator-PagedSearchProgressEmitter.html#event:error">error</a></li><li><a href="module-opensearch_paginator-PagedSearchProgressEmitter.html#event:page">page</a></li><li><a href="module-opensearch_paginator-PagedSearchProgressEmitter.html#event:success">success</a></li></ul><h3>Interfaces</h3><ul><li><a href="module-opensearch_formats.FormatInterface.html">FormatInterface</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Jan 10 2018 13:14:22 GMT+0100 (CET)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>