Skip to content

Commit

Permalink
Fixes from recent merge "conflicts". Fix "sorted" so it works in old IE.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed Apr 15, 2017
1 parent 068a041 commit 187788a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
7 changes: 7 additions & 0 deletions spec/observableArrayBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ describe('Observable Array', function() {
expect(newArray).toEqual([ 1, 3, 5, 7 ]);
expect(newArray).not.toBe(testObservableArray());

var newArray2 = testObservableArray.sorted(function(a b) {
return b - a;
});
expect(newArray2).toEqual([ 7, 5, 3, 1 ]);
expect(newArray2).not.toBe(testObservableArray());
expect(newArray2).not.toBe(newArray);

expect(notifiedValues).toEqual([]);
});

Expand Down
8 changes: 4 additions & 4 deletions spec/observableArrayChangeTrackingBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ describe('Observable Array change tracking', function() {
// return values from getChanges are the same array instances
expect(callLog.length).toBe(1);
expect(changelist1).toEqual([
{ status: 'deleted', value: 'hello', index: 0 },
{ status: 'added', value: 1, index: 0 }
{ status: 'added', value: 1, index: 0 },
{ status: 'deleted', value: 'hello', index: 0 }
]);

// Objects get converted to Array
myArray({a: 1});
expect(callLog.length).toBe(2);
expect(changelist1).toEqual([
{ status: 'deleted', value: 1, index: 0 },
{ status: 'added', value: {a: 1}, index: 0 }
{ status: 'added', value: {a: 1}, index: 0 },
{ status: 'deleted', value: 1, index: 0 }
]);
});
});
Expand Down
18 changes: 9 additions & 9 deletions spec/utilsBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,36 +126,36 @@ describe('arrayFirst', function () {
expect(result).toBe("b");
});

it('Should return null with empty arrays, and not call the predicate', function () {
it('Should return undefined with empty arrays, and not call the predicate', function () {
var predicate = jasmine.createSpy('predicate');

var result = ko.utils.arrayFirst([], predicate);

expect(result).toBe(null);
expect(result).toBe(undefined);
expect(predicate).not.toHaveBeenCalled();
});

it('Should test the predicate on every element before the first matching element', function () {
ko.utils.arrayFirst(["a", "b", "c"], matchB);

expect(matchB.calls.length).toBe(2);
expect(matchB.calls[0].args).toEqual(["a", 0]);
expect(matchB.calls[1].args).toEqual(["b", 1]);
expect(matchB.calls[0].args).toEqual(["a", 0, ["a", "b", "c"]]);
expect(matchB.calls[1].args).toEqual(["b", 1, ["a", "b", "c"]]);
});

it('Should return null if no element matches', function () {
it('Should return undefined if no element matches', function () {
var result = ko.utils.arrayFirst(["a", "b", "c"], matchD);

expect(result).toBe(null);
expect(result).toBe(undefined);
});

it('Should test every element if no element matches', function () {
ko.utils.arrayFirst(["a", "b", "c"], matchD);

expect(matchD.calls.length).toBe(3);
expect(matchD.calls[0].args).toEqual(["a", 0]);
expect(matchD.calls[1].args).toEqual(["b", 1]);
expect(matchD.calls[2].args).toEqual(["c", 2]);
expect(matchD.calls[0].args).toEqual(["a", 0, ["a", "b", "c"]]);
expect(matchD.calls[1].args).toEqual(["b", 1, ["a", "b", "c"]]);
expect(matchD.calls[2].args).toEqual(["c", 2, ["a", "b", "c"]]);
});
});

Expand Down
3 changes: 2 additions & 1 deletion src/subscribables/observableArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ ko.observableArray['fn'] = {
},

'sorted': function (compareFunction) {
return this().slice(0).sort(compareFunction);
var arrayCopy = this().slice(0);
return compareFunction ? arrayCopy.sort(compareFunction) : arrayCopy.sort();
},

'reversed': function () {
Expand Down
6 changes: 3 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ ko.utils = (function () {
array.forEach(action, actionOwner);
} else {
for (var i = 0, j = array.length; i < j; i++) {
action.call(actionOwner, array[i], i);
action.call(actionOwner, array[i], i, array);
}
}
},
Expand All @@ -126,9 +126,9 @@ ko.utils = (function () {
return array.find(predicate, predicateOwner);
}
for (var i = 0, j = array.length; i < j; i++)
if (predicate.call(predicateOwner, array[i], i))
if (predicate.call(predicateOwner, array[i], i, array))
return array[i];
return null;
return undefined;
},

arrayRemoveItem: function (array, itemToRemove) {
Expand Down

0 comments on commit 187788a

Please sign in to comment.