Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move longestCommonPrefix outside tab complete
  • Loading branch information
princejwesley committed Aug 3, 2016
commit 233ecc555a989b5dbf8f74ecef935356554edc14
38 changes: 19 additions & 19 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,25 @@ function complete(line, callback) {
}
}

function longestCommonPrefix(arr = []) {
const cnt = arr.length;
if (cnt === 0) return '';
if (cnt === 1) return arr[0];

const first = arr[0];
// complexity: O(m * n)
for (let m = 0; m < first.length; m++) {
const c = first[m];
for (let n = 1; n < cnt; n++) {
const entry = arr[n];
if (m >= entry.length || c !== entry[m]) {
return first.substring(0, m);
}
}
}
return first;
}

REPLServer.prototype.completeOnEditorMode = (callback) => (err, results) => {
if (err) return callback(err);

Expand All @@ -1062,25 +1081,6 @@ REPLServer.prototype.completeOnEditorMode = (callback) => (err, results) => {
const data = completions.filter(isNotEmpty).map(trimCompleteOnPrefix);

callback(null, [[`${completeOn}${longestCommonPrefix(data)}`], completeOn]);

function longestCommonPrefix(arr = []) {
const cnt = arr.length;
if (cnt === 0) return '';
if (cnt === 1) return arr[0];

const first = arr[0];
// complexity: O(m * n)
for (let m = 0; m < first.length; m++) {
const c = first[m];
for (let n = 1; n < cnt; n++) {
const entry = arr[n];
if (m >= entry.length || c !== entry[m]) {
return first.substring(0, m);
}
}
}
return first;
}
};

/**
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-.editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function run(input, output, event) {
stream.emit('data', input);
replServer.write('', event);
replServer.close();
assert(found === expected, `Expected: ${expected}, Found: ${found}`);
assert.strictEqual(found, expected);
}

const tests = [
Expand Down