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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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