aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPriyansh <[email protected]>2021-03-07 20:17:21 +0530
committerPriyansh <[email protected]>2021-03-07 20:17:21 +0530
commit1c2e74850b073b5963e7d10081e3c893f0eb6a9b (patch)
tree0d4c3012fe9a0d8e6a35a32da6639a0cb363b223
parentf7cd4af089eac108716bb8336221e63b295f4d10 (diff)
downloadcalculator-1c2e74850b073b5963e7d10081e3c893f0eb6a9b.tar.xz
calculator-1c2e74850b073b5963e7d10081e3c893f0eb6a9b.zip
Added MathJS Library
-rw-r--r--bin/index.js27
-rw-r--r--package-lock.json55
-rw-r--r--package.json1
3 files changed, 79 insertions, 4 deletions
diff --git a/bin/index.js b/bin/index.js
index 6c1d14b..6d87d1d 100644
--- a/bin/index.js
+++ b/bin/index.js
@@ -5,6 +5,7 @@ const terminalLink = require('terminal-link');
const boxen = require("boxen");
const chalk = require("chalk");
const packageInfo = require('./../package.json');
+const math = require('mathjs');
const infoMessage = {
getIntroductoryMessage: () => {
// If the command is run without any options we will show the about screen
@@ -38,7 +39,7 @@ Command Help Example:
$0 add --help`)
.command({
command: 'add',
- describe: 'Takes a list of numbers all separated by commas and adds them and returns the output. Optional arguments can be passed.',
+ describe: 'Takes a list of numbers all separated by commas and adds them consecutively and returns the output. Optional arguments can be passed.',
builder: (yargs) => yargs
.option('rt', {
alias: 'round-to',
@@ -47,10 +48,28 @@ Command Help Example:
}),
handler: function (args) {
if (args._.length === 2) {
- const result = args._[1].split(',').reduce((a, b) => Number(a) + Number(b), 0)
- console.log(args.rt ? Math.round((result + Number.EPSILON) * Math.pow(10, args.rt)) / Math.pow(10, args.rt) : result)
+ const result = math.evaluate(args._[1].split(',').join('+'));
+ console.log(args.rt ? math.round(result, args.rt) : result)
} else {
- infoMessage.showError('add', 'needs a list of comma-separated numbers to be passed.');
+ infoMessage.showError('add', `needs 2 arugments, ${args._.length} passed.`);
+ }
+ }
+ })
+ .command({
+ command: ['sub', 'subtract'],
+ describe: 'Takes a list of numbers all separated by commas and subtracts them consecutively and returns the output. Optional arguments can be passed.',
+ builder: (yargs) => yargs
+ .option('rt', {
+ alias: 'round-to',
+ desc: 'Round upto the number of decimal places passed',
+ type: 'number'
+ }),
+ handler: function (args) {
+ if (args._.length === 2) {
+ const result = math.evaluate(args._[1].split(',').join('-'));
+ console.log(args.rt ? math.round(result, args.rt) : result)
+ } else {
+ infoMessage.showError('subtract', `needs 2 arugments, ${args._.length} passed.`);
}
}
})
diff --git a/package-lock.json b/package-lock.json
index d91760d..28806e8 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -133,6 +133,16 @@
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz",
"integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU="
},
+ "complex.js": {
+ "version": "2.0.12",
+ "resolved": "https://registry.npmjs.org/complex.js/-/complex.js-2.0.12.tgz",
+ "integrity": "sha512-oQX99fwL6LrTVg82gDY1dIWXy6qZRnRL35N+YhIX0N7tSwsa0KFy6IEMHTNuCW4mP7FS7MEqZ/2I/afzYwPldw=="
+ },
+ "decimal.js": {
+ "version": "10.2.1",
+ "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.2.1.tgz",
+ "integrity": "sha512-KaL7+6Fw6i5A2XSnsbhm/6B+NuEA7TZ4vqxnd5tXz9sbKtrN9Srj8ab4vKVdK8YAqZO9P1kg45Y6YLoduPf+kw=="
+ },
"emoji-regex": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz",
@@ -143,11 +153,21 @@
"resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz",
"integrity": "sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw=="
},
+ "escape-latex": {
+ "version": "1.2.0",
+ "resolved": "https://registry.npmjs.org/escape-latex/-/escape-latex-1.2.0.tgz",
+ "integrity": "sha512-nV5aVWW1K0wEiUIEdZ4erkGGH8mDxGyxSeqPzRNtWP7ataw+/olFObw7hujFWlVjNsaDFw5VZ5NzVSIqRgfTiw=="
+ },
"escape-string-regexp": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
"integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ="
},
+ "fraction.js": {
+ "version": "4.0.13",
+ "resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-4.0.13.tgz",
+ "integrity": "sha512-E1fz2Xs9ltlUp+qbiyx9wmt2n9dRzPsS11Jtdb8D2o+cC7wr9xkkKsVKJuBX0ST+LVS+LhLO+SbLJNtfWcJvXA=="
+ },
"get-caller-file": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
@@ -163,11 +183,36 @@
"resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz",
"integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8="
},
+ "javascript-natural-sort": {
+ "version": "0.7.1",
+ "resolved": "https://registry.npmjs.org/javascript-natural-sort/-/javascript-natural-sort-0.7.1.tgz",
+ "integrity": "sha1-+eIwPUUH9tdDVac2ZNFED7Wg71k="
+ },
+ "mathjs": {
+ "version": "9.2.0",
+ "resolved": "https://registry.npmjs.org/mathjs/-/mathjs-9.2.0.tgz",
+ "integrity": "sha512-R2fQxaOmyifxgP4+c59dnfLwpKI1KYHdnT5lLwDuHIZvgyGb71M8ay6kTJTEv9rG04pduqvX4tbBUoG5ypTF8A==",
+ "requires": {
+ "complex.js": "^2.0.11",
+ "decimal.js": "^10.2.1",
+ "escape-latex": "^1.2.0",
+ "fraction.js": "^4.0.13",
+ "javascript-natural-sort": "^0.7.1",
+ "seedrandom": "^3.0.5",
+ "tiny-emitter": "^2.1.0",
+ "typed-function": "^2.0.0"
+ }
+ },
"require-directory": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
"integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I="
},
+ "seedrandom": {
+ "version": "3.0.5",
+ "resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-3.0.5.tgz",
+ "integrity": "sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg=="
+ },
"string-width": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
@@ -232,11 +277,21 @@
"supports-hyperlinks": "^2.0.0"
}
},
+ "tiny-emitter": {
+ "version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/tiny-emitter/-/tiny-emitter-2.1.0.tgz",
+ "integrity": "sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q=="
+ },
"type-fest": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.5.2.tgz",
"integrity": "sha512-DWkS49EQKVX//Tbupb9TFa19c7+MK1XmzkrZUR8TAktmE/DizXoaoJV6TZ/tSIPXipqNiRI6CyAe7x69Jb6RSw=="
},
+ "typed-function": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/typed-function/-/typed-function-2.0.0.tgz",
+ "integrity": "sha512-Hhy1Iwo/e4AtLZNK10ewVVcP2UEs408DS35ubP825w/YgSBK1KVLwALvvIG4yX75QJrxjCpcWkzkVRB0BwwYlA=="
+ },
"widest-line": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/widest-line/-/widest-line-3.1.0.tgz",
diff --git a/package.json b/package.json
index fd398a0..f5b5189 100644
--- a/package.json
+++ b/package.json
@@ -29,6 +29,7 @@
"dependencies": {
"boxen": "^4.0.0",
"chalk": "^2.4.2",
+ "mathjs": "^9.2.0",
"terminal-link": "^2.1.1",
"yargs": "^16.2.0"
}