Skip to content

Commit

Permalink
Make state parsing robust against more types
Browse files Browse the repository at this point in the history
The Flint API sometimes returns numeric HTTP statuses as well, for reasons not entirely understood to the commit author. Making the method more robust against any type simplifies using the interface, and checking for null results isn't any more annoying than it was before.

Signed-off-by: Simeon Widdis <[email protected]>
  • Loading branch information
Swiddis committed Jul 11, 2024
1 parent aeab4dd commit b70654c
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ export enum JobState {
}

/**
* Convert a string to a {@link JobState} if possible. Case-insensitive.
* Convert a string to a {@link JobState} if possible. Case-insensitive. Returns `null` for
* invalid strings or non-strings.
*
* @param maybeState An optional string.
* @returns The corresponding {@link JobState} if one exists, otherwise undefined.
* @returns The corresponding {@link JobState} if one exists, otherwise null.
*/
export const parseJobState = (maybeState: string | undefined): JobState | undefined => {
if (!maybeState) {
return undefined;
export const parseJobState = (maybeState: unknown): JobState | null => {
if (!maybeState || typeof maybeState !== 'string') {
return null;
}
maybeState = maybeState.toUpperCase();
return Object.values(JobState).find((state) => state === maybeState);
const result = Object.values(JobState).find((state) => state === maybeState);
return result ?? null;
};

export interface AsyncQueryContext {
Expand Down

0 comments on commit b70654c

Please sign in to comment.