Skip to content

Commit

Permalink
Use the logic gates package and add xand and xnand
Browse files Browse the repository at this point in the history
  • Loading branch information
w3nl committed Jun 30, 2020
1 parent f558723 commit b1de4f4
Show file tree
Hide file tree
Showing 17 changed files with 928 additions and 613 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ All options:
* NorTruthTable
* XoTruthTable
* XnoTruthTable
* XandTruthTable
* XnandTruthTable

[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 changes: 1 addition & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
'^.+\\.cjs?$': 'babel-jest',
},

transformIgnorePatterns: ['/node_modules/'],
transformIgnorePatterns: [],

moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
Expand Down
1,207 changes: 616 additions & 591 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hckrnews/truth-table",
"version": "2.0.0",
"version": "2.1.0",
"description": "",
"main": "src/TruthTable.mjs",
"files": [
Expand All @@ -12,6 +12,8 @@
"src/NorTruthTable.mjs",
"src/XorTruthTable.mjs",
"src/XnorTruthTable.mjs",
"src/XandTruthTable.mjs",
"src/XnandTruthTable.mjs",
"src/Helper.js",
"src/InvalidInputError.js"
],
Expand All @@ -32,6 +34,7 @@
"@babel/preset-env": "^7.10.3",
"@hckrnews/eslint-config": "^0.2.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.1.0",
"codecov": "^3.6.5",
"coveralls": "^3.0.13",
"eslint": "^6.6.0",
Expand All @@ -43,7 +46,7 @@
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-vue": "^6.2.2",
"jest": "^25.4.0",
"jest": "^26.1.0",
"jscpd": "^3.2.1",
"prettier": "1.19.1"
},
Expand All @@ -58,5 +61,8 @@
"truth",
"table",
"truth table"
]
],
"dependencies": {
"@hckrnews/logic-gates": "^1.0.1"
}
}
8 changes: 6 additions & 2 deletions src/AndTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { AndGate } from '@hckrnews/logic-gates';
import TruthTable from './BaseTruthTable.mjs';

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

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

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

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

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

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

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

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

export default TruthTable;
export {
Expand All @@ -14,4 +16,6 @@ export {
NorTruthTable,
XorTruthTable,
XnorTruthTable,
XandTruthTable,
XnandTruthTable,
};
14 changes: 14 additions & 0 deletions src/XandTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { XandGate } 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,
]);
}
}

export default XandTruthTable;
14 changes: 14 additions & 0 deletions src/XnandTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { XnandGate } 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,
]);
}
}

export default XnandTruthTable;
8 changes: 3 additions & 5 deletions src/XnorTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { XnorGate } from '@hckrnews/logic-gates';
import TruthTable from './BaseTruthTable.mjs';
import Helper from './Helper';

class XnorTruthTable extends TruthTable {
generateOutput() {
this.output = this.inputs.map(
row => Helper.totalTrueInputs(row) % 2 === 0
);
this.output = this.inputs.map(row => XnorGate.create(row).output);
this.result = this.inputs.map(row => [
Helper.totalTrueInputs(row) % 2 === 0,
XnorGate.create(row).output,
...row,
]);
}
Expand Down
8 changes: 3 additions & 5 deletions src/XorTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { XorGate } from '@hckrnews/logic-gates';
import TruthTable from './BaseTruthTable.mjs';
import Helper from './Helper';

class XorTruthTable extends TruthTable {
generateOutput() {
this.output = this.inputs.map(
row => Helper.totalTrueInputs(row) % 2 === 1
);
this.output = this.inputs.map(row => XorGate.create(row).output);
this.result = this.inputs.map(row => [
Helper.totalTrueInputs(row) % 2 === 1,
XorGate.create(row).output,
...row,
]);
}
Expand Down
51 changes: 51 additions & 0 deletions tests/unit/xand.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { XandTruthTable } from '../../src/TruthTable.mjs';

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

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

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

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

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

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

0 comments on commit b1de4f4

Please sign in to comment.