| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ViteHotContext} from 'vite/types/hot'; |
| b69ab31 | | | 9 | import type {CommitInfo, Disposable, Hash} from './types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | export function firstOfIterable<T>(it: IterableIterator<T>): T | undefined { |
| b69ab31 | | | 12 | return it.next().value; |
| b69ab31 | | | 13 | } |
| b69ab31 | | | 14 | |
| b69ab31 | | | 15 | /** Get the short 12-character hash from a full hash. */ |
| b69ab31 | | | 16 | export function short(hash: Hash): string { |
| b69ab31 | | | 17 | return hash.slice(0, 12); |
| b69ab31 | | | 18 | } |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | export function assert(shouldBeTrue: boolean, error: string): asserts shouldBeTrue { |
| b69ab31 | | | 21 | if (!shouldBeTrue) { |
| b69ab31 | | | 22 | throw new Error(error); |
| b69ab31 | | | 23 | } |
| b69ab31 | | | 24 | } |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | export function arraysEqual<T>(a: Array<T>, b: Array<T>): boolean { |
| b69ab31 | | | 27 | if (a.length !== b.length) { |
| b69ab31 | | | 28 | return false; |
| b69ab31 | | | 29 | } |
| b69ab31 | | | 30 | return a.every((val, i) => b[i] === val); |
| b69ab31 | | | 31 | } |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | export type NonNullReactElement = React.ReactElement | React.ReactFragment; |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | export function getWindowWidthInPixels(): number { |
| b69ab31 | | | 36 | if (isTest) { |
| b69ab31 | | | 37 | return 1000; |
| b69ab31 | | | 38 | } |
| b69ab31 | | | 39 | // Use client width and not screen width to handle embedding as an iframe. |
| b69ab31 | | | 40 | return document.body.clientWidth; |
| b69ab31 | | | 41 | } |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | export function leftPad(val: string | number, len: number, char: string) { |
| b69ab31 | | | 44 | const str = val.toString(); |
| b69ab31 | | | 45 | return `${Array(len - str.length + 1).join(char)}${str}`; |
| b69ab31 | | | 46 | } |
| b69ab31 | | | 47 | |
| b69ab31 | | | 48 | /** Whether running in a test environment. */ |
| b69ab31 | | | 49 | export const isTest = typeof process !== 'undefined' && process.env.NODE_ENV === 'test'; |
| b69ab31 | | | 50 | |
| b69ab31 | | | 51 | export const isDev = process.env.NODE_ENV === 'development'; |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | const cleanUpRegister = new FinalizationRegistry<() => void>((cleanUp: () => void) => { |
| b69ab31 | | | 54 | cleanUp(); |
| b69ab31 | | | 55 | }); |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | /** |
| b69ab31 | | | 58 | * Register a clean up callback or a disposable when `obj` is GC-ed. |
| b69ab31 | | | 59 | * |
| b69ab31 | | | 60 | * If `hot` is set (`import.meta.hot`), the `cleanUp` is registered with the |
| b69ab31 | | | 61 | * hot reload API instead. Note the `import.meta` depends on where it lives. |
| b69ab31 | | | 62 | * So we cannot use `import.meta` here (which will affect this `utils.ts` hot |
| b69ab31 | | | 63 | * reloading behavior, not the callsite module). |
| b69ab31 | | | 64 | */ |
| b69ab31 | | | 65 | export function registerCleanup(obj: object, cleanUp: () => void, hot?: ViteHotContext): void { |
| b69ab31 | | | 66 | if (hot != null) { |
| b69ab31 | | | 67 | hot.dispose(() => { |
| b69ab31 | | | 68 | cleanUp(); |
| b69ab31 | | | 69 | }); |
| b69ab31 | | | 70 | } else { |
| b69ab31 | | | 71 | cleanUpRegister.register(obj, cleanUp); |
| b69ab31 | | | 72 | } |
| b69ab31 | | | 73 | } |
| b69ab31 | | | 74 | |
| b69ab31 | | | 75 | /** Similar to `registerCleanup`, but takes a `Disposable` */ |
| b69ab31 | | | 76 | export function registerDisposable( |
| b69ab31 | | | 77 | obj: object, |
| b69ab31 | | | 78 | disposable: Disposable, |
| b69ab31 | | | 79 | hot?: ViteHotContext, |
| b69ab31 | | | 80 | ): void { |
| b69ab31 | | | 81 | registerCleanup(obj, () => disposable.dispose(), hot); |
| b69ab31 | | | 82 | } |
| b69ab31 | | | 83 | |
| b69ab31 | | | 84 | /** Calculate hour difference between two dates (date1 - date2) */ |
| b69ab31 | | | 85 | export function calculateHourDifference(date1: Date, date2: Date): number { |
| b69ab31 | | | 86 | const msDifference = date1.getTime() - date2.getTime(); |
| b69ab31 | | | 87 | const hoursDifference = msDifference / (1000 * 60 * 60); |
| b69ab31 | | | 88 | return hoursDifference; |
| b69ab31 | | | 89 | } |
| b69ab31 | | | 90 | |
| b69ab31 | | | 91 | /** |
| b69ab31 | | | 92 | * Check if a commit is on the master branch |
| b69ab31 | | | 93 | */ |
| b69ab31 | | | 94 | export function isCommitMaster(commit: CommitInfo): boolean { |
| b69ab31 | | | 95 | if (commit.remoteBookmarks == null) { |
| b69ab31 | | | 96 | return false; |
| b69ab31 | | | 97 | } |
| b69ab31 | | | 98 | return commit.remoteBookmarks.some(bookmark => bookmark.includes('master')); |
| b69ab31 | | | 99 | } |