[Filesystem] Fixing file filter 07/177007/2
authorPiotr Kosko <p.kosko@samsung.com>
Tue, 24 Apr 2018 10:40:19 +0000 (12:40 +0200)
committerPiotr Kosko <p.kosko@samsung.com>
Tue, 24 Apr 2018 11:58:34 +0000 (13:58 +0200)
[Bug]
  1. FileFilter name was not matching '%' wildcard on the beginning of filename
  2. wildcard escaping need to be done by using '\\%' not '\%' - proper comment
     added in code to ease future investigations.

[Verification] Code compiles successfully.
  TCT passrate is 100%.

Below code:
var filter = "piotr\\%kosko%";
function onsuccess(files) {
  console.log("There are " + files.length  + " in the selected folder");
  for (var i =0; i < files.length; ++i) { console.log(files[i].name) }
}
function onerror(error) {
  console.log("The error " + error.message +
              " occurred when listing the files in the selected folder");
}
tizen.filesystem.resolve("documents", function(dir) {
  dir.listFiles(onsuccess, onerror, {name: filter});
}, function(e) { console.log("Error " + e.message);}, "r");

for filter "piotr\\%kosko%" filters file:
piotr%koskoTest

for filter "%kosko%" filter files:
piotrkoskoTest
piotrkosko
piotrTestkoskoTest
kosko
piotr%koskoTest
piotr%kosko

Change-Id: If7e6c3c3ad97a3d01a800916ba73c5f7749ae0cb
Signed-off-by: Piotr Kosko <p.kosko@samsung.com>
src/filesystem/js/file.js

index 524d7b6a9d936bd10290439f25d0dfdc71ac7761..d5d2d20d4696254443ed3c82663b5b9f297cdf81 100644 (file)
@@ -73,7 +73,7 @@ function stringToRegex(str) {
   if (str === '') {
     return new RegExp(_regString, 'i');
   }
-
+  // single '\' sign is not visible in JS string, escaping % wildcard need to be done by '\\%'
   str = str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
 
   var _percentTokens = str.split('%');
@@ -81,11 +81,13 @@ function stringToRegex(str) {
   for (i = 0; i < _percentTokens.length - 1; ++i) {
     _regString = _regString + _percentTokens[i];
     if (_regString[_regString.length - 1] === '\\') {
+      // special handling \\% sequence - '%' sign is threaten as regular sign - not wildcard
       _regString = _regString.split('');
       _regString.pop();
       _regString = _regString.join('') + '%';
     }
-    else if (_regString.lastIndexOf('\*') !== _regString.length - 2) {
+    else {
+      // handling '%' as a wildcard
       _regString = _regString + '.*';
     }
   }