Skip to content

Commit

Permalink
Refactor the truth table, extend it for every logic type
Browse files Browse the repository at this point in the history
  • Loading branch information
w3nl committed Jun 28, 2020
1 parent 28eaffc commit 11e5412
Show file tree
Hide file tree
Showing 23 changed files with 178 additions and 138 deletions.
26 changes: 10 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ or

Example usage:
```
import TruthTable from "@hckrnews/truth-table";
import AndTruthTable from "@hckrnews/truth-table";
const table = TruthTable.create(3);
const table = AndTruthTable.create(3);
```

Table inputs result:
Expand All @@ -45,7 +45,7 @@ Output:

To receive results of the and operator:
```
table.and
table.output
```
Output:
```
Expand All @@ -54,7 +54,7 @@ Output:

Do you with te receive the result with the inputs of the and operator:
```
table.andWithInputs
table.result
```
Output:
```
Expand All @@ -71,18 +71,12 @@ Output:
```

All options:
* and
* andWithInputs
* nand
* nandWithInputs
* or
* orWithInputs
* nor
* norWithInputs
* xor
* xorWithInputs
* xnor
* xnorWithInputs
* AndTruthTable
* NandTruthTable
* OrTruthTable
* NorTruthTable
* XoTruthTable
* XnoTruthTable

[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 package-lock.json

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

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{
"name": "@hckrnews/truth-table",
"version": "1.1.0",
"version": "2.0.0",
"description": "",
"main": "src/TruthTable.mjs",
"files": [
"src/TruthTable.mjs",
"src/AndTruthTable.mjs",
"src/NandTruthTable.mjs",
"src/OrTruthTable.mjs",
"src/NorTruthTable.mjs",
"src/XorTruthTable.mjs",
"src/XnorTruthTable.mjs",
"src/Helper.js",
"src/InvalidInputError.js"
],
Expand Down
10 changes: 10 additions & 0 deletions src/AndTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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]);
}
}

export default AndTruthTable;
48 changes: 48 additions & 0 deletions src/BaseTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Helper from './Helper';
import InvalidInputError from './InvalidInputError';

class TruthTable {
constructor(propositions) {
if (!Number.isInteger(propositions)) {
throw new InvalidInputError(
propositions,
'propositions isnt a number'
);
}

this.propositions = propositions;
this.inputs = [];
this.output = [];
this.result = [];
}

get combinations() {
return 2 ** this.propositions;
}

generateInputs() {
const inputs = Array(this.combinations).fill(false);

this.inputs = inputs.map((value, index) => this.addRow(index));
}

generateOutput() {}

addRow(row) {
const binary = Helper.dec2bin(row, this.propositions);
const inputs = Array(this.propositions).fill(false);

return inputs.map((value, index) => binary.substr(index, 1) === '1');
}

static create(propositions) {
const table = new this(propositions);

table.generateInputs();
table.generateOutput();

return table;
}
}

export default TruthTable;
10 changes: 10 additions & 0 deletions src/NandTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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]);
}
}

export default NandTruthTable;
10 changes: 10 additions & 0 deletions src/NorTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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]);
}
}

export default NorTruthTable;
10 changes: 10 additions & 0 deletions src/OrTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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]);
}
}

export default OrTruthTable;
110 changes: 15 additions & 95 deletions src/TruthTable.mjs
Original file line number Diff line number Diff line change
@@ -1,97 +1,17 @@
import Helper from './Helper';
import InvalidInputError from './InvalidInputError';

class TruthTable {
constructor(propositions) {
if (!Number.isInteger(propositions)) {
throw new InvalidInputError(
propositions,
'propositions isnt a number'
);
}

this.propositions = propositions;
this.inputs = [];
}

get combinations() {
return 2 ** this.propositions;
}

generate() {
const inputs = Array(this.combinations).fill(false);

this.inputs = inputs.map((value, index) => this.addRow(index));
}

addRow(row) {
const binary = Helper.dec2bin(row, this.propositions);
const inputs = Array(this.propositions).fill(false);

return inputs.map((value, index) => binary.substr(index, 1) === '1');
}

get and() {
return this.inputs.map(row => row.every(Boolean));
}

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

get or() {
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 => Helper.totalTrueInputs(row) % 2 === 1);
}

get xnor() {
return this.inputs.map(row => Helper.totalTrueInputs(row) % 2 === 0);
}

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

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

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

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

get xorWithInputs() {
return this.inputs.map(row => [
Helper.totalTrueInputs(row) % 2 === 1,
...row,
]);
}

get xnorWithInputs() {
return this.inputs.map(row => [
Helper.totalTrueInputs(row) % 2 === 0,
...row,
]);
}

static create(propositions) {
const table = new TruthTable(propositions);

table.generate();

return table;
}
}
import TruthTable from './BaseTruthTable.mjs';
import AndTruthTable from './AndTruthTable.mjs';
import NandTruthTable from './NandTruthTable.mjs';
import OrTruthTable from './OrTruthTable.mjs';
import NorTruthTable from './NorTruthTable.mjs';
import XorTruthTable from './XorTruthTable.mjs';
import XnorTruthTable from './XnorTruthTable.mjs';

export default TruthTable;
export {
AndTruthTable,
NandTruthTable,
OrTruthTable,
NorTruthTable,
XorTruthTable,
XnorTruthTable,
};
16 changes: 16 additions & 0 deletions src/XnorTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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.result = this.inputs.map(row => [
Helper.totalTrueInputs(row) % 2 === 0,
...row,
]);
}
}

export default XnorTruthTable;
16 changes: 16 additions & 0 deletions src/XorTruthTable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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.result = this.inputs.map(row => [
Helper.totalTrueInputs(row) % 2 === 1,
...row,
]);
}
}

export default XorTruthTable;
6 changes: 3 additions & 3 deletions tests/unit/and.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TruthTable from '../../src/TruthTable.mjs';
import { AndTruthTable } from '../../src/TruthTable.mjs';

const TestCasesResult = [
{
Expand Down Expand Up @@ -44,8 +44,8 @@ describe.each(TestCasesResult)(
'Test and',
({ description, input, expectedResult }) => {
it(description, () => {
const table = TruthTable.create(input);
expect(table.and).toMatchObject(expectedResult);
const table = AndTruthTable.create(input);
expect(table.output).toMatchObject(expectedResult);
});
}
);
4 changes: 2 additions & 2 deletions tests/unit/andResult.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TruthTable from '../../src/TruthTable.mjs';
import TruthTable from '../../src/AndTruthTable.mjs';

const TestCasesResult = [
{
Expand Down Expand Up @@ -62,7 +62,7 @@ describe.each(TestCasesResult)(
({ description, input, expectedResult }) => {
it(description, () => {
const table = TruthTable.create(input);
expect(table.andWithInputs).toMatchObject(expectedResult);
expect(table.result).toMatchObject(expectedResult);
});
}
);
4 changes: 2 additions & 2 deletions tests/unit/nand.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TruthTable from '../../src/TruthTable.mjs';
import TruthTable from '../../src/NandTruthTable.mjs';

const TestCasesResult = [
{
Expand Down Expand Up @@ -45,7 +45,7 @@ describe.each(TestCasesResult)(
({ description, input, expectedResult }) => {
it(description, () => {
const table = TruthTable.create(input);
expect(table.nand).toMatchObject(expectedResult);
expect(table.output).toMatchObject(expectedResult);
});
}
);
4 changes: 2 additions & 2 deletions tests/unit/nandResult.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TruthTable from '../../src/TruthTable.mjs';
import TruthTable from '../../src/NandTruthTable.mjs';

const TestCasesResult = [
{
Expand Down Expand Up @@ -62,7 +62,7 @@ describe.each(TestCasesResult)(
({ description, input, expectedResult }) => {
it(description, () => {
const table = TruthTable.create(input);
expect(table.nandWithInputs).toMatchObject(expectedResult);
expect(table.result).toMatchObject(expectedResult);
});
}
);
4 changes: 2 additions & 2 deletions tests/unit/nor.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TruthTable from '../../src/TruthTable.mjs';
import TruthTable from '../../src/NorTruthTable.mjs';

const TestCasesResult = [
{
Expand Down Expand Up @@ -45,7 +45,7 @@ describe.each(TestCasesResult)(
({ description, input, expectedResult }) => {
it(description, () => {
const table = TruthTable.create(input);
expect(table.nor).toMatchObject(expectedResult);
expect(table.output).toMatchObject(expectedResult);
});
}
);

0 comments on commit 11e5412

Please sign in to comment.