forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
164 lines (149 loc) · 4.12 KB
/
util.ts
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
import { HttpPostMessage } from 'http-post-message';
/**
* Raises a custom event with event data on the specified HTML element.
*
* @export
* @param {HTMLElement} element
* @param {string} eventName
* @param {*} eventData
*/
export function raiseCustomEvent(element: HTMLElement, eventName: string, eventData: any): void {
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);
}
/**
* Finds the index of the first value in an array that matches the specified predicate.
*
* @export
* @template T
* @param {(x: T) => boolean} predicate
* @param {T[]} xs
* @returns {number}
*/
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;
}
/**
* Finds the first value in an array that matches the specified predicate.
*
* @export
* @template T
* @param {(x: T) => boolean} predicate
* @param {T[]} xs
* @returns {T}
*/
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
/**
* Copies the values of all enumerable properties from one or more source objects to a target object, and returns the target object.
*
* @export
* @param {any} args
* @returns
*/
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;
}
/**
* Generates a random 7 character string.
*
* @export
* @returns {string}
*/
export function createRandomString(): string {
return (Math.random() + 1).toString(36).substring(7);
}
/**
* Generates a 20 charachter uuid.
*
* @export
* @returns {string}
*/
export function generateUUID(): string {
let d = new Date().getTime();
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
d += performance.now();
}
return 'xxxxxxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
// Generate a random number, scaled from 0 to 16.
// Bitwise-and by 15 since we only care about the last 4 bits.
const r = (d + Math.random() * 16) & 15 | 0;
// Shift 4 times to divide by 16
d >>= 4;
return r.toString(16);
});
}
/**
* Adds a parameter to the given url
*
* @export
* @param {string} url
* @param {string} paramName
* @param {string} value
* @returns {string}
*/
export function addParamToUrl(url: string, paramName: string, value: string): string {
let parameterPrefix = url.indexOf('?') > 0 ? '&' : '?';
url += parameterPrefix + paramName + '=' + value;
return url;
}
/**
* Checks if the report is saved.
*
* @export
* @param {HttpPostMessage} hpm
* @param {string} uid
* @param {Window} contentWindow
* @returns {Promise<boolean>}
*/
export function isSavedInternal(hpm: HttpPostMessage, uid: string, contentWindow: Window): Promise<boolean> {
return hpm.get<boolean>('/report/hasUnsavedChanges', { uid }, contentWindow)
.then(response => !response.body,
response => {
throw response.body;
});
}