File jquery.tablesorter.js
has 2314 lines of code (exceeds 250 allowed). Consider refactoring. Open
/*! TableSorter (FORK) v2.29.2 *//*
* Client-side table sorting with ease!
* @requires jQuery v1.2.6+
*
* Copyright (c) 2007 Christian Bach
Function buildCache
has 128 lines of code (exceeds 25 allowed). Consider refactoring. Open
buildCache : function( c, callback, $tbodies ) {
var cache, val, txt, rowIndex, colIndex, tbodyIndex, $tbody, $row,
cols, $cells, cell, cacheTime, totalRows, rowData, prevRowData,
colMax, span, cacheIndex, hasParser, max, len, index,
table = c.table,
Function initSort
has 113 lines of code (exceeds 25 allowed). Consider refactoring. Open
initSort : function( c, cell, event ) {
if ( c.table.isUpdating ) {
// let any updates complete before initializing a sort
return setTimeout( function(){
ts.initSort( c, cell, event );
Function setup
has 100 lines of code (exceeds 25 allowed). Consider refactoring. Open
setup : function( table, c ) {
// if no thead or tbody, or tablesorter is already present, quit
if ( !table || !table.tHead || table.tBodies.length === 0 || table.hasInitialized === true ) {
if ( c.debug ) {
if ( table.hasInitialized ) {
Function bindMethods
has 87 lines of code (exceeds 25 allowed). Consider refactoring. Open
bindMethods : function( c ) {
var $table = c.$table,
namespace = c.namespace,
events = ( 'sortReset update updateRows updateAll updateHeaders addRows updateCell updateComplete ' +
'sorton appendCache updateCache applyWidgetId applyWidgets refreshWidgets destroy mouseup ' +
Function setupParsers
has 83 lines of code (exceeds 25 allowed). Consider refactoring. Open
setupParsers : function( c, $tbodies ) {
var rows, list, span, max, colIndex, indx, header, configHeaders,
noParser, parser, extractor, time, tbody, len,
table = c.table,
tbodyIndex = 0,
Function buildHeaders
has 82 lines of code (exceeds 25 allowed). Consider refactoring. Open
buildHeaders : function( c ) {
var $temp, icon, timer, indx;
c.headerList = [];
c.headerContent = [];
c.sortVars = [];
Function setHeadersCss
has 70 lines of code (exceeds 25 allowed). Consider refactoring. Open
setHeadersCss : function( c ) {
var indx, column,
list = c.sortList,
len = list.length,
none = ts.css.sortNone + ' ' + c.cssNone,
Function bindEvents
has 64 lines of code (exceeds 25 allowed). Consider refactoring. Open
bindEvents : function( table, $headers, core ) {
table = $( table )[ 0 ];
var tmp,
c = table.config,
namespace = c.namespace,
Function multisort
has 61 lines of code (exceeds 25 allowed). Consider refactoring. Open
multisort : function( c ) { /*jshint loopfunc:true */
var tbodyIndex, sortTime, colMax, rows, tmp,
table = c.table,
sorter = [],
dir = 0,
Function addRows
has 60 lines of code (exceeds 25 allowed). Consider refactoring. Open
addRows : function( c, $row, resort, callback ) {
var txt, val, tbodyIndex, rowIndex, rows, cellIndex, len, order,
cacheIndex, rowData, cells, cell, span,
// allow passing a row string if only one non-info tbody exists in the table
valid = typeof $row === 'string' && c.$tbodies.length === 1 && /<tr/.test( $row || '' ),
Function applyWidget
has 55 lines of code (exceeds 25 allowed). Consider refactoring. Open
applyWidget : function( table, init, callback ) {
table = $( table )[ 0 ]; // in case this is called externally
var indx, len, names, widget, time,
c = table.config,
widgets = [];
Function updateCell
has 54 lines of code (exceeds 25 allowed). Consider refactoring. Open
updateCell : function( c, cell, resort, callback ) {
// updateCell for child rows is a mess - we'll ignore them for now
// eventually I'll break out the "update" row cache code to make everything consistent
if ( $( cell ).closest( 'tr' ).hasClass( c.cssChildRow ) ) {
console.warn('Tablesorter Warning! "updateCell" for child row content has been disabled, use "update" instead');
Function $headers
has 48 lines of code (exceeds 25 allowed). Consider refactoring. Open
c.$headers = $( $.map( c.$table.find( c.selectorHeaders ), function( elem, index ) {
var configHeaders, header, column, template, tmp,
$elem = $( elem );
// ignore cell (don't add it to c.$headers) if row has ignoreRow class
if ( ts.getClosest( $elem, 'tr' ).hasClass( c.cssIgnoreRow ) ) { return; }
Function updateHeaderSortCount
has 45 lines of code (exceeds 25 allowed). Consider refactoring. Open
updateHeaderSortCount : function( c, list ) {
var col, dir, group, indx, primary, temp, val, order,
sortList = list || c.sortList,
len = sortList.length;
c.sortList = [];
Function getColumnText
has 44 lines of code (exceeds 25 allowed). Consider refactoring. Open
getColumnText : function( table, column, callback, rowFilter ) {
table = $( table )[0];
var tbodyIndex, rowIndex, cache, row, tbodyLen, rowLen, raw, parsed, $cell, result,
hasCallback = typeof callback === 'function',
allColumns = column === 'all',
Function destroy
has 42 lines of code (exceeds 25 allowed). Consider refactoring. Open
destroy : function( table, removeClasses, callback ) {
table = $( table )[ 0 ];
if ( !table.hasInitialized ) { return; }
// remove all widgets
ts.removeWidget( table, true, false );
Function appendCache
has 40 lines of code (exceeds 25 allowed). Consider refactoring. Open
appendCache : function( c, init ) {
var parsed, totalRows, $tbody, $curTbody, rowIndex, tbodyIndex, appendTime,
table = c.table,
wo = c.widgetOptions,
$tbodies = c.$tbodies,
Function applyWidgetId
has 39 lines of code (exceeds 25 allowed). Consider refactoring. Open
applyWidgetId : function( table, id, init ) {
table = $(table)[0];
var applied, time, name,
c = table.config,
wo = c.widgetOptions,
Function computeColumnIndex
has 39 lines of code (exceeds 25 allowed). Consider refactoring. Open
computeColumnIndex : function( $rows, c ) {
var i, j, k, l, cell, cells, rowIndex, rowSpan, colSpan, firstAvailCol,
// total columns has been calculated, use it to set the matrixrow
columns = c && c.columns || 0,
matrix = [],
Function removeWidget
has 31 lines of code (exceeds 25 allowed). Consider refactoring. Open
removeWidget : function( table, name, refreshing ) {
table = $( table )[ 0 ];
var index, widget, indx, len,
c = table.config;
// if name === true, add all widgets from $.tablesorter.widgets
Function detectParserForColumn
has 29 lines of code (exceeds 25 allowed). Consider refactoring. Open
detectParserForColumn : function( c, rows, rowIndex, cellIndex ) {
var cur, $node, row,
indx = ts.parsers.length,
node = false,
nodeValue = '',
Function sortNatural
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
sortNatural : function( a, b ) {
if ( a === b ) { return 0; }
a = a.toString();
b = b.toString();
var aNum, bNum, aFloat, bFloat, indx, max,
Consider simplifying this complex logical expression. Open
if ( ( ( e.which || e.button ) !== 1 && !type.match( ' ' + c.pointerClick + ' | sort | keyup ' ) ) ||
// allow pressing enter
( type === ' keyup ' && e.which !== ts.keyCodes.enter ) ||
// allow triggering a click event (e.which is undefined) & ignore physical clicks
( type.match( ' ' + c.pointerClick + ' ' ) && typeof e.which !== 'undefined' ) ) {
Avoid deeply nested control flow statements. Open
if ( cell ) {
prevRowData.child[ len ][ colIndex ] = ts.getParsedText( c, cell, colIndex );
span = $cells[ colIndex ].colSpan - 1;
if ( span > 0 ) {
cacheIndex += span;
Function sortNumericDesc
has 6 arguments (exceeds 4 allowed). Consider refactoring. Open
sortNumericDesc : function( a, b, num, max, col, c ) {
Avoid deeply nested control flow statements. Open
if ( c.debug ) {
debug[ '(' + colIndex + ') ' + header.text() ] = {
parser : parser.id,
extractor : extractor ? extractor.id : 'none',
string : c.strings[ colIndex ],
Avoid deeply nested control flow statements. Open
for ( indx = 1; indx < th.colSpan; indx++ ) {
c.sortList[ c.sortList.length ] = [ col + indx, dir ];
// update count on columns in colSpan
c.sortVars[ col + indx ].count = $.inArray( dir, order );
}
Avoid deeply nested control flow statements. Open
if ( span > 0 ) {
colIndex += span;
max += span;
while ( span + 1 > 0 ) {
// set colspan columns to use the same parsers & extractors
Avoid deeply nested control flow statements. Open
if ( !hasParser && c.debug ) {
console.warn( 'No parser found for row: ' + rowIndex + ', column: ' + colIndex +
'; cell containing: "' + $(cell).text() + '"; does it have a header?' );
}
Avoid deeply nested control flow statements. Open
if ( !parser ) {
parser = ts.detectParserForColumn( c, rows, -1, colIndex );
}
Avoid deeply nested control flow statements. Open
if ( $.inArray( setting, ignore ) < 0 && typ === 'undefined' ) {
console.warn( 'Tablesorter Warning! "table.config.' + setting + '.' + setting2 + '" option not recognized' );
}
Avoid deeply nested control flow statements. Open
if ( !$sorted[ column ].sortDisabled ) {
updateColumnSort( $sorted.eq( column ) , list[ indx ][ 1 ] );
}
Function sortNumericAsc
has 6 arguments (exceeds 4 allowed). Consider refactoring. Open
sortNumericAsc : function( a, b, num, max, col, c ) {
Avoid deeply nested control flow statements. Open
if ( noParser ) {
parser = ts.getParserById( 'no-parser' );
}
Avoid deeply nested control flow statements. Open
if ( temp ) {
tmp = c.sortList[ 0 ][ 1 ];
switch ( temp[ 0 ] ) {
case 'd' :
dir = 1;
Avoid deeply nested control flow statements. Open
if ( span > 0 ) {
index = 0;
while ( index <= span ) {
// duplicate text (or not) to spanned columns
// instead of setting duplicate span to empty string, use textExtraction to try to get a value
Avoid deeply nested control flow statements. Open
if ( hasParser && ( parsers[ cacheIndex ].type || '' ).toLowerCase() === 'numeric' ) {
// determine column max value (ignore sign)
colMax[ cacheIndex ] = Math.max( Math.abs( txt ) || 0, colMax[ cacheIndex ] || 0 );
}
Avoid deeply nested control flow statements. Open
if ( !extractor ) {
// For now, maybe detect someday
extractor = false;
}
Avoid deeply nested control flow statements. Open
if ( tmp[1] === 2 ) {
c.sortList.splice( indx, 1 );
c.sortVars[ col ].count = -1;
}
Consider simplifying this complex logical expression. Open
if ( header && header.length ) {
// get column indexed table cell; adding true parameter fixes #1362 but
// it would break backwards compatibility...
configHeaders = ts.getColumnData( table, c.headers, colIndex ); // , true );
// get column parser/extractor
Function getColumnData
has 5 arguments (exceeds 4 allowed). Consider refactoring. Open
getColumnData : function( table, obj, indx, getCell, $headers ) {
Avoid too many return
statements within this function. Open
return $.trim( tmp( $node[ 0 ], c.table, cellIndex ) );
Avoid too many return
statements within this function. Open
return $.trim( $node[ 0 ].textContent || $node.text() );
Avoid too many return
statements within this function. Open
if ( aFloat > bFloat ) { return 1; }
Avoid too many return
statements within this function. Open
return 0;
Avoid too many return
statements within this function. Open
if ( aFloat < bFloat ) { return -1; }
Similar blocks of code found in 2 locations. Consider refactoring. Open
;( function( $ ) {
'use strict';
var ts = $.tablesorter = {
version : '2.29.2',
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 29428.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76