Skip to content

series

Create an ordered series object

637 bytes
since v12.1.0

Usage

The series function allows you to work with ordered values from an enum or union type, such as a status, by returning an object for performing ordered logic.

import * as _ from 'radashi'
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'

Complex Data Types

For objects, pass a second argument to series: a function that converts non-primitive values into an identity that can be checked for equality.

import * as _ from 'radashi'
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' }