Skip to content

Commit

Permalink
Updated Chunk.toArray and Chunk.toReadonlyArray. Improved functio… (
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Jun 10, 2024
1 parent d31b874 commit 1aed347
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-seahorses-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Updated `Chunk.toArray` and `Chunk.toReadonlyArray`. Improved function signatures to preserve non-empty status of chunks during conversion.
20 changes: 20 additions & 0 deletions packages/effect/dtslint/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,23 @@ Chunk.reverse(nonEmptyNumbers)

// $ExpectType NonEmptyChunk<number>
pipe(nonEmptyNumbers, Chunk.reverse)

// -------------------------------------------------------------------------------------
// toArray
// -------------------------------------------------------------------------------------

// $ExpectType string[]
Chunk.toArray(hole<Chunk.Chunk<string>>())

// $ExpectType [string, ...string[]]
Chunk.toArray(hole<Chunk.NonEmptyChunk<string>>())

// -------------------------------------------------------------------------------------
// toArray
// -------------------------------------------------------------------------------------

// $ExpectType readonly string[]
Chunk.toReadonlyArray(hole<Chunk.Chunk<string>>())

// $ExpectType readonly [string, ...string[]]
Chunk.toReadonlyArray(hole<Chunk.NonEmptyChunk<string>>())
31 changes: 22 additions & 9 deletions packages/effect/src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,21 +279,21 @@ const copyToArray = <A>(self: Chunk<A>, array: Array<any>, initial: number): voi
}
}

/**
* Converts the specified `Chunk` to a `Array`.
*
* @category conversions
* @since 2.0.0
*/
export const toArray = <A>(self: Chunk<A>): Array<A> => toReadonlyArray(self).slice()
const toArray_ = <A>(self: Chunk<A>): Array<A> => toReadonlyArray(self).slice()

/**
* Converts the specified `Chunk` to a `ReadonlyArray`.
* Converts a `Chunk` into an `Array`. If the provided `Chunk` is non-empty
* (`NonEmptyChunk`), the function will return a `NonEmptyArray`, ensuring the
* non-empty property is preserved.
*
* @category conversions
* @since 2.0.0
*/
export const toReadonlyArray = <A>(self: Chunk<A>): ReadonlyArray<A> => {
export const toArray: <S extends Chunk<any>>(
self: S
) => S extends NonEmptyChunk<any> ? RA.NonEmptyArray<Chunk.Infer<S>> : Array<Chunk.Infer<S>> = toArray_ as any

const toReadonlyArray_ = <A>(self: Chunk<A>): ReadonlyArray<A> => {
switch (self.backing._tag) {
case "IEmpty": {
return emptyArray
Expand All @@ -316,6 +316,19 @@ export const toReadonlyArray = <A>(self: Chunk<A>): ReadonlyArray<A> => {
}
}

/**
* Converts a `Chunk` into a `ReadonlyArray`. If the provided `Chunk` is
* non-empty (`NonEmptyChunk`), the function will return a
* `NonEmptyReadonlyArray`, ensuring the non-empty property is preserved.
*
* @category conversions
* @since 2.0.0
*/
export const toReadonlyArray: <S extends Chunk<any>>(
self: S
) => S extends NonEmptyChunk<any> ? RA.NonEmptyReadonlyArray<Chunk.Infer<S>> : ReadonlyArray<Chunk.Infer<S>> =
toReadonlyArray_ as any

const reverseChunk = <A>(self: Chunk<A>): Chunk<A> => {
switch (self.backing._tag) {
case "IEmpty":
Expand Down

0 comments on commit 1aed347

Please sign in to comment.