forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
80 lines (69 loc) · 2.44 KB
/
util.ts
File metadata and controls
80 lines (69 loc) · 2.44 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
export function raiseCustomEvent(element: HTMLElement, eventName: string, eventData: any) {
let customEvent;
if (typeof CustomEvent === 'function') {
customEvent = new CustomEvent(eventName, {
detail: eventData,
bubbles: true,
cancelable: true
});
} else {
customEvent = document.createEvent('CustomEvent');
customEvent.initCustomEvent(eventName, true, true, eventData);
}
element.dispatchEvent(customEvent);
if (customEvent.defaultPrevented || !customEvent.returnValue) {
return;
}
// TODO: Remove this? Should be better way to handle events than using eval?
// What is use case? <div powerbi-type="report" onload="alert('loaded');"></div>
const inlineEventAttr = 'on' + eventName.replace('-', '');
const inlineScript = element.getAttribute(inlineEventAttr);
if (inlineScript) {
eval.call(element, inlineScript);
}
}
export function findIndex<T>(predicate: (x: T) => boolean, xs: T[]): number {
if (!Array.isArray(xs)) {
throw new Error(`You attempted to call find with second parameter that was not an array. You passed: ${xs}`);
}
let index;
xs.some((x, i) => {
if (predicate(x)) {
index = i;
return true;
}
});
return index;
}
export function find<T>(predicate: (x: T) => boolean, xs: T[]): T {
const index = findIndex(predicate, xs);
return xs[index];
}
export function remove<T>(predicate: (x: T) => boolean, xs: T[]): void {
const index = findIndex(predicate, xs);
xs.splice(index, 1);
}
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
// TODO: replace in favor of using polyfill
export function assign(...args) {
var target = args[0];
'use strict';
if (target === undefined || target === null) {
throw new TypeError('Cannot convert undefined or null to object');
}
var output = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source !== undefined && source !== null) {
for (var nextKey in source) {
if (source.hasOwnProperty(nextKey)) {
output[nextKey] = source[nextKey];
}
}
}
}
return output;
}
export function createRandomString(): string {
return (Math.random() + 1).toString(36).substring(7);
}