Skip to content
Permalink
Browse files
refactor: use arrow functions where applicable (#113)
This commit makes use of arrow function expressions wherever applicable. Focus
is put on minimizing the diff instead of optimizing or formatting the code
touched by this commit.
  • Loading branch information
raphinesse committed Dec 13, 2019
1 parent 1318ab5 commit 574eea17e7eca414ec7b76d5e9d289ac9746f36d
Showing 10 changed files with 155 additions and 195 deletions.
@@ -39,7 +39,7 @@ var xml_helpers = require('../util/xml-helpers');

exports.PlatformMunger = PlatformMunger;

exports.process = function (plugins_dir, project_dir, platform, platformJson, pluginInfoProvider) {
exports.process = (plugins_dir, project_dir, platform, platformJson, pluginInfoProvider) => {
var munger = new PlatformMunger(platform, project_dir, platformJson, pluginInfoProvider);
munger.process(plugins_dir);
munger.save_all();
@@ -274,8 +274,8 @@ function generate_config_xml_munge (config, config_changes, type) {
id = config.id;
}

config_changes.forEach(function (change) {
change.xmls.forEach(function (xml) {
config_changes.forEach(change => {
change.xmls.forEach(xml => {
// 1. stringify each xml
var stringified = (new et.ElementTree(xml)).write({ xml_declaration: false });
// 2. add into munge
@@ -303,13 +303,13 @@ function generate_plugin_config_munge (pluginInfo, vars, edit_config_changes) {
Array.prototype.push.apply(changes, edit_config_changes);
}

changes.forEach(function (change) {
change.xmls.forEach(function (xml) {
changes.forEach(change => {
change.xmls.forEach(xml => {
// 1. stringify each xml
var stringified = (new et.ElementTree(xml)).write({ xml_declaration: false });
// interp vars
if (vars) {
Object.keys(vars).forEach(function (key) {
Object.keys(vars).forEach(key => {
var regExp = new RegExp('\\$' + key, 'g');
stringified = stringified.replace(regExp, vars[key]);
});
@@ -336,7 +336,7 @@ function is_conflicting (editchanges, config_munge, self, force) {
var conflictingParent;
var conflictingPlugin;

editchanges.forEach(function (editchange) {
editchanges.forEach(editchange => {
if (files[editchange.file]) {
var parents = files[editchange.file].parents;
var target = parents[editchange.target];
@@ -409,13 +409,13 @@ function PlatformMunger_process (plugins_dir) {
var platform_config = self.platformJson.root;

// Uninstallation first
platform_config.prepare_queue.uninstalled.forEach(function (u) {
platform_config.prepare_queue.uninstalled.forEach(u => {
var pluginInfo = self.pluginInfoProvider.get(path.join(plugins_dir, u.plugin));
self.remove_plugin_changes(pluginInfo, u.topLevel);
});

// Now handle installation
platform_config.prepare_queue.installed.forEach(function (u) {
platform_config.prepare_queue.installed.forEach(u => {
var pluginInfo = self.pluginInfoProvider.get(path.join(plugins_dir, u.plugin));
self.add_plugin_changes(pluginInfo, u.vars, u.topLevel, true, u.force);
});
@@ -54,7 +54,7 @@ ConfigKeeper.prototype.get = function ConfigKeeper_get (project_dir, platform, f

ConfigKeeper.prototype.save_all = function ConfigKeeper_save_all () {
var self = this;
Object.keys(self._cached).forEach(function (fake_path) {
Object.keys(self._cached).forEach(fake_path => {
var config_file = self._cached[fake_path];
if (config_file.is_changed) config_file.save();
});
@@ -23,10 +23,8 @@ exports.deep_add = function deep_add (obj, keys /* or key1, key2 .... */) {
keys = Array.prototype.slice.call(arguments, 1);
}

return exports.process_munge(obj, true/* createParents */, function (parentArray, k) {
var found = _.find(parentArray, function (element) {
return element.xml === k.xml;
});
return exports.process_munge(obj, true/* createParents */, (parentArray, k) => {
var found = _.find(parentArray, element => element.xml === k.xml);
if (found) {
found.after = found.after || k.after;
found.count += k.count;
@@ -44,9 +42,9 @@ exports.deep_remove = function deep_remove (obj, keys /* or key1, key2 .... */)
keys = Array.prototype.slice.call(arguments, 1);
}

var result = exports.process_munge(obj, false/* createParents */, function (parentArray, k) {
var result = exports.process_munge(obj, false/* createParents */, (parentArray, k) => {
var index = -1;
var found = _.find(parentArray, function (element) {
var found = _.find(parentArray, element => {
index++;
return element.xml === k.xml;
});
@@ -74,10 +72,8 @@ exports.deep_find = function deep_find (obj, keys /* or key1, key2 .... */) {
keys = Array.prototype.slice.call(arguments, 1);
}

return exports.process_munge(obj, false/* createParents? */, function (parentArray, k) {
return _.find(parentArray, function (element) {
return element.xml === (k.xml || k);
});
return exports.process_munge(obj, false/* createParents? */, (parentArray, k) => {
return _.find(parentArray, element => element.xml === (k.xml || k));
}, keys);
};

@@ -76,11 +76,11 @@ function getCordovaNamespacePrefix (doc) {
function findElementAttributeValue (attributeName, elems) {
elems = Array.isArray(elems) ? elems : [elems];

var value = elems.filter(function (elem) {
return elem.attrib.name.toLowerCase() === attributeName.toLowerCase();
}).map(function (filteredElems) {
return filteredElems.attrib.value;
}).pop();
var value = elems.filter(elem =>
elem.attrib.name.toLowerCase() === attributeName.toLowerCase()
).map(filteredElems =>
filteredElems.attrib.value
).pop();

return value || '';
}
@@ -173,9 +173,9 @@ ConfigParser.prototype = {
throw new CordovaError('platform does not exist (received platform: ' + platform + ')');
}
const elems = this.doc.findall('./platform[@name="' + platform + '"]/preference');
let pref = elems.filter(function (elem) {
return elem.attrib.name.toLowerCase() === name.toLowerCase();
}).pop();
let pref = elems.filter(elem =>
elem.attrib.name.toLowerCase() === name.toLowerCase()
).pop();

if (!pref) {
pref = new et.Element('preference');
@@ -216,7 +216,7 @@ ConfigParser.prototype = {
var ret = [];
var staticResources = [];
if (platform) { // platform specific icons
this.doc.findall('./platform[@name="' + platform + '"]/' + resourceName).forEach(function (elt) {
this.doc.findall('./platform[@name="' + platform + '"]/' + resourceName).forEach(elt => {
elt.platform = platform; // mark as platform specific resource
staticResources.push(elt);
});
@@ -225,7 +225,7 @@ ConfigParser.prototype = {
staticResources = staticResources.concat(this.doc.findall(resourceName));
// parse resource elements
var that = this;
staticResources.forEach(function (elt) {
staticResources.forEach(elt => {
var res = {};
res.src = elt.attrib.src;
res.target = elt.attrib.target || undefined;
@@ -249,8 +249,8 @@ ConfigParser.prototype = {
* @param {number} height Height of resource.
* @return {Resource} Resource object or null if not found.
*/
ret.getBySize = function (width, height) {
return ret.filter(function (res) {
ret.getBySize = (width, height) => {
return ret.filter(res => {
if (!res.width && !res.height) {
return false;
}
@@ -264,16 +264,12 @@ ConfigParser.prototype = {
* @param {string} density Density of resource.
* @return {Resource} Resource object or null if not found.
*/
ret.getByDensity = function (density) {
return ret.filter(function (res) {
return res.density === density;
})[0] || null;
ret.getByDensity = density => {
return ret.filter(res => res.density === density)[0] || null;
};

/** Returns default icons */
ret.getDefault = function () {
return ret.defaultResource;
};
ret.getDefault = () => ret.defaultResource;

return ret;
},
@@ -307,20 +303,18 @@ ConfigParser.prototype = {
var fileResources = [];

if (platform) { // platform specific resources
fileResources = this.doc.findall('./platform[@name="' + platform + '"]/resource-file').map(function (tag) {
return {
platform: platform,
src: tag.attrib.src,
target: tag.attrib.target,
versions: tag.attrib.versions,
deviceTarget: tag.attrib['device-target'],
arch: tag.attrib.arch
};
});
fileResources = this.doc.findall('./platform[@name="' + platform + '"]/resource-file').map(tag => ({
platform: platform,
src: tag.attrib.src,
target: tag.attrib.target,
versions: tag.attrib.versions,
deviceTarget: tag.attrib['device-target'],
arch: tag.attrib.arch
}));
}

if (includeGlobal) {
this.doc.findall('resource-file').forEach(function (tag) {
this.doc.findall('resource-file').forEach(tag => {
fileResources.push({
platform: platform || null,
src: tag.attrib.src,
@@ -346,7 +340,7 @@ ConfigParser.prototype = {
var scriptElements = self.doc.findall('./hook');

if (platforms) {
platforms.forEach(function (platform) {
platforms.forEach(platform => {
scriptElements = scriptElements.concat(self.doc.findall('./platform[@name="' + platform + '"]/hook'));
});
}
@@ -366,11 +360,9 @@ ConfigParser.prototype = {
*/
getPluginIdList: function () {
var plugins = this.doc.findall('plugin');
var result = plugins.map(function (plugin) {
return plugin.attrib.name;
});
var result = plugins.map(plugin => plugin.attrib.name);
var features = this.doc.findall('feature');
features.forEach(function (element) {
features.forEach(element => {
var idTag = element.find('./param[@name="id"]');
if (idTag) {
result.push(idTag.attrib.value);
@@ -400,14 +392,14 @@ ConfigParser.prototype = {

// support arbitrary object as variables source
if (variables && typeof variables === 'object' && !Array.isArray(variables)) {
variables = Object.keys(variables)
.map(function (variableName) {
return { name: variableName, value: variables[variableName] };
});
variables = Object.keys(variables).map(variableName => ({
name: variableName,
value: variables[variableName]
}));
}

if (variables) {
variables.forEach(function (variable) {
variables.forEach(variable => {
el.append(new et.Element('variable', { name: variable.name, value: variable.value }));
});
}
@@ -442,7 +434,7 @@ ConfigParser.prototype = {
plugin.spec = pluginElement.attrib.spec || pluginElement.attrib.src || pluginElement.attrib.version;
plugin.variables = {};
var variableElements = pluginElement.findall('variable');
variableElements.forEach(function (varElement) {
variableElements.forEach(varElement => {
var name = varElement.attrib.name;
var value = varElement.attrib.value;
if (name) {
@@ -499,7 +491,7 @@ ConfigParser.prototype = {
},
getEngines: function () {
var engines = this.doc.findall('./engine');
return engines.map(function (engine) {
return engines.map(engine => {
var spec = engine.attrib.spec || engine.attrib.version;
return {
name: engine.attrib.name,
@@ -510,7 +502,7 @@ ConfigParser.prototype = {
/* Get all the access tags */
getAccesses: function () {
var accesses = this.doc.findall('./access');
return accesses.map(function (access) {
return accesses.map(access => {
var minimum_tls_version = access.attrib['minimum-tls-version']; /* String */
var requires_forward_secrecy = access.attrib['requires-forward-secrecy']; /* Boolean */
var requires_certificate_transparency = access.attrib['requires-certificate-transparency']; /* Boolean */
@@ -534,7 +526,7 @@ ConfigParser.prototype = {
/* Get all the allow-navigation tags */
getAllowNavigations: function () {
var allow_navigations = this.doc.findall('./allow-navigation');
return allow_navigations.map(function (allow_navigation) {
return allow_navigations.map(allow_navigation => {
var minimum_tls_version = allow_navigation.attrib['minimum-tls-version']; /* String */
var requires_forward_secrecy = allow_navigation.attrib['requires-forward-secrecy']; /* Boolean */
var requires_certificate_transparency = allow_navigation.attrib['requires-certificate-transparency']; /* Boolean */
@@ -550,18 +542,16 @@ ConfigParser.prototype = {
/* Get all the allow-intent tags */
getAllowIntents: function () {
var allow_intents = this.doc.findall('./allow-intent');
return allow_intents.map(function (allow_intent) {
return {
href: allow_intent.attrib.href
};
});
return allow_intents.map(allow_intent => ({
href: allow_intent.attrib.href
}));
},
/* Get all edit-config tags */
getEditConfigs: function (platform) {
var platform_edit_configs = this.doc.findall('./platform[@name="' + platform + '"]/edit-config');
var edit_configs = this.doc.findall('edit-config').concat(platform_edit_configs);

return edit_configs.map(function (tag) {
return edit_configs.map(tag => {
var editConfig = {
file: tag.attrib.file,
target: tag.attrib.target,
@@ -578,7 +568,7 @@ ConfigParser.prototype = {
var platform_config_files = this.doc.findall('./platform[@name="' + platform + '"]/config-file');
var config_files = this.doc.findall('config-file').concat(platform_config_files);

return config_files.map(function (tag) {
return config_files.map(tag => {
var configFile = {
target: tag.attrib.target,
parent: tag.attrib.parent,
@@ -604,7 +594,7 @@ function featureToPlugin (featureElement) {
pluginSrc;

var nodes = featureElement.findall('param');
nodes.forEach(function (element) {
nodes.forEach(element => {
var n = element.attrib.name;
var v = element.attrib.value;
if (n === 'id') {

0 comments on commit 574eea1

Please sign in to comment.