express icon indicating copy to clipboard operation
express copied to clipboard

Use Array.flat instead of array-flatten

Open almic opened this issue 1 year ago • 7 comments

Depends upon #5595, Adopt Node@18 as minimum supported version.

With this new version, we get Array.flat(depth), which can be used instead of a package. Infinity would have the same behavior as the package, recursively unwrapping nested arrays indefinitely. But, perhaps it might be worth selecting a more reasonable value, to avoid a RangeError, such as when an array contains a reference to itself.

Note

Mimimum Node.js version supported is 11

almic avatar May 20 '24 15:05 almic

This is going to fail CI for now, but I figured it was better to approve the CI run for reference. I think this is a good change, but we have a bunch on our plate first so please be patient with us as we get the other things done first.

wesleytodd avatar May 20 '24 16:05 wesleytodd

@gooroodev please take a look

admsev avatar May 26 '24 09:05 admsev

Thanks for the alert, @admsev!

1. Summary of Changes

The pull request makes the following changes:

  • Replaces the use of the array-flatten library with the native JavaScript Array.prototype.flat method.
  • Removes the array-flatten dependency from package.json.
  • Updates the code in lib/application.js to use Array.prototype.flat instead of the array-flatten library.

2. Issues, Bugs, and Typos

Issue 1: Incorrect usage of Array.prototype.flat

  • Current Code:

    var flatten = Array.prototype.flat;
    var fns = flatten.call(slice.call(arguments, offset), Infinity);
    
    • Problem: The Array.prototype.flat method does not need to be called with call. Instead, it should be used directly on the array.
  • Proposed Improvement:

    var fns = slice.call(arguments, offset).flat(Infinity);
    

Issue 2: Redundant variable declaration

  • Current Code:

    var flatten = Array.prototype.flat;
    
    • Problem: Declaring flatten is redundant since Array.prototype.flat can be used directly.
  • Proposed Improvement: Remove the line:

    var flatten = Array.prototype.flat;
    

3. General Review of Code Quality and Style

Code Quality:

  • The code change is straightforward and improves the maintainability by removing an unnecessary dependency.
  • The usage of native methods is generally preferred for better performance and reduced dependency overhead.

Code Style:

  • The code style remains consistent with the rest of the codebase.
  • The changes are minimal and do not introduce any new stylistic issues.

Final Improved Code

Here is the final improved version of the code after addressing the issues:

diff --git a/lib/application.js b/lib/application.js
index 43c9f34b06..ecfe2186db 100644
--- a/lib/application.js
+++ b/lib/application.js
@@ -21,7 +21,6 @@ var http = require('http');
 var compileETag = require('./utils').compileETag;
 var compileQueryParser = require('./utils').compileQueryParser;
 var compileTrust = require('./utils').compileTrust;
-var flatten = require('array-flatten').flatten
 var merge = require('utils-merge');
 var resolve = require('path').resolve;
 var once = require('once')
@@ -34,6 +33,5 @@ var setPrototypeOf = require('setprototypeof')
  */
 
 var slice = Array.prototype.slice;
-var flatten = Array.prototype.flat;
 
 /**
  * Application prototype.
@@ -209,7 +208,7 @@ app.use = function use(fn) {
     }
   }
 
-  var fns = flatten.call(slice.call(arguments, offset), Infinity);
+  var fns = slice.call(arguments, offset).flat(Infinity);
 
   if (fns.length === 0) {
     throw new TypeError('app.use() requires a middleware function')
diff --git a/package.json b/package.json
index d3e2f0a190..5ec37a8f77 100644
--- a/package.json
+++ b/package.json
@@ -29,7 +29,6 @@
   ],
   "dependencies": {
     "accepts": "~1.3.8",
-    "array-flatten": "3.0.0",
     "body-parser": "2.0.0-beta.2",
     "content-disposition": "0.5.4",
     "content-type": "~1.0.4",

This version ensures that Array.prototype.flat is used correctly and removes any redundant variable declarations.

Yours, Gooroo.dev. I'd appreciate it if you could react or reply!

gooroodev avatar May 26 '24 09:05 gooroodev

why is this even a dependency to begin with?

mmaazahmed avatar May 27 '24 19:05 mmaazahmed

why is this even a dependency to begin with?

Array.prototype.flat does not exist in pre-ES2019 ECMAScript standards and Express currently supports old Node.js versions (starting with 0.10), which are way older than this feature.

As for why specifically array-flatten is used, see #2624 and comments in #4707. Basically the idea was to move the util code to separate packages and array-flatten is owned by blakeembrey - an Express.js Technical Committee member.

krzysdz avatar May 27 '24 22:05 krzysdz

Hi Krzysdz Appreciate the response. I was actually referring to having it as a dependency, rather than integrating the functionality directly into the project as a helper function. What were the design considerations of having it as an external dependency whens its only a couple of lines of trivial code. Thanks

mmaazahmed avatar May 31 '24 14:05 mmaazahmed

Hey @mmaazahmed, can we keep the discussion limited to the changes made in this PR going forward? I am going to hide all our comments here as off topic so that we don't distract the conversation.

wesleytodd avatar May 31 '24 14:05 wesleytodd

clopened to rerun CI but forgot 5.0 hasn't dropped the old versions in CI

jonchurch avatar Aug 01 '24 22:08 jonchurch