Просмотр исходного кода

orderBy: change reverse argument check

Evan You 10 лет назад
Родитель
Сommit
ebb99df5d7
2 измененных файлов с 4 добавлено и 14 удалено
  1. 1 8
      src/filters/array-filters.js
  2. 3 6
      test/unit/specs/filters/filters_spec.js

+ 1 - 8
src/filters/array-filters.js

@@ -62,14 +62,7 @@ exports.orderBy = function (arr, sortKey, reverse) {
   if (!sortKey) {
     return arr
   }
-  var order = 1
-  if (arguments.length > 2) {
-    if (reverse === '-1') {
-      order = -1
-    } else {
-      order = reverse ? -1 : 1
-    }
-  }
+  var order = (reverse && reverse < 0) ? -1 : 1
   // sort on a copy to avoid mutating original array
   return arr.slice().sort(function (a, b) {
     if (sortKey !== '$key') {

+ 3 - 6
test/unit/specs/filters/filters_spec.js

@@ -175,13 +175,10 @@ describe('Filters', function () {
     res = filter(arr, 'a.b')
     assertArray(res, [arr[0], arr[2], arr[1]])
     // reverse key
-    res = filter(arr, 'a.b', true)
+    res = filter(arr, 'a.b', -1)
     assertArray(res, [arr[1], arr[2], arr[0]])
-    // literal args
-    res = filter(arr, 'c', '-1')
-    assertArray(res, [arr[1], arr[0], arr[2]])
-    // negate reverse
-    res = filter(arr, 'c', false)
+    // literal asc
+    res = filter(arr, 'c', 1)
     assertArray(res, [arr[2], arr[0], arr[1]])
     // no sort key
     res = filter(arr, null)