{"id":15699007,"url":"https://github.com/pgilad/check-constants","last_synced_at":"2025-05-08T22:40:29.380Z","repository":{"id":19062610,"uuid":"22289282","full_name":"pgilad/check-constants","owner":"pgilad","description":"Find numbers and strings that should be extracted as a declaration statement","archived":false,"fork":false,"pushed_at":"2015-05-22T17:24:22.000Z","size":972,"stargazers_count":14,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-20T09:39:47.452Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://github.com/pgilad/check-constants","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pgilad.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-26T15:14:40.000Z","updated_at":"2020-09-15T11:16:16.000Z","dependencies_parsed_at":"2022-09-06T19:10:40.639Z","dependency_job_id":null,"html_url":"https://github.com/pgilad/check-constants","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgilad%2Fcheck-constants","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgilad%2Fcheck-constants/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgilad%2Fcheck-constants/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgilad%2Fcheck-constants/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pgilad","download_url":"https://codeload.github.com/pgilad/check-constants/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253160727,"owners_count":21863624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T19:37:20.661Z","updated_at":"2025-05-08T22:40:29.362Z","avatar_url":"https://github.com/pgilad.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# check-constants\n\u003e Find numbers and strings that should be extracted as a declaration statement\n\n[![NPM Version](http://img.shields.io/npm/v/check-constants.svg?style=flat)](https://npmjs.org/package/check-constants)\n[![NPM Downloads](http://img.shields.io/npm/dm/check-constants.svg?style=flat)](https://npmjs.org/package/check-constants)\n[![Build Status](http://img.shields.io/travis/pgilad/check-constants.svg?style=flat)](https://travis-ci.org/pgilad/check-constants)\n\nThe idea behind this project is that numbers and strings should be extracted as declared constants (or vars), so that they could be easily controlled \u0026 changed.\nImagine that you have a function which will calculate the total sum owed after taxes:\n\n```js\n//basic.js\n\nfunction getTotal(subtotal) {\n    var beforeTax = subtotal + 9.99;\n    return beforeTax + (beforeTax * 0.13);\n}\n```\n\nAs 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\nalso, suppose we have several instances of the number `0.13` and we want to change it? Now you need to refactor\nall occurrences hoping you didn't miss anything or over-do it.\n\n`check-constants` will find the numbers that you should extract as a declaration statement:\n![Basic output example of check-constants](media/table-output.png)\n\nThe example above, could be re-factored to:\n\n```js\n//corrected.js\n\nvar FIXED_COST = 9.99;\nvar TAX = 0.13;\n\nfunction getTotal(subtotal) {\n    var beforeTax = subtotal + FIXED_COST;\n    return beforeTax + (beforeTax * TAX);\n}\n```\n\nNow let's see what happens when we run `check-constants` on the corrected file:\n![Corrected output example of check-constants](media/corrected.png)\n\nThis project uses [Rocambole](https://github.com/millermedeiros/rocambole) to parse your JS,\nand it's a simplified version of [buddy.js](https://github.com/danielstjules/buddy.js) which I found overcomplicated and too heavy.\n\n## Usage\n\n### Command Line\n\n#### Installation\n\n```bash\n$ npm install --global check-constants\n```\n\n#### Examples\n\n```bash\n# show the help menu\n❯ check-constants --help\n\n  Usage: check-constants [options] \u003cfile ...\u003e\n\n  Options:\n\n    -h, --help                    output usage information\n    -V, --version                 output the version number\n    -e, --enforce-const           require literals to be defined using const\n    -i, --ignore \u003cnumbers\u003e        list numbers to ignore (default: 0,1)\n    -I, --disable-ignore          disables the ignore list\n    -s, --strings                 check strings as well\n    -m, --min-length [minLength]  minimum length of strings to be checked [0]\n    -r, --reporter [reporter]     specify the reporter to use [table|json] (default: table)\n\n  Examples:\n\n    $ check-constants index.js\n    $ check-constants --reporter json index.js\n    $ cat index.js | check-constants\n\n# Easily check a file by path\n$ check-constants file.js\n\n# Check files by using glob pattern\n$ check-constants js/**/*.js\n\n# Check a file by piping it\n$ cat file.js | check-constants\n\n# Format output as json\n$ check-constants file.js --reporter json\n\n# Override ignored numbers\n$ check-constants file.js --ignore 1,5,13\n\n# Disable default ignored numbers (0,1)\n$ check-constants file.js --disable-ignore\n\n# Make sure variables are declared as const\n$ check-constants --enforce-const file.js\n\n# Check the current version of the cli app\n$ check-constants --version\n```\n\n### Programmatic\n\n#### Installation\n\n```bash\n$ npm install --save-dev check-constants\n```\n\n#### Examples\n\n```js\nvar fs = require('fs');\nvar checkConstants = require('check-constants');\nvar options = {};\n\nvar contents = fs.readFileSync('./contents.js', 'utf8');\nvar errors = checkConstants.inspect(contents, options);\n// -\u003e errors will contain possible variables that need extraction\n```\n\n### Build Time\n\n`check-constants` can also be used in conjunction with other javascript build systems, such as:\n\n* [gulp-check-constants](https://github.com/pgilad/gulp-check-constants)\n* [grunt-check-constants](https://github.com/pgilad/grunt-check-constants)\n\n## The Output\n```js\n[{\n    \"file\": \"index.js\",\n    \"code\": \"i = i + 2\",\n    \"value\": 2,\n    \"loc\": {\n        \"start\": {\n            \"line\": 5,\n            \"column\": 28\n            },\n        \"end\": {\n            \"line\": 5,\n            \"column\": 29\n            }\n        }\n}]\n```\n\n## API\n\n`check-constants` exposes the following API:\n\n### .inspect(contents, options)\n\n#### contents\n\n`String` - the contents to check\n\n#### options\n\nOptions is an optional object containing the following properties:\n\n##### strings\n\nType: `Boolean`\n\nDefault: `false`\n\nWhether to check for strings as well as numbers.\n\n##### minLength\n\nType: `Number`\n\nDefault: `0`\n\nOnly used when option `strings` is true. Limits the minimum string length checking.\n\n##### enforceConst\n\nType: `Boolean`\n\nDefault: `false`\n\nWhether to enforce declarations to be used with `const`.\n\n##### ignore\n\nType: `Array`\n\nDefault: `[0, 1]`\n\nStrings and numbers to ignore\n\n##### file\n\nType: `String`\n\nDefault: `null`\n\nFilename being checked if available (i.e not from a stream). Will be attached\nto the result object.\n\n### .log\\[reporter](results)\n\n#### reporter\n\nWhich reporter to use. Currently supported `json` and `table`.\n\n#### results\n\nThe resulting object from `.inspect()`\n\n## License\n\nMIT ©[Gilad Peleg](http://giladpeleg.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgilad%2Fcheck-constants","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpgilad%2Fcheck-constants","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgilad%2Fcheck-constants/lists"}