Skip to content

Commit

Permalink
Add sortingMap wich fix #6
Browse files Browse the repository at this point in the history
packages stay as they are, since this new approach avoid splicing them.
  • Loading branch information
jalal246 committed Mar 16, 2020
1 parent 980a8e0 commit 47f6b49
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
50 changes: 29 additions & 21 deletions src/packageSorter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ let unSorted;
let coreDep;
let elemAdded;

let indexes;

/**
* Checks if targeted dependency is already added to sorted array.
*
Expand Down Expand Up @@ -60,17 +62,12 @@ function isPackageNeedCoreDep(packageDeps) {
function addTo(packages, at, isSorted) {
const target = isSorted ? sorted : unSorted;

target.push(packages[at]);

/**
* remove it from packages so it won't be checked next time.
*/
const from = packages.splice(at, 1);
const to = target.push(packages[at]) - 1;

if (isSorted) {
sortingMap.push({
from: from,
to: at
from: at,
to
});
}

Expand All @@ -90,23 +87,27 @@ function sort(packages) {
let dep = {};

for (let i = 0; i < packages.length; i += 1) {
const pkg = packages[i];
if (!indexes[i]) {
const pkg = packages[i];

const { dependencies } = pkg;
const { dependencies } = pkg;

({ hasCoreDep, dep } = isPackageNeedCoreDep(dependencies));
({ hasCoreDep, dep } = isPackageNeedCoreDep(dependencies));

/**
* When to add package to sorted?
* - Neutral. Doesn't have hasCoreDep, then add it to sorted.
* - Not natural, but its core dep is already added.
*/
isAddToSorted = !hasCoreDep || isDepInSorted(dep);
/**
* When to add package to sorted?
* - Neutral. Doesn't have hasCoreDep, then add it to sorted.
* - Not natural, but its core dep is already added.
*/
isAddToSorted = !hasCoreDep || isDepInSorted(dep);

if (isAddToSorted) {
addTo(packages, i, true);
if (isAddToSorted) {
indexes[i] = true;

break;
addTo(packages, i, true);

break;
}
}
}

Expand All @@ -133,6 +134,8 @@ function sort(packages) {
*/
function packageSorter(packages = [], coreDependency) {
unSorted = [];
sortingMap = [];
indexes = {};

/**
* Nothing to sort when:
Expand All @@ -151,15 +154,20 @@ function packageSorter(packages = [], coreDependency) {

elemAdded = 0;

let i = 0;

while (sorted.length < totalLength) {
sort(packages);
i += 1;

if (i === 10) break;

if (elemAdded === totalLength) {
break;
}
}

return { sorted, unSorted };
return { sorted, unSorted, sortingMap };
}

module.exports = packageSorter;
14 changes: 12 additions & 2 deletions test/packageSorter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ const pkgFoloForms = {
};

describe("sortPackages test", () => {
it("sorts all packages with given core dependency", () => {
it.only("sorts all packages with given core dependency", () => {
const packages = [
pkgFoloValues,
pkgFoloUtils,
pkgFoloLayout,
pkgFoloContext,
pkgFoloForms
];
const { sorted, unSorted } = sortPackages(packages, "@folo");
const { sorted, unSorted, sortingMap } = sortPackages(packages, "@folo");

const expectedResult = [
pkgFoloUtils,
Expand All @@ -53,7 +53,17 @@ describe("sortPackages test", () => {
];

expect(sorted).to.have.ordered.members(expectedResult);

// all sorted, so unSorted is empty
expect(unSorted.length).to.be.equal(0);

expect(sortingMap).to.be.deep.equal([
{ from: 1, to: 0 },
{ from: 3, to: 1 },
{ from: 0, to: 2 },
{ from: 2, to: 3 },
{ from: 4, to: 4 }
]);
});

it("it extracts core dependency if not passed by default then sorts", () => {
Expand Down

0 comments on commit 47f6b49

Please sign in to comment.