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 🙏

© 2026 – Pkg Stats / Ryan Hefner

lex-luthor

v0.0.1

Published

A lexical scanner implementation based off a talk by Rob Pike

Downloads

10

Readme

Lex Luthor

This package is a lexical scanner written in Javascript based on a talk by Rob Pike. In the video he goes over a method that uses state functions to tokenize input, and uses Go channels to emit the tokens. In Node, I thought it would be possible to use streams and event emitters to achieve the same thing.

Example

This basic example will find comments of the multi-line format (/*...*/). The general idea is that all state function should return a new state function or null if it is the end of the file.

// Register the default state function
Lexer.registerState('default', function(lex) {
	
	// Get the next character from the stream
	var next = lex.next();

	// Check if it is null, meaning end of the file
	if (next === null) {
		return null;
	}

	// Find comments start character
	if (lex.value == constants.COMMENT_MULTILINE_START_CHAR) {
		// If the next character is a star, start a comment
		if (lex.accepts(constants.STAR)) {
			lex.emitToken('comment_start');
			return Lexer.getState('comment');
		}
	}

	// Ignore whitespace
	lex.acceptsRun(constants.WHITESPACE);
	lex.ignore();

	// If nothing matched, start over
	return Lexer.getState('default');

});

// Register inside comment state function
Lexer.registerState('comment', function(lex) {
	// Get any input that is not the start of an ending token ('*')
	lex.notAcceptsRun(constants.COMMENT_MULTILINE_END_CHAR);

	// Check the next two characters, if they are '*/' then end the comment
	if (lex.lookAhead(2) == constants.COMMENT_MULTILINE_END) {

		// Emit the new token
		lex.emitToken('comment_content');

		// Advance past the ending token and emit
		lex.next();
		lex.next();
		lex.emitToken('comment_end');

		// Return to the default state
		return Lexer.getState('default');
	}

	// If it was not the end of a comment, then advance past the star and contiune
	var next = lex.next();
	if (next === null) {
		return null;
	}
	return Lexer.getState('comment');
});

// Create the lexer and give it the input file
var lex = new Lexer().inputFile('./test/files/comments.css');

// Create an array to store the tokens
// In real live you would probably have a parser listen for this
var tokens = [];
lex.on('token', function(token) {
	tokens.push(token);
	// If it reaches the end of the file, then just log all the tokens
	if (token.type == Lexer.EOFToken) {
		console.log(tokens);
	}
});

// Run the lexer
lex.run();

Tests

There are unit tests and end-to-end tests. To run both you can use grunt test, or you can run them individually with npm run-script test-unit and npm run-script test-e2e.

To generate the coverage report you can run grunt test-coverage. This will run both unit and end-to-end tests, and generate a report for each in the coverage directory. This task also opens up a server where you can view the html output of the report at: