Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Refactoring notes from feature/cleanup and address PR comments #566

Merged
merged 6 commits into from Jun 15, 2020
Merged

Revert for loops to forEach when iterating over arrays

  • Loading branch information
IAmThePan committed Jun 12, 2020
commit 5cf04a6c0b0572e0f6868f2ea0a90ddf589118fa
@@ -161,11 +161,10 @@ const Click2PlayContentScript = (function(win, doc) {
if (message) {
// Dequeue C2P data stored while the script injection was taking place
const messageKeys = Object.keys(message);
for (let i = 0; i < messageKeys.length; i++) {
const app_id = messageKeys[i];
messageKeys.forEach((app_id) => {
applyC2P(app_id, message[app_id].data, message[app_id].html);
delete message[app_id];
}
});
}
}

@@ -51,16 +51,14 @@ export default class FixedMenu extends React.Component {
switch (type) {
case 'enable_anti_tracking': {
const categories = Object.keys(this.antiTrackingData);
for (let i = 0; i < categories.length; i++) {
const category = categories[i];
categories.forEach((category) => {
const apps = Object.keys(this.antiTrackingData[category]);
for (let j = 0; j < apps.length; j++) {
const app = apps[j];
apps.forEach((app) => {
if (this.antiTrackingData[category][app] === 'unsafe') {
total++;
}
}
}
});
});
return total;
}
case 'enable_ad_block':
@@ -122,14 +122,13 @@ const _importSettingsNative = (state, action) => {
const { settings } = action;
const updated_state = {};
const settingsKeys = Object.keys(settings);
for (let i = 0; i < settingsKeys.length; i++) {
const key = settingsKeys[i];
settingsKeys.forEach((key) => {
let value = settings[key];
if (key === 'alert_bubble_timeout') {
value = (value > 30) ? 30 : value;
}
updated_state[key] = value;
}
});

updated_state.settings_last_imported = Number((new Date()).getTime());
updated_state.importResultText = `${t('settings_import_success')} ${moment(updated_state.settings_last_imported).format('LLL')}`;
@@ -163,14 +163,13 @@ function setGhosteryDefaultBlocking() {
log('Blocking all trackers in categories:', ...categoriesBlock);
const selected_app_ids = {};
const app_ids = Object.keys(bugDb.db.apps);
for (let i = 0; i < app_ids.length; i++) {
const app_id = app_ids[i];
app_ids.forEach((app_id) => {
const category = bugDb.db.apps[app_id].cat;
if (categoriesBlock.indexOf(category) >= 0 &&
!selected_app_ids.hasOwnProperty(app_id)) {
selected_app_ids[app_id] = 1;
}
}
});
panelData.set({ selected_app_ids });
}

@@ -580,12 +579,11 @@ function handleGhosteryHub(name, message, callback) {
panelData.set({ setup_block: 3 });
const selected_app_ids = {};
const app_ids = Object.keys(bugDb.db.apps);
for (let i = 0; i < app_ids.length; i++) {
const app_id = app_ids[i];
app_ids.forEach((app_id) => {
if (!selected_app_ids.hasOwnProperty(app_id)) {
selected_app_ids[app_id] = 1;
}
}
});
panelData.set({ selected_app_ids });
break;
}
@@ -76,7 +76,6 @@ class BugDb extends Updatable {
let appId;
let category;
let blocked;
let categoryName;

const categoryArray = [];
const categories = {};
@@ -123,8 +122,7 @@ class BugDb extends Updatable {
}

const categoryNames = Object.keys(categories);
for (let i = 0; i < categoryNames.length; i++) {
categoryName = categoryNames[i];
categoryNames.forEach((categoryName) => {
const cat = categories[categoryName];
if (cat.trackers) {
cat.trackers.sort((a, b) => {
@@ -135,7 +133,7 @@ class BugDb extends Updatable {
}

categoryArray.push(cat);
}
});

// Sort categories by tracker numbers
categoryArray.sort((a, b) => {
@@ -176,10 +174,9 @@ class BugDb extends Updatable {
log('initializing bugdb regexes...');

const regexesKeys = Object.keys(regexes);
for (let i = 0; i < regexesKeys.length; i++) {
const id = regexesKeys[i];
regexesKeys.forEach((id) => {
db.patterns.regex[id] = new RegExp(regexes[id], 'i');
}
});

log('setting bugdb noneSelected/allSelected...');

@@ -68,15 +68,14 @@ class Click2PlayDb extends Updatable {

let keep = false;
const allowKeys = Object.keys(this.allowOnceList[tab_id]);
for (let i = 0; i < allowKeys.length; i++) {
const appID = allowKeys[i];
allowKeys.forEach((appID) => {
const count = this.allowOnceList[tab_id][appID];
const newCount = count - 1;
this.allowOnceList[tab_id][appID] = newCount;
if (newCount > 0) {
keep = true;
}
}
});
if (!keep) {
delete this.allowOnceList[tab_id];
}
@@ -194,20 +194,15 @@ class FoundBugs {
const bugs = this.getBugs(tab_id);
const { db } = bugDb;

let id;
let appid;
let cid; // category id

if (!bugs) {
return bugs;
}

// squish all the bugs into categories first
const ids = Object.keys(bugs);
for (let i = 0; i < ids.length; i++) {
id = ids[i];
appid = db.bugs[id].aid;
cid = db.apps[appid].cat;
ids.forEach((id) => {
const appid = db.bugs[id].aid;
const cid = db.apps[appid].cat;

if (cats_obj.hasOwnProperty(cid)) {
if (!cats_obj[cid].appIds.includes(appid)) {
@@ -239,14 +234,13 @@ class FoundBugs {
total: 1
};
}
}
});

// convert categories hash to array
const cids = Object.keys(cats_obj);
for (let i = 0; i < cids.length; i++) {
cid = cids[i];
cids.forEach((cid) => {
cats_arr.push(cats_obj[cid]);
}
});

if (sorted) {
cats_arr.sort((a, b) => {
@@ -590,8 +590,7 @@ class PanelData {

// Set the conf from data
const dataKeys = Object.keys(data);
for (let i = 0; i < dataKeys.length; i++) {
const key = dataKeys[i];
dataKeys.forEach((key) => {
const value = data[key];
if (conf.hasOwnProperty(key) && !isEqual(conf[key], value)) {
conf[key] = value;
@@ -613,7 +612,7 @@ class PanelData {
}
PanelData._toggleBlockingHelper();
}
}
});

if (data.needsReload && this._activeTab) {
tabInfo.setTabInfo(this._activeTab.id, 'needsReload', data.needsReload);
@@ -58,17 +58,15 @@ export function getCliqzData(tabId, tabHostUrl, antiTracking) {
return tracker.ads;
};

for (let i = 0; i < bugsValues.length; i++) {
const bug = bugsValues[i];
bugsValues.forEach((bug) => {
const dataPoints = getDataPoints(bug);
if (dataPoints) {
totalUnsafeCount += dataPoints;
trackerCount++;
}
}
});

for (let i = 0; i < othersValues.length; i++) {
const other = othersValues[i];
othersValues.forEach((other) => {
let whitelisted = false;
const dataPoints = getDataPoints(other);

@@ -98,7 +96,7 @@ export function getCliqzData(tabId, tabHostUrl, antiTracking) {
name, domains, ads, cookies, fingerprints, whitelisted, type
});
}
}
});

return {
totalUnsafeCount,
@@ -354,11 +354,10 @@ function _fetchJson(method, url, query, extraHeaders, referrer = 'no-referrer',
});
if (extraHeaders) {
const extraHeadersKeys = Object.keys(extraHeaders);
for (let i = 0; i < extraHeadersKeys.length; i++) {
const key = extraHeadersKeys[i];
extraHeadersKeys.forEach((key) => {
const value = extraHeaders[key];
headers.append(key, value);
}
});
}
const options = {
method,
@@ -450,11 +449,10 @@ function _fetchJson(method, url, query, extraHeaders, referrer = 'no-referrer',
xhr.setRequestHeader('Accept', 'application/json');
if (extraHeaders) {
const extraHeadersKeys = Object.keys(extraHeaders);
for (let i = 0; i < extraHeadersKeys.length; i++) {
const key = extraHeadersKeys[i];
extraHeadersKeys.forEach((key) => {
const value = extraHeaders[key];
xhr.setRequestHeader(key, value);
}
});
}
xhr.overrideMimeType('application/json');
xhr.send(query);
@@ -66,10 +66,9 @@ const leet_convert = function(string) {
output = output.replace(/cks/g, 'x');

const characterKeys = Object.keys(characterMap);
for (let i = 0; i < characterKeys.length; i++) {
const letter = characterKeys[i];
characterKeys.forEach((letter) => {
output = output.replace(new RegExp(letter, 'g'), characterMap[letter]);
}
});

return output;
};
@@ -85,14 +84,13 @@ if (!fs.existsSync('./tools/leet/messages.en.copy.json')) {

// Create a LEETed version of the messages.json file
const enKeys = Object.keys(en);
for (let i = 0; i < enKeys.length; i++) {
const key = enKeys[i];
enKeys.forEach((key) => {
if (en[key].hasOwnProperty('message')) {
const message = leet_convert(en[key].message);
const { placeholders } = en[key];
leet[key] = { message, placeholders };
}
}
});

// Save the leeted version and override the existing English messages.json file
fs.writeFileSync('./tools/leet/messages.leet.json', JSON.stringify(leet), 'utf-8');
ProTip! Use n and p to navigate between commits in a pull request.