Skip to content

Commit

Permalink
style(lib): use optional type in places where it makes sense semantic…
Browse files Browse the repository at this point in the history
…ally (#48)
  • Loading branch information
TomerAberbach authored Nov 27, 2024
1 parent 01b22ce commit eb529c7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
12 changes: 6 additions & 6 deletions src/operations/splices.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ type SubConcur = {
* @category Splices
* @since v0.0.1
*/
export const first: <Value>(iterable: Iterable<Value>) => Iterable<Value>
export const first: <Value>(iterable: Iterable<Value>) => Optional<Value>

/**
* Returns an async iterable containing the first value of `asyncIterable`, or
Expand All @@ -401,7 +401,7 @@ export const first: <Value>(iterable: Iterable<Value>) => Iterable<Value>
*/
export const firstAsync: <Value>(
asyncIterable: AsyncIterable<Value>,
) => AsyncIterable<Value>
) => AsyncOptional<Value>

/**
* Returns a concur iterable containing the first value of `concurIterable`, or
Expand All @@ -424,7 +424,7 @@ export const firstAsync: <Value>(
*/
export const firstConcur: <Value>(
concurIterable: ConcurIterable<Value>,
) => ConcurIterable<Value>
) => ConcurOptional<Value>

/**
* Returns an iterable containing the last value of `iterable`, or an empty
Expand All @@ -445,7 +445,7 @@ export const firstConcur: <Value>(
* @category Splices
* @since v0.0.1
*/
export const last: <Value>(iterable: Iterable<Value>) => Iterable<Value>
export const last: <Value>(iterable: Iterable<Value>) => Optional<Value>

/**
* Returns an async iterable containing the last value of `asyncIterable`, or
Expand All @@ -468,7 +468,7 @@ export const last: <Value>(iterable: Iterable<Value>) => Iterable<Value>
*/
export const lastAsync: <Value>(
asyncIterable: AsyncIterable<Value>,
) => AsyncIterable<Value>
) => AsyncOptional<Value>

/**
* Returns a concur iterable containing the last value of `concurIterable`, or
Expand All @@ -491,7 +491,7 @@ export const lastAsync: <Value>(
*/
export const lastConcur: <Value>(
concurIterable: ConcurIterable<Value>,
) => ConcurIterable<Value>
) => ConcurOptional<Value>

/**
* Returns an iterable containing the values of `iterable` between `start` and
Expand Down
67 changes: 34 additions & 33 deletions src/operations/statistics.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { MaybePromiseLike } from '../internal/types.js'
import type { ConcurIterable } from './core.js'
import type { AsyncOptional, ConcurOptional, Optional } from './optionals.js'
import type {
AsyncOptionalReducer,
OptionalReducer,
Expand Down Expand Up @@ -281,8 +282,8 @@ type ToMinOrMaxBy = <Value>(fn: Compare<Value>) => OptionalReducer<Value>

/** @internal */
type MinOrMaxBy = {
<Value>(fn: Compare<Value>, iterable: Iterable<Value>): Iterable<Value>
<Value>(fn: Compare<Value>): (iterable: Iterable<Value>) => Iterable<Value>
<Value>(fn: Compare<Value>, iterable: Iterable<Value>): Optional<Value>
<Value>(fn: Compare<Value>): (iterable: Iterable<Value>) => Optional<Value>
}

/** @internal */
Expand All @@ -295,21 +296,21 @@ type MinOrMaxByAsync = {
<Value>(
fn: AsyncCompare<Value>,
asyncIterable: AsyncIterable<Value>,
): AsyncIterable<Value>
): AsyncOptional<Value>
<Value>(
fn: AsyncCompare<Value>,
): (asyncIterable: AsyncIterable<Value>) => AsyncIterable<Value>
): (asyncIterable: AsyncIterable<Value>) => AsyncOptional<Value>
}

/** @internal */
type MinOrMaxByConcur = {
<Value>(
fn: AsyncCompare<Value>,
concurIterable: ConcurIterable<Value>,
): ConcurIterable<Value>
): ConcurOptional<Value>
<Value>(
fn: AsyncCompare<Value>,
): (concurIterable: ConcurIterable<Value>) => ConcurIterable<Value>
): (concurIterable: ConcurIterable<Value>) => ConcurOptional<Value>
}

/**
Expand Down Expand Up @@ -569,10 +570,10 @@ export const minMaxBy: {
<Value>(
fn: Compare<Value>,
iterable: Iterable<Value>,
): Iterable<MinMax<Value>>
): Optional<MinMax<Value>>
<Value>(
fn: Compare<Value>,
): (iterable: Iterable<Value>) => Iterable<MinMax<Value>>
): (iterable: Iterable<Value>) => Optional<MinMax<Value>>
}

/**
Expand Down Expand Up @@ -614,10 +615,10 @@ export const minMaxByAsync: {
<Value>(
fn: AsyncCompare<Value>,
asyncIterable: AsyncIterable<Value>,
): AsyncIterable<MinMax<Value>>
): AsyncOptional<MinMax<Value>>
<Value>(
fn: AsyncCompare<Value>,
): (asyncIterable: AsyncIterable<Value>) => AsyncIterable<MinMax<Value>>
): (asyncIterable: AsyncIterable<Value>) => AsyncOptional<MinMax<Value>>
}

/**
Expand Down Expand Up @@ -645,10 +646,10 @@ export const minMaxByConcur: {
<Value>(
fn: (left: Value, right: Value) => MaybePromiseLike<number>,
concurIterable: ConcurIterable<Value>,
): ConcurIterable<MinMax<Value>>
): ConcurOptional<MinMax<Value>>
<Value>(
fn: (left: Value, right: Value) => MaybePromiseLike<number>,
): (concurIterable: ConcurIterable<Value>) => ConcurIterable<MinMax<Value>>
): (concurIterable: ConcurIterable<Value>) => ConcurOptional<MinMax<Value>>
}

/** @internal */
Expand All @@ -661,10 +662,10 @@ type MinOrMaxWith = {
<Value>(
fn: (value: Value) => number,
iterable: Iterable<Value>,
): Iterable<Value>
): Optional<Value>
<Value>(
fn: (value: Value) => number,
): (iterable: Iterable<Value>) => Iterable<Value>
): (iterable: Iterable<Value>) => Optional<Value>
}

/** @internal */
Expand All @@ -677,21 +678,21 @@ type MinOrMaxWithAsync = {
<Value>(
fn: (value: Value) => MaybePromiseLike<number>,
asyncIterable: AsyncIterable<Value>,
): AsyncIterable<Value>
): AsyncOptional<Value>
<Value>(
fn: (value: Value) => MaybePromiseLike<number>,
): (asyncIterable: AsyncIterable<Value>) => AsyncIterable<Value>
): (asyncIterable: AsyncIterable<Value>) => AsyncOptional<Value>
}

/** @internal */
type MinOrMaxWithConcur = {
<Value>(
fn: (value: Value) => MaybePromiseLike<number>,
concurIterable: ConcurIterable<Value>,
): ConcurIterable<Value>
): ConcurOptional<Value>
<Value>(
fn: (value: Value) => MaybePromiseLike<number>,
): (concurIterable: ConcurIterable<Value>) => ConcurIterable<Value>
): (concurIterable: ConcurIterable<Value>) => ConcurOptional<Value>
}

/**
Expand Down Expand Up @@ -958,10 +959,10 @@ export const minMaxWith: {
<Value>(
fn: (value: Value) => number,
iterable: Iterable<Value>,
): Iterable<MinMax<Value>>
): Optional<MinMax<Value>>
<Value>(
fn: (value: Value) => number,
): (iterable: Iterable<Value>) => Iterable<MinMax<Value>>
): (iterable: Iterable<Value>) => Optional<MinMax<Value>>
}

/**
Expand Down Expand Up @@ -1004,10 +1005,10 @@ export const minMaxWithAsync: {
<Value>(
fn: (value: Value) => MaybePromiseLike<number>,
asyncIterable: AsyncIterable<Value>,
): AsyncIterable<MinMax<Value>>
): AsyncOptional<MinMax<Value>>
<Value>(
fn: (value: Value) => MaybePromiseLike<number>,
): (asyncIterable: AsyncIterable<Value>) => AsyncIterable<MinMax<Value>>
): (asyncIterable: AsyncIterable<Value>) => AsyncOptional<MinMax<Value>>
}

/**
Expand Down Expand Up @@ -1035,10 +1036,10 @@ export const minMaxWithConcur: {
<Value>(
fn: (value: Value) => MaybePromiseLike<number>,
concurIterable: ConcurIterable<Value>,
): ConcurIterable<MinMax<Value>>
): ConcurOptional<MinMax<Value>>
<Value>(
fn: (value: Value) => MaybePromiseLike<number>,
): (concurIterable: ConcurIterable<Value>) => ConcurIterable<MinMax<Value>>
): (concurIterable: ConcurIterable<Value>) => ConcurOptional<MinMax<Value>>
}

/**
Expand Down Expand Up @@ -1077,7 +1078,7 @@ export const toMin: () => OptionalReducer<number>
* @category Statistics
* @since v0.0.1
*/
export const min: (iterable: Iterable<number>) => Iterable<number>
export const min: (iterable: Iterable<number>) => Optional<number>

/**
* Returns an async iterable containing a minimum value of `asyncIterable` if
Expand All @@ -1095,7 +1096,7 @@ export const min: (iterable: Iterable<number>) => Iterable<number>
*/
export const minAsync: (
asyncIterable: AsyncIterable<number>,
) => AsyncIterable<number>
) => AsyncOptional<number>

/**
* Returns a concur iterable containing a minimum value of `concurIterable` if
Expand All @@ -1113,7 +1114,7 @@ export const minAsync: (
*/
export const minConcur: (
concurIterable: ConcurIterable<number>,
) => ConcurIterable<number>
) => ConcurOptional<number>

/**
* Returns an optional reducer that finds the maximum value of the values it
Expand Down Expand Up @@ -1151,7 +1152,7 @@ export const toMax: () => OptionalReducer<number>
* @category Statistics
* @since v0.0.1
*/
export const max: (iterable: Iterable<number>) => Iterable<number>
export const max: (iterable: Iterable<number>) => Optional<number>

/**
* Returns an async iterable containing a maximum value of `asyncIterable` if
Expand All @@ -1169,7 +1170,7 @@ export const max: (iterable: Iterable<number>) => Iterable<number>
*/
export const maxAsync: (
asyncIterable: AsyncIterable<number>,
) => AsyncIterable<number>
) => AsyncOptional<number>

/**
* Returns a concur iterable containing a maximum value of `concurIterable` if
Expand All @@ -1187,7 +1188,7 @@ export const maxAsync: (
*/
export const maxConcur: (
concurIterable: ConcurIterable<number>,
) => ConcurIterable<number>
) => ConcurOptional<number>

/**
* Returns an optional reducer that finds the {@link MinMax} value of the values
Expand Down Expand Up @@ -1226,7 +1227,7 @@ export const toMinMax: () => OptionalReducer<MinMax<number>>
* @category Statistics
* @since v0.0.2
*/
export const minMax: (iterable: Iterable<number>) => Iterable<MinMax<number>>
export const minMax: (iterable: Iterable<number>) => Optional<MinMax<number>>

/**
* Returns an async iterable containing a {@link MinMax} value of
Expand All @@ -1244,7 +1245,7 @@ export const minMax: (iterable: Iterable<number>) => Iterable<MinMax<number>>
*/
export const minMaxAsync: (
asyncIterable: AsyncIterable<number>,
) => AsyncIterable<MinMax<number>>
) => AsyncOptional<MinMax<number>>

/**
* Returns a concur iterable containing a {@link MinMax} value of
Expand All @@ -1262,4 +1263,4 @@ export const minMaxAsync: (
*/
export const minMaxConcur: (
concurIterable: ConcurIterable<number>,
) => ConcurIterable<MinMax<number>>
) => ConcurOptional<MinMax<number>>

0 comments on commit eb529c7

Please sign in to comment.