Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 31175200B9C for ; Fri, 2 Sep 2016 02:37:55 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 2F89B160AB5; Fri, 2 Sep 2016 00:37:55 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 57B85160AC9 for ; Fri, 2 Sep 2016 02:37:54 +0200 (CEST) Received: (qmail 14779 invoked by uid 500); 2 Sep 2016 00:37:53 -0000 Mailing-List: contact commits-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list commits@cordova.apache.org Received: (qmail 14754 invoked by uid 99); 2 Sep 2016 00:37:53 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 02 Sep 2016 00:37:53 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 3D394E00DB; Fri, 2 Sep 2016 00:37:53 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: purplecabbage@apache.org To: commits@cordova.apache.org Date: Fri, 02 Sep 2016 00:37:54 -0000 Message-Id: In-Reply-To: <238e6c2a709e46bd9b6cc1e6a4e4fcc0@git.apache.org> References: <238e6c2a709e46bd9b6cc1e6a4e4fcc0@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/3] cordova-lib git commit: use array methods and object.keys for iterating. avoiding for-in loops archived-at: Fri, 02 Sep 2016 00:37:55 -0000 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 Authored: Wed Aug 31 13:46:56 2016 -0700 Committer: Jesse MacFadyen 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