Skip to content

Commit e5a15b2

Browse files
Jason Jarrettporada
authored andcommitted
Handle undefined collections
Resolves #2.
1 parent ca4c0ff commit e5a15b2

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

angular-semver-sort.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,8 +1046,12 @@ if (typeof define === 'function' && define.amd)
10461046
;angular.module('semverSort', []).factory('semverSortArrayHelpers', function() {
10471047
'use strict';
10481048

1049+
var clone = function(collection) {
1050+
return (collection || []).slice(0);
1051+
};
1052+
10491053
var sortAlphabetically = function(collection, prop) {
1050-
var items = collection.slice(0);
1054+
var items = clone(collection);
10511055
return !prop ? items.sort() : items.sort(function(A, B) {
10521056
var a = A[prop], b = B[prop];
10531057
if ( a > b ) return 1;
@@ -1057,10 +1061,11 @@ if (typeof define === 'function' && define.amd)
10571061
};
10581062

10591063
var reverse = function(collection) {
1060-
return collection.slice(0).reverse();
1064+
return clone(collection).reverse();
10611065
};
10621066

10631067
return {
1068+
clone: clone,
10641069
sortAlphabetically: sortAlphabetically,
10651070
reverse: reverse
10661071
};
@@ -1080,12 +1085,14 @@ if (typeof define === 'function' && define.amd)
10801085
return _.sortAlphabetically;
10811086
}
10821087

1088+
var isItemValid = function(item) {
1089+
return semver.valid(item, true);
1090+
};
1091+
10831092
var areItemsValid = function(collection, prop) {
1084-
var items = !prop ? collection : collection.map(function(item) {
1085-
return item[prop];
1086-
});
1087-
return items.every(function(item) {
1088-
return semver.valid(item, true);
1093+
if ( !collection ) return false;
1094+
return !prop ? collection.every(isItemValid) : collection.every(function(item) {
1095+
return isItemValid(item[prop]);
10891096
});
10901097
};
10911098

@@ -1095,7 +1102,7 @@ if (typeof define === 'function' && define.amd)
10951102
return _.sortAlphabetically(collection, prop);
10961103
}
10971104

1098-
var items = collection.slice(0);
1105+
var items = _.clone(collection);
10991106
return !prop ? semver.sort(items, true) : items.sort(function(A, B) {
11001107
return semver.compare(A[prop], B[prop], true);
11011108
});

src/angular-semver-sort.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
;angular.module('semverSort', []).factory('semverSortArrayHelpers', function() {
22
'use strict';
33

4+
var clone = function(collection) {
5+
return (collection || []).slice(0);
6+
};
7+
48
var sortAlphabetically = function(collection, prop) {
5-
var items = collection.slice(0);
9+
var items = clone(collection);
610
return !prop ? items.sort() : items.sort(function(A, B) {
711
var a = A[prop], b = B[prop];
812
if ( a > b ) return 1;
@@ -12,10 +16,11 @@
1216
};
1317

1418
var reverse = function(collection) {
15-
return collection.slice(0).reverse();
19+
return clone(collection).reverse();
1620
};
1721

1822
return {
23+
clone: clone,
1924
sortAlphabetically: sortAlphabetically,
2025
reverse: reverse
2126
};
@@ -35,12 +40,14 @@
3540
return _.sortAlphabetically;
3641
}
3742

43+
var isItemValid = function(item) {
44+
return semver.valid(item, true);
45+
};
46+
3847
var areItemsValid = function(collection, prop) {
39-
var items = !prop ? collection : collection.map(function(item) {
40-
return item[prop];
41-
});
42-
return items.every(function(item) {
43-
return semver.valid(item, true);
48+
if ( !collection ) return false;
49+
return !prop ? collection.every(isItemValid) : collection.every(function(item) {
50+
return isItemValid(item[prop]);
4451
});
4552
};
4653

@@ -50,7 +57,7 @@
5057
return _.sortAlphabetically(collection, prop);
5158
}
5259

53-
var items = collection.slice(0);
60+
var items = _.clone(collection);
5461
return !prop ? semver.sort(items, true) : items.sort(function(A, B) {
5562
return semver.compare(A[prop], B[prop], true);
5663
});

test/test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ describe('Filter: semverSort', function() {
1212
semverSort.should.be.a.Function;
1313
});
1414

15+
describe('given undefined', function() {
16+
it('should not throw', function() {
17+
(function() {
18+
semverSort();
19+
}).should.not.throw();
20+
});
21+
22+
it('should return an empty array', function() {
23+
semverSort().should.be.an.Array.with.lengthOf(0);
24+
});
25+
});
26+
1527
describe('given an array of semver strings', function() {
1628
it('should return the array of strings sorted by version number', function() {
1729
semverSort([
@@ -103,6 +115,18 @@ describe('Filter: semverReverseSort', function() {
103115
semverReverseSort.should.be.a.Function;
104116
});
105117

118+
describe('given undefined', function() {
119+
it('should not throw', function() {
120+
(function() {
121+
semverReverseSort();
122+
}).should.not.throw();
123+
});
124+
125+
it('should return an empty array', function() {
126+
semverReverseSort().should.be.an.Array.with.lengthOf(0);
127+
});
128+
});
129+
106130
describe('given an array of semver strings', function() {
107131
it('should return the array of strings sorted by version number in reverse order', function() {
108132
semverReverseSort([

0 commit comments

Comments
 (0)