Mholloway has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/395099 )
Change subject: Hygiene: remove adjustMemberKeys and fillInMemberKeys from
mobile-util
......................................................................
Hygiene: remove adjustMemberKeys and fillInMemberKeys from mobile-util
Standard Array functions can and should be used to perform these
operations.
Change-Id: Iefabb9e8e56a5be9bb2c80979b605c8398cecc23
---
M lib/feed/most-read.js
M lib/mobile-util.js
M test/lib/mobile-util/mobile-util-test.js
3 files changed, 8 insertions(+), 70 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps
refs/changes/99/395099/1
diff --git a/lib/feed/most-read.js b/lib/feed/most-read.js
index 862e1b0..6ea5333 100644
--- a/lib/feed/most-read.js
+++ b/lib/feed/most-read.js
@@ -22,17 +22,6 @@
}
}
-/**
- * Construct a list of title strings from the array of good article objects.
- */
-function constructQueryListFrom(goodTitles) {
- const result = [];
- for (let i = 0, n = goodTitles.length; i < n; i++) {
- result.push(goodTitles[i].article);
- }
- return result;
-}
-
function getTopPageviews(app, req, domain, date) {
const apiDomain = 'wikimedia.org';
const restReq = {
@@ -94,11 +83,13 @@
const desktopItems = desktopResults && desktopResults.items;
const firstDesktopItems = desktopItems && desktopResults.items[0];
const combinedArticles = firstCombinedItems &&
firstCombinedItems.articles;
- const combinedArticlesSlice = combinedArticles &&
combinedArticles.slice(0, QUERY_TITLES);
+ const combinedSlice = combinedArticles && combinedArticles.slice(0,
QUERY_TITLES);
const desktopArticles = firstDesktopItems &&
firstDesktopItems.articles;
- const desktopArticlesSlice = desktopArticles &&
desktopArticles.slice(0, DESKTOP_TITLES);
+ const desktopSlice = desktopArticles && desktopArticles.slice(0,
DESKTOP_TITLES);
- goodTitles = filter.filterBotTraffic(combinedArticlesSlice,
desktopArticlesSlice);
+ goodTitles = filter.filterBotTraffic(combinedSlice, desktopSlice)
+ .map(i => Object.assign({ title: i.article }, i));
+
if (mUtil.isEmpty(goodTitles)) {
mUtil.throw404('No results found.');
}
@@ -107,7 +98,7 @@
const month = firstCombinedItems.month;
const day = firstCombinedItems.day;
resultsDate = `${year}-${month}-${day}Z`;
- return mwapi.getMostReadMetadata(app, req,
constructQueryListFrom(goodTitles).join('|'));
+ return mwapi.getMostReadMetadata(app, req, goodTitles.map(i =>
i.article).join('|'));
}).then((response) => {
const query = response.body && response.body.query;
const normalizations = query && query.normalized;
@@ -119,11 +110,10 @@
}
if (normalizations) {
- mUtil.adjustMemberKeys(normalizations, ['article', 'from'],
['title', 'to']);
- mUtil.mergeByProp(goodTitles, normalizations, 'article');
+ const adjusted = normalizations.map(norm => ({ article: norm.from,
title: norm.to }));
+ mUtil.mergeByProp(goodTitles, adjusted, 'article');
}
- mUtil.fillInMemberKeys(goodTitles, ['title', 'article']);
mUtil.mergeByProp(goodTitles, pages, 'title', true);
goodTitles = filter.filterSpecialPages(goodTitles, mainPageTitle);
diff --git a/lib/mobile-util.js b/lib/mobile-util.js
index ee461ad..ee28266 100644
--- a/lib/mobile-util.js
+++ b/lib/mobile-util.js
@@ -166,41 +166,6 @@
});
};
-/**
- * Takes an array of objects and makes the specified changes to the keys of
each
- * member object. E.g., adjustMemberKeys(arr, ['to', 'from'], ['to', 'from'],
...)
- * @param {!Array} arr an array of objects that will receive the change pairs
passed in as
- * additional params
- */
-mUtil.adjustMemberKeys = function(arr) {
- for (let i = 0, n = arr.length; i < n; i++) {
- for (let j = 1, m = arguments.length; j < m; j++) {
- if (arr[i][arguments[j][1]]) {
- arr[i][arguments[j][0]] = arr[i][arguments[j][1]];
- delete arr[i][arguments[j][1]];
- }
- }
- }
-};
-
-/**
- * Takes an array of objects and, for each object, creates the specified key
(if
- * not already present) with the same value as the specified source key for
each
- * change pair passed in as an additional parameter.
- * E.g., fillInMemberKeys(arr, ['to', 'from'], ['to', 'from'], ...)
- * @param {!Array} arr an array of objects that will receive the change pairs
passed in as
- * additional params
- */
-mUtil.fillInMemberKeys = function(arr) {
- for (let i = 0, n = arr.length; i < n; i++) {
- for (let j = 1, m = arguments.length; j < m; j++) {
- if (!arr[i][arguments[j][0]]) {
- arr[i][arguments[j][0]] = arr[i][arguments[j][1]];
- }
- }
- }
-};
-
mUtil.getRbPageSummaryUrl = function(restbaseTpl, domain, title) {
const request = restbaseTpl.expand({
request: {
diff --git a/test/lib/mobile-util/mobile-util-test.js
b/test/lib/mobile-util/mobile-util-test.js
index cdbdf23..cd210d5 100644
--- a/test/lib/mobile-util/mobile-util-test.js
+++ b/test/lib/mobile-util/mobile-util-test.js
@@ -5,19 +5,14 @@
const domino = require('domino');
const obj1 = { hello: true, world: true };
-const obj2 = { goodbye: true, sea: true, again: false };
const obj3 = { hello: true, world: true, again: false };
const obj4 = { goodbye: true, sea: true };
-const obj5 = { goodbye: true, sea: true, world: true, again: true };
const obj6 = { goodbye: true, sea: true, again: true };
const arr1 = [ obj1, obj3 ];
const arr2 = [ obj4, obj6 ];
-const arr3 = [ obj4, obj5 ];
-const arr4 = [ obj5, obj5 ];
const arr5 = [ obj1, obj3, obj6 ];
const arr6 = [ obj1, obj3 ];
-const arr7 = [ obj4, obj2 ];
const ordered = [
{ order: 1, join: 'foo' },
@@ -77,16 +72,4 @@
assert.deepEqual(arr1, arr5);
});
- it('adjustMemberKeys should make the specified adjustments', () => {
- mUtil.adjustMemberKeys(arr6, ['goodbye', 'hello'], ['sea', 'world']);
- assert.deepEqual(arr6, arr7);
-
- mUtil.adjustMemberKeys(arr3, ['goodbye', 'hello'], ['sea', 'world']);
- assert.deepEqual(arr3, arr2);
- });
-
- it('fillInMemberKeys should make the specified adjustments', () => {
- mUtil.fillInMemberKeys(arr3, ['world', 'goodbye'], ['again', 'sea']);
- assert.deepEqual(arr3, arr4);
- });
});
--
To view, visit https://gerrit.wikimedia.org/r/395099
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iefabb9e8e56a5be9bb2c80979b605c8398cecc23
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Mholloway <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits