Function series

  • Type Parameters

    • T

    Parameters

    • items: readonly T[]
    • toKey: (item: T) => string | symbol = ...

    Returns {
        first: () => T;
        last: () => T;
        max: (a: T, b: T) => T;
        min: (a: T, b: T) => T;
        next: (current: T, defaultValue?: T) => T;
        previous: (current: T, defaultValue?: T) => T;
        spin: (current: T, num: number) => T;
    }

    Creates a series object around a list of values that should be treated with order.

    type Weekday = 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday'

    const weekdays = series<Weekday>([
    'monday',
    'tuesday',
    'wednesday',
    'thursday',
    'friday'
    ])

    weekdays.min('tuesday', 'thursday') // => 'tuesday'
    weekdays.max('wednesday', 'monday') // => 'wednesday'
    weekdays.next('wednesday') // => 'thursday'
    weekdays.previous('tuesday') // => 'monday'
    weekdays.first() // => 'monday'
    weekdays.last() // => 'friday'
    weekdays.next('friday') // => null
    weekdays.next('friday', weekdays.first()) // => 'monday'
    weekdays.spin('monday', 3) // => 'thursday'

    -------------------------------------------
    type Weekday = {
    day: 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday'
    }

    const weekdays = series<Weekday>(
    [
    { day: 'monday' },
    { day: 'tuesday' },
    { day: 'wednesday' },
    { day: 'thursday' },
    { day: 'friday' }
    ],
    w => w.day
    )

    weekdays.next({ day: 'wednesday' }) // => { day: 'thursday' }
    weekdays.previous({ day: 'tuesday' }) // => { day: 'monday' }