From 1050b712bbf36550f6cfbad4ac186d6511aa1eae Mon Sep 17 00:00:00 2001 From: Dana Sulit Date: Mon, 30 Oct 2017 11:45:25 -0400 Subject: [PATCH] Start writing expressions with jsep --- package-lock.json | 5 +++++ package.json | 3 +++ src/ast-to-expression.js | 11 +++++++++++ src/index.js | 5 ----- src/string-to-ast.js | 7 +++++++ test/ast-to-expression.test.js | 24 ++++++++++++++++++++++++ test/index.test.js | 7 ------- test/string-to-ast.test.js | 23 +++++++++++++++++++++++ 8 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 src/ast-to-expression.js delete mode 100644 src/index.js create mode 100644 src/string-to-ast.js create mode 100644 test/ast-to-expression.test.js delete mode 100644 test/index.test.js create mode 100644 test/string-to-ast.test.js diff --git a/package-lock.json b/package-lock.json index b2b2214..16ab23d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3785,6 +3785,11 @@ } } }, + "jsep": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/jsep/-/jsep-0.3.2.tgz", + "integrity": "sha512-b/QP7Apx3EEvFk5/O+bp8BmMP97EypcxqTXWs8nK+Mr9DKhxLX/EWlxERtCZ7mwWIssnOLGCGMoJ08oMiwrjIw==" + }, "jsesc": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/jsesc/-/jsesc-1.3.0.tgz", diff --git a/package.json b/package.json index b076a54..992d68b 100644 --- a/package.json +++ b/package.json @@ -46,5 +46,8 @@ "jest": "^21.2.1", "lint-staged": "^4.3.0", "prettier": "^1.7.4" + }, + "dependencies": { + "jsep": "^0.3.2" } } diff --git a/src/ast-to-expression.js b/src/ast-to-expression.js new file mode 100644 index 0000000..21fcc1a --- /dev/null +++ b/src/ast-to-expression.js @@ -0,0 +1,11 @@ +function astToExpression(input) { + if (input.value) return input.value; + if (input.type === 'BinaryExpression') { + const left = astToExpression(input.left); + const right = astToExpression(input.right); + + return [input.operator, left, right]; + } +} + +export default astToExpression; diff --git a/src/index.js b/src/index.js deleted file mode 100644 index 4b1a58c..0000000 --- a/src/index.js +++ /dev/null @@ -1,5 +0,0 @@ -function returnTrue() { - return true; -} - -export default returnTrue; diff --git a/src/string-to-ast.js b/src/string-to-ast.js new file mode 100644 index 0000000..1fd6b73 --- /dev/null +++ b/src/string-to-ast.js @@ -0,0 +1,7 @@ +import jsep from 'jsep'; + +function stringToAst(input) { + return jsep(input); +} + +export default stringToAst; diff --git a/test/ast-to-expression.test.js b/test/ast-to-expression.test.js new file mode 100644 index 0000000..0fa078e --- /dev/null +++ b/test/ast-to-expression.test.js @@ -0,0 +1,24 @@ +import stringToAst from '../src/string-to-ast'; +import astToExpression from '../src/ast-to-expression'; + +describe('astToExpression', () => { + test('3', () => { + const actual = astToExpression(stringToAst('3')); + expect(actual).toBe(3); + }); + + test('3 + 4', () => { + const actual = astToExpression(stringToAst('3 + 4')); + expect(actual).toEqual(['+', 3, 4]); + }); + + test('(3 + 4) / 7', () => { + const actual = astToExpression(stringToAst('(3 + 4) / 7')); + expect(actual).toEqual(['/', ['+', 3, 4], 7]); + }); + + test('((3 + 4) * 2) / 7', () => { + const actual = astToExpression(stringToAst('((3 + 4) * 2) / 7')); + expect(actual).toEqual(['/', ['*', ['+', 3, 4], 2], 7]); + }); +}); diff --git a/test/index.test.js b/test/index.test.js deleted file mode 100644 index 1dc338b..0000000 --- a/test/index.test.js +++ /dev/null @@ -1,7 +0,0 @@ -import returnTrue from '../src/index'; - -describe('returnTrue', function() { - test('returns true', function() { - expect(returnTrue()).toBe(true); - }); -}); diff --git a/test/string-to-ast.test.js b/test/string-to-ast.test.js new file mode 100644 index 0000000..ca47638 --- /dev/null +++ b/test/string-to-ast.test.js @@ -0,0 +1,23 @@ +import stringToAst from '../src/string-to-ast'; + +describe('stringToAst', () => { + test('3 + 4', () => { + const actual = stringToAst('3 + 4'); + const output = { + type: 'BinaryExpression', + operator: '+', + left: { + type: 'Literal', + value: 3, + raw: '3' + }, + right: { + type: 'Literal', + value: 4, + raw: '4' + } + }; + + expect(actual).toEqual(output); + }); +});