From 3abc12a76c6231e2a0dbf32e0e1a7688dde78406 Mon Sep 17 00:00:00 2001 From: Gorrie Date: Fri, 24 Mar 2017 23:20:12 +1300 Subject: [PATCH 1/3] Multiple changes Added string-template for formating. Refer to https://www.npmjs.com/package/string-template Removed L file. Now calls logic file directly. Updated documentation to suite. --- README.md | 24 ++++++++++++++---------- package.json | 6 ++++-- src/L.js | 1 - src/eval.js | 24 ------------------------ src/logic.js | 12 ++++++++++++ test/unit/eval.js | 44 ++++++++++++++++++++++---------------------- 6 files changed, 52 insertions(+), 59 deletions(-) delete mode 100644 src/L.js delete mode 100644 src/eval.js create mode 100644 src/logic.js diff --git a/README.md b/README.md index 4009f27..5131e62 100644 --- a/README.md +++ b/README.md @@ -29,39 +29,43 @@ Calculate logical expressions in string. ## Usage ``` -const L = require('logic-string'); +const logic = require('logic-string'); -L.eval('true && false'); // false -L.eval('true || false'); // true +logic('true && false'); // false +logic('true || false'); // true -L.eval('a && b', { // false +logic('{a} && {b}', { // false a: true, b: false, }); -L.eval('a || b', { // true +logic('{a} || {b}', { // true a: true, b: false, }); -L.eval('!false'); // true -L.eval('!true'); // false +logic('!false'); // true +logic('!true'); // false -L.eval('(a && b) || c', { // true +logic('({a} && {b}) || {c}', { // true a: true, b: true, c: false, }); -L.eval('( a && b )||c', { // true +logic('( {a} && {b} )||{c}', { // true a: true, b: true, c: false, }); + +logic('{int} > 2', { // true + int: 5 +}); ``` ## API -### L.eval(pattern, variables) +### logic(pattern, variables) ```js /** diff --git a/package.json b/package.json index f4530e9..6b7121e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "logic-string", "version": "0.1.0", "description": "Calculate logical expressions in string.", - "main": "src/L.js", + "main": "src/logic.js", "scripts": { "lint": "npm-run-all lint-*", "lint-src": "eslint --quiet src", @@ -30,7 +30,9 @@ "bugs": { "url": "https://github.com/adoyle-h/logic-string/issues" }, - "dependencies": {}, + "dependencies": { + "string-template": "^1.0.0" + }, "devDependencies": { "ava": "^0.17.0", "eslint": "^3.12.2", diff --git a/src/L.js b/src/L.js deleted file mode 100644 index ca1f0ea..0000000 --- a/src/L.js +++ /dev/null @@ -1 +0,0 @@ -exports.eval = require('./eval.js'); diff --git a/src/eval.js b/src/eval.js deleted file mode 100644 index 4830d00..0000000 --- a/src/eval.js +++ /dev/null @@ -1,24 +0,0 @@ -const regex = /[A-Za-z0-9_-]+/g; - -function format(pattern, variables) { - if (!variables) return pattern; - return pattern.replace(regex, (matched) => { - const replaced = variables[matched]; - if (replaced === undefined) { - throw new Error(`No any matched variable for "${matched}"`); - } - return replaced; - }); -} - -/** - * It is based on `eval` function. You should be in charge of the security with injection risks. - * - * @param {String} pattern - * @param {Object} [variables] - * @return {Boolean} - */ -module.exports = (pattern, variables) => { - const str = format(pattern, variables); - return eval(str); // eslint-disable-line no-eval -}; diff --git a/src/logic.js b/src/logic.js new file mode 100644 index 0000000..5a351b1 --- /dev/null +++ b/src/logic.js @@ -0,0 +1,12 @@ +var format = require("string-template") + +/** + * It is based on `eval` function. You should be in charge of the security with injection risks. + * + * @param {String} pattern + * @param {Object} [variables] + * @return {Boolean} + */ +module.exports = (pattern, variables) => { + return eval(format(pattern, variables)); // eslint-disable-line no-eval +}; diff --git a/test/unit/eval.js b/test/unit/eval.js index 0adc7f4..b4e5486 100644 --- a/test/unit/eval.js +++ b/test/unit/eval.js @@ -1,24 +1,24 @@ import test from 'ava'; -import L from '../../src/L'; +import logic from '../../src/logic'; test('true && false', (t) => { - t.is(L.eval('true && false'), false); + t.is(logic('true && false'), false); }); test('true || false', (t) => { - t.is(L.eval('true || false'), true); + t.is(logic('true || false'), true); }); -test('a && b', (t) => { - const result = L.eval('a && b', { +test('{a} && {b}', (t) => { + const result = logic('{a} && {b}', { a: true, b: false, }); t.is(result, false); }); -test('a || b', (t) => { - const result = L.eval('a || b', { +test('{a} || {b}', (t) => { + const result = logic('{a} || {b}', { a: true, b: false, }); @@ -26,20 +26,20 @@ test('a || b', (t) => { }); test('!true', (t) => { - t.is(L.eval('!true'), false); + t.is(logic('!true'), false); }); test('!false', (t) => { - t.is(L.eval('!false'), true); + t.is(logic('!false'), true); }); test('!a', (t) => { - t.is(L.eval('!a', {a: false}), true); - t.is(L.eval('!a', {a: true}), false); + t.is(logic('!a', {a: false}), true); + t.is(logic('!a', {a: true}), false); }); -test('a && b || c', (t) => { - const result = L.eval('a && b || c', { +test('{a} && {b} || {c}', (t) => { + const result = logic('{a} && {b} || {c}', { a: true, b: false, c: false, @@ -47,8 +47,8 @@ test('a && b || c', (t) => { t.is(result, false); }); -test('a && (b || c)', (t) => { - const result = L.eval('a && (b || c)', { +test('{a} && ({b} || {c})', (t) => { + const result = logic('{a} && ({b} || {c})', { a: true, b: false, c: false, @@ -56,8 +56,8 @@ test('a && (b || c)', (t) => { t.is(result, false); }); -test('a && b || c', (t) => { - const result = L.eval('a && b || c', { +test('{a} && {b} || {c}', (t) => { + const result = logic('{a} && {b} || {c}', { a: true, b: true, c: false, @@ -65,8 +65,8 @@ test('a && b || c', (t) => { t.is(result, true); }); -test('(a && b) || c', (t) => { - const result = L.eval('(a && b) || c', { +test('({a} && {b}) || {c}', (t) => { + const result = logic('({a} && {b}) || {c}', { a: true, b: true, c: false, @@ -74,8 +74,8 @@ test('(a && b) || c', (t) => { t.is(result, true); }); -test('( a && b )||c', (t) => { - const result = L.eval('( a && b )||c', { +test('( {a} && {b} )||{c}', (t) => { + const result = logic('( {a} && {b} )||{c}', { a: true, b: true, c: false, @@ -84,7 +84,7 @@ test('( a && b )||c', (t) => { }); test('throw error when no matched variable ', (t) => { - const error = t.throws(() => L.eval('!a', {ha: false})); + const error = t.throws(() => logic('!{a}', {ha: false})); t.is(error instanceof Error, true); t.is(error.message, 'No any matched variable for "a"'); }); From da20959c8c62025bdf6f2739f0e3f77b4fcfc2e9 Mon Sep 17 00:00:00 2001 From: Gorrie Date: Sat, 25 Mar 2017 10:18:25 +1300 Subject: [PATCH 2/3] Fixes to errors --- src/logic.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logic.js b/src/logic.js index 5a351b1..4cb31cb 100644 --- a/src/logic.js +++ b/src/logic.js @@ -1,4 +1,4 @@ -var format = require("string-template") +const format = require('string-template'); /** * It is based on `eval` function. You should be in charge of the security with injection risks. From cccdd08d9147a3f90829c89fac36fb2665d4c14d Mon Sep 17 00:00:00 2001 From: Gorrie Date: Sat, 25 Mar 2017 12:52:14 +1300 Subject: [PATCH 3/3] Fix errors in test --- test/unit/eval.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/eval.js b/test/unit/eval.js index b4e5486..2640565 100644 --- a/test/unit/eval.js +++ b/test/unit/eval.js @@ -33,9 +33,9 @@ test('!false', (t) => { t.is(logic('!false'), true); }); -test('!a', (t) => { - t.is(logic('!a', {a: false}), true); - t.is(logic('!a', {a: true}), false); +test('!{a}', (t) => { + t.is(logic('!{a}', {a: false}), true); + t.is(logic('!{a}', {a: true}), false); }); test('{a} && {b} || {c}', (t) => {