Skip to content

Commit

Permalink
cli, update docs, added changelog, remove innecesary files
Browse files Browse the repository at this point in the history
  • Loading branch information
juliandavidmr committed Jul 10, 2017
1 parent b37538e commit 19c6757
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 49 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -1,3 +1,6 @@
# js
src/parser.js

# Logs
logs
*.log
Expand Down Expand Up @@ -57,3 +60,4 @@ typings/
# dotenv environment variables file
.env

/.idea/
15 changes: 15 additions & 0 deletions .npmignore
@@ -0,0 +1,15 @@
.*.swp
._*
.DS_Store
.git
.hg
.npmrc
.lock-wscript
.svn
.wafpickle-*
config.gypi
CVS
npm-debug.log
/.idea/
/src/dev.js
/src/grammar.js
17 changes: 17 additions & 0 deletions CHANGELOG
@@ -0,0 +1,17 @@
# Sylver changelog

## Version 1.0.2 #
- [[]()] .
- [[]()] CLI.

## Version 1.0.1 #
- [[de802a0](https://github.com/juliandavidmr/sylver/commit/b37538ebb08eaf251cc1b99038a2ca884be4c696)]
Update readme.

## Version 1.0.0 #

- [[55aff29](https://github.com/juliandavidmr/sylver/commit/de802a02e6e97450ed8eb95043d134d07c4ef340)]
Basic operations: `+`, `-`, `/`, `*`, `~`, `^`.
- [[de802a0](https://github.com/juliandavidmr/sylver/commit/f99feae1470c2d8ee5608713744977244808d377)]
Trigonometry functions were added: `cos`, `sin`, `tan`, `acos`, `asin`, `atan`, `atanh`, `acosh`, `asinh`, `abs`.

21 changes: 19 additions & 2 deletions README.md
Expand Up @@ -22,7 +22,9 @@ Is easily extensible.
## Usage

```bash
npm install --save sylver # or yarn
npm install --save sylver
# or
yarn add sylver
```

```js
Expand Down Expand Up @@ -57,8 +59,21 @@ sylver("atanh(-0.4)") // -0.4236489301936018
sylver("abs(ln(2)*(4^3))*(if(3<>6) 2:5)") // 88.722839111673
```

## Browser support
### Usage globally _(Available in the next versions)_

```bash
npm i -g sylver

# or
yarn global add sylver
```
Enter from the command terminal:
```bash
sylver --help
```

## Browser support
__// TODO__
Sylver works on any ES5 compatible JavaScript engine: node.js 0.10, and Internet Explorer 9 and newer, and all other browsers (Chrome, Firefox, Safari). If support for old browsers like Internet Explorer 8 is required, the es5-shim library has to be loaded.

## Build
Expand Down Expand Up @@ -92,6 +107,8 @@ npm test
The tests are run with [ava](https://github.com/avajs/ava)


### [See changelog](./CHANGELOG)

## License

Copyright (c) 2017 Julian David
Expand Down
30 changes: 30 additions & 0 deletions cli/cli.js
@@ -0,0 +1,30 @@
const readline = require('readline');
const sylver = require('../');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

module.exports = function () {
rl.on('line', (input) => {
switch (input.trim()) {
case 'exit':
console.log('Exiting the command line interface. Bye :*');
process.exit(1);
break;
default:
try {
console.log(sylver(input));
} catch (e) {
console.log("Error: ", e);
}
break;
}
rl.prompt();
}).on('close', () => {
console.log('Have a great day!');
process.exit(0);
});
;
};
7 changes: 7 additions & 0 deletions cli/help.js
@@ -0,0 +1,7 @@
module.exports = `
Usage: sylver [expression] [flags]
A lightweight math library for JavaScript
Options:
-h, --help output usage information
-c, --cli access the command line interface (experimental)
`;
28 changes: 28 additions & 0 deletions cli/main.js
@@ -0,0 +1,28 @@
var log = console.log;


/**
* Has a flag
* @param {Array<string>} args
* @param {string} flag
* @returns {boolean}
*/
function has(args, flag) {
return (args.indexOf(flag) > -1);
}

/**
* main cli
* @param {Array} args
*/
function main(args) {
if (has(args, '-h') || has(args, '--help')) {
log(require('./help'));
} else if (has(args, '-c') || has(args, '--cli')) {
require('./cli')();
} else {
log('No argument is recognized. Enter sylver --help');
}
}

main(process.argv.slice(1));
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -3,6 +3,9 @@
"version": "1.0.1",
"description": "A lightweight math library for JavaScript",
"main": "./src/lib.js",
"bin": {
"sylver": "./cli/main.js"
},
"files": [
"src/",
"package.json",
Expand Down
14 changes: 7 additions & 7 deletions src/dev.js
Expand Up @@ -5,12 +5,12 @@ var parser = new Parser(require('./grammar'));
var parserSource = parser.generate();
// console.log('Source: ', parserSource)
function save(content) {
var fs = require('fs');
fs.writeFile("./parser.js", content, function (err) {
if (err) {
return console.log(err);
}
// console.log("The file was saved!");
});
var fs = require('fs');
fs.writeFile("./parser.js", content, function (err) {
if (err) {
return console.log(err);
}
console.log("The file parser was saved!");
});
}
save(parserSource);
8 changes: 4 additions & 4 deletions src/grammar.js
Expand Up @@ -17,7 +17,7 @@ module.exports = {
["ln\\b", "return 'log'"],
["print\\b", "return 'print'"],
["cos\\b", "return 'cos'"],
["sin\\b", "return 'sin'"],
["sin\\b", "return 'sin'"],
["tan\\b", "return 'tan'"],
["atan\\b", "return 'atan'"],
["acos\\b", "return 'acos'"],
Expand All @@ -44,7 +44,7 @@ module.exports = {
["<=", "return '<='"],
[">=\\b", "return '>='"],
["==\\b", "return '=='"],
["eq\\b", "return '=='"],
["eq\\b", "return '=='"],
["to\\b", "return 'to'"],
["if\\b", "return 'if'"],
[":", "return ':'"],
Expand Down Expand Up @@ -109,14 +109,14 @@ module.exports = {
["e %", "$$ = $1/100"],
["~ e", "$$ = ($2*-1)"],
["- e", "$$ = -$2", {"prec": "UMINUS"}],
["( e )", "$$ = $2"],
["( e )", "$$ = $2"],
["NUMBER", "$$ = Number(yytext)"],
["log e", "$$ = Math.log($2)"],
["cos e", "$$ = Math.cos($2)"],
["sin e", "$$ = Math.sin($2)"],
["tan e", "$$ = Math.tan($2)"],
["atan e", "$$ = Math.atan($2)"],
["acos e", "$$ = Math.acos($2)"],
["acos e", "$$ = Math.acos($2)"],
["asin e", "$$ = Math.asin($2)"],
["abs e", "$$ = Math.abs($2)"],
["acosh e", "$$ = Math.acosh($2)"],
Expand Down
4 changes: 4 additions & 0 deletions src/lib.js
@@ -1,5 +1,9 @@
var parser = require('./parser');

/**
* parse expression
* @param {string} input
*/
function exec(input) {
return parser.parse(input)
}
Expand Down
42 changes: 13 additions & 29 deletions test/basic.js
@@ -1,30 +1,14 @@
var test = require('ava');
var sylver = require('../')

test('2 is equal to 2', t => {
t.is(sylver("2"), 2)
})

test('3+2 is equal to 5', t => {
t.is(sylver("3+2"), 5)
})

test('(3*2)+6 is equal to 12', t => {
t.is(sylver("(3*2)+6"), 12)
})

test('Reverse symbol: ~16 is equal to -16', t => {
t.is(sylver("~16"), -16)
})

test('Reverse symbol 2: 3*~2 is equal to -6', t => {
t.is(sylver("3*~2"), -6)
})

test('The factorial of 7 is 5040', t => {
t.is(sylver("7!"), 5040)
})

test('The factorial of 13 is 6227020800', t => {
t.is(sylver("13!"), 6227020800)
})
var sylver = require('../');

test('Basic operations', t => {
t.is(sylver("2"), 2);
t.is(sylver("3+2"), 5);
t.is(sylver("(3*2)+6"), 12);
t.is(sylver("3*2+6"), 12);
t.is(sylver("25+5"), 25);
t.is(sylver("~16"), -16);
t.is(sylver("3*~2"), -6);
t.is(sylver("7!"), 5040);
t.is(sylver("13!"), 6227020800);
});
7 changes: 2 additions & 5 deletions test/constants.js
@@ -1,10 +1,7 @@
var test = require('ava');
var sylver = require('../')

test('Constant PI', t => {
test('Constants', t => {
t.is(sylver("PI"), Math.PI)
})

test('Constant E', t => {
t.is(sylver("E"), Math.E)
})
});
4 changes: 2 additions & 2 deletions test/interval.js
Expand Up @@ -3,10 +3,10 @@ var sylver = require('../')

test('2 to 8 is equal to [2, 3, 4, 5, 6, 7, 8]', t => {
t.deepEqual(sylver("2 to 8"), [2, 3, 4, 5, 6, 7, 8])
})
});

test('(4*PI) to (23/2)") is equal to [11.5, 12.5]', t => {
t.deepEqual(sylver("(4*PI) to (23/2)"), [11.5, 12.5])
})
});

// console.log(parser.parse("2 to (4*3)"))

0 comments on commit 19c6757

Please sign in to comment.