Find numbers and strings that should be extracted as a declaration statement
npm install check-constants


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:
``js
//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, and0.13
also, suppose we have several instances of the number 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:
`js
//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.
#### Installation
`bash`
$ npm install --global check-constants
#### Examples
`bashshow the help menu
❯ check-constants --help
Usage: check-constants [options]
Options:
-h, --help output usage information
-V, --version output the version number
-e, --enforce-const require literals to be defined using const
-i, --ignore
-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
$3
#### Installation
`bash
$ npm install --save-dev check-constants
`#### Examples
`js
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
`$3
check-constants can also be used in conjunction with other javascript build systems, such as:* gulp-check-constants
* grunt-check-constants
The Output
`js
[{
"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:$3
#### contents
String - the contents to check#### options
Options is an optional object containing the following properties:
##### strings
Type:
BooleanDefault:
falseWhether to check for strings as well as numbers.
##### minLength
Type:
NumberDefault:
0Only used when option
strings is true. Limits the minimum string length checking.##### enforceConst
Type:
BooleanDefault:
falseWhether to enforce declarations to be used with
const.##### ignore
Type:
ArrayDefault:
[0, 1]Strings and numbers to ignore
##### file
Type:
StringDefault:
nullFilename being checked if available (i.e not from a stream). Will be attached
to the result object.
$3
#### reporter
Which reporter to use. Currently supported
json and table.#### results
The resulting object from
.inspect()`MIT ©Gilad Peleg