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

redeyed

v2.1.1

Published

Takes JavaScript code, along with a config and returns the original code with tokens wrapped as configured.

Downloads

13,594,151

Readme

redeyed build status

Add color to your JavaScript!

frog

Red Eyed Tree Frog (Agalychnis callidryas)

What?

Takes JavaScript code, along with a config and returns the original code with tokens wrapped and/or replaced as configured.

Where?

  • server side using nodejs
  • in the browser

What for?

One usecase is adding metadata to your code that can then be used to apply syntax highlighting.

How?

  • copy the config.js and edit it in order to specify how certain tokens are to be surrounded/replaced
  • replace the undefined of each token you want to configure with one of the following

{String} config

'before:after'

wraps the token inside before/after

{Object} config

{ _before: 'before', _after: 'after' }

wraps token inside before/after

Missing before and after resolution for {String} and {Object} config

For the {String} and {Object} configurations, 'before' or 'after' may be omitted:

  • {String}:
    • 'before:' (omitting 'after')
    • ':after' (omitting 'before')
  • {Object}:
    • { _before: 'before' } (omitting '_after')
    • { _after: 'after' } (omitting '_before')

In these cases the missing half is resolved as follows:

  • from the parent._default (i.e., Keyword._default) if found
  • otherwise from the config._default if found
  • otherwise '' (empty string)

{Function} config

function (tokenString, info) { return {String}|{Object}; }

Inputs

  • tokenString: the content of the token that is currently being processed
  • info: an object with the following structure
{
    // {Int}
    // the index of the token being processed inside tokens
    tokenIndex

    // {Array}
    // all tokens that are being processed including comments 
    // (i.e. the result of merging esprima tokens and comments)
  , tokens  

    // {Object} 
    // the abstract syntax tree of the parsed code
  , ast  

    // {String}
    // the code that was parsed (same string as the one passed to redeyed(code ..)
  , code
}

In most cases the tokenString is all you need. The extra info object is passed in case you need to gather more information about the token's surroundings in order to decide how to transform it. See: replace-log-example

Output

You can return a {String} or an {Object} from a {Function} config.

  • when returning a {String}, the token value will be replaced with it
  • when returning an {Object}, it should be of the following form:
{
    // {String}
    // the string that should be substituted for the value of the current and all skipped tokens
    replacement

    // {Object} (Token)
    // the token after which processing should continue
    // all tokens in between the current one and this one inclusive will be ignored
  , skipPastToken
}

Transforming JavaScript code

redeyed(code, config[, opts])

Invoke redeyed with your configuration, a code snippet and maybe opts as in the below example:

var redeyed = require('redeyed')
  , config = require('./path/to/config')
  , code = 'var a = 3;'
  , result;

// redeyed will throw an error (caused by the esprima parser) if the code has invalid javascript
try {
  result = redeyed(code, config);
  console.log(result.code);
} catch(err) {
  console.error(err);
}

opts:

{ // {Boolean}
  // if true `result.ast` property contains the abstract syntax tree of the code
  // if false (default) `result.ast` is not assigned and therefore `undefined`
  buildAst: true|false
  // {Boolean}
  // if `true`, jsx syntax is supported, default `false`
  // due to how esprima works, the AST is built when this option is `true`, even if
  // `buildAST` is `false`
, jsx: true|false
  // {Boolean}
  // if true `result.code` is not assigned and therefore `undefined`
  // if false (default) `result.code` property contains the result of `split.join`
  nojoin: true|false
  // {Object}
  // overrides default parser `esprima-fb` and needs to be compatible with it
  parser: require('esprima') 
}

return value:

{   ast      
  , tokens   
  , comments 
  , splits   
  , code     
}
  • ast {Array}: abstract syntax tree as returned by esprima parse
  • tokens {Array}: tokens provided by esprima (excluding comments)
  • comments {Array}: block and line comments as provided by esprima
  • splits {Array}: code pieces split up, some of which where transformed as configured
  • code {String}: transformed code, same as splits.join('') unless this step has been skipped (see opts)

Browser Support

AMD

Ensure to include esprima as one of your dependencies

define(['redeyed'], function (redeyed) {
 [ .. ]
});

Attached to global window object

The redeyed {Function} will be exposed globally as window.redeyed - big surprise!

<script type="text/javascript" src="https://unpkg.com/esprima"></script>
<script type="text/javascript" src="https://unpkg.com/redeyed"></script>

redeyed in the wild

  • cardinal: Syntax highlights JavaScript code with ANSI colors to be printed to the terminal
  • peacock: JavaScript syntax highlighter that generates html that is compatible with pygments styles.

Examples