Skip to content
Browse files

Rewrite to be more human-readable

  • Loading branch information...
1 parent ee56bd4 commit f69dbcb25e5447a20a5bfdeeb8270cba8c5d3853 @darashi darashi committed Aug 14, 2012
Showing with 59 additions and 60 deletions.
  1. +59 −60 lib/nroonga.js
View
119 lib/nroonga.js
@@ -1,74 +1,73 @@
-// Generated by CoffeeScript 1.3.3
-(function() {
- var formatResult, msgpack, nroonga, optionsToCommandString, overrideOutputType;
+var nroonga = module.exports = require('../build/Release/nroonga_bindings.node');
+var msgpack = require('msgpack2');
- nroonga = module.exports = require('../build/Release/nroonga_bindings.node');
+function optionsToCommandString(command, options) {
+ var args, key;
+ args = [command];
- msgpack = require('msgpack2');
-
- optionsToCommandString = function(command, options) {
- var args, key, value;
- args = [command];
- if (options != null) {
- for (key in options) {
- value = options[key];
- if (value != null) {
- args.push('--' + key);
- args.push(JSON.stringify(value));
- }
+ if (options) {
+ for (key in options) {
+ if (options[key] != null) {
+ args.push('--' + key);
+ args.push(JSON.stringify(options[key]));
}
}
- return args.join(' ');
- };
+ }
+ return args.join(' ');
+}
- overrideOutputType = function(optionsGiven, type) {
- var key, options, value;
- options = {};
- for (key in optionsGiven) {
- value = optionsGiven[key];
- options[key] = value;
- }
- options.output_type = type;
- return options;
- };
+function overrideOutputType (optionsGiven, type) {
+ var key, options;
+ options = {};
+ for (key in optionsGiven) {
+ options[key] = optionsGiven[key];
+ }
+ options.output_type = type;
+ return options;
+}
- formatResult = function(result, command) {
- if (command === 'dump') {
- return result.toString('UTF-8');
+function formatResult(result, command) {
+ if (command === 'dump') {
+ return result.toString('UTF-8');
+ } else {
+ if (result.length > 0) {
+ return msgpack.unpack(result);
} else {
- if (result.length > 0) {
- return msgpack.unpack(result);
- } else {
- return void 0;
- }
+ return null;
}
- };
+ }
+}
- nroonga.Database.prototype.commandSync = function(command, options) {
- var result;
- if (options == null) {
- options = {};
- }
- options = overrideOutputType(options, 'msgpack');
- result = this.commandSyncString(optionsToCommandString(command, options));
- return formatResult(result, command);
- };
+nroonga.Database.prototype.commandSync = function(command, options) {
+ var overridedOptions = overrideOutputType(options || {}, 'msgpack');
+ var commandString = optionsToCommandString(command, overridedOptions);
- nroonga.Database.prototype.command = function(command, options, callback) {
- var wrappedCallback;
- if (arguments.length === 2) {
- callback = options;
- options = {};
- }
- options = overrideOutputType(options, 'msgpack');
- wrappedCallback = callback != null ? function(error, data) {
- if (error != null) {
+ var result = this.commandSyncString(commandString);
+ return formatResult(result, command);
+};
+
+nroonga.Database.prototype.command = function(command, options, callback) {
+ var wrappedCallback = null;
+ var overridedOptions;
+ var commandString;
+
+ if (arguments.length === 2) {
+ callback = options;
+ options = {};
+ }
+
+ if (callback) {
+ wrappedCallback = function(error, data) {
+ if (error) {
return callback(error);
} else {
- return callback(void 0, formatResult(data, command));
+ return callback(null, formatResult(data, command));
}
- } : void 0;
- return this.commandString(optionsToCommandString(command, options), wrappedCallback);
- };
+ };
+ }
+
+ overridedOptions = overrideOutputType(options, 'msgpack');
+ commandString = optionsToCommandString(command, overridedOptions);
-}).call(this);
+ return this.commandString(commandString, wrappedCallback);
+};

0 comments on commit f69dbcb

Please sign in to comment.
Something went wrong with that request. Please try again.