prototypes
Advanced tools
Comparing version 0.2.7 to 0.3.0
@@ -195,4 +195,8 @@ 'use strict'; | ||
*/ | ||
newObject.filter = function(callback) | ||
newObject.filterIn = function(callback) | ||
{ | ||
if (this.isArray()) | ||
{ | ||
return this.filter(callback); | ||
} | ||
var result = {}; | ||
@@ -210,2 +214,13 @@ for (var key in this) | ||
/** | ||
* Return a new object with just the properties that *do not* pass the callback. | ||
*/ | ||
newObject.filterOut = function(callback) | ||
{ | ||
return this.filterIn(function(element) | ||
{ | ||
return (!callback(element)); | ||
}); | ||
}; | ||
function testFilter(callback) | ||
@@ -218,3 +233,3 @@ { | ||
}; | ||
var filtered = object.filter(function(value) | ||
var filtered = object.filterIn(function(value) | ||
{ | ||
@@ -226,3 +241,3 @@ return value < 3; | ||
testing.assertEquals(filtered.b, 2, 'Invalid b in filtered', callback); | ||
filtered = object.filter(function(value) | ||
filtered = object.filterIn(function(value) | ||
{ | ||
@@ -233,2 +248,8 @@ return value > 2; | ||
testing.assertEquals(filtered.c, 3, 'Invalid c in filtered', callback); | ||
filtered = object.filterOut(function(value) | ||
{ | ||
return value > 2; | ||
}); | ||
testing.assertEquals(filtered.countProperties(), 2, 'Invalid !>3 count', callback); | ||
testing.assertEquals(filtered.a, 1, 'Invalid c in filtered out', callback); | ||
testing.success(callback); | ||
@@ -235,0 +256,0 @@ } |
{ | ||
"name": "prototypes", | ||
"version": "0.2.7", | ||
"version": "0.3.0", | ||
"description": "Some common prototypes for node.js: string.startsWith(), object.countProperties() and more. Facilities for functional programming with objects: object.forEach(), object.filter(). Functions are added safely using Object.defineProperty().", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/alexfernandez/prototypes", |
@@ -10,3 +10,3 @@ [![Build Status](https://secure.travis-ci.org/alexfernandez/prototypes.png)](http://travis-ci.org/alexfernandez/prototypes) | ||
Includes nice facilities for functional programming with objects: | ||
`object.forEach()`, `object.filter()` and so on. | ||
`object.forEach()`, `object.filterIn()` and so on. | ||
@@ -188,12 +188,13 @@ ## Installation | ||
### object.filter(callback) | ||
### object.filterIn(callback) | ||
Return a new object that only includes those properties of the object | ||
that return true for the given callback, i.e.: | ||
that return `true` for the given callback, i.e.: | ||
`callback(value) == true`. | ||
Does not modify the original object. | ||
For an array it is equivalent to `array.filter()`. | ||
Example: | ||
``` | ||
{a: 1, b: 2}.forEach(function(value) | ||
{a: 1, b: 2}.filterIn(function(value) | ||
{ | ||
@@ -205,2 +206,19 @@ return value > 1; | ||
### object.filterOut(callback) | ||
Return a new object that only includes those properties of the object | ||
that return `false` for the given callback, i.e.: | ||
`callback(value) != true`. | ||
Does not modify the original object. | ||
For an array it is equivalent to `array.filterOut()` added above. | ||
Example: | ||
``` | ||
{a: 1, b: 2}.filterOut(function(value) | ||
{ | ||
return value > 1; | ||
}); | ||
\=> {a: 1} | ||
``` | ||
### object.isArray() | ||
@@ -207,0 +225,0 @@ |
33998
913
361