Yoji SHIDARA
null+****@clear*****
Tue Aug 14 15:19:49 JST 2012
Yoji SHIDARA 2012-08-14 15:19:49 +0900 (Tue, 14 Aug 2012) New Revision: f69dbcb25e5447a20a5bfdeeb8270cba8c5d3853 https://github.com/nroonga/nroonga/commit/f69dbcb25e5447a20a5bfdeeb8270cba8c5d3853 Log: Rewrite to be more human-readable Modified files: lib/nroonga.js Modified: lib/nroonga.js (+59 -60) =================================================================== --- lib/nroonga.js 2012-08-14 15:17:19 +0900 (cd724cb) +++ lib/nroonga.js 2012-08-14 15:19:49 +0900 (414e438) @@ -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); +}; -------------- next part -------------- HTML����������������������������... Download