Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
stdlib-js/repl
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Read-Eval-Print Loop (REPL) environment.
A Read-Eval-Print Loop (REPL) environment is an interactive programming environment which takes individual user inputs (e.g., single expressions), evaluates those inputs, and returns the result. Accordingly, a program written in a REPL environment is executed piecewise and sequentially.
REPL environments find common use in exploratory programming, prototyping, and debugging.
The REPL environment exposed here is available both as a standalone application and as a library which is embeddable in other libraries and applications.
To use in Observable,
REPL=require('https://cdn.jsdelivr.net/gh/stdlib-js/repl@umd/browser.js')
To vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:
varREPL=require('path/to/vendor/umd/repl/index.js')
To include the bundle in a webpage,
<scripttype="text/javascript"src="https://cdn.jsdelivr.net/gh/stdlib-js/repl@umd/browser.js"></script>
If no recognized module system is present, access bundle contents via the global scope:
<scripttype="text/javascript">(function(){window.REPL;})();</script>
Returns aREPL instance.
// Create a new REPL:varrepl=newREPL();// ...// Close the REPL:repl.close();
The function accepts the followingoptions:
- input: input (readable) stream. Default:
stdin. - output: output (writable) stream. Default:
stdout. - sandbox:
booleanindicating whether to run a REPL in a sandboxed context. Default:false. - timeout: number of milliseconds to execute a command before terminating execution. Default:
4294967295. - isTTY:
booleanindicating whether the input and output streams should be treated like a TTY (terminal) and whether the REPL should use ANSI/VT100 escape codes when writing to the output stream. - inputPrompt: input prompt. If the input prompt includes the character sequence
%d, the input prompt includes line numbers. Default:'In [%d]: '. - outputPrompt: output prompt. If the output prompt includes the character sequence
%d, the output prompt includes line numbers. Default:'Out[%d]: '. - welcome: welcome message.
- padding: number of empty lines between consecutive commands. Default:
1. - load: file path specifying a JavaScript file to load and evaluate line-by-line (e.g., a previous REPL history file).
- save: file path specifying where to save REPL command history.
- log: file path specifying where to save REPL commands and printed output.
- quiet:
booleanindicating whether log information, confirmation messages, and other possible REPL diagnostics should be silenced. Default:false.
Returns a REPL context.
vardebug=require('@stdlib/streams/node/debug-sink');// Create a new REPL:varrepl=newREPL({'output':debug()});// ...// Return a new REPL context:varctx=repl.createContext();// ...// Close the REPL:repl.close();
Resets a REPL's execution context.
vardebug=require('@stdlib/streams/node/debug-sink');// Create a new REPL:varrepl=newREPL({'output':debug()});// ...// Reset the REPL context:repl.resetContext();// ...// Close the REPL:repl.close();
Clears a REPL's history.
vardebug=require('@stdlib/streams/node/debug-sink');// Create a new REPL:varrepl=newREPL({'output':debug()});// ...// Clear the REPL history:repl.clearHistory();// ...// Close the REPL:repl.close();
Clears user-defined documentation.
vardebug=require('@stdlib/streams/node/debug-sink');// Create a new REPL:varrepl=newREPL({'output':debug()});// ...// Clear user-defined documentation:repl.clearUserDocs();// ...// Close the REPL:repl.close();
Loads and evaluates a JavaScript file line-by-line.
vardebug=require('@stdlib/streams/node/debug-sink');// Create a new REPL:varrepl=newREPL({'output':debug()});// ...// Load and evaluate a file line-by-line:repl.load('./path/to/file.js',done);functiondone(){// Close the REPL:repl.close();}
Resets a REPL.
vardebug=require('@stdlib/streams/node/debug-sink');// Create a new REPL:varrepl=newREPL({'output':debug()});// ...// Reset the REPL:repl.reset();// ...// Close the REPL:repl.close();
Clears the entire REPL screen and scrollback history.
vardebug=require('@stdlib/streams/node/debug-sink');// Create a new REPL:varrepl=newREPL({'output':debug()});// ...// Clear the REPL:repl.clear();// ...// Close the REPL:repl.close();
This method isonly applicable for TTY REPLs. In non-TTY REPLs, this method is a non-operation.
Clears the current line.
vardebug=require('@stdlib/streams/node/debug-sink');// Create a new REPL:varrepl=newREPL({'output':debug()});// ...// Clear the current line:repl.clearLine();// ...// Close the REPL:repl.close();
This method isonly applicable for TTY REPLs. In non-TTY REPLs, this method is a non-operation.
Clears the current REPL command buffer (i.e., clear any command which has been buffered but not yet executed).
vardebug=require('@stdlib/streams/node/debug-sink');// Create a new REPL:varrepl=newREPL({'output':debug()});// ...// Clear the command buffer:repl.clearCommand();// ...// Close the REPL:repl.close();
Closes a REPL.
vardebug=require('@stdlib/streams/node/debug-sink');// Create a new REPL:varrepl=newREPL({'output':debug()});// ...// Close the REPL:repl.close();
REPL instances support the following commands...
Returns the package name associated with a provided alias or class instance.
In [1]: var v = alias2pkg( 'base.sin' )Out[1]: '@stdlib/math/base/special/sin'Note: only direct instances of documented built-in constructors are supported.
Returns aliases related to a specified alias or class instance.
In [1]: var v = alias2related( 'base.sin' )Out[1]: [...]Note: only direct instances of documented built-in constructors are supported.
Result of the last successfully executed command.
In [1]: identity( 3.14 )Out[1]: 3.14In [2]: ansOut[2]: 3.14Reads a value from a specifiedworkspace.
In [1]: workspace( 'foobar' );In [2]: var x = 3.14;In [3]: workspace( 'barfoo' );In [4]: var y = assignfrom( 'foobar', 'x' )Out[4]: 3.14Assigns avalue to avariable in a specifiedworkspace.
In [1]: assignin( 'base', 'x', 3.14 );In [2]: xOut[2]: 3.14Prints how to cite stdlib in publications.
In [1]: citation()Clears the entire REPL screen and scrollback history.
In [1]: clear()Note: this function is only applicable for TTY REPLs. In non-TTY REPLs, this function is a non-operation.
Clears the REPL history.
In [1]: clearHistory()Deletes user-defined documentation.
In [1]: clearUserDocs()The function supports the followingoptions:
include: inclusion filter. May be either an array-like
objector a regular expression.exclude: exclusion filter. May be either an array-like
objector a regular expression.filter: filter type. This option is only applicable for array-like
objectfilters. Must be one of the following values:'alias': filter based on documentation alias. If a filter matches a documentation alias, the user-defined documentation is removed.'value': filter based on object reference. If a filter matches an associated object reference, the user-defined documentation is removed.'*': filter based on both documentation alias and object reference. If a filter matches either a documentation alias or an associated object reference, the user-defined documentation is removed.
Default:
'*'.
In [1]: clearUserDocs( { 'include': /^foo/ } )Deletes user-defined variables in the current workspace.
In [1]: clearvars()The function supports the followingoptions:
- include: variable name inclusion filter. May be either an array-like
objector a regular expression. - exclude: variable name exclusion filter. May be either an array-like
objector a regular expression.
In [1]: clearvars( { 'include': /^foo/ } )Deletes user-defined variables in a specified workspace.
In [1]: clearWorkspace()By default, the function clears user-defined variables in the current workspace. To clear user-defined variables in a different workspace, provide a workspacename.
// Create a new workspace:In [1]: workspace( 'foobar' );In [2]: var x = 3.14;// Switch to another workspace:In [3]: workspace( 'barfoo' );In [4]: typeof xOut[4]: 'undefined'// List the variables in the previous workspace:In [5]: varsWorkspace( 'foobar' )Out[5]: [ 'x' ]// Delete the variables in the previous workspace:In [6]: clearWorkspace( 'foobar' );In [7]: varsWorkspace( 'foobar' )Out[7]: []// Navigate to the previous workspace:In [8]: workspace( 'foobar' );// Confirm that the variables were deleted:In [9]: xError: x is not definedThe function supports the followingoptions:
- include: variable name inclusion filter. May be either an array-like
objector a regular expression. - exclude: variable name exclusion filter. May be either an array-like
objector a regular expression.
In [1]: clearWorkspace( 'foobar', { 'include': /^x/ } );Prints a list of stdlib contributors.
In [1]: contributors()Prints copyright information.
In [1]: copyright()Prints credits.
In [1]: credits()Returns the name of the current workspace.
In [1]: currentWorkspaceOut[1]: 'base'Re-imports a module, JSON, or local file and all of its associated module dependencies.
In [1]: var foo = require( './foo.js' );// Modify `./foo.js` and/or its module dependencies...// Re-import the module:In [2]: foo = deeprerequire( './foo.js' );Modules can be imported fromnode_modules. Local modules and JSON files can be imported using a relative path (e.g.,'./foo.js','./../bar.json', etc) that will be resolved against the current working directory.
Deletes a workspace.
In [1]: workspace( 'foobar' );In [2]: var x = 3.14;In [3]: workspace( 'barfoo' );In [4]: workspaces()Out[4]: [ 'base', 'foobar', 'barfoo' ]In [5]: deleteWorkspace( 'foobar' );In [6]: workspaces()Out[6]: [ 'base', 'barfoo' ]If not provided an argument, the REPL deletes the current workspace and switches to the'base' workspace.
In [1]: workspace( 'foobar' );In [2]: currentWorkspaceOut[2]: 'foobar'In [3]: deleteWorkspace();In [4]: workspaces()Out[4]: [ 'base' ]In [5]: currentWorkspaceOut[5]: 'base'Note: the'base' workspacecannot be deleted.
Prints donation information.
In [1]: donate()Evaluates anexpression in a specifiedworkspace.
// Create a workspace:In [1]: workspace( 'foobar' );// Create and switch to another workspace:In [2]: workspace( 'barfoo' );// Attempt to evaluate an expression in the first workspace:In [3]: evalin( 'foobar', 'var x = 3.14;' );// Check that nothing has changed in the current workspace:In [4]: xError: x is not defined// Switch to the first workspace:In [5]: workspace( 'foobar' );// Check that the expression was successfully evaluated:In [6]: xOut[6]: 3.14Runs examples for a specified alias, property, or class instance.
In [1]: example( base.sin )Note: only direct instances of documented built-in constructors are supported.
Prints help text.
In [1]: help()To print help text for an alias,
In [1]: help( 'base.sin' )In [2]: help( base.sin )To print help text for a property,
In [1]: help( random.streams.randu.factory )To print help text for a class instance,
In [1]: var x = new Float64Array( 10 );In [2]: help( x )Note: only direct instances of documented built-in constructors are supported.
Prints abbreviated help text for a provided alias, property, or class instance.
In [1]: info( base.sin )In [2]: info( 'base.sin' )Note: only direct instances of documented built-in constructors are supported.
Returns aboolean indicating whether a string is a reserved keyword in the REPL environment.
In [1]: isKeyword( 'base.sin' )Out[1]: truePrints license information.
In [1]: license()Loads and evaluates a JavaScript file, such as a REPL history file, line-by-line.
In [1]: load( './path/to/file.js' )Loads variables from a specified workspace into the current workspace.
In [1]: workspace( 'foobar' );In [2]: var x = 3.14;In [3]: workspace( 'barfoo' );In [4]: typeof xOut[4]: 'undefined'In [5]: loadWorkspace( 'foobar' );In [6]: xOut[6]: 3.14The function supports the followingoptions:
- include: variable name inclusion filter. May be either an array-like
objector a regular expression. - exclude: variable name exclusion filter. May be either an array-like
objector a regular expression. - override:
booleanindicating whether to override existing workspace variables. Default:true.
In [1]: workspace( 'foobar' );In [2]: var x = 3.14, y = 6.28;In [3]: workspace( 'barfoo' );In [4]: typeof xOut[4]: 'undefined'In [5]: loadWorkspace( 'foobar', { 'include': /^x/ } );In [6]: xOut[6]: 3.14In [7]: typeof yOut[7]: 'undefined'Starts logging commands and printed output to a specified file path.
// TODOStops logging commands and printed output to a file path associated with a specified record identifier.
// TODOStarts a REPLpresentation.
In [1]: var id = presentationStart( 'beep\n---\nboop\n' );The function accepts the followingoptions:
- borderTop: top border character sequence. Default:
'*'. - borderBottom: bottom border character sequence. Default:
'*'. - borderLeft: left border character sequence. Default:
'* '. - borderRight: right border character sequence. Default:
' *'. - counter: slide counter. Can either be
true,false, or'progress'. Default:false. - width:presentation width. If
null, thepresentation width is either computed based on the screen size (if a REPL output stream is TTY) or set to80characters. Default:null. - height:presentation height. If
null, thepresentation height is either computed based on the screen size (if a REPL output stream is TTY) or set to25rows. Default:null. - workspace: REPL workspace name. Apresentation adds commands to the specified workspace, thus allowingpresentation navigation and interaction. Default:
'presentation-<n>', wherenis an assignedpresentation identifier. - load: file path specifying apresentation file to load. If presentation
textis provided, this option isignored. Otherwise, this option isrequired. - watch:
booleanindicating whether to watch apresentation source file for changes. This option is only applicable if not provided presentationtextand theoptionsobject specifies a presentation file to load. Default:false. - autoClear:
booleanindicating whether to automatically clear the screen before writing a rendered slide to the REPL. Default:true. - loop:
booleanindicating whether to "loop" a presentation. Default:false.
A few notes:
- When not provided presentation
text, an options argumentmust specify a presentation file to load. - If a specified workspace already exists, the workspace is silently cleared and a newpresentation bound. In order to preserve an existing workspace, specify an alternative presentation workspace name.
Stops a REPLpresentation.
In [1]: var id = presentationStart( 'beep\n---\nboop\n' );In [2]: presentationStop();A few notes:
- If provided a presentation identifier, the correspondingpresentation is stopped. Otherwise, assuming the function is invoked in a presentation workspace, the currentpresentation is stopped.
- When stopping a REPLpresentation, the presentation's REPL workspace is bothcleared anddeleted.
Exits the REPL.
In [1]: quit()Renames a workspace.
// Create a new workspace:In [1]: workspace( 'foobar' );In [2]: var x = 3.14;// List current workspaces:In [3]: workspaces()Out[3]: [ 'base', 'foobar' ]// Switch to another workspace:In [4]: workspace( 'barfoo' );// List current workspaces:In [5]: workspaces()Out[5]: [ 'base', 'foobar', 'barfoo' ]// Rename the first workspace:In [6]: renameWorkspace( 'foobar', 'beepboop' );// Check the updated list of workspaces:In [7]: workspaces()Out[7]: [ 'base', 'barfoo', 'beepboop' ]// Switch to the renamed workspace:In [8]: workspace( 'beepboop' );// Confirm that workspace contains previously defined variables:In [9]: xOut[9]: 3.14Note: the new workspace name mustnot already exist. The only exception is when the old name and the new name are the same; in which case, invoking this command is a non-operation.
Note: as a'base' workspace mustalways exist, when renaming the'base' workspace, variables from the'base' workspace are simply copied to a new workspace and the'base' workspace cleared of user-defined variables.
Imports a module, JSON, or local file.
In [1]: var crypto = require( 'crypto' );Modules can be imported fromnode_modules. Local modules and JSON files can be imported using a relative path (e.g.,'./foo.js','./../bar.json', etc) that will be resolved against the current working directory.
Re-imports a module, JSON, or local file.
In [1]: var foo = require( './foo.js' );// Modify `./foo.js`...// Re-import the module:In [2]: foo = rerequire( './foo.js' );Modules can be imported fromnode_modules. Local modules and JSON files can be imported using a relative path (e.g.,'./foo.js','./../bar.json', etc) that will be resolved against the current working directory.
Reruns previous commands.
In [1]: 1+1Out[1]: 2In [2]: rerun()By default, the command re-executes the previous command. If provided aninteger, the previousn commands are re-executed.
In [1]: 1+1Out[1]: 2In [2]: 2+2Out[2]: 4In [3]: rerun( 2 )If provided a regular expression, the most recent command matching the regular expression is rerun.
In [1]: var x = base.sin( 3.14 );In [2]: 1+1Out[2]: 2In [3]: rerun( /base\.sin/ )If provided anarray of command identifiers, the command corresponding to each identifier is rerun.
In [1]: 1+1Out[1]: 2In [2]: 2+2Out[2]: 4In [3]: 3+3Out[3]: 6In [4]: rerun( [ 1, 3 ] )If provided a subsequencestring, the command corresponding to each resolved command identifier is rerun.
In [1]: 1+1Out[1]: 2In [2]: 2+2Out[2]: 4In [3]: 3+3Out[3]: 6In [4]: rerun( '1:3:2' )WARNING: be careful when re-evaluating previously executedrerun commands, as this can lead to infinite execution loops.
Resets the REPL.
In [1]: reset()Saves previous commands to a specified file path.
// TODOStarts saving commands to a specified file path.
// TODOStops saving commands to a file path associated with a specified record identifier.
// TODOStarts a tutorial.
In [1]: var id = tutorial( 'repl' );The function accepts the followingoptions:
- borderTop: top border character sequence. Default:
'*'. - borderBottom: bottom border character sequence. Default:
'*'. - borderLeft: left border character sequence. Default:
'* '. - borderRight: right border character sequence. Default:
' *'. - counter: slide counter. Can either be
true,false, or'progress'. Default:progress. - workspace: REPL workspace name. A tutorialpresentation adds commands to the specified workspace, thus allowing tutorial navigation and interaction. Default:
'tutorial-<name>-<n>', wherenameis the tutorialnameandnis an assigned tutorialpresentation identifier. - autoClear:
booleanindicating whether to automatically clear the screen before writing a rendered tutorial slide to the REPL. Default:true.
A few notes:
- When not provided a tutorial
name, the function prints a list of available tutorials. - If a specified workspace already exists, the workspace is silently cleared and a new tutorialpresentation bound. In order to preserve an existing workspace, specify an alternative tutorial workspace name.
Adds user-defined documentation.
In [1]: function foo() {};In [2]: userDoc( 'foo', foo, '\nfoo()\n Foo bar.\n' );In [3]: help( foo )If user-defined documentation already exists for the provided alias, the current documentation is overwritten.
In [1]: function foo() {};In [2]: userDoc( 'foo', foo, '\nfoo()\n Foo bar.\n' );In [3]: help( foo )foo() Foo bar.In [4]: userDoc( 'foo', foo, '\nfoo()\n Beep boop.\n' );In [5]: help( foo )foo() Beep boop.Returns a list of variable names in the current workspace.
In [1]: var x = 3.14;In [2]: vars()Out[2]: [ 'x' ]The function supports the followingoptions:
- include: regular expression variable name inclusion filter.
- exclude: regular expression variable name exclusion filter.
- types: array-like
objectcontaining variable type inclusion filter(s). - details:
booleanindicating whether to include additional variable details, such as variable type, contents, etc. Default:false.
In [1]: var x = 3.14;In [2]: vars( { 'details': true } )Returns a list of variable names in a specified workspace.
In [1]: var x = 3.14;// List variable names in the current workspace:In [2]: varsWorkspace()Out[2]: [ 'x' ]In [3]: workspace( 'foo' );In [4]: varsWorkspace( 'base' )Out[4]: [ 'x' ]The function supports the followingoptions:
- include: regular expression variable name inclusion filter.
- exclude: regular expression variable name exclusion filter.
- types: array-like
objectcontaining variable type inclusion filter(s). - details:
booleanindicating whether to include additional variable details, such as variable type, contents, etc. Default:false.
In [1]: var x = 3.14;In [2]: workspace( 'foo' );In [3]: varsWorkspace( 'base', { 'details': true } )Switch to a specified workspace.
In [1]: workspace( 'foobar' );In [2]: var x = 3.14;In [3]: workspace( 'beepboop' );In [4]: xError: x is not definedIn [5]: workspace( 'foobar' );In [6]: xOut[6]: 3.14If a workspace with the specifiedname does not exist, the workspace is created.
Returns a list of workspace names.
In [1]: workspaces()Out[1]: [ 'base' ]The function supports the followingoptions:
- include: regular expression workspace name inclusion filter.
- exclude: regular expression workspace name exclusion filter.
- details:
booleanindicating whether to include additional workspace details, such as variable names, types, contents, etc. Default:false.
In [1]: workspaces( { 'details': true } )<!DOCTYPE html><htmllang="en"><body><scripttype="text/javascript">(function(){varjoin=require('path').join;varREPL=require('@stdlib/repl');functiononExit(){console.log('');console.log('REPL closed.');}// Create a new REPL:varrepl=newREPL();repl.on('exit',onExit);// Load a history file:repl.load(join(__dirname,'examples','history.txt'),done);functiondone(){// Close the REPL:repl.close();}})();</script></body></html>
This package is part ofstdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to developstdlib, see the main projectrepository.
SeeLICENSE.
Copyright © 2016-2022. The StdlibAuthors.
About
REPL environment.
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.