Mholloway has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/298331

Change subject: Consolidate mobile-util methods in single export object
......................................................................

Consolidate mobile-util methods in single export object

Exporting a single object with Function properties rather than a list
of 15 functions (and counting) will ease maintenance.

Also deletes the spliceString function, which is no longer used.

Change-Id: If02467bff52dc0a988c8c87e742cf22028f43ef8
---
M lib/mobile-util.js
1 file changed, 38 insertions(+), 58 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps 
refs/changes/31/298331/1

diff --git a/lib/mobile-util.js b/lib/mobile-util.js
index 236f2ef..a5de338 100644
--- a/lib/mobile-util.js
+++ b/lib/mobile-util.js
@@ -3,45 +3,48 @@
 var underscore = require('underscore');
 var uuid = require('cassandra-uuid').TimeUuid;
 var HTTPError = require('./util').HTTPError;
+var mUtil = {};
 
 
 /**
  * @returns true if val is null, undefined, an empty object, an empty array, or
  *          an empty string.
  */
-function isEmpty(val) {
+mUtil.isEmpty = function(val) {
   return !underscore.isNumber(val)
       && !underscore.isBoolean(val)
       && underscore.isEmpty(val);
-}
+};
 
-var isNonempty = underscore.negate(isEmpty);
+mUtil.isNonempty = function() {
+    underscore.negate(this.isEmpty);
+};
 
 /**
  * @param [fallback]
  * @returns val if nonempty, else fallback.
 */
-function defaultVal(val, fallback) {
+mUtil.defaultVal = function(val, fallback) {
     return underscore.isEmpty(val) ? fallback : val;
-}
+};
 
 /**
  * @returns val less empty elements.
 */
-function filterEmpty(val) {
+mUtil.filterEmpty = function(val) {
     if (Array.isArray(val)) {
-        return val.map(filterEmpty).filter(isNonempty);
+        return val.map(this.filterEmpty).filter(this.isNonempty);
     }
     if (underscore.isObject(val)) {
-        return underscore.pick(underscore.mapObject(val, filterEmpty), 
isNonempty);
+        return underscore.pick(underscore.mapObject(val, this.filterEmpty), 
this.isNonempty);
     }
     return val;
-}
+};
 
-function setContentType(app, res) {
+mUtil.setContentType = function(app, res) {
     let specVersion = app.conf.spec.info.version;
     res.type(`application/json; charset=utf-8; 
profile="https://www.mediawiki.org/wiki/Specs/content-services/${specVersion}"`);
-}
+};
 
 /**
  * Sets the ETag header on the response object to a specified value.
@@ -49,9 +52,9 @@
  * @param {Object}  response the HTTPResponse object on which to set the header
  * @param {Object}  value to set the ETag to
  */
-function setETagToValue(response, value) {
+mUtil.setETagToValue = function(response, value) {
     response.set('etag', '' + value);
-}
+};
 
 /**
  * Sets the ETag header on the response object. First, the request object is
@@ -65,7 +68,7 @@
  * @param {integer} revision the revision ID to use
  * @param {string}  tid      the time UUID to use; optional
  */
-function setETag(request, response, revision, tid) {
+mUtil.setETag = function(request, response, revision, tid) {
     if (request && request.headers && request.headers['x-restbase-etag']) {
         response.set('etag', request.headers['x-restbase-etag']);
         return;
@@ -73,36 +76,36 @@
     if (!tid) {
         tid = uuid.now().toString();
     }
-    setETagToValue(response, revision + '/' + tid);
-}
+    this.setETagToValue(response, revision + '/' + tid);
+};
 
 /**
  * Convert mobile to canonical domain,
  * e.g., 'en.m.wikipedia.org' => 'en.wikipedia.org'
  */
-function mobileToCanonical(domain) {
+mUtil.mobileToCanonical = function(domain) {
    return domain.replace('.m.', '.');
-}
+};
 
 /**
  * Remove the top-level domain from a domain string, e.g., 'en.wikipedia.org' 
->
  * 'en.wikipedia'.
  */
-function removeTLD(domain) {
+mUtil.removeTLD = function(domain) {
     return domain.split('.').slice(0,2).join('.');
-}
+};
 
 /**
  * Get the language of the wiki base don the domain name.
  * Example: 'en.wikipedia.org' -> 'en'.
  */
-function getLanguageFromDomain(domain) {
+mUtil.getLanguageFromDomain = function(domain) {
     return domain.split('.')[0];
-}
+};
 
 // Merge two arrays of objects by the specified property.
 // Stolen from https://jsfiddle.net/guya/eAWKR/.
-function mergeByProp(arr1, arr2, prop, pushIfKeyNotFound) {
+mUtil.mergeByProp = function(arr1, arr2, prop, pushIfKeyNotFound) {
     underscore.each(arr2, function(arr2obj) {
         var arr1obj = underscore.find(arr1, function(arr1obj) {
             return arr1obj[prop] === arr2obj[prop];
@@ -118,7 +121,7 @@
             }
         }
     });
-}
+};
 
 /**
  * Takes an array of objects and makes the specified changes to the keys of 
each
@@ -128,7 +131,7 @@
  *                          key changes, of the following form:
  *                          [['to', 'from'], ['to', 'from'], ...]
  */
-function adjustMemberKeys(arr, changes) {
+mUtil.adjustMemberKeys = function(arr, changes) {
     for (var i = 0, n = arr.length; i < n; i++) {
         for (var j = 0, m = changes.length; j < m; j++) {
             if (arr[i][changes[j][1]]) {
@@ -137,7 +140,7 @@
             }
         }
     }
-}
+};
 
 /**
  * Takes an array of objects and, for each object, creates the specified key 
(if
@@ -148,7 +151,7 @@
  *                          key changes, of the following form:
  *                          [['to', 'from'], ['to', 'from'], ...]
  */
-function fillInMemberKeys(arr, changes) {
+mUtil.fillInMemberKeys = function(arr, changes) {
     for (var i = 0, n = arr.length; i < n; i++) {
         for (var j = 0, m = changes.length; j < m; j++) {
             if (!arr[i][changes[j][0]]) {
@@ -156,53 +159,30 @@
             }
         }
     }
-}
+};
 
 /**
  * Construct an etag using the date from the feed endpoint request.
  * Example: '2016/03/05' -> '20160305/bb6b7552-2cea-11e6-8490-df3f275c37a6'
  */
-function getDateStringEtag(dateString) {
+mUtil.getDateStringEtag = function(dateString) {
     return dateString + '/' + uuid.now().toString();
-}
-
-/**
- * Basic string equivalent to Array.prototype.splice().
- */
-function spliceString(str, start, end) {
-    return str.slice(0, start) + str.slice(end + 1);
-}
+};
 
 /**
  * Strip HTML markup from a string.
  */
-function stripMarkup(text) {
+mUtil.stripMarkup = function(text) {
     return text.replace(/<[^>]*>/g, '');
-}
+};
 
-function throw404(message) {
+mUtil.throw404 = function(message) {
     throw new HTTPError({
         status: 404,
         type: 'not_found',
         title: 'Not found',
         detail: message
     });
-}
-
-module.exports = {
-    filterEmpty: filterEmpty,
-    defaultVal: defaultVal,
-    setContentType: setContentType,
-    setETagToValue: setETagToValue,
-    setETag: setETag,
-    getDateStringEtag: getDateStringEtag,
-    mobileToCanonical: mobileToCanonical,
-    removeTLD: removeTLD,
-    getLanguageFromDomain: getLanguageFromDomain,
-    mergeByProp: mergeByProp,
-    adjustMemberKeys: adjustMemberKeys,
-    fillInMemberKeys: fillInMemberKeys,
-    spliceString: spliceString,
-    stripMarkup: stripMarkup,
-    throw404: throw404
 };
+
+module.exports = mUtil;

-- 
To view, visit https://gerrit.wikimedia.org/r/298331
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If02467bff52dc0a988c8c87e742cf22028f43ef8
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

Reply via email to