prototypes
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -18,5 +18,4 @@ 'use strict'; | ||
/** | ||
* Count the number of properties in an object. Params: | ||
* - filter: can be a string that has to be contained in every key, | ||
* or a function which will be used as a filter to pass keys. | ||
* Returns true if the array contains the given element. Params: | ||
* - element: the element to check. | ||
*/ | ||
@@ -32,5 +31,2 @@ newArray.contains = function(element) | ||
/** | ||
* Test array.contains(). | ||
*/ | ||
function testContains(callback) | ||
@@ -45,2 +41,67 @@ { | ||
/** | ||
* Remove an element from an array and return it if present; | ||
* otherwise returns null. Params: | ||
* - element: the element to remove. | ||
*/ | ||
newArray.remove = function(element) | ||
{ | ||
if (!element) | ||
{ | ||
return undefined; | ||
} | ||
var index = this.indexOf(element); | ||
if (index == -1) | ||
{ | ||
return undefined; | ||
} | ||
var removed = this.splice(index, 1); | ||
return removed[0]; | ||
}; | ||
function testRemove(callback) | ||
{ | ||
var array = ['a', 'b']; | ||
testing.assertEquals(array.remove('c'), undefined, 'Should not remove missing element', callback); | ||
testing.assertEquals(array.remove('b'), 'b', 'Should remove 2nd element', callback); | ||
testing.assertEquals(array, ['a'], 'Should not contain 2nd element', callback); | ||
testing.assertEquals(array.remove('a'), 'a', 'Should remove 1st element', callback); | ||
testing.assertEquals(array, [], 'Should not contain 1st element', callback); | ||
testing.success(callback); | ||
} | ||
/** | ||
* Return a new array with only those elements that do not match the checker. Params: | ||
* - checker: a function that checks all elements, and only lets through | ||
* those for which the checker does _not_ return true. | ||
*/ | ||
newArray.filterOut = function(checker) | ||
{ | ||
var result = []; | ||
this.forEach(function(element) | ||
{ | ||
if (!checker(element)) | ||
{ | ||
result.push(element); | ||
} | ||
}); | ||
return result; | ||
}; | ||
function testFilterOut(callback) | ||
{ | ||
var array = ['a', 'b', 'c', 'd1', 'd2']; | ||
var stringified = JSON.stringify(array); | ||
var removed = array.filterOut(function(element) | ||
{ | ||
return element.startsWith('d'); | ||
}); | ||
testing.assertEquals(array.length, 5, 'Original array shortened', callback); | ||
testing.assertEquals(JSON.stringify(array), stringified, 'Original array modified', callback); | ||
testing.assertEquals(removed.length, 3, 'Elements not removed', callback); | ||
testing.assert(!removed.contains('d1'), 'Wrong first element not filtered', callback); | ||
testing.assert(!removed.contains('d2'), 'Wrong second element not filtered', callback); | ||
testing.success(callback); | ||
} | ||
// add new object functions as properties | ||
@@ -56,2 +117,4 @@ core.addProperties(Object.prototype, newArray); | ||
testContains, | ||
testRemove, | ||
testFilterOut, | ||
]; | ||
@@ -58,0 +121,0 @@ testing.run(tests, callback); |
{ | ||
"name": "prototypes", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Some common prototypes for node.js: string.startsWith(), object.countProperties() and more. Functions are added using Object.defineProperty() to avoid polluting new objects.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/alexfernandez/prototypes", |
@@ -178,2 +178,29 @@ [![Build Status](https://secure.travis-ci.org/alexfernandez/prototypes.png)](http://travis-ci.org/alexfernandez/prototypes) | ||
### array.remove(element) | ||
Remove the element from the array if present, and return it. | ||
If not present, returns null. Example: | ||
``` | ||
var array = ['a', 'b']; | ||
array.remove('a'); | ||
\=> 'a' | ||
array | ||
\=> ['b'] | ||
``` | ||
### array.filterOut(checker) | ||
Similar to `array.filter()` but reversed: returns an array | ||
whose members do _not_ return `true` with the checker. | ||
Example: | ||
``` | ||
['a', 'b', 'c1', 'c2'].filterOut(function(element) | ||
{ | ||
return element.startsWith('c'); | ||
}); | ||
\=> true | ||
``` | ||
## Math-related Functions | ||
@@ -180,0 +207,0 @@ |
28531
753
277