UNPKG

1.1 kBJavaScriptView Raw
1// Helper function to retrieve options from element attributes
2export const getOptions = function(obj) {
3 const options = Array.prototype.reduce.call(
4 obj,
5 (acc, attribute) => {
6 const option = attribute.name.match(/data-simplebar-(.+)/);
7 if (option) {
8 const key = option[1].replace(/\W+(.)/g, (x, chr) => chr.toUpperCase());
9 switch (attribute.value) {
10 case 'true':
11 acc[key] = true;
12 break;
13 case 'false':
14 acc[key] = false;
15 break;
16 case undefined:
17 acc[key] = true;
18 break;
19 default:
20 acc[key] = attribute.value;
21 }
22 }
23 return acc;
24 },
25 {}
26 );
27 return options;
28};
29
30export function getElementWindow(element) {
31 if (
32 !element ||
33 !element.ownerDocument ||
34 !element.ownerDocument.defaultView
35 ) {
36 return window;
37 }
38 return element.ownerDocument.defaultView;
39}
40
41export function getElementDocument(element) {
42 if (!element || !element.ownerDocument) {
43 return document;
44 }
45 return element.ownerDocument;
46}