Skip to content

Multiple changes #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion src/L.js

This file was deleted.

24 changes: 0 additions & 24 deletions src/eval.js

This file was deleted.

12 changes: 12 additions & 0 deletions src/logic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const 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
};
46 changes: 23 additions & 23 deletions test/unit/eval.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
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,
});
t.is(result, true);
});

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);
test('!{a}', (t) => {
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,
});
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,
});
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,
});
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,
});
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,
Expand All @@ -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"');
});