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

minirepl

v0.0.2

Published

Mini Javascript REPL

Downloads

5

Readme

minirepl

A mini Javascript REPL. Converts one-liner snippets into runnable code.

The purpose is to enliven tutorials/manual pages and allow small experimentation. The HTML content remains semantically pure, and degrades gracefully.

Mini REPL is meant to be extremely simple and lightweight, so it only supports simple variable declaration (via var/let/const), math and function invocations. Anything more complex than that would probably fail.

See the demo source

Usage

In HTML

In your HTML, pre-define the sample (and results even) as so:

  <!-- Use class 'repl' to pick up default styling -->
  <code class="repl">
    <!--
      Repl can have some preset code and results:
    -->
    <kbd>1 + 1</kbd>
    <samp>2</samp>
  </code>
  • Use the repl class for the <CODE> block to pick up the styling provided by repl.css. Or style yourself.
  • Use KBD to show what the user would have typed
  • Use SAMP to show what the system response would be

In Javascript

The following snippets show how to initialise a mini REPL. Each instance is associated with a element.

<script type="module">
  import { Repl } from "../build/index.js"
  // Create a repl for a single element:
  //const r = new Repl(document.querySelector('code'));

  // Create a repl for all matching elements:
  Repl.create(document.querySelectorAll('code.repl'));
</script>

The REPL instance has a few basic commands if needed:

r.reset();       // Reset contents, remove prompt and deactivating
r.activate();    // Activate REPL, creating a prompt
r.input(string); // Sends some input to the REPL, as if the user typed it and hit enter

Interaction

User clicks on the box to create a prompt. They can type and hit ENTER, and whatever they have typed is evaluated. Reponses or errors are appended. Hitting ESC will clear the currently typed line. Clicking away from the REPL will remove the prompt if it's empty.

A little button will appear in the top-left of the REPL to reset it back to its original contents. This can be withheld by setting showMenu to FALSE in the constructor:

const r = new Repl(document.querySelector('code'), false);

REPL execution

Code is executed as a new function, with 'use strict' enabled. This means that variables defined or accessed are within the scope of just that function.

Don't use mini REPL if there is a sensitive execution context. That said, it's no more dangerous than the user popping open the console and typing there.

In order to demonstrate defining and manipulating variables, mini REPL will special-case lines that begin with let, const or var. These lines are pushed to a stack and replayed before subsequent REPL code.

It's all very simple, so don't expect compound lines of code to run. It prefixes whatever is typed in with return , so something like for (var i=0;i<10;i++) { console.log(i) } won't work, because it becomes return for (var i=0;i<10;i++) { console.log(i) }. It also doesn't capture the console output.