Mvolz has uploaded a new change for review.

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

Change subject: Fix style in all .js files
......................................................................

Fix style in all .js files

Reviewed style in all .js files

* Added documentation to methods where
needed
* Fixed spaces and capitalisation in
method documentation
* Chained variable definitions in
module import statements

Change-Id: Ibfb8e5c8768f435ad58d3ea26381948e716f747f
---
M lib/distinguish.js
M lib/pubMedRequest.js
M lib/requests.js
M lib/scrape.js
M lib/unshorten.js
M lib/zotero.js
M server.js
7 files changed, 140 insertions(+), 79 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/citoid 
refs/changes/48/180448/1

diff --git a/lib/distinguish.js b/lib/distinguish.js
index 052860c..9205013 100644
--- a/lib/distinguish.js
+++ b/lib/distinguish.js
@@ -6,14 +6,15 @@
  * URL or DOI, and sends to appropriate request handler.
  */
 
-var request = require('request');
-var urlParse = require('url');
-var requestFromURL = require('./requests.js').requestFromURL;
-var requestFromDOI = require('./requests.js').requestFromDOI;
+/* Import Modules */
+var request = require('request'),
+       urlParse = require('url'),
+       requestFromURL = require('./requests.js').requestFromURL,
+       requestFromDOI = require('./requests.js').requestFromDOI;
 
 /**
- * Determine type of string (doi, url) and callback on correct gandler
- * @param  {[type]}   searchString what the end user searched for
+ * Determine type of string (doi, url) and callback on correct handler
+ * @param  {String}   searchString what the end user searched for
  * @param  {Function} callback     callback(extractedValue, correctFunction)
  */
 var distinguish = function (searchString, callback){
@@ -40,14 +41,15 @@
        }
 };
 
-module.exports = {
-       distinguish: distinguish
-};
-
 /*Test methods in main */
 if (require.main === module) {
 
        distinguish("example.com", function (extracted, selectedFcn){
                console.log(extracted);
        });
-}
\ No newline at end of file
+}
+
+/* Exports */
+module.exports = {
+       distinguish: distinguish
+};
\ No newline at end of file
diff --git a/lib/pubMedRequest.js b/lib/pubMedRequest.js
index 231d939..472318e 100644
--- a/lib/pubMedRequest.js
+++ b/lib/pubMedRequest.js
@@ -7,19 +7,18 @@
 
 (function() {
 
-    var request = require('request');
-       var bunyan = require('bunyan');
-       var log = bunyan.createLogger({name: "citoid"});
+    var request = require('request'),
+       bunyan = require('bunyan'),
+       log = bunyan.createLogger({name: "citoid"});
 
        /**
         * Requests a PubMed object using any supported identifier
         * @param  {String}   identifier Valid PubMed identifier (PMID, PMCID, 
Manuscript ID, versioned ID)
         * @param  {Function} callback   callback (error, object)
         */
-
        var pubMedRequest = function (identifier, callback){
-        var escapedId = encodeURIComponent(identifier);
-               var url = 
"http://www.ncbi.nlm.nih.gov/pmc/utils/idconv/v1.0/?tool=citoid&email=citoid@mediawiki&format=json&ids=";
 + escapedId;
+        var escapedId = encodeURIComponent(identifier),
+               url = 
"http://www.ncbi.nlm.nih.gov/pmc/utils/idconv/v1.0/?tool=citoid&email=citoid@mediawiki&format=json&ids=";
 + escapedId;
 
                request(url, function(error, response, body){
                        log.info("PubMed query made for: " + url);
diff --git a/lib/requests.js b/lib/requests.js
index b51eb8a..588accf 100644
--- a/lib/requests.js
+++ b/lib/requests.js
@@ -6,14 +6,18 @@
  * such as URL or DOI
  */
 
-var bunyan = require('bunyan');
+/* Import External Modules */
+var util = require('util'),
+       bunyan = require('bunyan');
+
+/* Import Local Modules */
+var unshorten = require('./unshorten.js'),
+       scrape = require('./scrape.js').scrape,
+       zoteroWebRequest = require('./zotero.js').zoteroWebRequest,
+       zoteroExportRequest = require('./zotero.js').zoteroExportRequest,
+       pubMedRequest = require('./pubMedRequest.js');
+
 var log = bunyan.createLogger({name: "citoid"});
-var unshorten = require('./unshorten.js');
-var scrape = require('./scrape.js').scrape;
-var zoteroWebRequest = require('./zotero.js').zoteroWebRequest;
-var zoteroExportRequest = require('./zotero.js').zoteroExportRequest;
-var pubMedRequest = require('./pubMedRequest.js');
-var util = require('util');
 
 /**
  * Request citation metadata from a URL
@@ -21,7 +25,6 @@
  * @param  {Object}   opts         zoteroWebRequest options object
  * @param  {Function} callback     callback (error, statusCode, body)
  */
-
 var requestFromURL = function (requestedURL, opts, callback){
        zoteroWebRequest(requestedURL, opts, function(error, response, body){
                log.info("Zotero request made for: " + requestedURL);
@@ -102,7 +105,6 @@
  * @param  {Object}   opts         zoteroWebRequest options object
  * @param  {Function} callback     callback (error, statusCode, body)
  */
-
 var requestFromDOI = function (requestedDOI, opts, callback){
        var doiLink = 'http://dx.doi.org/'+requestedDOI;
        //TODO: optimise this (can skip some steps in requestFromURL)
@@ -117,7 +119,6 @@
  * @param  {Object}   opts               zoteroWebRequest options object
  * @param  {Function} callback           callback (error, statusCode, body)
  */
-
 var requestFromPubMedID = function (requestedPubMedID, opts, callback){
     pubMedRequest(requestedPubMedID, function(error, obj){
                if(error){
@@ -147,6 +148,7 @@
        });
 }
 
+/* Exports */
 module.exports = {
        requestFromURL: requestFromURL,
        requestFromDOI: requestFromDOI,
diff --git a/lib/scrape.js b/lib/scrape.js
index cd7be26..ae07062 100644
--- a/lib/scrape.js
+++ b/lib/scrape.js
@@ -3,10 +3,10 @@
  * https://www.mediawiki.org/wiki/citoid
  */
 
-var request = require('request');
-var urlParse = require('url');
-var cheerio = require('cheerio');
-
+/* Import Modules */
+var request = require('request'),
+       urlParse = require('url'),
+       cheerio = require('cheerio');
 
 /**
  * Currently scrapes title only
diff --git a/lib/unshorten.js b/lib/unshorten.js
index f71d25b..f4af7b0 100644
--- a/lib/unshorten.js
+++ b/lib/unshorten.js
@@ -1,10 +1,10 @@
 (function() {
 
-       var request = require('request');
-       var urlParse = require('url');
+       var request = require('request'),
+               urlParse = require('url');
 
        /**
-        * [unshorten description]
+        * Follows redirects in a URL
         * @param  {String}   url      url trying to be unshortened
         * @param  {Function} callback callback(detectedRedirect, url)
         */
diff --git a/lib/zotero.js b/lib/zotero.js
index cff1a1c..d8bdc76 100644
--- a/lib/zotero.js
+++ b/lib/zotero.js
@@ -5,10 +5,11 @@
  * Supplies methods to send requests to a Zotero server
  */
 
-var request = require('request');
-var async = require('async');
-var util = require('util');
-var pubMedRequest = require('./pubMedRequest.js');
+/* Import Modules */
+var request = require('request'),
+       async = require('async'),
+       util = require('util'),
+       pubMedRequest = require('./pubMedRequest.js');
 
 /**
  * Requests to Zotero server endpoint /web
@@ -62,7 +63,11 @@
        });
 };
 
-/*Picks fcn given format*/
+/**
+ * Selects the format function given format string
+ * @param  {String}   format   string describing format
+ * @param  {Function} callback callback(desiredFunctionHere)
+ */
 var selectFormatFcn = function (format, callback){
        var formatFcns = {
                'mwDeprecated':convertToMWDeprecatedAsync,
@@ -73,7 +78,15 @@
        callback(formatFcns[format]);
 };
 
-/*Specific conversion methods*/
+/* Specific Conversion Methods */
+
+/**
+ * Takes Zotero output and standardises it
+ * @param  {String}   url      URL provided by user
+ * @param  {Object}   opts     opts object for Zotero requests
+ * @param  {Array}    body     Array of citation objects
+ * @param  {Function} callback callback(arrayOfCitationObjs)
+ */
 var convertToZoteroAsync = function (url, opts, body, callback){
        var citation = body[0];
        if (citation instanceof Array){
@@ -92,6 +105,13 @@
 
 };
 
+/**
+ * Takes Zotero output, standardises, and exports to BibTex
+ * @param  {String}   url      URL provided by user
+ * @param  {Object}   opts     opts object for Zotero requests
+ * @param  {Array}    body     Array of citation objects
+ * @param  {Function} callback callback(arrayOfCitationObjs)
+ */
 var convertToBibtexAsync = function(url, opts, body, callback){
        var citation = body[0];
        if (citation instanceof Array) {
@@ -114,6 +134,13 @@
 
 };
 
+/**
+ * Takes Zotero output and converts to 'mediawiki' format
+ * @param  {String}   url      URL provided by user
+ * @param  {Object}   opts     opts object for Zotero requests
+ * @param  {Array}    body     Array of citation objects
+ * @param  {Function} callback callback(arrayOfCitationObjs)
+ */
 var convertToMediawikiAsync = function (url, opts, body, callback){
        var citation = body[0];
        if (citation instanceof Array){
@@ -136,6 +163,13 @@
 
 };
 
+/**
+ * Takes Zotero output and converts to 'mwDeprecated' format
+ * @param  {String}   url      URL provided by user
+ * @param  {Object}   opts     opts object for Zotero requests
+ * @param  {Array}    body     Array of citation objects
+ * @param  {Function} callback callback(arrayOfCitationObjs)
+ */
 var convertToMWDeprecatedAsync = function (url, opts, body, callback){
        var zotCreators, creatorFieldName,
                creatorTypeCount = {},
@@ -181,10 +215,14 @@
 
 };
 
-/*Methods for particular fields-
-* Targets Mediawiki format if not otherwise specified
-*/
+/* Methods for Particular Fields in Citation Object*/
 
+/**
+ * Remove creators and add 2D array of fname, lname keyed by creatorType
+ * Used to convert to 'mediawiki' format
+ * @param  {Object}   citation     citation object
+ * @param  {Function} callback     callback on citation object
+ */
 var replaceCreators = function(citation, callback){
        if (citation.creators) {
                zotCreators = citation.creators;
@@ -213,7 +251,6 @@
  * @param  {Object}   citation citation object to add PMID
  * @param  {Function} callback callback (error, citation)
  */
-
 var addPubMedIdentifiers = function(citation, callback){
        if (citation.extra) {
                //get pmid from extra fields
@@ -246,6 +283,12 @@
        }
 };
 
+/**
+ * Add URL provided by user if none in Zotero response
+ * @param  {String}   url      url provided by user
+ * @param  {Object}   citation citation object to add PMID
+ * @param  {Function} callback callback (error, citation)
+ */
 var fixURL = function(url, citation, callback){
        if (!citation.url){
                citation.url = url;
@@ -253,6 +296,11 @@
        callback(null, citation);
 };
 
+/**
+ * Replace Zotero output of CURRENT_TIMESTAMP with ISO time
+ * @param  {Object}   citation     citation object
+ * @param  {Function} callback     callback on citation object
+ */
 var fixAccessDate = function(citation, callback){
        if (!citation.accessDate || (citation.accessDate == 
"CURRENT_TIMESTAMP")){
                citation.accessDate = (new Date()).toISOString().substring(0, 
10);
@@ -260,6 +308,11 @@
        callback(null, citation);
 };
 
+/**
+ * Convert String of ISSNs into an Array of ISSNs
+ * @param  {Object}   citation     citation object
+ * @param  {Function} callback     callback on citation object
+ */
 var fixISSN = function(citation, callback){
        var match, i,
                issn = citation.ISSN;
@@ -281,6 +334,11 @@
        callback(null, citation);
 };
 
+/**
+ * Convert String of ISBNs into an Array of ISBNs
+ * @param  {Object}   citation     citation object
+ * @param  {Function} callback     callback on citation object
+ */
 var fixISBN = function(citation, callback){
        var match, i,
                isbn = citation.ISBN;
@@ -302,7 +360,7 @@
        callback(null, citation);
 };
 
-/*Test response alterations without having to use server*/
+/* Test response alterations without having to use server */
 var testJSON = function(){
        var sampleBody = require("../test_files/4_input.json");
        console.log("before:");
@@ -321,12 +379,12 @@
        });
 };
 
-/*Test methods in main */
+/* Test methods in main */
 if (require.main === module) {
        testJSON();
 }
 
-/*Exports*/
+/* Exports */
 module.exports = {
        zoteroWebRequest: zoteroWebRequest,
     zoteroExportRequest: zoteroExportRequest
diff --git a/server.js b/server.js
index 6fa5b9e..4af484e 100644
--- a/server.js
+++ b/server.js
@@ -3,45 +3,45 @@
  * https://www.mediawiki.org/wiki/citoid
  */
 
-/*external modules*/
-var bodyParser = require('body-parser');
-var bunyan = require('bunyan');
-var express = require('express');
-var path = require('path');
-var urlParse = require('url');
-var util = require('util');
-var opts = require('yargs')
+/* Import External Modules */
+var bodyParser = require('body-parser'),
+       bunyan = require('bunyan'),
+       crypto = require('crypto'),
+       express = require('express'),
+       path = require('path'),
+       urlParse = require('url'),
+       util = require('util'),
+       opts = require('yargs')
        .usage('Usage: $0 [-c configfile|--config=configfile]')
        .default({
                c: __dirname + '/localsettings.js'
        })
-       .alias( 'c', 'config' );
-var argv = opts.argv;
-var crypto = require('crypto');
+       .alias( 'c', 'config' ),
+       argv = opts.argv;
 
-/*Local modules*/
-var distinguish = require('./lib/distinguish.js').distinguish;
-var requestFromURL = require('./lib/requests.js').requestFromURL;
+/* Import Local Modules */
+var distinguish = require('./lib/distinguish.js').distinguish,
+       requestFromURL = require('./lib/requests.js').requestFromURL;
 
-/* import local settings*/
-var settingsFile = path.resolve(process.cwd(), argv.c);
-var CitoidConfig = require(settingsFile).CitoidConfig;
-var citoidPort = CitoidConfig.citoidPort;
-var citoidInterface = CitoidConfig.citoidInterface;
-var zoteroPort = CitoidConfig.zoteroPort;
-var zoteroInterface = CitoidConfig.zoteroInterface;
-var debug = CitoidConfig.debug;
-var allowCORS = CitoidConfig.allowCORS;
+/* Import Local Settings */
+var settingsFile = path.resolve(process.cwd(), argv.c),
+       CitoidConfig = require(settingsFile).CitoidConfig,
+       citoidPort = CitoidConfig.citoidPort,
+       citoidInterface = CitoidConfig.citoidInterface,
+       zoteroPort = CitoidConfig.zoteroPort,
+       zoteroInterface = CitoidConfig.zoteroInterface,
+       debug = CitoidConfig.debug,
+       allowCORS = CitoidConfig.allowCORS;
 
-//url base which allows further formatting by adding a single endpoint, i.e. 
'web'
+// URL base which allows further formatting by adding a single endpoint, i.e. 
'web'
 var zoteroURL = util.format('http://%s:%s/%s', zoteroInterface, 
zoteroPort.toString());
 
-//Init citoid webserver
+// Init citoid webserver
 var citoid = express();
 var log = bunyan.createLogger({name: "citoid"});
 
-//SECURITY WARNING: ALLOWS ALL REQUEST ORIGINS
-//change allowCORS in localsettings.js
+// SECURITY WARNING: ALLOWS ALL REQUEST ORIGINS
+// change allowCORS in localsettings.js
 citoid.all('*', function(req, res, next) {
   res.header("Access-Control-Allow-Origin", allowCORS);
   res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
@@ -52,8 +52,8 @@
 citoid.use(bodyParser.urlencoded({extended: false}));
 citoid.use(express.static('api')); //cache api pages
 
-/*Landing page*/
-/*jshint multistr: true */
+/* Landing Page */
+/* jshint multistr: true */
 citoid.get('/', function(req, res){
        res.setHeader("Content-Type", "text/html");
        res.send('<!DOCTYPE html>\
@@ -74,7 +74,7 @@
        ');
 });
 
-/*Endpoint for retrieving citations in JSON format from a URL*/
+/* Endpoint for retrieving citations in JSON format from a URL */
 citoid.post('/url', function(req, res){
 
        res.type('application/json');
@@ -123,7 +123,7 @@
        }
 });
 
-/**Endpoint for retrieving citations based on search term (URL,DOI)*/
+/* Endpoint for retrieving citations based on search term (URL, DOI) */
 citoid.get('/api', function(req, res){
 
        res.type('application/json');
@@ -174,5 +174,5 @@
 
 log.info('Server started on http://localhost:'+citoidPort);
 
-/*Exports*/
+/* Exports */
 exports = module.exports = citoid;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibfb8e5c8768f435ad58d3ea26381948e716f747f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/citoid
Gerrit-Branch: master
Gerrit-Owner: Mvolz <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to