Skip to content

Commit

Permalink
Preserve Array.modify and Array.modifyOption non emptiness (Effec…
Browse files Browse the repository at this point in the history
  • Loading branch information
vinassefranche authored and dilame committed Aug 22, 2024
1 parent 6a4873e commit 0229c2d
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-cats-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

preserve `Array.modify` `Array.modifyOption` non emptiness
114 changes: 114 additions & 0 deletions packages/effect/dtslint/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,9 @@ pipe(nonEmptyNumbers, Array.replace(0, "a" as const))
// $ExpectType ("a" | 1 | 2)[]
pipe(new Set([1, 2] as const), Array.replace(0, "a" as const))

// $ExpectType [number | "a", ...(number | "a")[]]
pipe(Array.of(1), Array.replace(0, "a" as const))

// -------------------------------------------------------------------------------------
// replaceOption
// -------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1390,3 +1393,114 @@ pipe(nonEmptyNumbers, Array.replaceOption(0, "a" as const))

// $ExpectType Option<("a" | 1 | 2)[]>
pipe(new Set([1, 2] as const), Array.replaceOption(0, "a" as const))

// -------------------------------------------------------------------------------------
// modify
// -------------------------------------------------------------------------------------

// $ExpectType string[]
Array.modify([], 0, (
_n // $ExpectType never
) => "a")
// $ExpectType (string | number)[]
Array.modify(numbers, 0, (
_n // $ExpectType number
) => "a")

// $ExpectType [number | "a", ...(number | "a")[]]
Array.modify(nonEmptyNumbers, 0, (
_n // $ExpectType number
) => "a" as const)

// $ExpectType ("a" | 1 | 2)[]
Array.modify(new Set([1, 2] as const), 0, (
_n // $ExpectType 1 | 2
) => "a" as const)

// $ExpectType string[]
pipe(
[],
Array.modify(0, (
_n // $ExpectType never
) => "a")
)

// $ExpectType (string | number)[]
pipe(
numbers,
Array.modify(0, (
_n // $ExpectType number
) => "a")
)

// $ExpectType [number | "a", ...(number | "a")[]]
pipe(
nonEmptyNumbers,
Array.modify(0, (
_n // $ExpectType number
) => "a" as const)
)

// $ExpectType ("a" | 1 | 2)[]
pipe(
new Set([1, 2] as const),
Array.modify(0, (
_n // $ExpectType 1 | 2
) => "a" as const)
)

// -------------------------------------------------------------------------------------
// modifyOption
// -------------------------------------------------------------------------------------

// $ExpectType Option<string[]>
Array.modifyOption([], 0, (
_n // $ExpectType never
) => "a")

// $ExpectType Option<(string | number)[]>
Array.modifyOption(numbers, 0, (
_n // $ExpectType number
) => "a")

// $ExpectType Option<[number | "a", ...(number | "a")[]]>
Array.modifyOption(nonEmptyNumbers, 0, (
_n // $ExpectType number
) => "a" as const)

// $ExpectType Option<("a" | 1 | 2)[]>
Array.modifyOption(new Set([1, 2] as const), 0, (
_n // $ExpectType 1 | 2
) => "a" as const)

// $ExpectType Option<string[]>
pipe(
[],
Array.modifyOption(0, (
_n // $ExpectType never
) => "a")
)

// $ExpectType Option<(string | number)[]>
pipe(
numbers,
Array.modifyOption(0, (
_n // $ExpectType number
) => "a")
)

// $ExpectType Option<[number | "a", ...(number | "a")[]]>
pipe(
nonEmptyNumbers,
Array.modifyOption(0, (
_n // $ExpectType number
) => "a" as const)
)

// $ExpectType Option<("a" | 1 | 2)[]>
pipe(
new Set([1, 2] as const),
Array.modifyOption(0, (
_n // $ExpectType 1 | 2
) => "a" as const)
)
22 changes: 18 additions & 4 deletions packages/effect/src/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1109,8 +1109,15 @@ export const replaceOption: {
* @since 2.0.0
*/
export const modify: {
<A, B>(i: number, f: (a: A) => B): (self: Iterable<A>) => Array<A | B>
<A, B>(self: Iterable<A>, i: number, f: (a: A) => B): Array<A | B>
<A, B, S extends Iterable<A> = Iterable<A>>(
i: number,
f: (a: ReadonlyArray.Infer<S>) => B
): (self: S) => ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>
<A, B, S extends Iterable<A> = Iterable<A>>(
self: S,
i: number,
f: (a: ReadonlyArray.Infer<S>) => B
): ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>
} = dual(
3,
<A, B>(self: Iterable<A>, i: number, f: (a: A) => B): Array<A | B> =>
Expand All @@ -1134,8 +1141,15 @@ export const modify: {
* @since 2.0.0
*/
export const modifyOption: {
<A, B>(i: number, f: (a: A) => B): (self: Iterable<A>) => Option<Array<A | B>>
<A, B>(self: Iterable<A>, i: number, f: (a: A) => B): Option<Array<A | B>>
<A, B, S extends Iterable<A> = Iterable<A>>(
i: number,
f: (a: ReadonlyArray.Infer<S>) => B
): (self: S) => Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>
<A, B, S extends Iterable<A> = Iterable<A>>(
self: S,
i: number,
f: (a: ReadonlyArray.Infer<S>) => B
): Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>
} = dual(3, <A, B>(self: Iterable<A>, i: number, f: (a: A) => B): Option<Array<A | B>> => {
const out = Array.from(self)
if (isOutOfBound(i, out)) {
Expand Down

0 comments on commit 0229c2d

Please sign in to comment.