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

lexer.js

v0.2.6

Published

A lexer and longest common sequence finder (between source code files)

Downloads

7

Readme

lexer.js

A lexical analyzer and longest common shared sequence finder between a list of JS files.

Build Status

Table of Contents

What it does

Suppose, you have two files with the same function but different function calls:

test1.js

function add(a, b){
    return a + b;
}

const sum = add(11 + 11);

test2.js

function add(a, b){
    return a + b;
}

let sum = add(11 + 11);

The longest common shared sequence between these two files is the entire function definition.

LCSS

function add(a, b){
    return a + b;
}

Results

Installation

CLI

You can download the module via npm. (To install npm, which ships with node.js, you can download node from nodejs.org for your OS.) node.js > 6.x

$ npm install -g lexer.js

Or, if you prefer yarn

$ yarn global add lexer.js

That's it. 🎉

As module

In your application:

const lexerJS = require('lexer.js');

const result = lexerJS(files, options);

files: An array of files.

options: See options This parameter is (ironically) optional.

Running lexer.js

To run it on your own set of files, you can either provide the files in CSV/JSON, or as command line arguments like this:

lexer.js test1.js test2.js

JSON configuration

The JSON must have a key named files and it's value should be an array of the paths of files you want to test on.

{
    "files": [
        "./example/test1.js",
        "./example/test2.js",
    ]
}

CSV configuration

The CSV config file only has one header (or column) and is called filename. Each new line should contain the path of a source code file.

filename,
./example/test1.js,
./example/test2.js

Result

The result contains the longest common shared sequence found between the set of files. The default format is JSON, but can be configured. (See options below.)

It also asssigns a score to each subsequnce using the following formula:

score = log2(count) * log2(total)

count: Total number of occurences of the subsequence

total: Total number of tokens in the subsequence

Options

-o Output Mode [default: "json"]

lexer.js supports JSON as well as CSV output. JSON is the defualt output format if you do not specify any during invocation.

lexer.js test.json -o csv

Output would now be a CSV file. To know what that file would contain, check out result.

-s Save Tokens [default: false]

This is a boolean option, which when set, saves the tokens for each test file.

lexer.js test.json -s

It will generate a tokens folder, and save individual tokens for each file in that directory.

-f Output File Name [default: "result"]

lexer.js test.json -f YayTheResultsYay

Note: If you provide a file name with extension, such as art.json, then the output mode will be determined from the fileName and the output mode flag ( if passed) will be overriden.

Running Examples

The examples directory contains a minimal example set that you can run lexer.js on. To do so, clone the repo, fire a terminal, and run:

npm install

This will downdload the dependencies. Then, run:

lexer.js test.json

This assumes that you already have lexer.js installed. If you don't, you can directly invoke the node script as follows:

node index.js test.json

As always, you must have node installed.

Test GitHub project

The repo also contains a script to test lexer.js on any GitHub project. The script does the following:

  1. Find all JS files in a project.
  2. Select a file which has more than n commits. n is configurable.
  3. Downloads the file at that point in time (when that commit was made).
  4. Generates a configuration file for lexer.js.
  5. Run lexer.js with that config file.

To run it, fire a terminal and run (assuming you are inside the project directory):

node runGitHubExamples.js --owner OWNERNAME --repo REPONAME -n 20

OWNERNAME: Owner of the repo [default: prettier]

REPONAME: Name of the repo [default: prettier]

n: Minimum commits the selected file must have [default: 10]

The results are saved in result.json. The command line options for lexer.js can also be passed.

Tests

To run tests, clone the repo (That green button above the repo contents) and run the following command:

npm install && npm run test

This will first download the dependencies, and then run the tests (using mocha) and output the result.