Merge pull request #626 from koalyptus/625-fix-empty-filter-operator

refine empty-nonempty operator checks
This commit is contained in:
koalyptus 2018-07-29 11:12:02 +10:00 committed by GitHub
commit e09bab000b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 15 deletions

4
dist/starter.html vendored
View File

@ -1,10 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>tablefilter v0.6.42 - Starter</title>
<title>tablefilter v0.6.50 - Starter</title>
</head>
<body>
<h1>tablefilter v0.6.42</h1>
<h1>tablefilter v0.6.50</h1>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{
"name": "tablefilter",
"version": "0.6.49",
"version": "0.6.50",
"description": "A Javascript library making HTML tables filterable and a bit more",
"license": "MIT",
"author": {

View File

@ -4,7 +4,6 @@ import {
removeClass, tag
} from './dom';
import {contains, matchCase, rgxEsc, trim} from './string';
import {isEmpty as isEmptyString} from './string';
import {
isArray, isEmpty, isFn, isNumber, isObj, isString, isUndef, EMPTY_FN,
isBoolean
@ -1623,7 +1622,7 @@ export class TableFilter {
// isolate search term and check occurence in cell data
for (let w = 0, len = s.length; w < len; w++) {
cS = trim(s[w]);
found = this._match(cS, cellValue, j);
found = this._match(cS, cellValue, cells[j]);
if (found) {
emitter.emit('highlight-keyword', this,
@ -1642,7 +1641,8 @@ export class TableFilter {
}
//single search parameter
else {
occurence[j] = this._match(trim(sA), cellValue, j);
occurence[j] =
this._match(trim(sA), cellValue, cells[j]);
if (occurence[j]) {
emitter.emit('highlight-keyword', this, cells[j],
sA);
@ -1688,14 +1688,15 @@ export class TableFilter {
/**
* Match search term in cell data
* @param {String} term Search term
* @param {String} term Search term
* @param {String} cellValue Cell data
* @param {Number} colIdx Column index
* @param {DOMElement} cell Current cell
* @return {Boolean}
* @private
*/
_match(term, cellValue, colIdx) {
_match(term, cellValue, cell) {
let numData;
let colIdx = cell.cellIndex;
let decimal = this.getDecimal(colIdx);
let reLe = new RegExp(this.leOperator),
reGe = new RegExp(this.geOperator),
@ -1797,11 +1798,11 @@ export class TableFilter {
}
//empty
else if (hasEM) {
occurence = isEmptyString(cellValue);
occurence = !cell.hasChildNodes();
}
//non-empty
else if (hasNM) {
occurence = !isEmptyString(cellValue);
occurence = cell.hasChildNodes();
} else {
occurence = contains(term, cellValue,
this.isExactMatch(colIdx), this.caseSensitive);
@ -1887,11 +1888,11 @@ export class TableFilter {
}
//empty
else if (hasEM) {
occurence = isEmptyString(cellValue);
occurence = !cell.hasChildNodes();
}
//non-empty
else if (hasNM) {
occurence = !isEmptyString(cellValue);
occurence = cell.hasChildNodes();
} else {
// If numeric type data, perform a strict equality test and
// fallback to unformatted number string comparison

View File

@ -51,10 +51,13 @@
});
test('Empty operator - [empty]', function() {
var cell = tf.getWorkingRows()[4].cells[4];
cell.innerHTML = '';
tf.clearFilters();
tf.setFilterValue(4, '[empty]');
tf.filter();
var filteredData = tf.getFilteredData();
deepEqual(tf.getValidRows().length, 1, 'Expected number of matches');
deepEqual(
filteredData[0],
@ -63,7 +66,18 @@
);
});
test('Empty operator - with cell containing dom element', function() {
var cell = tf.getWorkingRows()[4].cells[4];
cell.innerHTML = '<img>';
tf.clearFilters();
tf.setFilterValue(4, '[empty]');
tf.filter();
deepEqual(tf.getValidRows().length, 0, 'No matches expected');
});
test('Non-empty operator - [nonempty]', function() {
var cell = tf.getWorkingRows()[4].cells[4];
cell.innerHTML = '';
tf.clearFilters();
tf.setFilterValue(4, '[nonempty]');
tf.filter();
@ -75,6 +89,18 @@
'Expected row data');
});
test('Non-empty operator - with cell containing dom element', function() {
var cell = tf.getWorkingRows()[4].cells[4];
cell.innerHTML = '<img>';
tf.clearFilters();
tf.setFilterValue(4, '[nonempty]');
tf.filter();
var filteredData = tf.getFilteredData();
console.log(filteredData);
deepEqual(tf.getValidRows().length, 7, 'Expected number of matches');
});
test('Or operator - ||', function() {
tf.clearFilters();
tf.setFilterValue(1, 'Canberra||Alice Springs');