Use 'Object.values' and 'Object.keys' instead of '_.each(object)'

This commit is contained in:
Hongarc 2018-12-01 12:33:23 +07:00
parent bd8009386d
commit 96ddb5041a
9 changed files with 52 additions and 33 deletions

View file

@ -102,8 +102,10 @@ var commands = {
},
loop: function(callback, context) {
_.each(commandConfigs, function(commandConfig, vcs) {
_.each(commandConfig, function(config, name) {
Object.keys(commandConfigs).forEach(function(vcs) {
var commandConfig = commandConfigs[vcs];
Object.keys(commandConfig).forEach(function(name) {
var config = commandConfig[name];
callback(config, name, vcs);
});
});
@ -116,8 +118,11 @@ var parse = function(str) {
var options;
// see if we support this particular command
_.each(commands.getRegexMap(), function (map, thisVCS) {
_.each(map, function(regex, thisMethod) {
var regexMap = commands.getRegexMap();
Object.keys(regexMap).forEach(function (thisVCS) {
var map = regexMap[thisVCS];
Object.keys(map).forEach(function(thisMethod) {
var regex = map[thisMethod];
if (regex.exec(str)) {
vcs = thisVCS;
method = thisMethod;

View file

@ -839,9 +839,10 @@ var instantCommands = [
var commands = require('../commands').commands.getOptionMap()['git'];
// build up a nice display of what we support
_.each(commands, function(commandOptions, command) {
Object.keys(commands).forEach(function(command) {
var commandOptions = commands[command];
lines.push('git ' + command);
_.each(commandOptions, function(vals, optionName) {
Object.keys(commandOptions).forEach(function(optionName) {
lines.push('\t ' + optionName);
}, this);
}, this);

View file

@ -280,18 +280,18 @@ GitEngine.prototype.instantiateFromTree = function(tree) {
// now we do the loading part
var createdSoFar = {};
_.each(tree.commits, function(commitJSON) {
Object.values(tree.commits).forEach(function(commitJSON) {
var commit = this.getOrMakeRecursive(tree, createdSoFar, commitJSON.id, this.gitVisuals);
this.commitCollection.add(commit);
}, this);
_.each(tree.branches, function(branchJSON) {
Object.values(tree.branches).forEach(function(branchJSON) {
var branch = this.getOrMakeRecursive(tree, createdSoFar, branchJSON.id, this.gitVisuals);
this.branchCollection.add(branch, {silent: true});
}, this);
_.each(tree.tags, function(tagJSON) {
Object.values(tree.tags || {}).forEach(function(tagJSON) {
var tag = this.getOrMakeRecursive(tree, createdSoFar, tagJSON.id, this.gitVisuals);
this.tagCollection.add(tag, {silent: true});
@ -360,7 +360,8 @@ GitEngine.prototype.makeOrigin = function(treeString) {
var originTree = JSON.parse(unescape(treeString));
// make an origin branch for each branch mentioned in the tree if its
// not made already...
_.each(originTree.branches, function(branchJSON, branchName) {
Object.keys(originTree.branches).forEach(function(branchName) {
var branchJSON = originTree.branches[branchName];
if (this.refs[ORIGIN_PREFIX + branchName]) {
// we already have this branch
return;
@ -1802,7 +1803,7 @@ GitEngine.prototype.updateBranchesForHg = function(branchList) {
GitEngine.prototype.updateCommitParentsForHgRebase = function(commitSet) {
var anyChange = false;
_.each(commitSet, function(val, commitID) {
Object.keys(commitSet).forEach(function(commitID) {
var commit = this.refs[commitID];
var thisUpdated = commit.checkForUpdatedParent(this);
anyChange = anyChange || thisUpdated;
@ -1819,7 +1820,7 @@ GitEngine.prototype.pruneTree = function() {
var set = this.getUpstreamBranchSet();
// don't prune commits that HEAD depends on
var headSet = Graph.getUpstreamSet(this, 'HEAD');
_.each(headSet, function(val, commitID) {
Object.keys(headSet).forEach(function(commitID) {
set[commitID] = true;
});
@ -2066,14 +2067,14 @@ GitEngine.prototype.hgRebase = function(destination, base) {
// and NOWWWwwww get all the descendants of this set
var moreSets = [];
_.each(upstream, function(val, id) {
Object.keys(upstream).forEach(function(id) {
moreSets.push(this.getDownstreamSet(id));
}, this);
var masterSet = {};
masterSet[baseCommit.get('id')] = true;
[upstream, downstream].concat(moreSets).forEach(function(set) {
_.each(set, function(val, id) {
Object.keys(set).forEach(function(id) {
masterSet[id] = true;
});
});
@ -2081,7 +2082,7 @@ GitEngine.prototype.hgRebase = function(destination, base) {
// we also need the branches POINTING to master set
var branchMap = {};
var upstreamSet = this.getUpstreamBranchSet();
_.each(masterSet, function(val, commitID) {
Object.keys(masterSet).forEach(function(commitID) {
// now loop over that commits branches
upstreamSet[commitID].forEach(function(branchJSON) {
branchMap[branchJSON.id] = true;
@ -2305,7 +2306,7 @@ GitEngine.prototype.filterRebaseCommits = function(
options
) {
var changesAlreadyMade = {};
_.each(stopSet, function(val, key) {
Object.keys(stopSet).forEach(function(key) {
changesAlreadyMade[this.scrapeBaseID(key)] = true;
}, this);
var uniqueIDs = {};

View file

@ -145,8 +145,7 @@ TreeCompare.compareAllBranchesWithinTreesHashAgnostic = function(treeA, treeB) {
treeA.branches,
treeB.branches
);
var branchNames = [];
_.each(allBranches, function(obj, name) { branchNames.push(name); });
var branchNames = Object.keys(allBranches || {});
return this.compareBranchesWithinTreesHashAgnostic(treeA, treeB, branchNames);
};
@ -188,7 +187,8 @@ TreeCompare.compareBranchesWithinTreesHashAgnostic = function(treeA, treeB, bran
TreeCompare.evalAsserts = function(tree, assertsPerBranch) {
var result = true;
_.each(assertsPerBranch, function(asserts, branchName) {
Object.keys(assertsPerBranch).forEach(function(branchName) {
var asserts = assertsPerBranch[branchName];
result = result && this.evalAssertsOnBranch(tree, branchName, asserts);
}, this);
return result;
@ -327,9 +327,10 @@ TreeCompare.lowercaseTree = function(tree) {
tree.HEAD.target = tree.HEAD.target.toLocaleLowerCase();
}
var branches = tree.branches;
var branches = tree.branches || {};
tree.branches = {};
_.each(branches, function(obj, name) {
Object.keys(branches).forEach(function(name) {
var obj = branches[name];
obj.id = obj.id.toLocaleLowerCase();
tree.branches[name.toLocaleLowerCase()] = obj;
});
@ -378,7 +379,8 @@ TreeCompare.reduceTreeFields = function(trees) {
};
trees.forEach(function(tree) {
_.each(treeDefaults, function(val, key) {
Object.keys(treeDefaults).forEach(function(key) {
var val = treeDefaults[key];
if (tree[key] === undefined) {
tree[key] = val;
}
@ -388,7 +390,8 @@ TreeCompare.reduceTreeFields = function(trees) {
// this function saves only the specified fields of a tree
var saveOnly = function(tree, treeKey, saveFields, sortFields) {
var objects = tree[treeKey];
_.each(objects, function(obj, objKey) {
Object.keys(objects).forEach(function(objKey) {
var obj = objects[objKey];
// our blank slate to copy over
var blank = {};
saveFields.forEach(function(field) {
@ -399,7 +402,7 @@ TreeCompare.reduceTreeFields = function(trees) {
}
});
_.each(sortFields, function(field) {
Object.values(sortFields || {}).forEach(function(field) {
// also sort some fields
if (obj[field]) {
obj[field].sort();

View file

@ -406,8 +406,9 @@ var Level = Sandbox.extend({
}
var matched = false;
_.each(Commands.commands.getCommandsThatCount(), function(map) {
_.each(map, function(regex) {
var commandsThatCount = Commands.commands.getCommandsThatCount();
Object.values(commandsThatCount).forEach(function(map) {
Object.values(map).forEach(function(regex) {
matched = matched || regex.test(command.get('rawStr'));
});
});

View file

@ -75,8 +75,10 @@ ParseWaterfall.prototype.expandAllShortcuts = function(commandStr) {
};
ParseWaterfall.prototype.expandShortcut = function(commandStr, shortcutMap) {
_.each(shortcutMap, function(map, vcs) {
_.each(map, function(regex, method) {
Object.keys(shortcutMap).forEach(function(vcs) {
var map = shortcutMap[vcs];
Object.keys(map).forEach(function(method) {
var regex = map[method];
var results = regex.exec(commandStr);
if (results) {
commandStr = vcs + ' ' + method + ' ' + commandStr.slice(results[0].length);

View file

@ -82,7 +82,8 @@ var Command = Backbone.Model.extend({
return this.replaceDotWithHead(arg);
}, this);
var newMap = {};
_.each(options, function(args, key) {
Object.keys(options).forEach(function(key) {
var args = options[key];
newMap[key] = Object.values(args).map(function (arg) {
return this.replaceDotWithHead(arg);
}, this);
@ -273,7 +274,8 @@ var Command = Backbone.Model.extend({
return false;
}
_.each(results.toSet, function(obj, key) {
Object.keys(results.toSet).forEach(function(key) {
var obj = results.toSet[key];
// data comes back from the parsing functions like
// options (etc) that need to be set
this.set(key, obj);

View file

@ -139,8 +139,11 @@ var getAllCommands = function() {
require('../level').regexMap,
regexMap
);
_.each(Commands.commands.getRegexMap(), function(map, vcs) {
_.each(map, function(regex, method) {
var mRegexMap = Commands.commands.getRegexMap();
Object.keys(mRegexMap).forEach(function(vcs) {
var map = mRegexMap[vcs];
Object.keys(map).forEach(function(method) {
var regex = map[method];
allCommands[vcs + ' ' + method] = regex;
});
});

View file

@ -52,7 +52,8 @@ var validateLevel = function(level) {
/**
* Unpack the level sequences.
*/
_.each(levelSequences, function(levels, levelSequenceName) {
Object.keys(levelSequences).forEach(function(levelSequenceName) {
var levels = levelSequences[levelSequenceName];
_sequences.push(levelSequenceName);
if (!levels || !levels.length) {
throw new Error('no empty sequences allowed');