This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkglock: Make package-lock.json sorting locale-agnostic (#17844)
Fixes: #17048 PR-URL: #17844 Credit: @LotharSee Reviewed-By: @zkat
- Loading branch information
Showing
1 changed file
with
10 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,7 +101,7 @@ function shrinkwrapDeps (deps, top, tree, seen) { | |
if (!seen) seen = new Set() | ||
if (seen.has(tree)) return | ||
seen.add(tree) | ||
tree.children.sort(function (aa, bb) { return moduleName(aa).localeCompare(moduleName(bb)) }).forEach(function (child) { | ||
sortModules(tree.children).forEach(function (child) { | ||
if (child.fakeChild) { | ||
deps[moduleName(child)] = child.fakeChild | ||
return | ||
|
@@ -130,7 +130,7 @@ function shrinkwrapDeps (deps, top, tree, seen) { | |
if (isOnlyOptional(child)) pkginfo.optional = true | ||
if (child.requires.length) { | ||
pkginfo.requires = {} | ||
child.requires.sort((a, b) => moduleName(a).localeCompare(moduleName(b))).forEach((required) => { | ||
sortModules(child.requires).forEach((required) => { | ||
var requested = required.package._requested || getRequested(required) || {} | ||
pkginfo.requires[moduleName(required)] = childVersion(top, required, requested) | ||
}) | ||
|
@@ -142,6 +142,14 @@ function shrinkwrapDeps (deps, top, tree, seen) { | |
}) | ||
} | ||
|
||
function sortModules (modules) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
// sort modules with the locale-agnostic Unicode sort | ||
var sortedModuleNames = modules.map(moduleName).sort() | ||
return modules.sort((a, b) => ( | ||
sortedModuleNames.indexOf(moduleName(a)) - sortedModuleNames.indexOf(moduleName(b)) | ||
This comment has been minimized.
Sorry, something went wrong.
ursaj
|
||
)) | ||
} | ||
|
||
function childVersion (top, child, req) { | ||
if (req.type === 'directory' || req.type === 'file') { | ||
return 'file:' + unixFormatPath(path.relative(top.path, child.package._resolved || req.fetchSpec)) | ||
|
This sort method is slower than bubble sort. Because on each comparison is searches element in name array, it gives us
O(n)
complexity per each.Total it would
O(n*n*log(n))
The fastest way is: