fix: array compare function
diff --git a/lib/difflib.js b/lib/difflib.js
index e00e5ab..6d0cfc0 100644
--- a/lib/difflib.js
+++ b/lib/difflib.js
@@ -43,12 +43,13 @@
};
_arrayCmp = function(a, b) {
- var i, _ref;
- for (i = 0, _ref = max(a.length, b.length); 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
+ var i, la, lb, _ref, _ref2;
+ _ref = [a.length, b.length], la = _ref[0], lb = _ref[1];
+ for (i = 0, _ref2 = min(la, lb); 0 <= _ref2 ? i < _ref2 : i > _ref2; 0 <= _ref2 ? i++ : i--) {
if (a[i] < b[i]) return -1;
if (a[i] > b[i]) return 1;
}
- return 0;
+ return la - lb;
};
SequenceMatcher = (function() {
@@ -628,7 +629,7 @@
result.push([s.ratio(), x]);
}
}
- result = Heap.nlargest(n, result);
+ result = Heap.nlargest(n, result, _arrayCmp);
_results = [];
for (_j = 0, _len2 = result.length; _j < _len2; _j++) {
_ref = result[_j], score = _ref[0], x = _ref[1];
@@ -1318,6 +1319,8 @@
return lines;
};
+ exports._arrayCmp = _arrayCmp;
+
exports.SequenceMatcher = SequenceMatcher;
exports.getCloseMatches = getCloseMatches;
diff --git a/src/difflib.coffee b/src/difflib.coffee
index 3eb1c97..0c6887a 100644
--- a/src/difflib.coffee
+++ b/src/difflib.coffee
@@ -33,10 +33,11 @@
if length then (2.0 * matches / length) else 1.0
_arrayCmp = (a, b) ->
- for i in [0...max(a.length, b.length)]
+ [la, lb] = [a.length, b.length]
+ for i in [0...min(la, lb)]
return -1 if a[i] < b[i]
return 1 if a[i] > b[i]
- 0
+ la - lb
class SequenceMatcher
###
@@ -711,7 +712,7 @@
result.push([s.ratio(), x])
# Move the best scorers to head of list
- result = Heap.nlargest(n, result)
+ result = Heap.nlargest(n, result, _arrayCmp)
# Strip scores for the best n matches
(x for [score, x] in result)
@@ -1314,6 +1315,7 @@
lines
# exports to global
+exports._arrayCmp = _arrayCmp
exports.SequenceMatcher = SequenceMatcher
exports.getCloseMatches = getCloseMatches
exports._countLeading = _countLeading
diff --git a/test/global.coffee b/test/global.coffee
index a3eaf60..a5bb19c 100644
--- a/test/global.coffee
+++ b/test/global.coffee
@@ -1,4 +1,5 @@
{
+ _arrayCmp,
getCloseMatches,
_countLeading,
IS_LINE_JUNK,
@@ -13,6 +14,16 @@
suite 'global'
+test '._arrayCmp', ->
+ _arrayCmp([1, 2], [1, 2]).should.eql 0
+ _arrayCmp([1, 2, 3], [1, 2, 4]).should.below 0
+ _arrayCmp([1], [1, 2]).should.below 0
+ _arrayCmp([2, 1], [1, 2]).should.above 0
+ _arrayCmp([2, 0, 0], [2, 3]).should.below 0
+ _arrayCmp([], [1]).should.below 0
+ _arrayCmp([1], []).should.above 0
+ _arrayCmp([], []).should.eql 0
+
test '.getCloseMatches', ->
getCloseMatches('appel', ['ape', 'apple', 'peach', 'puppy'])
.should.eql ['apple', 'ape']