SLOC

Calculate the source lines of code (SLOC) in a JavaScript program.

Usage

var sloc = require( '@stdlib/_tools/static-analysis/js/sloc' );

sloc( src )

Returns the source lines of code (SLOC) in a JavaScript program, where src may be either an input string or Buffer.

var n = sloc( 'var beep = "boop";\n' );
// returns 1

Notes

  • Counting methodology is biased toward the coding conventions of stdlib.

  • Source lines of code (SLOC) is defined as the total number of program lines, excluding empty lines (which includes lines containing only whitespace) and lines containing only comments. When calculating SLOC, analysis assumes specific stylistic forms. For example, analysis does not distinguish between "verbose" if statements and if statement shorthand, always opting for the verbose form.

    var x = -5.0;
    
    // Shorthand:
    if ( x < 0.0 ) x *= -1;
    
    // "Verbose":
    if ( x < 0.0 ) {
        x *= -1;
    }
    
  • For further details regarding how SLOC is calculated, see program-summary.

Examples

var readFile = require( '@stdlib/fs/read-file' );
var sloc = require( '@stdlib/_tools/static-analysis/js/sloc' );

readFile( __filename, onFile );

function onFile( error, data ) {
    if ( error ) {
        throw error;
    }
    console.log( sloc( data ) );
}

CLI

Usage

Usage: js-sloc [options] [<string>]

Options:

  -h,    --help                Print this message.
  -V,    --version             Print the package version.

Examples

$ js-sloc 'var beep = "boop";\n'
1

To use as a standard stream,

$ echo -n $'var beep = "boop";\n' | js-sloc
1