Skip to content

Commit

Permalink
refactor: move short circuit util to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
platypusrex committed Jun 11, 2022
1 parent 8b0afca commit fb9d1e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
25 changes: 2 additions & 23 deletions src/utils/getResultsFromFnsList.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
import { orange } from './cliColors';
import { shouldShortCircuit } from './shouldShortCircuit';
import type {
AnyObject,
Context,
MergePropsOptions,
MergePropsOptionsSequential,
PropsResult,
ShortCircuitType,
} from '../types';
import { orange } from './cliColors';

const shouldShortCircuit = (
result: PropsResult,
shortCircuit?: ShortCircuitType
) => {
if (!shortCircuit || shortCircuit === 'never') {
return false;
}

const hasRedirect: boolean = 'redirect' in result && !!result?.redirect;
const hasNotFound: boolean = 'notFound' in result && !!result?.notFound;

switch (shortCircuit) {
case 'redirect-and-notfound':
return hasRedirect || hasNotFound;
case 'redirect-only':
return hasRedirect;
case 'notfound-only':
return hasNotFound;
}
};

export const getResultsFromFnsList = async <P = AnyObject>(
ctx: Context,
Expand Down
22 changes: 22 additions & 0 deletions src/utils/shouldShortCircuit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { PropsResult, ShortCircuitType } from '../types';

export const shouldShortCircuit = (
result: PropsResult,
shortCircuit?: ShortCircuitType
) => {
if (!shortCircuit || shortCircuit === 'never') {
return false;
}

const hasRedirect: boolean = 'redirect' in result && !!result?.redirect;
const hasNotFound: boolean = 'notFound' in result && !!result?.notFound;

switch (shortCircuit) {
case 'redirect-and-notfound':
return hasRedirect || hasNotFound;
case 'redirect-only':
return hasRedirect;
case 'notfound-only':
return hasNotFound;
}
};

0 comments on commit fb9d1e0

Please sign in to comment.