Making callbacks the last parameter (standard node practice)

This commit is contained in:
Jacob Marttinen 2015-05-07 20:09:17 -04:00
parent 1b926b75c7
commit 82cd4e5118
3 changed files with 88 additions and 97 deletions

View file

@ -44,8 +44,10 @@ var Bing = function( options ) {
//merge options passed in with defaults
this.options = _.extend(defaults, options);
this.searchVertical = function(query, vertical, callback, options) {
this.searchVertical = function(query, vertical, options, callback) {
if (typeof options === 'function') {
callback = options;
}
if(typeof callback != 'function') {
throw "Error: Callback function required!";
}
@ -53,7 +55,9 @@ var Bing = function( options ) {
// Create a copy of the options, to avoid permanent overwrites
var opts = JSON.parse(JSON.stringify(this.options));
_.extend(opts, options);
if (typeof options === 'object') {
_.extend(opts, options);
}
var reqUri = opts.rootUri
+ vertical
@ -67,7 +71,7 @@ var Bing = function( options ) {
+ (opts.newslocationoverride ? "&NewsLocationOverride=%27" + opts.newslocationoverride + "%27" : '')
+ (opts.market ? "&Market=%27" + opts.market + "%27" : '')
+ (opts.adult ? "&Adult=%27" + opts.adult + "%27" : '')
+ (opts.imagefilters
+ (opts.imagefilters
? '&' + qs.stringify({ "ImageFilters": "'" + opts.imagefilters + "'" })
: '')
+ (opts.videofilters
@ -118,16 +122,17 @@ var Bing = function( options ) {
*
* @param {String} query Query term to search for.
*
* @param {requestCallback} callback Callback called with (potentially
* json-parsed) response.
*
* @param {Object} options Options to command, allows overriding
* of rootUri, accKey (Bing API key),
* userAgent, reqTimeout, top, skip
*
* @param {requestCallback} callback Callback called with (potentially
* json-parsed) response.
*
* @function
*/
Bing.prototype.web = function(query, callback, options) {
this.searchVertical(query, "Web", callback, options);
Bing.prototype.web = function(query, options, callback) {
this.searchVertical(query, "Web", options, callback);
};
// Alias Bing.search to Bing.web
@ -140,16 +145,16 @@ Bing.prototype.search = Bing.prototype.web;
*
* @param {String} query Query term to search for.
*
* @param {requestCallback} callback Callback called with (potentially
* json-parsed) response.
*
* @param {Object} options Options to command, allows overriding
* of rootUri, accKey (Bing API key),
* userAgent, reqTimeout, top, skip,
*
* @param {requestCallback} callback Callback called with (potentially
* json-parsed) response.
* @function
*/
Bing.prototype.composite = function(query, callback, options) {
this.searchVertical(query, "Composite", callback, options);
Bing.prototype.composite = function(query, options, callback) {
this.searchVertical(query, "Composite", options, callback);
};
/**
@ -157,16 +162,16 @@ Bing.prototype.composite = function(query, callback, options) {
*
* @param {String} query Query term to search for.
*
* @param {requestCallback} callback Callback called with (potentially
* json-parsed) response.
*
* @param {Object} options Options to command, allows overriding
* of rootUri, accKey (Bing API key),
* userAgent, reqTimeout, top, skip,
*
* @param {requestCallback} callback Callback called with (potentially
* json-parsed) response.
* @function
*/
Bing.prototype.news = function(query, callback, options) {
this.searchVertical(query, "News", callback, options);
Bing.prototype.news = function(query, options, callback) {
this.searchVertical(query, "News", options, callback);
};
/**
@ -174,16 +179,17 @@ Bing.prototype.news = function(query, callback, options) {
*
* @param {String} query Query term to search for.
*
* @param {requestCallback} callback Callback called with (potentially
* json-parsed) response.
*
* @param {Object} options Options to command, allows overriding
* of rootUri, accKey (Bing API key),
* userAgent, reqTimeout, top, skip,
*
* @param {requestCallback} callback Callback called with (potentially
* json-parsed) response.
* @function
*/
Bing.prototype.video = function(query, callback, options) {
Bing.prototype.video = function(query, options, callback) {
if (options
&& typeof options === 'object'
&& options.videofilters
&& typeof options.videofilters === 'object') {
var filterQuery = '';
@ -196,7 +202,7 @@ Bing.prototype.video = function(query, callback, options) {
});
options.videofilters = filterQuery;
}
this.searchVertical(query, "Video", callback, options);
this.searchVertical(query, "Video", options, callback);
};
@ -206,18 +212,19 @@ Bing.prototype.video = function(query, callback, options) {
*
* @param {String} query Query term to search for.
*
* @param {requestCallback} callback Callback called with (potentially
* json-parsed) response.
*
* @param {Object} options Options to command, allows overriding of
* rootUri, accKey (Bing API key),
* userAgent, reqTimeout, top, skip,
* imagefilters
*
* @param {requestCallback} callback Callback called with (potentially
* json-parsed) response.
* @function
*/
Bing.prototype.images = function(query, callback, options) {
if (options
&& options.imagefilters
Bing.prototype.images = function(query, options, callback) {
if (options
&& typeof options === 'object'
&& options.imagefilters
&& typeof options.imagefilters === 'object') {
var filterQuery = '';
var filters = Object.keys(options.imagefilters);
@ -229,7 +236,7 @@ Bing.prototype.images = function(query, callback, options) {
});
options.imagefilters = filterQuery;
}
this.searchVertical(query, "Image", callback, options);
this.searchVertical(query, "Image", options, callback);
};
function capitalizeFirstLetter(s) {
@ -237,4 +244,3 @@ function capitalizeFirstLetter(s) {
}
module.exports = Bing;