Skip to content

Commit

Permalink
Merge 6875dcf into 4c2d111
Browse files Browse the repository at this point in the history
  • Loading branch information
w3nl committed Jun 27, 2020
2 parents 4c2d111 + 6875dcf commit 4796bdd
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hckrnews/truth-table",
"version": "1.0.4",
"version": "1.1.0",
"description": "",
"main": "src/TruthTable.mjs",
"files": [
Expand Down
22 changes: 18 additions & 4 deletions src/TruthTable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,32 @@ class TruthTable {
return this.inputs.map(row => row.some(Boolean));
}

get nor() {
return this.inputs.map(row => !row.some(Boolean));
get xor() {
return this.inputs.map(row => this.totalTrueInputs(row) % 2 === 1);
}

get andWithInputs() {
return this.inputs.map(row => [row.every(Boolean), ...row]);
get xnor() {
return this.inputs.map(row => this.totalTrueInputs(row) % 2 === 0);
}

totalTrueInputs(row) {
return row.reduce(
(accumulator, currentValue) => accumulator + (currentValue ? 1 : 0)
);
}

get nor() {
return this.inputs.map(row => !row.some(Boolean));
}

get orWithInputs() {
return this.inputs.map(row => [row.some(Boolean), ...row]);
}

get andWithInputs() {
return this.inputs.map(row => [row.every(Boolean), ...row]);
}

get nandWithInputs() {
return this.inputs.map(row => [!row.every(Boolean), ...row]);
}
Expand Down
68 changes: 68 additions & 0 deletions tests/unit/xnor.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import TruthTable from '../../src/TruthTable.mjs';

const TestCasesResult = [
{
description: '1 proposition',
input: 1,
expectedResult: [
true, // 0
false, // 1
],
},
{
description: '2 propositions',
input: 2,
expectedResult: [
true, // 00
false, // 01
false, // 10
true, // 11
],
},
{
description: '3 propositions',
input: 3,
expectedResult: [
true, // 000
false, // 001
false, // 010
true, // 011
false, // 100
true, // 101
true, // 110
false, // 111
],
},
{
description: '4 propositions',
input: 4,
expectedResult: [
true, // 0000
false, // 0001
false, // 0010
true, // 0011
false, // 0100
true, // 0101
true, // 0110
false, // 0111
false, // 1000
true, // 1001
true, // 1010
false, // 1011
true, // 1100
false, // 1101
false, // 1110
true, // 1111
],
},
];

describe.each(TestCasesResult)(
'Test xnor',
({ description, input, expectedResult }) => {
it(description, () => {
const table = TruthTable.create(input);
expect(table.xnor).toMatchObject(expectedResult);
});
}
);
68 changes: 68 additions & 0 deletions tests/unit/xor.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import TruthTable from '../../src/TruthTable.mjs';

const TestCasesResult = [
{
description: '1 proposition',
input: 1,
expectedResult: [
false, // 0
true, // 1
],
},
{
description: '2 propositions',
input: 2,
expectedResult: [
false, // 00
true, // 01
true, // 10
false, // 11
],
},
{
description: '3 propositions',
input: 3,
expectedResult: [
false, // 000
true, // 001
true, // 010
false, // 011
true, // 100
false, // 101
false, // 110
true, // 111
],
},
{
description: '4 propositions',
input: 4,
expectedResult: [
false, // 0000
true, // 0001
true, // 0010
false, // 0011
true, // 0100
false, // 0101
false, // 0110
true, // 0111
true, // 1000
false, // 1001
false, // 1010
true, // 1011
false, // 1100
true, // 1101
true, // 1110
false, // 1111
],
},
];

describe.each(TestCasesResult)(
'Test xor',
({ description, input, expectedResult }) => {
it(description, () => {
const table = TruthTable.create(input);
expect(table.xor).toMatchObject(expectedResult);
});
}
);

0 comments on commit 4796bdd

Please sign in to comment.