Skip to content

Commit

Permalink
Merge pull request #66 from d3vco/master
Browse files Browse the repository at this point in the history
Allow incomplete fact selection
  • Loading branch information
elegantmoose authored Dec 16, 2024
2 parents 26e5ea8 + 71e1c86 commit 14322ec
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
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;
}

0 comments on commit 14322ec

Please sign in to comment.