From 1aed347a125ed3847ec90863424810d6759cbc85 Mon Sep 17 00:00:00 2001 From: Giulio Canti Date: Mon, 10 Jun 2024 16:02:35 +0200 Subject: [PATCH] =?UTF-8?q?Updated=20`Chunk.toArray`=20and=20`Chunk.toRead?= =?UTF-8?q?onlyArray`.=20Improved=20functio=E2=80=A6=20(#2970)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/lucky-seahorses-obey.md | 5 +++++ packages/effect/dtslint/Chunk.ts | 20 +++++++++++++++++++ packages/effect/src/Chunk.ts | 31 +++++++++++++++++++++--------- 3 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 .changeset/lucky-seahorses-obey.md diff --git a/.changeset/lucky-seahorses-obey.md b/.changeset/lucky-seahorses-obey.md new file mode 100644 index 0000000000..d5a6efc64e --- /dev/null +++ b/.changeset/lucky-seahorses-obey.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +Updated `Chunk.toArray` and `Chunk.toReadonlyArray`. Improved function signatures to preserve non-empty status of chunks during conversion. diff --git a/packages/effect/dtslint/Chunk.ts b/packages/effect/dtslint/Chunk.ts index 27b8bafaec..60648e1dee 100644 --- a/packages/effect/dtslint/Chunk.ts +++ b/packages/effect/dtslint/Chunk.ts @@ -487,3 +487,23 @@ Chunk.reverse(nonEmptyNumbers) // $ExpectType NonEmptyChunk pipe(nonEmptyNumbers, Chunk.reverse) + +// ------------------------------------------------------------------------------------- +// toArray +// ------------------------------------------------------------------------------------- + +// $ExpectType string[] +Chunk.toArray(hole>()) + +// $ExpectType [string, ...string[]] +Chunk.toArray(hole>()) + +// ------------------------------------------------------------------------------------- +// toArray +// ------------------------------------------------------------------------------------- + +// $ExpectType readonly string[] +Chunk.toReadonlyArray(hole>()) + +// $ExpectType readonly [string, ...string[]] +Chunk.toReadonlyArray(hole>()) diff --git a/packages/effect/src/Chunk.ts b/packages/effect/src/Chunk.ts index e043723851..4e3aa3cb32 100644 --- a/packages/effect/src/Chunk.ts +++ b/packages/effect/src/Chunk.ts @@ -279,21 +279,21 @@ const copyToArray = (self: Chunk, array: Array, initial: number): voi } } -/** - * Converts the specified `Chunk` to a `Array`. - * - * @category conversions - * @since 2.0.0 - */ -export const toArray = (self: Chunk): Array => toReadonlyArray(self).slice() +const toArray_ = (self: Chunk): Array => 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 = (self: Chunk): ReadonlyArray => { +export const toArray: >( + self: S +) => S extends NonEmptyChunk ? RA.NonEmptyArray> : Array> = toArray_ as any + +const toReadonlyArray_ = (self: Chunk): ReadonlyArray => { switch (self.backing._tag) { case "IEmpty": { return emptyArray @@ -316,6 +316,19 @@ export const toReadonlyArray = (self: Chunk): ReadonlyArray => { } } +/** + * 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: >( + self: S +) => S extends NonEmptyChunk ? RA.NonEmptyReadonlyArray> : ReadonlyArray> = + toReadonlyArray_ as any + const reverseChunk = (self: Chunk): Chunk => { switch (self.backing._tag) { case "IEmpty":