Skip to content

Commit

Permalink
[fix] refactor reduce loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Apr 16, 2012
1 parent ac5b66e commit fcdd926
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function complete(options) {
if (process.platform === 'win32') return;

options = options || {};
commands = options.commands || exports.commands;
commands = options.commands || exports.commands || {};
opt = options.options || exports.options;
program = options.program || exports.program;

Expand Down Expand Up @@ -59,21 +59,25 @@ function complete(options) {
function completeWords() {
var cur = words[words.length-1]
, prev = words[words.length-2]
, com = commands
, command = com
, command = commands
, l = words.length
, i = 1
, word
, out;

// Reduce
while (com) {
if (Array.isArray(com)) {
if (~com.indexOf(words[i++])) {
for (; i < l; i++) {
word = words[i];
if (Array.isArray(command)) {
if (~command.indexOf(word)) {
command = true;
}
break;
} else {
if (com = com[words[i++]]) {
command = com;
if (command[word] && hop.call(command, word)) {
command = command[word];
} else {
break;
}
}
}
Expand All @@ -85,7 +89,7 @@ function completeWords() {
: command(words, output);
} else if (command) {
// Already matched
if (i === words.length + 1) {
if (i === l) {
echo(cur);
return;
}
Expand Down Expand Up @@ -273,6 +277,8 @@ function output(key, list) {
return echo(match(key, list));
}

var hop = Object.prototype.hasOwnProperty;

/**
* Callback Style
* Expose a lower level
Expand Down

0 comments on commit fcdd926

Please sign in to comment.