Skip to content

Commit

Permalink
Sort name, not files
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed May 5, 2024
1 parent aa1817d commit f2f64d0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 81 deletions.
45 changes: 45 additions & 0 deletions util/compare.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function parseDigits(s) {
return /^[\d.]+$/.test(s) ? s.split('.').map(s => +s || 0) : null;
}

export function primitive(a, b) {
if (a === null && b !== null) {
return -1;
}
if (b === null && a !== null) {
return 1;
}
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}

export function human(a, b) {
const aV = parseDigits(a);
const bV = parseDigits(b);
if (aV && bV) {
const l = Math.max(a.length, b.length);
for (let i = 0; i < l; i++) {
const cmp = primitive(aV[i] || 0, bV[i] || 0);
if (cmp) {
return cmp;
}
}
}
return primitive(a, b);
}

export function humanTokens(a, b) {
const l = Math.max(a.length, b.length);
for (let i = 0; i < l; i++) {
const cmp = human(a[i], b[i]);
if (cmp) {
return cmp;
}
}
return 0;
}
93 changes: 12 additions & 81 deletions util/packages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {dirname, join as pathJoin} from 'node:path';
import {fileURLToPath} from 'node:url';

import {walk} from './util.mjs';
import {humanTokens} from './compare.mjs';

const __dirname = dirname(fileURLToPath(import.meta.url));

Expand All @@ -15,83 +16,8 @@ const misplacedChildren = new Map([
['flash-player-9.0.262.0-linux-sa', 'flash-player-10']
]);

function comparePrimitive(a, b) {
if (a === null && b !== null) {
return -1;
}
if (b === null && a !== null) {
return 1;
}
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}

function compareVersions(a, b) {
const aCmps = a.map(parseVersionPiece);
const bCmps = b.map(parseVersionPiece);
for (let i = 0; i < 3; i++) {
const aCmp = aCmps[i];
const bCmp = bCmps[i];
const l = Math.max(aCmp.length, bCmp.length);
for (let j = 0; j < l; j++) {
const aV = j < aCmp.length ? aCmp[j] : null;
const bV = j < bCmp.length ? bCmp[j] : null;

const cmp = comparePrimitive(aV, bV);
if (cmp) {
return cmp;
}
}
}
return 0;
}

function comparePart(a, b) {
const aV = parseVersion(a);
const bV = parseVersion(b);
if (aV && bV) {
return compareVersions(aV, bV);
}
return comparePrimitive(a, b);
}

function pathToParts(p) {
return p.replace(/\.json$/, '').split('/');
}

function parseVersionPiece(s) {
if (!/^[\d.]+$/.test(s)) {
return [s];
}
return s.split('.').map(Number);
}

function parseVersion(s) {
const m = s.match(/^(([^.]*)-)?([\d.]+)(-(.*))?$/);
if (!m) {
return null;
}
const [, , pre, ver, , suf] = m;
return [pre || null, ver, suf || null];
}

function comparePaths(a, b) {
a = pathToParts(a);
b = pathToParts(b);

const l = Math.max(a.length, b.length);
for (let i = 0; i < l; i++) {
const cmp = comparePart(a[i], b[i]);
if (cmp) {
return cmp;
}
}
return 0;
function compareNames(a, b) {
return humanTokens(a.name.split('-'), b.name.split('-'));
}

export async function readPackageFile(f) {
Expand Down Expand Up @@ -126,8 +52,13 @@ export async function readPackageFile(f) {
}

export async function read() {
const files = (await readdir(directory, {recursive: true}))
.filter(s => /^([^.][^/]*\/)*[^.][^/]*\.json$/.test(s))
.sort(comparePaths);
return (await Promise.all(files.map(readPackageFile))).flat();
const files = (await readdir(directory, {recursive: true})).filter(s =>
/^([^.][^/]*\/)*[^.][^/]*\.json$/.test(s)
);
const pkgs = (await Promise.all(files.map(readPackageFile))).flat();
for (const [pkg] of walk(pkgs, p => p.packages)) {
pkg.packages?.sort(compareNames);
}
pkgs.sort(compareNames);
return pkgs;
}

0 comments on commit f2f64d0

Please sign in to comment.