npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

programmatic-repl

v1.1.4

Published

A module to programmatically simulate a repl

Downloads

41

Readme

programmatic-repl

programmatic-repl is a Node.js module that allows one to simulate a repl programmatically. This is very useful for embedded application where you'd like to simulate a repl. If you have plans to use this for a CLI application, I would suggest the official repl module instead.

Initializing

The module exports the REPL class. The parameters to this class are options, context.

Options

All of the options are optional.

Parameter | Type | Description | Default --- | --- | --- | --- options.includeNative | Boolean | This property will include native Node.js functions and properties in the REPL context. Specifically require, Buffer, __dirname, setImmediate, clearImmediate, clearInterval, clearTimeout and process. | false options.includeBuiltinLibs | Boolean | This property will include all of the native Node.js modules and libraries in the REPL context (e.g. child_process and fs). A full list of these modules can be found here. | false options.indentation | Number | This is the amount of spaces of indentation the REPL will show intermediate outputs with. | 2 options.name | String | The "name" of the repl - shows up as the filename in stack traces / errors | 'programmatic-repl'

Context

The Context parameter is an object of values you want to include in the REPL.

Example

const ProgrammaticREPL = require('programmatic-repl');

const REPL = new ProgrammaticREPL({
  includeNative: true,
  includeBuiltinLibs: true,
  indentation: 2
}, {
  foo: 'bar',
  baz: 'qux'
});

Usage

Once you've initiated your REPL, you can use the execute method. The execute method takes 1 parameter (in the form of a String): the input. The method always returns a Promise. The result will either be the computed value or an intermediate output.

The input is ofcourse your JavaScript, but the following commands / variables are available:

Name | Description --- | --- .clear | This command deletes any variables you've made and resets the context fully. _ | In the form of a variable, this is the output of the last ran command.

Examples

The examples assume the REPL has already been initiated.

await REPL.execute('5;');
// returns 5
await REPL.execute('const myObject = { foo: \'bar\' };');
// returns undefined

await REPL.execute('myObject;');
// returns { foo: 'bar' }

await REPL.execute('Object.keys(_);');
// returns [ 'foo' ]

await REPL.execute('.clear');
// returns 'Successfully cleared variables.'

await REPL.execute('myObject;');
// throws a ReferenceError: myObject is not defined

The module also lets you leave brackets open and view intermediate results.

await REPL.execute('if (true) {');
// returns (String):
`
if (true) {
  ...
`
  
await REPL.execute('if (false || true) {');
// returns (String):
`
if (true) {
  if (false || true) {
    ...
`

await REPL.execute('Promise.resolve(42);');
// returns (String):
`
if (true) {
  if (false || true) {
    Promise.resolve(42);
    ...
`

await REPL.execute('}');
// returns (String):
`
if (true) {
  if (false || true) {
    Promise.resolve(42);
  }
  ...
`

await REPL.execute('}');
// returns (Number, resolved Promise): 42

And lastly, some examples for REPL parameters:

const REPL = new ProgrammaticREPL({
  includeNative: true,
  includeBuiltinLibs: true,
  indentation: 2
}, {
  foo: 'bar',
  baz: 'qux'
});

// Node.js-specific variables are included, because we specified it:
await REPL.execute('require(\'./some-file.js\');');
await REPL.execute('new Buffer(42);')

// Builtin libs too, because we specified it:
await REPL.execute('querystring.stringify({ foo: \'bar\' });');
await REPL.execute('child_process.execSync(\'echo "Hi!"\').toString();');

// And, our own passed context:
await REPL.execute('foo === \'bar\' && baz === \'qux\';')
// returns true

Contributing

it's late i'll do this tomorrow