Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Chunk.toArray and Chunk.toReadonlyArray. Improved functio… #2970

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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