All files / src/react utils.ts

76.67% Statements 23/30
47.06% Branches 8/17
50% Functions 1/2
75% Lines 21/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 761x       1x                               4x 4x   4x     4x                 4x       4x     4x   4x         4x     4x   4x     4x     4x     4x   4x   4x   4x   4x     1x  
import { ControllerMap } from './global'
import { Controller } from './index'
import { NormalizedUserTapableOptions, UserTapableOptions } from './types'
 
const ModeTypeMap: Record<string, 'sync' | 'async' | 'promise'> = {
    tap: 'sync',
    tapAsync: 'async',
    tapPromise: 'promise'
}
 
function throwError(msg: string): never {
    throw new Error(`react-tapable: ${msg}`)
}
 
function getNormalizedTapableOptions<T>(
    selector: string,
    rawOptions: UserTapableOptions<T>,
    rawFn: (...args: any[]) => any,
    rawUseAliasArr?: any[]
): NormalizedUserTapableOptions {
    const options = {} as NormalizedUserTapableOptions
    const controller = ControllerMap.get(selector) as Controller<T>
 
    Iif (typeof rawOptions.mode !== 'string') {
        throwError(`useTapable rawOptions' mode must be string`)
    } else {
        Iif (
            rawOptions.mode !== 'tap' &&
            rawOptions.mode !== 'tapAsync' &&
            rawOptions.mode !== 'tapPromise'
        ) {
            throwError(
                `useTapable rawOptions' mode must be tap/tapAsync/tapPromise, but get ${rawOptions.mode}`
            )
        } else {
            options.mode = rawOptions.mode
        }
    }
 
    Iif (typeof rawOptions.hook !== 'string') {
        throwError(`useTapable rawOptions' hook must be string, but get ${typeof rawOptions.hook}`)
    } else {
        const hook = controller.getHook(rawOptions.hook)
 
        Iif (!hook) {
            throwError(
                `useTapable please check to see if the controller has this hook ${rawOptions.hook}`
            )
        }
        options.hook = hook
    }
 
    options.type = ModeTypeMap[options.mode]
 
    Iif (!(rawFn instanceof Function)) {
        throwError(`useTapable rawOptions' fn must be function`)
    } else {
        Iif (options.hook._isRegistered(options.type, rawFn)) {
            throwError(`useTapable rawOptions' fn has been registered`)
        }
        options.fn = rawFn
    }
 
    options.useAliasArr = rawUseAliasArr || []
 
    options.context = Boolean(rawOptions.context)
 
    options.name = controller.getHookTapName(rawOptions.hook)
 
    options.once = Boolean(rawOptions.once)
 
    return options
}
 
export { getNormalizedTapableOptions, throwError, ModeTypeMap }