Permalink
Browse files

Command Line Interface loads faster

  • Loading branch information...
1 parent d76b342 commit cc1a6925af3ea846d873f46cd5876fe66be05bc9 @josdejong committed Oct 30, 2016
Showing with 21 additions and 1 deletion.
  1. +5 −0 HISTORY.md
  2. +16 −1 bin/cli.js
View
@@ -1,6 +1,11 @@
# History
+## 2016-10-21, version 3.6.1
+
+- Command Line Interface loads faster.
+
+
## 2016-10-21, version 3.6.0
- Implemented function `erf()`. THanks @patgrasso.
View
@@ -44,18 +44,29 @@
* the License.
*/
-var math = require('../index');
var scope = {};
var fs = require('fs');
var PRECISION = 14; // decimals
/**
+ * "Lazy" load math.js: only require when we actually start using it.
+ * This ensures the cli application looks like it loads instantly.
+ * When requesting help or version number, math.js isn't even loaded.
+ * @return {*}
+ */
+function getMath () {
+ return require('../index');
+}
+
+/**
* Helper function to format a value. Regular numbers will be rounded
* to 14 digits to prevent round-off errors from showing up.
* @param {*} value
*/
function format(value) {
+ var math = getMath();
+
return math.format(value, {
fn: function (value) {
if (typeof value === 'number') {
@@ -75,6 +86,7 @@ function format(value) {
* @return {[Array, String]} completions
*/
function completer (text) {
+ var math = getMath();
var name;
var matches = [];
var m = /[a-zA-Z_0-9]+$/.exec(text);
@@ -171,6 +183,9 @@ function runStream (input, output, mode, parenthesis) {
rl.prompt();
}
+ // load math.js now, right *after* loading the prompt.
+ var math = getMath();
+
// TODO: automatic insertion of 'ans' before operators like +, -, *, /
rl.on('line', function(line) {

0 comments on commit cc1a692

Please sign in to comment.