Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
pkglock: Make package-lock.json sorting locale-agnostic (#17844)
Browse files Browse the repository at this point in the history
Fixes: #17048

PR-URL: #17844
Credit: @LotharSee
Reviewed-By: @zkat
  • Loading branch information
LotharSee authored and zkat committed Jul 28, 2017
1 parent 3aee598 commit b6d5549
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
Expand All @@ -142,6 +142,14 @@ function shrinkwrapDeps (deps, top, tree, seen) {
})
}

function sortModules (modules) {

This comment has been minimized.

Copy link
@leonovalex

leonovalex Sep 13, 2017

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:

function sortModules(modules) {
  return modules.sort((x, y) => {
    var xv = x.moduleName();
    var vy = y.moduleName();
    return +(xv > yv) || +(xv === yv) - 1;
  });
}
// 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.

Copy link
@ursaj

ursaj Sep 13, 2017

var n1 = a.moduleName, n2 = b.moduleName;
return n1 == n2 ? 0 : (n1 < n2 ? -1 : 1);
))
}

function childVersion (top, child, req) {
if (req.type === 'directory' || req.type === 'file') {
return 'file:' + unixFormatPath(path.relative(top.path, child.package._resolved || req.fetchSpec))
Expand Down

0 comments on commit b6d5549

Please sign in to comment.