summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Leibman <michael.leibman@gmail.com>2013-07-11 10:53:52 -0700
committerMichael Leibman <michael.leibman@gmail.com>2013-07-11 10:53:52 -0700
commit9a7e510e46452c1f5a0781d607f63566e2a65f3a (patch)
tree6996111255ce4b830b722695f8e5bd75f6e53668
parent5952bc6282c4d04cc893cd6319b6c80157808f95 (diff)
parentd773a09d0f7daaad8bdba6f6668a622544ee6ebf (diff)
downloadSlickGrid-9a7e510e46452c1f5a0781d607f63566e2a65f3a.zip
SlickGrid-9a7e510e46452c1f5a0781d607f63566e2a65f3a.tar.gz
SlickGrid-9a7e510e46452c1f5a0781d607f63566e2a65f3a.tar.bz2
Merge pull request #587 from pandell/fix-inline-filters
Improve `return` matching for inlined filter functions
-rw-r--r--slick.dataview.js16
-rw-r--r--tests/dataview/dataview.js34
2 files changed, 42 insertions, 8 deletions
diff --git a/slick.dataview.js b/slick.dataview.js
index 45439b6..5bee4ff 100644
--- a/slick.dataview.js
+++ b/slick.dataview.js
@@ -613,10 +613,10 @@
var filterInfo = getFunctionInfo(filter);
var filterBody = filterInfo.body
- .replace(/return false[;}]/gi, "{ continue _coreloop; }")
- .replace(/return true[;}]/gi, "{ _retval[_idx++] = $item$; continue _coreloop; }")
- .replace(/return ([^;}]+?);/gi,
- "{ if ($1) { _retval[_idx++] = $item$; }; continue _coreloop; }");
+ .replace(/return false\s*([;}]|$)/gi, "{ continue _coreloop; }$1")
+ .replace(/return true\s*([;}]|$)/gi, "{ _retval[_idx++] = $item$; continue _coreloop; }$1")
+ .replace(/return ([^;}]+?)\s*([;}]|$)/gi,
+ "{ if ($1) { _retval[_idx++] = $item$; }; continue _coreloop; }$2");
// This preserves the function template code after JS compression,
// so that replace() commands still work as expected.
@@ -645,10 +645,10 @@
var filterInfo = getFunctionInfo(filter);
var filterBody = filterInfo.body
- .replace(/return false[;}]/gi, "{ continue _coreloop; }")
- .replace(/return true[;}]/gi, "{ _cache[_i] = true;_retval[_idx++] = $item$; continue _coreloop; }")
- .replace(/return ([^;}]+?);/gi,
- "{ if ((_cache[_i] = $1)) { _retval[_idx++] = $item$; }; continue _coreloop; }");
+ .replace(/return false\s*([;}]|$)/gi, "{ continue _coreloop; }$1")
+ .replace(/return true\s*([;}]|$)/gi, "{ _cache[_i] = true;_retval[_idx++] = $item$; continue _coreloop; }$1")
+ .replace(/return ([^;}]+?)\s*([;}]|$)/gi,
+ "{ if ((_cache[_i] = $1)) { _retval[_idx++] = $item$; }; continue _coreloop; }$2");
// This preserves the function template code after JS compression,
// so that replace() commands still work as expected.
diff --git a/tests/dataview/dataview.js b/tests/dataview/dataview.js
index 04278c1..c1eb384 100644
--- a/tests/dataview/dataview.js
+++ b/tests/dataview/dataview.js
@@ -377,6 +377,40 @@ test("all then none", function() {
assertConsistency(dv);
});
+test("inlining replaces absolute returns", function() {
+ var dv = new Slick.Data.DataView({ inlineFilters: true });
+ dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
+ dv.setFilter(function(o) {
+ if (o.val === 1) { return true; }
+ else if (o.val === 4) { return true }
+ return false});
+ same(dv.getLength(), 1, "one row is remaining");
+
+ dv.onRowsChanged.subscribe(function() { ok(false, "onRowsChanged called") });
+ dv.onRowCountChanged.subscribe(function() { ok(false, "onRowCountChanged called") });
+ dv.onPagingInfoChanged.subscribe(function() { ok(false, "onPagingInfoChanged called") });
+ same(dv.getItems().length, 3, "original data is still there");
+ same(dv.getLength(), 1, "rows are filtered");
+ assertConsistency(dv);
+});
+
+test("inlining replaces evaluated returns", function() {
+ var dv = new Slick.Data.DataView({ inlineFilters: true });
+ dv.setItems([{id:0,val:0},{id:1,val:1},{id:2,val:2}]);
+ dv.setFilter(function(o) {
+ if (o.val === 0) { return o.id === 2; }
+ else if (o.val === 1) { return o.id === 2 }
+ return o.val === 2});
+ same(dv.getLength(), 1, "one row is remaining");
+
+ dv.onRowsChanged.subscribe(function() { ok(false, "onRowsChanged called") });
+ dv.onRowCountChanged.subscribe(function() { ok(false, "onRowCountChanged called") });
+ dv.onPagingInfoChanged.subscribe(function() { ok(false, "onPagingInfoChanged called") });
+ same(dv.getItems().length, 3, "original data is still there");
+ same(dv.getLength(), 1, "rows are filtered");
+ assertConsistency(dv);
+});
+
module("updateItem");
test("basic", function() {