Skip to content

Commit

Permalink
Use the new logic gates, add the not true table
Browse files Browse the repository at this point in the history
  • Loading branch information
w3nl committed Jul 1, 2020
1 parent 2b74ef5 commit 414fbc6
Show file tree
Hide file tree
Showing 15 changed files with 2,242 additions and 463 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ All options:
* XnoTruthTable
* XandTruthTable
* XnandTruthTable
* NotTruthTable

To check all used logic gates:
https://www.npmjs.com/package/@hckrnews/logic-gates

[npm-url]: https://www.npmjs.com/package/@hckrnews/truth-table
[npm-image]: https://img.shields.io/npm/v/@hckrnews/truth-table.svg
Expand Down
2,472 changes: 2,062 additions & 410 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hckrnews/truth-table",
"version": "2.1.0",
"version": "2.2.0",
"description": "Generate a truth table very easy.",
"main": "src/TruthTable.mjs",
"files": [
Expand All @@ -14,6 +14,7 @@
"src/XnorTruthTable.mjs",
"src/XandTruthTable.mjs",
"src/XnandTruthTable.mjs",
"src/NotTruthTable.mjs",
"src/Helper.js",
"src/InvalidInputError.js"
],
Expand All @@ -30,8 +31,8 @@
"url": "https://hckr.news/"
},
"devDependencies": {
"@babel/core": "^7.10.3",
"@babel/preset-env": "^7.10.3",
"@babel/core": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@hckrnews/eslint-config": "^0.2.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.1.0",
Expand All @@ -44,7 +45,7 @@
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react": "^7.20.3",
"eslint-plugin-vue": "^6.2.2",
"jest": "^26.1.0",
"jscpd": "^3.2.1",
Expand All @@ -63,6 +64,6 @@
"truth table"
],
"dependencies": {
"@hckrnews/logic-gates": "^1.0.2"
"@hckrnews/logic-gates": "^2.0.0"
}
}
9 changes: 3 additions & 6 deletions src/AndTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { AndGate } from '@hckrnews/logic-gates';
import { and } from '@hckrnews/logic-gates';
import TruthTable from './BaseTruthTable.mjs';

class AndTruthTable extends TruthTable {
generateOutput() {
this.output = this.inputs.map(row => AndGate.create(row).output);
this.result = this.inputs.map(row => [
AndGate.create(row).output,
...row,
]);
this.output = this.inputs.map(row => and(row));
this.result = this.inputs.map(row => [and(row), ...row]);
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/NandTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { NandGate } from '@hckrnews/logic-gates';
import { nand } from '@hckrnews/logic-gates';
import TruthTable from './BaseTruthTable.mjs';

class NandTruthTable extends TruthTable {
generateOutput() {
this.output = this.inputs.map(row => NandGate.create(row).output);
this.result = this.inputs.map(row => [
NandGate.create(row).output,
...row,
]);
this.output = this.inputs.map(row => nand(row));
this.result = this.inputs.map(row => [nand(row), ...row]);
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/NorTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { NorGate } from '@hckrnews/logic-gates';
import { nor } from '@hckrnews/logic-gates';
import TruthTable from './BaseTruthTable.mjs';

class NorTruthTable extends TruthTable {
generateOutput() {
this.output = this.inputs.map(row => NorGate.create(row).output);
this.result = this.inputs.map(row => [
NorGate.create(row).output,
...row,
]);
this.output = this.inputs.map(row => nor(row));
this.result = this.inputs.map(row => [nor(row), ...row]);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/NotTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { not } from '@hckrnews/logic-gates';
import TruthTable from './BaseTruthTable.mjs';

class NotTruthTable extends TruthTable {
generateOutput() {
this.output = this.inputs.map(row => not(row));
this.result = this.inputs.map(row => [not(row), ...row]);
}
}

export default NotTruthTable;
9 changes: 3 additions & 6 deletions src/OrTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { OrGate } from '@hckrnews/logic-gates';
import { or } from '@hckrnews/logic-gates';
import TruthTable from './BaseTruthTable.mjs';

class OrTruthTable extends TruthTable {
generateOutput() {
this.output = this.inputs.map(row => OrGate.create(row).output);
this.result = this.inputs.map(row => [
OrGate.create(row).output,
...row,
]);
this.output = this.inputs.map(row => or(row));
this.result = this.inputs.map(row => [or(row), ...row]);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/TruthTable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import XorTruthTable from './XorTruthTable.mjs';
import XnorTruthTable from './XnorTruthTable.mjs';
import XandTruthTable from './XandTruthTable.mjs';
import XnandTruthTable from './XnandTruthTable.mjs';
import NotTruthTable from './NotTruthTable.mjs';

export default TruthTable;
export {
Expand All @@ -18,4 +19,5 @@ export {
XnorTruthTable,
XandTruthTable,
XnandTruthTable,
NotTruthTable,
};
9 changes: 3 additions & 6 deletions src/XandTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { XandGate } from '@hckrnews/logic-gates';
import { xand } from '@hckrnews/logic-gates';
import TruthTable from './BaseTruthTable.mjs';

class XandTruthTable extends TruthTable {
generateOutput() {
this.output = this.inputs.map(row => XandGate.create(row).output);
this.result = this.inputs.map(row => [
XandGate.create(row).output,
...row,
]);
this.output = this.inputs.map(row => xand(row));
this.result = this.inputs.map(row => [xand(row), ...row]);
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/XnandTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { XnandGate } from '@hckrnews/logic-gates';
import { xnand } from '@hckrnews/logic-gates';
import TruthTable from './BaseTruthTable.mjs';

class XnandTruthTable extends TruthTable {
generateOutput() {
this.output = this.inputs.map(row => XnandGate.create(row).output);
this.result = this.inputs.map(row => [
XnandGate.create(row).output,
...row,
]);
this.output = this.inputs.map(row => xnand(row));
this.result = this.inputs.map(row => [xnand(row), ...row]);
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/XnorTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { XnorGate } from '@hckrnews/logic-gates';
import { xnor } from '@hckrnews/logic-gates';
import TruthTable from './BaseTruthTable.mjs';

class XnorTruthTable extends TruthTable {
generateOutput() {
this.output = this.inputs.map(row => XnorGate.create(row).output);
this.result = this.inputs.map(row => [
XnorGate.create(row).output,
...row,
]);
this.output = this.inputs.map(row => xnor(row));
this.result = this.inputs.map(row => [xnor(row), ...row]);
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/XorTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { XorGate } from '@hckrnews/logic-gates';
import { xor } from '@hckrnews/logic-gates';
import TruthTable from './BaseTruthTable.mjs';

class XorTruthTable extends TruthTable {
generateOutput() {
this.output = this.inputs.map(row => XorGate.create(row).output);
this.result = this.inputs.map(row => [
XorGate.create(row).output,
...row,
]);
this.output = this.inputs.map(row => xor(row));
this.result = this.inputs.map(row => [xor(row), ...row]);
}
}

Expand Down
65 changes: 65 additions & 0 deletions tests/unit/not.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { NotTruthTable } from '../../src/TruthTable.mjs';

const TestCasesResult = [
{
description: '1 proposition',
input: 1,
expectedResult: [[true], [false]],
},
{
description: '2 propositions',
input: 2,
expectedResult: [
[true, true],
[true, false],
[false, true],
[false, false],
],
},
{
description: '3 propositions',
input: 3,
expectedResult: [
[true, true, true],
[true, true, false],
[true, false, true],
[true, false, false],
[false, true, true],
[false, true, false],
[false, false, true],
[false, false, false],
],
},
{
description: '4 propositions',
input: 4,
expectedResult: [
[true, true, true, true],
[true, true, true, false],
[true, true, false, true],
[true, true, false, false],
[true, false, true, true],
[true, false, true, false],
[true, false, false, true],
[true, false, false, false],
[false, true, true, true],
[false, true, true, false],
[false, true, false, true],
[false, true, false, false],
[false, false, true, true],
[false, false, true, false],
[false, false, false, true],
[false, false, false, false],
],
},
];

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

const TestCasesResult = [
{
description: '1 proposition',
input: 1,
expectedResult: [
[[true], false],
[[false], true],
],
},
{
description: '2 propositions',
input: 2,
expectedResult: [
[[true, true], false, false],
[[true, false], false, true],
[[false, true], true, false],
[[false, false], true, true],
],
},
{
description: '3 propositions',
input: 3,
expectedResult: [
[[true, true, true], false, false, false],
[[true, true, false], false, false, true],
[[true, false, true], false, true, false],
[[true, false, false], false, true, true],
[[false, true, true], true, false, false],
[[false, true, false], true, false, true],
[[false, false, true], true, true, false],
[[false, false, false], true, true, true],
],
},
{
description: '4 propositions',
input: 4,
expectedResult: [
[[true, true, true, true], false, false, false, false],
[[true, true, true, false], false, false, false, true],
[[true, true, false, true], false, false, true, false],
[[true, true, false, false], false, false, true, true],
[[true, false, true, true], false, true, false, false],
[[true, false, true, false], false, true, false, true],
[[true, false, false, true], false, true, true, false],
[[true, false, false, false], false, true, true, true],
[[false, true, true, true], true, false, false, false],
[[false, true, true, false], true, false, false, true],
[[false, true, false, true], true, false, true, false],
[[false, true, false, false], true, false, true, true],
[[false, false, true, true], true, true, false, false],
[[false, false, true, false], true, true, false, true],
[[false, false, false, true], true, true, true, false],
[[false, false, false, false], true, true, true, true],
],
},
];

describe.each(TestCasesResult)(
'Test not with inputs',
({ description, input, expectedResult }) => {
it(description, () => {
const table = NotTruthTable.create(input);
expect(table.result).toMatchObject(expectedResult);
});
}
);

0 comments on commit 414fbc6

Please sign in to comment.