-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathutil.ts
More file actions
50 lines (45 loc) · 1.31 KB
/
Copy pathutil.ts
File metadata and controls
50 lines (45 loc) · 1.31 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
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
export const endWithSlash = (s: string) => (s.endsWith('/') ? s : s + '/');
export const getRandomString = () =>
crypto.getRandomValues(new Uint32Array(1))[0].toString(32);
/**
* If the given URL object is a Skypack URL, perform an in-place update that
* switches from optimized mode to raw mode.
*
* See https://github.com/google/playground-elements/issues/107
*/
export const forceSkypackRawMode = (url: URL): URL => {
if (url.hostname === 'cdn.skypack.dev') {
url.pathname = url.pathname.replace(
/mode=imports\/(un)?optimized/,
'mode=raw'
);
}
return url;
};
export type Result<V, E> =
| {result: V; error?: undefined}
| {result?: undefined; error: E};
/**
* Re-fires an event on the given element, without bubbles or composed flags.
* This is useful for re-firing internal events on public component boundaries
* to avoid them crossing shadow DOM boundaries while still making them available
* to external consumers.
*/
export const refireEvent = (
element: EventTarget,
event: Event | CustomEvent
): void => {
const detail = (event as CustomEvent).detail;
element.dispatchEvent(
new CustomEvent(event.type, {
detail,
bubbles: false,
composed: false,
})
);
};