express
express copied to clipboard
Use Array.flat instead of array-flatten
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
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.
@gooroodev please take a look
Thanks for the alert, @admsev!
1. Summary of Changes
The pull request makes the following changes:
- Replaces the use of the
array-flattenlibrary with the native JavaScriptArray.prototype.flatmethod. - Removes the
array-flattendependency frompackage.json. - Updates the code in
lib/application.jsto useArray.prototype.flatinstead of thearray-flattenlibrary.
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.flatmethod does not need to be called withcall. Instead, it should be used directly on the array.
- Problem: The
-
Proposed Improvement:
var fns = slice.call(arguments, offset).flat(Infinity);
Issue 2: Redundant variable declaration
-
Current Code:
var flatten = Array.prototype.flat;- Problem: Declaring
flattenis redundant sinceArray.prototype.flatcan be used directly.
- Problem: Declaring
-
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!
why is this even a dependency to begin with?
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.
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
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.
clopened to rerun CI but forgot 5.0 hasn't dropped the old versions in CI