From 52592771703ac2a6e2094cd482f89dbc869eb373 Mon Sep 17 00:00:00 2001 From: chocolateboy Date: Mon, 3 Apr 2017 20:17:02 +0100 Subject: [PATCH] allow assignment to underscore (_) without a warning (closes #22) --- cli.js | 23 ++++++++++++++++++++++- readme.md | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index a6536cf..9a6dd70 100755 --- a/cli.js +++ b/cli.js @@ -88,7 +88,28 @@ if (hasFlag('--clear')) { } }) replHistory(replServer, TRYMODULE_HISTORY_PATH) - replServer.context = Object.assign(replServer.context, contextPackages) + Object.keys(contextPackages).forEach(as => { + var pkg = contextPackages[as] + // special-case assignment to _ so that it returns + // the required package (e.g. lodash) rather than the + // value of the last expression + if (as === '_') { + Object.defineProperty(replServer.context, as, { + configurable: true, + enumerable: false, + get: function () { return pkg }, + // allow (temporary) assignment to _: + // + // > _ = 42 + // 42 + // > _.each + // [Function: forEach] + set: function (val) { return val } + }) + } else { + replServer.context[as] = pkg + } + }) } }) } diff --git a/readme.md b/readme.md index 0205ac6..0989727 100644 --- a/readme.md +++ b/readme.md @@ -18,7 +18,7 @@ Downloads the module colors if needed, and starts a nodejs REPL with colors load Same as above but with many packages in one go! -`trymodule colors=c lodash=l` +`trymodule colors=c lodash=_` Assign packages to custom variable names.