Permalink
Browse files

Added 'without' array function

  • Loading branch information...
1 parent cab3084 commit 5add2ac5c4368c1b2c6304a85bc23357cd3c7218 @avdgaag committed Jul 7, 2011
Showing with 61 additions and 0 deletions.
  1. +24 −0 spec/mutil_spec.js
  2. +22 −0 spec/nativized_spec.js
  3. +15 −0 src/mutil.js
View
@@ -326,6 +326,30 @@ describe('Mutil', function() {
});
});
+ describe('without', function() {
+ var arr;
+
+ beforeEach(function() {
+ arr = [1, 2, 3];
+ });
+
+ it('should return input array when no arguments given', function() {
+ expect(Mutil.without(arr)).toEqual(arr);
+ });
+
+ it('should remove a single element', function() {
+ expect(Mutil.without(arr, 2)).toEqual([1,3]);
+ });
+
+ it('should remove multiple elements', function() {
+ expect(Mutil.without(arr, 2, 3)).toEqual([1]);
+ });
+
+ it('should not typecast', function() {
+ expect(Mutil.without(arr, '1')).toEqual([1,2,3]);
+ });
+ });
+
describe('map', function() {
it('should return empty array for empty input', function() {
expect(Mutil.map([], function() {})).toEqual([]);
View
@@ -94,6 +94,28 @@ describe('Mutil', function() {
obj = { 'a': 1, 'b': 2, 'c': 3 };
});
+ describe('without', function() {
+ beforeEach(function() {
+ arr = [1, 2, 3];
+ });
+
+ it('should return input array when no arguments given', function() {
+ expect(arr.without()).toEqual(arr);
+ });
+
+ it('should remove a single element', function() {
+ expect(arr.without(2)).toEqual([1,3]);
+ });
+
+ it('should remove multiple elements', function() {
+ expect(arr.without(2, 3)).toEqual([1]);
+ });
+
+ it('should not typecast', function() {
+ expect(arr.without('1')).toEqual([1,2,3]);
+ });
+ });
+
describe('forEach', function() {
it('should loop over all elements', function() {
var i = 0;
View
@@ -322,6 +322,21 @@
}
},
+ // Return a copy of the input array with all additional arguments
+ // removed from it.
+ //
+ // Example:
+ //
+ // Mutil.without([1,2,3,4], 1, 3);
+ // // => [2, 4]
+ //
+ without: function(obj) {
+ var exceptions = this.toArray(arguments).slice(1);
+ return this.filter(obj, function(el) {
+ return !exceptions.include(el);
+ });
+ },
+
// Create a new array with the values of a given attribute for every
// element in the input array -- a shortcut method for using `map`.
//

0 comments on commit 5add2ac

Please sign in to comment.