-
Notifications
You must be signed in to change notification settings - Fork 352
refactor(utils): migrate utility helpers from Flow to TypeScript #4843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mergify
merged 1 commit into
box:master
from
bonchevskyi:refactor/flow-to-ts-utils-helpers
Sep 21, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions
2
src/utils/__tests__/validators.test.js → src/utils/__tests__/validators.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /** | ||
| * Polyfills the .closest method for Element | ||
| * Currently being used for scrollIntoView | ||
| * For reference: https://github.com/zloirock/core-js/issues/317 | ||
| */ | ||
| window.Element.prototype.closest = function closest(this: Element, s: string): Element | null { | ||
| const elementProto = window.Element.prototype as Element & { | ||
| msMatchesSelector?: (selector: string) => boolean; | ||
| webkitMatchesSelector?: (selector: string) => boolean; | ||
| }; | ||
| if (!window.Element.prototype.matches) { | ||
| window.Element.prototype.matches = (elementProto.msMatchesSelector || | ||
| elementProto.webkitMatchesSelector) as typeof Element.prototype.matches; | ||
| } | ||
|
|
||
| if (this.matches(s)) { | ||
| return this; | ||
| } | ||
|
|
||
| let el: Element | Node | null = this.parentElement || this.parentNode; | ||
| while (el !== null && el.nodeType === 1) { | ||
| const element = el as Element; | ||
| if (element.matches(s)) { | ||
| return element; | ||
| } | ||
| el = element.parentElement || element.parentNode; | ||
| } | ||
|
|
||
| return null; | ||
| }; |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { | ||
| HTTP_STATUS_CODE_CONFLICT, | ||
| HTTP_STATUS_CODE_UNAUTHORIZED, | ||
| HTTP_STATUS_CODE_RATE_LIMIT, | ||
| HTTP_STATUS_CODE_INTERNAL_SERVER_ERROR, | ||
| } from '../constants'; | ||
|
|
||
| const getBadItemError = (): Error => new Error('Bad box item!'); | ||
|
|
||
| const getBadPermissionsError = (): Error => new Error('Insufficient Permissions!'); | ||
|
|
||
| const getBadUserError = (): Error => new Error('Bad box user!'); | ||
|
|
||
| const getMissingItemTextOrStatus = (): Error => new Error('Missing text or status!'); | ||
|
|
||
| const isUserCorrectableError = (status: number): boolean => | ||
| status === HTTP_STATUS_CODE_RATE_LIMIT || | ||
| status === HTTP_STATUS_CODE_UNAUTHORIZED || | ||
| status === HTTP_STATUS_CODE_CONFLICT || | ||
| status >= HTTP_STATUS_CODE_INTERNAL_SERVER_ERROR; | ||
|
|
||
| const getAbortError = (): Error => { | ||
| class AbortError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = 'AbortError'; | ||
| } | ||
| } | ||
|
|
||
| return new AbortError('Aborted'); | ||
| }; | ||
|
|
||
| export { | ||
| getAbortError, | ||
| getBadItemError, | ||
| getBadPermissionsError, | ||
| getBadUserError, | ||
| getMissingItemTextOrStatus, | ||
| isUserCorrectableError, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import getProp from 'lodash/get'; | ||
| import { | ||
| TYPED_ID_FILE_PREFIX, | ||
| TYPED_ID_FOLDER_PREFIX, | ||
| FILE_EXTENSION_BOX_CANVAS, | ||
| FILE_EXTENSION_BOX_NOTE, | ||
| FILE_EXTENSION_GOOGLE_DOC, | ||
| FILE_EXTENSION_GOOGLE_SHEET, | ||
| FILE_EXTENSION_GOOGLE_SLIDE, | ||
| FILE_EXTENSION_GOOGLE_SLIDE_LEGACY, | ||
| } from '../constants'; | ||
| import type { BoxItem } from '../common/types/core'; | ||
|
|
||
| const FILE_EXT_REGEX = /\.([0-9a-z]+)$/i; // Case insensitive regex to extract file extension without "." | ||
|
|
||
| /** | ||
| * Returns typed id for file. Useful for when | ||
| * making file based XHRs where auth token | ||
| * can be per file as used by Preview. | ||
| */ | ||
| export const getTypedFileId = (id: string): string => `${TYPED_ID_FILE_PREFIX}${id}`; | ||
|
|
||
| /** Returns typed id for folder. */ | ||
| export const getTypedFolderId = (id: string): string => `${TYPED_ID_FOLDER_PREFIX}${id}`; | ||
|
|
||
| /** Determines if the file is a box note. */ | ||
| export const isBoxNote = (file: BoxItem): boolean => file.extension === FILE_EXTENSION_BOX_NOTE; | ||
|
|
||
| /** Determines if the file is box canvas. */ | ||
| export const isBoxCanvas = (file: BoxItem): boolean => file.extension === FILE_EXTENSION_BOX_CANVAS; | ||
|
|
||
| /** Determines whether a file extension is associated with a G Suite file. */ | ||
| export const isGSuiteExtension = (extension: string): boolean => | ||
| extension === FILE_EXTENSION_GOOGLE_DOC || | ||
| extension === FILE_EXTENSION_GOOGLE_SHEET || | ||
| extension === FILE_EXTENSION_GOOGLE_SLIDE || | ||
| extension === FILE_EXTENSION_GOOGLE_SLIDE_LEGACY; | ||
|
|
||
| /** Returns the extension from the file name. */ | ||
| export const getFileExtension = (filename?: string): string => { | ||
| if (typeof filename !== 'string') { | ||
| return ''; | ||
| } | ||
|
|
||
| const result = FILE_EXT_REGEX.exec(filename); | ||
| return getProp(result, '[1]', ''); | ||
| }; |
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.