Skip to content

Commit

Permalink
When it walks like a duck and quacks like a duck, it must be a duck.
Browse files Browse the repository at this point in the history
Update the array utilities to use the 'native' methods if they are available.
  • Loading branch information
Raul E Rangel authored and Raul E Rangel committed Jul 7, 2014
1 parent 7ae7194 commit 6544469
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
6 changes: 3 additions & 3 deletions spec/utilsBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ describe('arrayForEach', function () {
ko.utils.arrayForEach(["a", "b", "c"], callback);

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

it('Should do nothing with empty arrays', function () {
Expand Down
28 changes: 21 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,27 @@ ko.utils = (function () {
fieldsIncludedWithJsonPost: ['authenticity_token', /^__RequestVerificationToken(_.*)?$/],

arrayForEach: function (array, action) {
if (array && typeof array.forEach == 'function') {
return array.forEach(action);
}
for (var i = 0, j = array.length; i < j; i++)
action(array[i], i);
action(array[i], i, array);
},

arrayIndexOf: function (array, item) {
if (typeof Array.prototype.indexOf == "function")
return Array.prototype.indexOf.call(array, item);
if (array && typeof array.indexOf == 'function') {
return array.indexOf(item);
}
for (var i = 0, j = array.length; i < j; i++)
if (array[i] === item)
return i;
return -1;
},

arrayFirst: function (array, predicate, predicateOwner) {
if (array && typeof array.find == 'function') {
return array.find(predicate, predicateOwner);
}
for (var i = 0, j = array.length; i < j; i++)
if (predicate.call(predicateOwner, array[i], i))
return array[i];
Expand All @@ -99,14 +106,18 @@ ko.utils = (function () {
arrayGetDistinctValues: function (array) {
array = array || [];
var result = [];
for (var i = 0, j = array.length; i < j; i++) {
if (ko.utils.arrayIndexOf(result, array[i]) < 0)
result.push(array[i]);
}
ko.utils.arrayForEach(array, function(item) {
if (ko.utils.arrayIndexOf(result, item) < 0)
result.push(item);
});

return result;
},

arrayMap: function (array, mapping) {
if (array && typeof array.map == 'function') {
return array.map(mapping);
}
array = array || [];
var result = [];
for (var i = 0, j = array.length; i < j; i++)
Expand All @@ -115,6 +126,9 @@ ko.utils = (function () {
},

arrayFilter: function (array, predicate) {
if (array && typeof array.filter == 'function') {
return array.filter(predicate);
}
array = array || [];
var result = [];
for (var i = 0, j = array.length; i < j; i++)
Expand Down

0 comments on commit 6544469

Please sign in to comment.