Skip to content

Commit

Permalink
Merge pull request #10 from i-VRESSE/toggleresidue
Browse files Browse the repository at this point in the history
Moved toggleResidue from webap to ui
  • Loading branch information
sverhoeven authored Oct 8, 2024
2 parents 6ad8217 + 997fbc8 commit cfd87ed
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@i-vresse/haddock3-ui",
"version": "0.3.0",
"version": "0.3.1",
"type": "module",
"private": false,
"sideEffects": false,
Expand Down
109 changes: 109 additions & 0 deletions src/toggles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { describe, expect, test } from "vitest";
import { toggleResidue } from "./toggles.js";

describe("toggleResidue()", () => {
test("give residue in pick act mode should add residue to active", () => {
const residue = 42;
const residueState = {
act: [],
pass: [],
};

const result = toggleResidue(residue, "act", residueState);

expect(result).toEqual({
act: [42],
pass: [],
});
});

test("give residue in pick pass mode should add residue to passive", () => {
const residue = 42;
const residueState = {
act: [],
pass: [],
};

const result = toggleResidue(residue, "pass", residueState);

expect(result).toEqual({
act: [],
pass: [42],
});
});

test("give residue in pick act mode should remove residue from active", () => {
const residue = 42;
const residueState = {
act: [42],
pass: [],
};

const result = toggleResidue(residue, "act", residueState);

expect(result).toEqual({
act: [],
pass: [],
});
});

test("give residue in pick pass mode should remove residue from passive", () => {
const residue = 42;
const residueState = {
act: [],
pass: [42],
};

const result = toggleResidue(residue, "pass", residueState);

expect(result).toEqual({
act: [],
pass: [],
});
});

test("give residue in pick act mode should remove residue from passive", () => {
const residue = 42;
const residueState = {
act: [],
pass: [42],
};

const result = toggleResidue(residue, "act", residueState);

expect(result).toEqual({
act: [42],
pass: [],
});
});

test("give residue in pick pass mode should remove residue from active", () => {
const residue = 42;
const residueState = {
act: [42],
pass: [],
};

const result = toggleResidue(residue, "pass", residueState);

expect(result).toEqual({
act: [],
pass: [42],
});
});

test("should keep current selection", () => {
const residue = 41;
const residueState = {
act: [42],
pass: [43],
};

const result = toggleResidue(residue, "act", residueState);

expect(result).toEqual({
act: [42, 41],
pass: [43],
});
});
});
39 changes: 39 additions & 0 deletions src/toggles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,42 @@ export function PickIn3D({
</div>
);
}

/**
* Toggles the selection state of a residue number in either the "act" or "pass" list.
*
* @param resno - The residue number to toggle.
* @param pick - The selection type, either "act" or "pass".
* @param current - The current selection state containing "act" and "pass" lists.
* @returns The new selection state with the residue number toggled in the appropriate list.
*/
export function toggleResidue(
resno: number,
pick: ActPass,
current: ResidueSelection,
): ResidueSelection {
const newSelection = {
act: current.act,
pass: current.pass,
};
if (pick === "act") {
if (newSelection.act.includes(resno)) {
newSelection.act = newSelection.act.filter((r) => r !== resno);
} else {
newSelection.act = [...newSelection.act, resno];
newSelection.pass = current.pass.filter(
(r) => !newSelection.act.includes(r),
);
}
} else {
if (newSelection.pass.includes(resno)) {
newSelection.pass = newSelection.pass.filter((r) => r !== resno);
} else {
newSelection.pass = [...newSelection.pass, resno];
newSelection.act = current.act.filter(
(r) => !newSelection.pass.includes(r),
);
}
}
return newSelection;
}

0 comments on commit cfd87ed

Please sign in to comment.