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

complexion-js

v0.2.2

Published

Tokenize JavaScript

Downloads

8,383

Readme

Complexion - JavaScript

Extends the Complexion tokenizing library so it is able to parse JavaScript into tokens. Useful if you want to create a JavaScript pretty printer, parser or anything that would need to understand the syntax of JavaScript.

npm version Build Status Dependencies Dev Dependencies

Usage

Install this package and Complexion with npm or bower or maybe just clone the two repositories. Then, use the library in your JavaScript. There is UMD markup (via FidUmd) that should make this library available under any module system that you prefer to use.

// NPM
var Complexion = require('complexion');
var instance = new Complexion();
require('complexion-js')(instance);
var tokenList = instance.tokenize('... JavaScript goes here ...');

// Browser
var instance = new Complexion();
complexionJs(instance);
var tokenList = instance.tokenize('... JavaScript goes here ...');

Options may be passed as well.

complexionJs(instance, {
    bom: false,
    shebang: false
});

Options

  • bom (boolean, enabled by default) - Allow searching for the byte order mark in files.
  • shebang (boolean, enabled by default) - Allow searching for a shebang at the top of a file.

Token Types

The following are all of the available tokens that the tokenizer produces.

  • BOM - UTF8 byte order mark at the very beginning of the file.
  • BOOLEAN_LITERAL - Either true or false.
  • IDENTIFIER_NAME - The names of functions, variables, properties. This does not include keywords.
  • IMPLICIT_SEMICOLON - A special token that has no content but indicates that there should have been a semicolon here. It's necessary for determining context and if something is division or a regular expression.
  • KEYWORD - Special words that the language reserves and are not allowed as variable names. Does not include null nor booleans.
  • LINE_TERMINATOR - Newlines (\r\n, \n and \r).
  • MULTI_LINE_COMMENT - A comment that uses /* */.
  • NULL_LITERAL - Only matches null.
  • NUMERIC_LITERAL - A number in decimal, octal or hex. Decimals may have exponents.
  • PUNCTUATOR - Most symbols including math, array literals, object literals, function calls.
  • REGULAR_EXPRESSION_LITERAL - Regular expressions can be tricky because they sometimes look like division and have lots of allowed escape sequences.
  • SHEBANG - Only matches the first line when it starts with #! and can be turned off.
  • SINGLE_LINE_COMMENT - A comment that starts with //.
  • STRING_LITERAL - A single or double quoted string.
  • UNKNOWN - An unknown character. If you hit this, you have poorly formed JavaScript. This is not a full lint test, just the beginning of one.
  • WHITESPACE - Spaces and tabs. Not newlines.

Token Objects

The tokens returned from tokenization will all be instances of a ComplexionJsToken class. They retain the same properties that the original token object has and get additional methods.

ComplexionJsToken.prototype.isAnyType(tokenTypes)

Returns true if the token is any of the types passed in.

if (token.isAnyType([ 'WHITESPACE', 'LINE_TERMINATOR' ])) {
    // This token does not appear on screens
}

ComplexionJsToken.prototype.isAnyType(tokenTypes)

Returns true if the token is exactly the type passed in.

if (token.isType('BOOLEAN_LITERAL')) {
    // token.content === "true" || token.content === "false"
}

ComplexionJsToken.prototype.isAnyType(tokenTypes)

Returns true when the token could be eliminated entirely from the output.

// Simple minifier
tokenList.forEach(function (token) {
    if (!token.isUnimportant()) {
        if (token.isType('IMPLICIT_SEMICOLON')) {
            console.log(';');
        } else {
            console.log(token.content);
        }
    }
});

Development

If you want to work on this library, you need to check out the repository and run npm install to get the dependencies.

Tests are always included. Make sure tests cover your changes. To run the current tests, just use npm test or grunt test (they will run the same test suite).

Make sure the matchers are fast. This library is tweaked to work on unminified source, so take that into consideration before shuffling the matchers too much.

License

This software is licensed under an MIT license with an additional non-advertising clause.