All files / src/api BindStrategy.ts

100% Statements 7/7
100% Branches 8/8
100% Functions 2/2
100% Lines 7/7

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                                                  26x                         26x 3586x               26x 18x           10x   8x          
import { OptionalType, RequiredType } from "@jonloucks/contracts-ts/api/Types";
 
/**
 * Binding strategy. Enumish type.
 * <p>
 * Used to dictate how or if binding should happen when the Contract is already bound.
 * </p>
 */
export type BindStrategy =
    /**
     * Bind the new promisor to the given contract always or else throws an error.
    */
    "ALWAYS" |
    /**
     * Bind the new promisor to the given contract if not already bound.
    */
    "IF_NOT_BOUND" |
    /**
     * Bind the new promisor to the given contract only if replacement is allowed
    */
    "IF_ALLOWED";
 
/**
 * The default binding strategy
 */
export const DEFAULT_BIND_STRATEGY: BindStrategy = "IF_ALLOWED"
 
/**
 * Type alias for optional BindStrategy
 */
export type BindStrategyType = OptionalType<BindStrategy>;
 
/**
 * Resolve the given BindStrategy or return the default
 *
 * @param bindStrategy the bind strategy to resolve
 * @return the resolved bind strategy
 */ 
export function resolveBindStrategy(bindStrategy: OptionalType<BindStrategy>): RequiredType<BindStrategy> {
    return bindStrategy ?? DEFAULT_BIND_STRATEGY;
}
 
/**
 * Check if given value is a BindStrategy or null/undefined
 * @param instance the value to check
 * @returns true if value is a BindStrategy or null/undefined
 */
export function guard(instance: unknown): instance is OptionalType<BindStrategy> {
    switch (instance) {
        case undefined:
        case null:
        case "ALWAYS":
        case "IF_NOT_BOUND":
        case "IF_ALLOWED":
            return true;
        default:
            return false;
    }
}