Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "public/match"

Index

References

Functions

References

arm

Renames and re-exports match

when

Renames and re-exports match

Functions

match

  • match<TIn, TInExt, TOut>(ifInstanceOf: Ctor<TInExt>, then: MapFnOrValue<TInExt, TOut>): MatchExecutor<TIn, TOut>
  • match<TIn, TInExt, TOut>(ifIsType: (val: TIn) => val is TInExt, then: MapFnOrValue<TInExt, TOut>): MatchExecutor<TIn, TOut>
  • match<TIn, TOut>(ifTrue: (val: TIn) => boolean, then: MapFnOrValue<TIn, TOut>): MatchExecutor<TIn, TOut>
  • match<TIn, TOut>(ifEquals: () => TIn, then: MapFnOrValue<TIn, TOut>): MatchExecutor<TIn, TOut>
  • match<TIn, TOut, TStructure>(ifMatches: TStructure, then: MapFnOrValue<TIn, TOut>): MatchExecutor<TIn, TOut>
  • description

    Match if val is an instance of a Constructor

    since

    1.0.0

    example
    import { match, strike, otherwise } from 'matchbook';
    
    // Get the level of Personal Protective Equipment for each career
    class Human { }
    class Nurse extends Human { }
    class ConstructionWorker extends Human { }
    class BombSquadTechnician extends Human { }
    
    const actual = strike(
        new Nurse(),
        match(Nurse, PpeLevel.Low),
        match(ConstructionWorker, PpeLevel.Medium),
        match(BombSquadTechnician, PpeLevel.High),
        otherwise(Ppe.None),
    );
    
    assertEq(actual, PpeLevel.Low);

    Type parameters

    • TIn

    • TInExt: TIn

    • TOut

    Parameters

    • ifInstanceOf: Ctor<TInExt>
    • then: MapFnOrValue<TInExt, TOut>

    Returns MatchExecutor<TIn, TOut>

  • description

    Match if val satisfies a Type Guard

    since

    1.0.0

    example
    import { match, strike, otherwise } from 'matchbook';
    
    const actual = strike<unknown, string>(
        4,
        match(
            n => typeof n === 'number',
            // NOTE: Even though we're matching on type unknown,
            //   since our type guard is satisfied,
            //   this delegate knows n is a number.
            n => `sqrt: ${Math.sqrt(n)}`
        ),
        otherwise('no clue')
    );
    
    assertEq(actual, 'sqrt: 2');

    Type parameters

    • TIn

    • TInExt: TIn

    • TOut

    Parameters

    • ifIsType: (val: TIn) => val is TInExt
        • (val: TIn): val is TInExt
        • Parameters

          • val: TIn

          Returns val is TInExt

    • then: MapFnOrValue<TInExt, TOut>

    Returns MatchExecutor<TIn, TOut>

  • description

    Match if val satisfies a Predicate

    since

    1.0.0

    example
    import { match, strike, otherwise } from 'matchbook';
    
    const actual = strike(
        '     ',
        match(s => s.trim() === '', `that's empty! nice try!`),
        otherwise('not empty!'),
    );
    
    assertEq(actual, `that's empty! outrageous!`);

    Type parameters

    • TIn

    • TOut

    Parameters

    • ifTrue: (val: TIn) => boolean
        • (val: TIn): boolean
        • Parameters

          • val: TIn

          Returns boolean

    • then: MapFnOrValue<TIn, TOut>

    Returns MatchExecutor<TIn, TOut>

  • description

    Match if val equals a lazy value

    since

    1.0.0

    example
    import { match, strike, otherwise } from 'matchbook';
    
    enum Emoji {
        Smiley = '😊',
        Sad = '😢',
        Eggplant = '🍆'
    }
    
    function getMostPopularEmoji() {
        return Emoji.Eggplant;
    }
    
    const actual = strike(
        '🍆',
        match(getMostPopularEmoji, 'very popular!'),
        otherwise('not popular...')
    );
    
    assertEq(actual, 'very popular!');

    Type parameters

    • TIn

    • TOut

    Parameters

    • ifEquals: () => TIn
        • (): TIn
        • Returns TIn

    • then: MapFnOrValue<TIn, TOut>

    Returns MatchExecutor<TIn, TOut>

  • description

    Match if val matches part of another

    since

    1.0.0

    example
    import { match, pattern, otherwise } from 'matchbook';
    
    const getGoodBirthdayGift = pattern(
        match({ hobby: Hobby.Golf }, 'golf cart'),
        match({ hobby: Hobby.Woodworking }, 'table saw'),
        match({ hobby: Hobby.Blacksmithing }, 'tongs'),
        otherwise('money')
    );
    
    const steve = {
        name: 'Steve',
        hobby: Hobby.Woodworking,
    };
    
    assertEq(getGoodBirthdayGift(steve), 'table saw');

    Type parameters

    • TIn

    • TOut

    • TStructure: Partial<TIn>

    Parameters

    • ifMatches: TStructure
    • then: MapFnOrValue<TIn, TOut>

    Returns MatchExecutor<TIn, TOut>

Generated using TypeDoc