forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
225 lines (202 loc) · 5.47 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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
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: 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: number;
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): any {
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 5 to 6 character string.
*
* @export
* @returns {string}
*/
export function createRandomString(): string {
return getRandomValue().toString(36).substring(1);
}
/**
* Generates a 20 character 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 15.
const r = (getRandomValue() % 16);
// 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 {
const 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 async function isSavedInternal(hpm: HttpPostMessage, uid: string, contentWindow: Window): Promise<boolean> {
try {
const response = await hpm.get<boolean>('/report/hasUnsavedChanges', { uid: uid }, contentWindow);
return !response.body;
} catch (response) {
throw response.body;
}
}
/**
* Checks if the embed url is for RDL report.
*
* @export
* @param {string} embedUrl
* @returns {boolean}
*/
export function isRDLEmbed(embedUrl: string): boolean {
return embedUrl && embedUrl.toLowerCase().indexOf("/rdlembed?") >= 0;
}
/**
* Checks if the embed url contains autoAuth=true.
*
* @export
* @param {string} embedUrl
* @returns {boolean}
*/
export function autoAuthInEmbedUrl(embedUrl: string): boolean {
return embedUrl && decodeURIComponent(embedUrl).toLowerCase().indexOf("autoauth=true") >= 0;
}
/**
* Returns random number
*/
export function getRandomValue(): number {
// window.msCrypto for IE
const cryptoObj = window.crypto || window.msCrypto;
const randomValueArray = new Uint32Array(1);
cryptoObj.getRandomValues(randomValueArray);
return randomValueArray[0];
}
/**
* Returns the time interval between two dates in milliseconds
*
* @export
* @param {Date} start
* @param {Date} end
* @returns {number}
*/
export function getTimeDiffInMilliseconds(start: Date, end: Date): number {
return Math.abs(start.getTime() - end.getTime());
}
/**
* Checks if the embed type is for create
*
* @export
* @param {string} embedType
* @returns {boolean}
*/
export function isCreate(embedType: string): boolean {
return embedType === 'create' || embedType === 'quickcreate';
}