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

check-constants

v1.2.1

Published

Find numbers and strings that should be extracted as a declaration statement

Downloads

4

Readme

check-constants

Find numbers and strings that should be extracted as a declaration statement

NPM Version NPM Downloads Build Status

The idea behind this project is that numbers and strings should be extracted as declared constants (or vars), so that they could be easily controlled & changed. Imagine that you have a function which will calculate the total sum owed after taxes:

//basic.js

function getTotal(subtotal) {
    var beforeTax = subtotal + 9.99;
    return beforeTax + (beforeTax * 0.13);
}

As you can see, in a month from now, and in a large code base, we might not remember what are those 9.99 and 0.13, and also, suppose we have several instances of the number 0.13 and we want to change it? Now you need to refactor all occurrences hoping you didn't miss anything or over-do it.

check-constants will find the numbers that you should extract as a declaration statement: Basic output example of check-constants

The example above, could be re-factored to:

//corrected.js

var FIXED_COST = 9.99;
var TAX = 0.13;

function getTotal(subtotal) {
    var beforeTax = subtotal + FIXED_COST;
    return beforeTax + (beforeTax * TAX);
}

Now let's see what happens when we run check-constants on the corrected file: Corrected output example of check-constants

This project uses Rocambole to parse your JS, and it's a simplified version of buddy.js which I found overcomplicated and too heavy.

Usage

Command Line

Installation

$ npm install --global check-constants

Examples

# show the help menu
❯ check-constants --help

  Usage: check-constants [options] <file ...>

  Options:

    -h, --help                    output usage information
    -V, --version                 output the version number
    -e, --enforce-const           require literals to be defined using const
    -i, --ignore <numbers>        list numbers to ignore (default: 0,1)
    -I, --disable-ignore          disables the ignore list
    -s, --strings                 check strings as well
    -m, --min-length [minLength]  minimum length of strings to be checked [0]
    -r, --reporter [reporter]     specify the reporter to use [table|json] (default: table)

  Examples:

    $ check-constants index.js
    $ check-constants --reporter json index.js
    $ cat index.js | check-constants

# Easily check a file by path
$ check-constants file.js

# Check files by using glob pattern
$ check-constants js/**/*.js

# Check a file by piping it
$ cat file.js | check-constants

# Format output as json
$ check-constants file.js --reporter json

# Override ignored numbers
$ check-constants file.js --ignore 1,5,13

# Disable default ignored numbers (0,1)
$ check-constants file.js --disable-ignore

# Make sure variables are declared as const
$ check-constants --enforce-const file.js

# Check the current version of the cli app
$ check-constants --version

Programmatic

Installation

$ npm install --save-dev check-constants

Examples

var fs = require('fs');
var checkConstants = require('check-constants');
var options = {};

var contents = fs.readFileSync('./contents.js', 'utf8');
var errors = checkConstants.inspect(contents, options);
// -> errors will contain possible variables that need extraction

Build Time

check-constants can also be used in conjunction with other javascript build systems, such as:

The Output

[{
    "file": "index.js",
    "code": "i = i + 2",
    "value": 2,
    "loc": {
        "start": {
            "line": 5,
            "column": 28
            },
        "end": {
            "line": 5,
            "column": 29
            }
        }
}]

API

check-constants exposes the following API:

.inspect(contents, options)

contents

String - the contents to check

options

Options is an optional object containing the following properties:

strings

Type: Boolean

Default: false

Whether to check for strings as well as numbers.

minLength

Type: Number

Default: 0

Only used when option strings is true. Limits the minimum string length checking.

enforceConst

Type: Boolean

Default: false

Whether to enforce declarations to be used with const.

ignore

Type: Array

Default: [0, 1]

Strings and numbers to ignore

file

Type: String

Default: null

Filename being checked if available (i.e not from a stream). Will be attached to the result object.

.log[reporter](results)

reporter

Which reporter to use. Currently supported json and table.

results

The resulting object from .inspect()

License

MIT ©Gilad Peleg