generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
45 lines (40 loc) · 1.29 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
const stringifyChildren = thing => Array.from(
thing[Symbol.iterator](),
node => node.nodeType === Node.ELEMENT_NODE
? node.outerHTML
: node.textContent
).join('');
export function stringify(thing) {
switch(typeof thing) {
case 'string':
return thing;
case 'function':
throw new TypeError('Functions are not supported.');
case 'undefined':
return '';
case 'object':
if (thing === null) {
return '';
} else if (thing instanceof DocumentFragment || thing instanceof Document) {
return stringifyChildren(thing.childNodes);
} else if (thing instanceof NodeList || thing instanceof HTMLCollection) {
return stringifyChildren(thing);
} else if (thing instanceof Element) {
return thing.outerHTML;
} else if (thing instanceof Date) {
return thing.toISOString();
} else if (Array.isArray(thing)) {
return thing.map(stringify).join('');
} else if (thing instanceof ArrayBuffer && Uint8Array.prototype.toBase64 instanceof Function) {
return new Uint8Array(thing).toBase64();
} else if (ArrayBuffer.isView(thing) && thing.toBase64 instanceof Function) {
return thing.toBase64();
} else if (thing instanceof Blob) {
return URL.createObjectURL(thing);
} else {
return thing.toString();
}
default:
return thing.toString();
}
}