From d1bc75aafb4c075f192dd363f77ad80f9d274643 Mon Sep 17 00:00:00 2001 From: Marlon Passos Date: Thu, 15 Aug 2024 22:44:13 -0300 Subject: [PATCH] chore: deprecate `flat` (#165) Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com> --- docs/array/flat.mdx | 6 ++++++ src/array/flat.ts | 7 +++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/array/flat.mdx b/docs/array/flat.mdx index 7f4a77d6..6c0e7abd 100644 --- a/docs/array/flat.mdx +++ b/docs/array/flat.mdx @@ -3,6 +3,12 @@ title: flat description: Flatten an array of arrays into a single dimension --- +:::caution[Deprecated] +This function is deprecated. Use `Array.prototype.flat` instead, which is widely available and supports both deep and shallow flattening. + +[MDN: Array.prototype.flat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) +::: + ### Usage Given an array that contains many arrays, return a new array where all items from the children are present at the top level. diff --git a/src/array/flat.ts b/src/array/flat.ts index 82326813..3325f00e 100644 --- a/src/array/flat.ts +++ b/src/array/flat.ts @@ -2,6 +2,8 @@ * Given an array of arrays, returns a single dimensional array with * all items in it. * + * @deprecated Use [Array.prototype.flat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) instead. + * * @see https://radashi-org.github.io/reference/array/flat * @example * ```ts @@ -10,8 +12,5 @@ * ``` */ export function flat(lists: readonly T[][]): T[] { - return lists.reduce((acc, list) => { - acc.push(...list) - return acc - }, []) + return lists.flat() }