Node.js REPL with lodash
Sometimes we use the Node.js REPL interface to experiment with code. Wouldn’t it be great to have that interface with lodash required by default?
$ npm install -g n_
$ n_
n_ >
lodash is now attached to the REPL context as _
, so just use it:
n_ > _.compact([0, 1, false, 2, '', 3]);
[ 1, 2, 3 ]
n_ >
It is possible to use lodash’s functional programming variant lodash/fp
:
$ n_ --fp
n_ > _.map(function(v) { return v * 2; }, [1, 2, 3]);
[ 2, 4, 6 ]
n_ >
It is possible to enable strict mode in Node.js >= 4.x:
$ n_ --use_strict
n_ >
Some commands are available to facilitate some operations, and are host under .lodash
repl command:
.lodash fp
: switch to lodash/fp.lodash vanilla
: switch to vanilla lodash mode.lodash reset
: switch to initial lodash mode.lodash swap
: switch to the other lodash mode (vanilla/fp).lodash current
: output current lodash flavor in use.lodash version
: output lodash version in use
and the .lodash help
to have more details about lodash repl commands
Special character _
refer to the lodash instance, and cannot hold value of last expression.
To provide the same feature, __
was introduced:
n_ > 10 + 2
12
n_ > 'number '+ __
'number 12'
Aside --fp
and --use_strict
/--use-strict
, some other options are available either as CLI flags, or via environment variables.(with a trailing _N_
)
The two main feature you can control is History persistance and Prompt Theme.
Flag | aliases | Env variable | Description | Default |
---|---|---|---|---|
--history-path |
--history , history-file |
_N_HISTORY_PATH |
Location of repl history file | ~/.n_repl_history |
--prompt.symbol |
_N_PROMPT__SYMBOL |
Symbol to use as $ prompt |
> |
|
--prompt.name |
_N_PROMPT__NAME |
Name for the prompt | n_ |
|
--prompt.color.name |
_N_PROMPT__COLOR__NAME |
Color for prompt name n_ |
blue |
|
--prompt.color.symbol |
_N_PROMPT__COLOR__SYMBOL |
Color for prompt symbol | red |
|
--prompt.color.flavor |
_N_PROMPT__COLOR__FLAVOR |
Color for section of prompt about lodash flavor in use | cyan |
|
--prompt.color.help |
_N_PROMPT__COLOR__HELP |
Color for section of prompt about lodash flavor in use | green |
About styling, valid colors are: black
, red
, green
, yellow
, blue
, magenta
, cyan
, white
,gray
, and dim
.
Enjoy! 🚀