Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow incomplete fact selection #66

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"preview": "vite preview --port 5050",
"lint": "npx eslint \"./**\"",
"lintfix": "npx eslint \"./**\" --fix",
"test": "jest --config jest.config.js",
"test-all": "jest --config jest.config.js && npx eslint \"./**\"",
"test-accessibility": "jest -t=accessibility --config jest.config.js"
},
Expand Down
16 changes: 2 additions & 14 deletions src/components/operations/AddPotentialLinkModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useOperationStore } from '@/stores/operationStore';
import { useAgentStore } from "@/stores/agentStore";
import { useAbilityStore } from "@/stores/abilityStore";
import { useSourceStore } from "@/stores/sourceStore";
import { cartesian } from "@/utils/utils";

const props = defineProps({
active: Boolean,
Expand Down Expand Up @@ -47,21 +48,8 @@ const filteredAbilities = computed(() => {

const potentialLinksToAdd = computed(() => {
let links = [];
function cartesian(args) {
if (!args.length) return [];
let r = [], max = args.length - 1;
function helper(arr, i) {
for (let j = 0; j < args[i].length; j++) {
let a = arr.slice(0);
a.push(args[i][j]);
(i === max) ? r.push(a) : helper(a, i + 1);
}
}
helper([], 0);
return r;
}

let combinations = [];

Object.keys(selectedPotentialLinkFacts.value).forEach((factName, i) => {
combinations.push(selectedPotentialLinkFacts.value[factName].facts.filter((fact) => fact.selected).map((fact) => `${factName.length}|${factName}${fact.value}`));
if (selectedPotentialLinkFacts.value[factName].customValue) {
Expand Down
37 changes: 37 additions & 0 deletions src/tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { cartesian } from '../utils/utils';

describe('cartesian function', () => {
it('should return an empty array when input is empty', () => {
expect(cartesian([])).toEqual([]);
});

it('should return the correct cartesian product for non-empty arrays', () => {
const input = [[1, 2], [3, 4]];
const expectedOutput = [
[1, 3],
[1, 4],
[2, 3],
[2, 4]
];
expect(cartesian(input)).toEqual(expectedOutput);
});

it('should allow arrays with empty sub-arrays', () => {
const input = [[1, 2], [], [3, 4]];
const expectedOutput = [
[1, 3],
[1, 4],
[2, 3],
[2, 4]
];
expect(cartesian(input)).toEqual(expectedOutput);
});

it('should handle arrays with single elements', () => {
const input = [[1], [2], [3]];
const expectedOutput = [
[1, 2, 3]
];
expect(cartesian(input)).toEqual(expectedOutput);
});
});
18 changes: 18 additions & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,21 @@ export function b64DecodeUnicode(str) {
}
} else return "";
}

export function cartesian(args) {
// Remove empty arrays from the input
const filteredArgs = args.filter(arr => arr.length > 0);
if (!filteredArgs.length) return [];
let r = [], max = filteredArgs.length - 1;

function helper(arr, i) {
for (const element of filteredArgs[i]) {
let a = arr.slice(0);
a.push(element);
(i === max) ? r.push(a) : helper(a, i + 1);
}
}

helper([], 0);
return r;
}
Loading