Skip to content

Commit

Permalink
feat(Node.ts): Native treefier
Browse files Browse the repository at this point in the history
  • Loading branch information
dedoussis committed Nov 20, 2019
1 parent 88faa0d commit a4f7dd1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 28 deletions.
12 changes: 1 addition & 11 deletions package-lock.json

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

13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "algebrain",
"version": "0.0.3c",
"version": "0.0.3e",
"description": "Combuter Algebra System focusing on symbolic transformations",
"keywords": [
"algebra",
Expand All @@ -13,9 +13,9 @@
"mathematics",
"compiler"
],
"main": "dist/lib/src/index.js",
"module": "dist/lib/src/index.js",
"typings": "dist/types/src/index.d.ts",
"main": "dist/lib/index.js",
"module": "dist/lib/index.js",
"typings": "dist/types/index.d.ts",
"files": [
"dist"
],
Expand All @@ -35,7 +35,6 @@
"test": "jest --coverage",
"test:watch": "jest --coverage --watch",
"test:prod": "npm run lint && npm run test -- --no-cache",
"deploy-docs": "ts-node tools/gh-pages-publish",
"report-coverage": "cat ./coverage/lcov.info | coveralls",
"commit": "git-cz",
"precommit": "lint-staged",
Expand Down Expand Up @@ -117,9 +116,7 @@
},
"dependencies": {
"@types/core-js": "^2.5.2",
"@types/treeify": "^1.0.0",
"antlr4ts": "^0.5.0-alpha.3",
"immutable": "^4.0.0-rc.12",
"treeify": "^1.1.0"
"immutable": "^4.0.0-rc.12"
}
}
12 changes: 5 additions & 7 deletions src/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { List, Map } from 'immutable';
import Executable, { Namespace, Output } from './Executable';
import Node from './Node';
import Transformation from './Transformation';
import { version } from '../package.json';
import { asTree } from 'treeify';

export default class Command implements Executable {
readonly execute: ExecuteFunc;
Expand Down Expand Up @@ -65,7 +63,7 @@ export const commandRegistry: Map<CommandName, CommandSpec> = Map([
stdOut: transformed.toString(),
};
},
description: 'Transform current expression using the active active transformation',
description: 'Transform current expression using the active transformation',
},
],
[
Expand Down Expand Up @@ -118,7 +116,7 @@ export const commandRegistry: Map<CommandName, CommandSpec> = Map([
executeConstructor: (_: Command): ExecuteFunc => (namespace: Namespace) => {
return {
namespace: namespace,
stdOut: `--- algebrain version ${version} ---
stdOut: `--- algebrain version 0.0.3-d ---
Commands:
${commandRegistry
.entrySeq()
Expand All @@ -133,18 +131,18 @@ export const commandRegistry: Map<CommandName, CommandSpec> = Map([
CommandName.Tree,
{
executeConstructor: (_: Command): ExecuteFunc => (namespace: Namespace) => {
if (namespace.expression === undefined) {
if (!(namespace.expression instanceof Node)) {
return {
namespace: namespace,
stdOut: ExecuteError.UndefinedExpression,
};
}
return {
namespace: namespace,
stdOut: asTree(namespace.expression as any, true, true),
stdOut: namespace.expression.treeify(),
};
},
description: 'Tree represantion of expression',
description: 'Tree representation of expression',
},
],
]);
Expand Down
18 changes: 18 additions & 0 deletions src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export default class Node implements Executable {
return this;
}

treeify(..._: any[]) {
return this.toString();
}

execute(namespace: Namespace): Output {
const evaluated: Node = this.evaluate();
return {
Expand Down Expand Up @@ -111,6 +115,20 @@ export class Operator extends Node {
return infix(this.value, stringifiedChildren);
}

treeify(childPrefix: string = '', space: string = '\xa0'): string {
return this.children.reduce(
(treeified: string, child: Node, index: number, children: List<Node>) => {
const [branch, prefixExtention]: [string, string] =
index === children.size - 1 ? ['└─', space] : ['β”œβ”€', 'β”‚'];
return `${treeified}\n${childPrefix}${branch} ${child.treeify(
`${childPrefix}${prefixExtention}${space}${space}`,
space
)}`;
},
this.value
);
}

evaluate(): Node {
const evaluatedChildren: List<Node> = this.evaluator.recursive
? this.children
Expand Down
31 changes: 31 additions & 0 deletions test/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,35 @@ describe('Operator', () => {
);
expect(one.equals(another)).toBeFalsy();
});

it('it treeifies', () => {
const expr: Operator = new Operator(
OperatorSymbol.Div,
List([
new Operator(
OperatorSymbol.Plus,
List([
new Operator(
OperatorSymbol.Minus,
List([new Rewritable('u'), new Symbol('x')])
),
new Num(5),
])
),
new Operator(OperatorSymbol.Pow, List([new Symbol('g'), new Num(5)])),
])
);
const treefied: string[] = [
'/',
'β”œβ”€ +',
'β”‚ β”œβ”€ -',
'β”‚ β”‚ β”œβ”€ $u',
'β”‚ β”‚ └─ x',
'β”‚ └─ 5',
'└─ ^',
' β”œβ”€ g',
' └─ 5',
];
expect(expr.treeify('', ' ')).toEqual(treefied.join('\n'));
});
});
2 changes: 0 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"compilerOptions": {
"resolveJsonModule": true,
"esModuleInterop": true,
"strictPropertyInitialization": false,
"moduleResolution": "node",
"target": "es2017",
Expand Down

0 comments on commit a4f7dd1

Please sign in to comment.