cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From purplecabb...@apache.org
Subject [2/3] cordova-lib git commit: use array methods and object.keys for iterating. avoiding for-in loops
Date Fri, 02 Sep 2016 00:37:54 GMT
use array methods and object.keys for iterating. avoiding for-in loops


Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/47d0be5d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/47d0be5d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/47d0be5d

Branch: refs/heads/master
Commit: 47d0be5da50bdfa9dcb9bf14ecd92d2ba2af10e6
Parents: b4b17d0
Author: Jesse MacFadyen <purplecabbage@gmail.com>
Authored: Wed Aug 31 13:46:56 2016 -0700
Committer: Jesse MacFadyen <purplecabbage@gmail.com>
Committed: Wed Aug 31 13:55:43 2016 -0700

----------------------------------------------------------------------
 cordova-common/src/FileUpdater.js | 84 ++++++++++++++++------------------
 1 file changed, 39 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/47d0be5d/cordova-common/src/FileUpdater.js
----------------------------------------------------------------------
diff --git a/cordova-common/src/FileUpdater.js b/cordova-common/src/FileUpdater.js
index 540db76..e97df5b 100644
--- a/cordova-common/src/FileUpdater.js
+++ b/cordova-common/src/FileUpdater.js
@@ -285,14 +285,14 @@ function mergeAndUpdateDir(sourceDirs, targetDir, options, log) {
     }
 
     // Scan the files in each of the source directories.
-    var sourceMaps = [];
-    for (var i in sourceDirs) {
-        var sourceFullPath = path.join(rootDir, sourceDirs[i]);
-        if (!fs.existsSync(sourceFullPath)) {
-            throw new Error("Source directory does not exist: " + sourceDirs[i]);
-        }
-        sourceMaps[i] = mapDirectory(rootDir, sourceDirs[i], include, exclude);
-    }
+    var sourceMaps = sourceDirs.map(function (sourceDir) {
+            return path.join(rootDir, sourceDir);
+        }).map(function (sourcePath) {
+            if (!fs.existsSync(sourcePath)) {
+                throw new Error("Source directory does not exist: " + sourcePath);
+            }
+            return mapDirectory(rootDir, sourcePath, include, exclude);
+        });
 
     // Scan the files in the target directory, if it exists.
     var targetMap = {};
@@ -331,46 +331,40 @@ function mapDirectory(rootDir, subDir, include, exclude) {
     function mapSubdirectory(rootDir, subDir, relativeDir, include, exclude, dirMap) {
         var itemMapped = false;
         var items = fs.readdirSync(path.join(rootDir, subDir, relativeDir));
-        for (var i in items) {
-            var relativePath = path.join(relativeDir, items[i]);
 
-            // Skip any files or directories (and everything under) that match an exclude
glob.
-            if (matchGlobArray(relativePath, exclude)) {
-                continue;
-            }
-
-            // Stats obtained here (required at least to know where to recurse in directories)
-            // are saved for later, where the modified times may also be used. This minimizes
-            // the number of file I/O operations performed.
-            var fullPath = path.join(rootDir, subDir, relativePath);
-            var stats = fs.statSync(fullPath);
-
-            if (stats.isDirectory()) {
-                // Directories are included if either something under them is included or
they
-                // match an include glob.
-                if (mapSubdirectory(rootDir, subDir, relativePath, include, exclude, dirMap)
||
-                        matchGlobArray(relativePath, include)) {
-                    dirMap[relativePath] = { subDir: subDir, stats: stats };
-                    itemMapped = true;
-                }
-            } else if (stats.isFile()) {
-                // Files are included only if they match an include glob.
-                if (matchGlobArray(relativePath, include)) {
-                    dirMap[relativePath] = { subDir: subDir, stats: stats };
-                    itemMapped = true;
+        items.forEach(function(item) {
+            var relativePath = path.join(relativeDir, item);
+            if(!matchGlobArray(relativePath, exclude)) {
+                // Stats obtained here (required at least to know where to recurse in directories)
+                // are saved for later, where the modified times may also be used. This minimizes
+                // the number of file I/O operations performed.
+                var fullPath = path.join(rootDir, subDir, relativePath);
+                var stats = fs.statSync(fullPath);
+
+                if (stats.isDirectory()) {
+                    // Directories are included if either something under them is included
or they
+                    // match an include glob.
+                    if (mapSubdirectory(rootDir, subDir, relativePath, include, exclude,
dirMap) ||
+                            matchGlobArray(relativePath, include)) {
+                        dirMap[relativePath] = { subDir: subDir, stats: stats };
+                        itemMapped = true;
+                    }
+                } else if (stats.isFile()) {
+                    // Files are included only if they match an include glob.
+                    if (matchGlobArray(relativePath, include)) {
+                        dirMap[relativePath] = { subDir: subDir, stats: stats };
+                        itemMapped = true;
+                    }
                 }
             }
-        }
+        });
         return itemMapped;
     }
 
     function matchGlobArray(path, globs) {
-        for (var i in globs) {
-            if (minimatch(path, globs[i], {dot:true})) {
-                return true;
-            }
-        }
-        return false;
+        return globs.some(function(elem) {
+            return minimatch(path, elem, {dot:true});
+        });
     }
 }
 
@@ -384,7 +378,7 @@ function mergePathMaps(sourceMaps, targetMap, targetDir) {
     // Target stats will be filled in below for targets that exist.
     var pathMap = {};
     sourceMaps.forEach(function (sourceMap) {
-        for (var sourceSubPath in sourceMap) {
+        Object.keys(sourceMap).forEach(function(sourceSubPath){
             var sourceEntry = sourceMap[sourceSubPath];
             pathMap[sourceSubPath] = {
                 targetPath: path.join(targetDir, sourceSubPath),
@@ -392,12 +386,12 @@ function mergePathMaps(sourceMaps, targetMap, targetDir) {
                 sourcePath: path.join(sourceEntry.subDir, sourceSubPath),
                 sourceStats: sourceEntry.stats
             };
-        }
+        });
     });
 
     // Fill in target stats for targets that exist, and create entries
     // for targets that don't have any corresponding sources.
-    for (var subPath in targetMap) {
+    Object.keys(targetMap).forEach(function(subPath){
         var entry = pathMap[subPath];
         if (entry) {
             entry.targetStats = targetMap[subPath].stats;
@@ -409,7 +403,7 @@ function mergePathMaps(sourceMaps, targetMap, targetDir) {
                 sourceStats: null
             };
         }
-    }
+    });
 
     return pathMap;
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message