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
import _
_
from 'radashi'
type
type Weekday = "monday" | "tuesday" | "wednesday" | "thursday" | "friday"
Weekday
= 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday'
const
const weekdays: _.Series<Weekday>
weekdays
=
import _
_
.
const series: <Weekday>(items: readonly Weekday[], toKey?: (item: Weekday) => string | symbol) => _.Series<Weekday>

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

@seehttps://radashi.js.org/reference/series/series

@example

const numbers = series([1, 2, 3])
numbers.first() // => 1
numbers.last() // => 3
numbers.next(2) // => 3
numbers.previous(2) // => 1
numbers.spin(2, 1) // => 3
numbers.spin(2, -1) // => 1

@version12.1.0

series
<
type Weekday = "monday" | "tuesday" | "wednesday" | "thursday" | "friday"
Weekday
>([
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
])
const
const a: Weekday
a
=
const weekdays: _.Series<Weekday>
weekdays
.
Series<Weekday>.min: (a: Weekday, b: Weekday) => Weekday
min
('tuesday', 'thursday') // => 'tuesday'
const
const b: Weekday
b
=
const weekdays: _.Series<Weekday>
weekdays
.
Series<Weekday>.max: (a: Weekday, b: Weekday) => Weekday
max
('wednesday', 'monday') // => 'wednesday'
const
const c: Weekday
c
=
const weekdays: _.Series<Weekday>
weekdays
.
Series<Weekday>.next: (current: Weekday, defaultValue?: Weekday | undefined) => Weekday
next
('wednesday') // => 'thursday'
const
const d: Weekday
d
=
const weekdays: _.Series<Weekday>
weekdays
.
Series<Weekday>.previous: (current: Weekday, defaultValue?: Weekday | undefined) => Weekday
previous
('tuesday') // => 'monday'
const
const e: Weekday
e
=
const weekdays: _.Series<Weekday>
weekdays
.
Series<Weekday>.first: () => Weekday
first
() // => 'monday'
const
const f: Weekday
f
=
const weekdays: _.Series<Weekday>
weekdays
.
Series<Weekday>.last: () => Weekday
last
() // => 'friday'
const
const g: Weekday
g
=
const weekdays: _.Series<Weekday>
weekdays
.
Series<Weekday>.next: (current: Weekday, defaultValue?: Weekday | undefined) => Weekday
next
('friday') // => null
const
const h: Weekday
h
=
const weekdays: _.Series<Weekday>
weekdays
.
Series<Weekday>.next: (current: Weekday, defaultValue?: Weekday | undefined) => Weekday
next
('friday',
const weekdays: _.Series<Weekday>
weekdays
.
Series<Weekday>.first: () => Weekday
first
()) // => 'monday'
const
const i: Weekday
i
=
const weekdays: _.Series<Weekday>
weekdays
.
Series<Weekday>.spin: (current: Weekday, num: number) => Weekday
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
import _
_
from 'radashi'
type
type Weekday = {
day: "monday" | "tuesday" | "wednesday" | "thursday" | "friday";
}
Weekday
= {
day: "monday" | "tuesday" | "wednesday" | "thursday" | "friday"
day
: 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday'
}
const
const weekdays: _.Series<Weekday>
weekdays
=
import _
_
.
const series: <Weekday>(items: readonly Weekday[], toKey?: (item: Weekday) => string | symbol) => _.Series<Weekday>

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

@seehttps://radashi.js.org/reference/series/series

@example

const numbers = series([1, 2, 3])
numbers.first() // => 1
numbers.last() // => 3
numbers.next(2) // => 3
numbers.previous(2) // => 1
numbers.spin(2, 1) // => 3
numbers.spin(2, -1) // => 1

@version12.1.0

series
<
type Weekday = {
day: "monday" | "tuesday" | "wednesday" | "thursday" | "friday";
}
Weekday
>(
[
{
day: "monday" | "tuesday" | "wednesday" | "thursday" | "friday"
day
: 'monday' },
{
day: "monday" | "tuesday" | "wednesday" | "thursday" | "friday"
day
: 'tuesday' },
{
day: "monday" | "tuesday" | "wednesday" | "thursday" | "friday"
day
: 'wednesday' },
{
day: "monday" | "tuesday" | "wednesday" | "thursday" | "friday"
day
: 'thursday' },
{
day: "monday" | "tuesday" | "wednesday" | "thursday" | "friday"
day
: 'friday' },
],
w: Weekday
w
=>
w: Weekday
w
.
day: "monday" | "tuesday" | "wednesday" | "thursday" | "friday"
day
,
)
const
const a: Weekday
a
=
const weekdays: _.Series<Weekday>
weekdays
.
Series<Weekday>.next: (current: Weekday, defaultValue?: Weekday | undefined) => Weekday
next
({
day: "monday" | "tuesday" | "wednesday" | "thursday" | "friday"
day
: 'wednesday' }) // => { day: 'thursday' }
const
const b: Weekday
b
=
const weekdays: _.Series<Weekday>
weekdays
.
Series<Weekday>.previous: (current: Weekday, defaultValue?: Weekday | undefined) => Weekday
previous
({
day: "monday" | "tuesday" | "wednesday" | "thursday" | "friday"
day
: 'tuesday' }) // => { day: 'monday' }