Skip to content

Commit

Permalink
remove unused variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Mottie committed Jan 31, 2015
1 parent 8d1fddd commit 418f202
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
50 changes: 30 additions & 20 deletions lib/jquery.tabletojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* jQuery plugin that reads an HTML table and returns a javascript object representing the values and columns of the table
*
* @author Daniel White
* @copyright Daniel White 2014
* @copyright Daniel White 2015
* @license MIT <https://github.com/lightswitch05/table-to-json/blob/master/MIT-LICENSE>
* @link https://github.com/lightswitch05/table-to-json
* @module tabletojson
Expand All @@ -22,10 +22,12 @@
ignoreEmptyRows: false,
headings: null,
allowHTML: false,
includeRowId : false
includeRowId: false,
textDataOverride: 'data-override',
textExtractor: null
};
opts = $.extend(defaults, opts);

var notNull = function(value) {
return value !== undefined && value !== null;
};
Expand All @@ -50,19 +52,27 @@
return result;
};

var cellValues = function(cellIndex, cell) {
var value, result;
var override = $(cell).data('override');
if ( opts.allowHTML ) {
value = $.trim($(cell).html());
var cellValues = function(cellIndex, cell, isHeader) {
var $cell = $(cell),
// textExtractor
extractor = opts.textExtractor,
override = $cell.attr(opts.textDataOverride);
// don't use extractor for header cells
if ( extractor === null || isHeader ) {
return $.trim( override || ( opts.allowHTML ? $cell.html() : cell.textContent || $cell.text() ) || '' );
} else {
value = $.trim($(cell).text());
// overall extractor function
if ( $.isFunction(extractor) ) {
return $.trim( override || extractor(cellIndex, $cell) );
} else if ( typeof extractor === 'object' && $.isFunction( extractor[cellIndex] ) ) {
return $.trim( override || extractor[cellIndex](cellIndex, $cell) );
}
}
result = notNull(override) ? override : value;
return result;
// fallback
return $.trim( override || ( opts.allowHTML ? $cell.html() : cell.textContent || $cell.text() ) || '' );
};

var rowValues = function(row) {
var rowValues = function(row, isHeader) {
var result = [];
var includeRowId = opts.includeRowId;
var useRowId = (typeof includeRowId === 'boolean') ? includeRowId : (typeof includeRowId === 'string') ? true : false;
Expand All @@ -73,14 +83,14 @@
}
}
$(row).children('td,th').each(function(cellIndex, cell) {
result.push( cellValues(cellIndex, cell) );
result.push( cellValues(cellIndex, cell, isHeader) );
});
return result;
};

var getHeadings = function(table) {
var firstRow = table.find('tr:first').first();
return notNull(opts.headings) ? opts.headings : rowValues(firstRow);
return notNull(opts.headings) ? opts.headings : rowValues(firstRow, true);
};

var construct = function(table, headings) {
Expand All @@ -90,11 +100,11 @@
if( rowIndex > 0 || notNull(opts.headings) ) {
var includeRowId = opts.includeRowId;
var useRowId = (typeof includeRowId === 'boolean') ? includeRowId : (typeof includeRowId === 'string') ? true : false;

$row = $(row);

var isEmpty = ($row.find('td').length === $row.find('td:empty').length) ? true : false;

if( ( $row.is(':visible') || !opts.ignoreHiddenRows ) && ( !isEmpty || !opts.ignoreEmptyRows ) && ( !$row.data('ignore') || $row.data('ignore') === 'false' ) ) {
cellIndex = 0;
if (!tmpArray[rowIndex]) {
Expand All @@ -108,14 +118,14 @@
tmpArray[rowIndex].push('');
}
}

$row.children().each(function(){
$cell = $(this);

// process rowspans
if ($cell.filter('[rowspan]').length) {
len = parseInt( $cell.attr('rowspan'), 10) - 1;
txt = cellValues(cellIndex, $cell, []);
txt = cellValues(cellIndex, $cell);
for (i = 1; i <= len; i++) {
if (!tmpArray[rowIndex + i]) { tmpArray[rowIndex + i] = []; }
tmpArray[rowIndex + i][cellIndex] = txt;
Expand All @@ -124,7 +134,7 @@
// process colspans
if ($cell.filter('[colspan]').length) {
len = parseInt( $cell.attr('colspan'), 10) - 1;
txt = cellValues(cellIndex, $cell, []);
txt = cellValues(cellIndex, $cell);
for (i = 1; i <= len; i++) {
// cell has both col and row spans
if ($cell.filter('[rowspan]').length) {
Expand All @@ -139,7 +149,7 @@
}
// skip column if already defined
while (tmpArray[rowIndex][cellIndex]) { cellIndex++; }
txt = tmpArray[rowIndex][cellIndex] || cellValues(cellIndex, $cell, []);
txt = tmpArray[rowIndex][cellIndex] || cellValues(cellIndex, $cell);
if (notNull(txt)) {
tmpArray[rowIndex][cellIndex] = txt;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/jquery.tabletojson.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/jquery.tabletojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
};

var cellValues = function(cellIndex, cell, isHeader) {
var value, result,
$cell = $(cell),
var $cell = $(cell),
// textExtractor
extractor = opts.textExtractor,
override = $cell.attr(opts.textDataOverride);
Expand Down

0 comments on commit 418f202

Please sign in to comment.