Skip to content

Commit

Permalink
Only shrink by removing inactive members
Browse files Browse the repository at this point in the history
  • Loading branch information
jouni-kantola committed Mar 30, 2024
1 parent d68497c commit 2d96e66
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
19 changes: 16 additions & 3 deletions resources/scripts/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,22 @@ function addMember(name: string, team: Array<Member>) {

export function adjustTeamSize(team: Array<Member>, newSize: number) {
if (newSize < 1) return;
else if (newSize === team.length - 1) team.pop();
else if (newSize < team.length) team.length = newSize;
else {

if (newSize < team.length) {
let i = team.length - 1;

while (team.length > newSize) {
if (!team[i].isActive) {
team.splice(i, 1);
}

i--;
}

for (let i = 0; i < team.length; i++) {
team[i].index = i;
}
} else {
for (let i = team.length; i < newSize; i++) {
addMember(`Member ${i + 1}`, team);
}
Expand Down
14 changes: 14 additions & 0 deletions test/team-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,17 @@ test("prevent shrinking team to zero", () => {
assert.strictEqual(team.at(-1)!.name, "Member 1");
assert.strictEqual(team.at(-1)!.index, 0);
});

test("only shrink by removing inactive members", () => {
const members = ["Member 1", "Member 2", "Member 3"];
const team = createTeam(members);

team[0].isActive = false;
team[1].isActive = true;

adjustTeamSize(team, 1);

assert.strictEqual(team.length, 1);
assert.strictEqual(team.at(-1)!.name, "Member 2");
assert.strictEqual(team.at(-1)!.index, 0);
});

0 comments on commit 2d96e66

Please sign in to comment.