prototypes
Advanced tools
Comparing version 0.3.4 to 0.4.0
@@ -115,1 +115,23 @@ 'use strict'; | ||
/** | ||
* Return an array with the property values. | ||
* If already an array, returns the unmodified array. | ||
*/ | ||
Array.toArray = function(object) | ||
{ | ||
if (!object) | ||
{ | ||
return null; | ||
} | ||
if (Array.isArray()) | ||
{ | ||
return object; | ||
} | ||
var result = []; | ||
object.forEach(function(value) | ||
{ | ||
result.push(value); | ||
}); | ||
return result; | ||
}; | ||
@@ -102,3 +102,3 @@ 'use strict'; | ||
{ | ||
if (this.isArray()) | ||
if (Array.isArray(this)) | ||
{ | ||
@@ -130,30 +130,4 @@ return this.filter(callback); | ||
/** | ||
* Find out if the object is an array. | ||
*/ | ||
newObject.isArray = function() | ||
{ | ||
return Array.isArray(this); | ||
}; | ||
/** | ||
* Return an array with the property values. | ||
* If already an array, returns the unmodified array. | ||
*/ | ||
newObject.toArray = function() | ||
{ | ||
if (this.isArray()) | ||
{ | ||
return this; | ||
} | ||
var result = []; | ||
this.forEach(function(value) | ||
{ | ||
result.push(value); | ||
}); | ||
return result; | ||
}; | ||
// add new object functions as properties | ||
core.addProperties(Object.prototype, newObject); | ||
{ | ||
"name": "prototypes", | ||
"version": "0.3.4", | ||
"version": "0.4.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", |
[![Build Status](https://secure.travis-ci.org/alexfernandez/prototypes.png)](http://travis-ci.org/alexfernandez/prototypes) | ||
[![Package quality](http://packagequality.com/badge/prototypes.png)](http://packagequality.com/#?package=prototypes) | ||
# prototypes | ||
@@ -238,25 +240,2 @@ | ||
### object.isArray() | ||
Return `true` if the object is an array, `false` otherwise. | ||
Examples: | ||
``` | ||
[].isArray(); | ||
\=> true | ||
{}.isArray(); | ||
\=> false | ||
``` | ||
### object.toArray() | ||
Return an array with the object property values. If already an array, | ||
returns the unmodified array. | ||
Example: | ||
``` | ||
{a: 1, b: 2}.toArray(); | ||
\=> [1, 2] | ||
``` | ||
## Array Prototypes | ||
@@ -334,2 +313,19 @@ | ||
### Array.toArray(object) | ||
Return an array with the object property values. If already an array, | ||
returns the unmodified array. | ||
Example: | ||
``` | ||
Array.toArray({a: 1, b: 2}); | ||
\=> [1, 2] | ||
``` | ||
*Note*: this function resides in the `Array` global like `Array.isArray()`, | ||
instead of in individual arrays as the previous functions. | ||
In versions up to 0.3.4 there were functions `object.toArray()` and `object.isArray()`, | ||
but they were removed due to | ||
[incompatibilities](https://github.com/alexfernandez/prototypes/issues/8) with lodash. | ||
## Math-related Functions | ||
@@ -336,0 +332,0 @@ |
@@ -89,2 +89,17 @@ 'use strict'; | ||
function testToArray(callback) | ||
{ | ||
var object = { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}; | ||
var converted = Array.toArray(object); | ||
var array = [1, 2, 3]; | ||
testing.assertEquals(converted, array, 'Invalid converted array', callback); | ||
converted = Array.toArray(array); | ||
testing.assertEquals(converted, array, 'Invalid reconverted array', callback); | ||
testing.success(callback); | ||
} | ||
/** | ||
@@ -102,2 +117,3 @@ * Run package tests. | ||
testDifference, | ||
testToArray, | ||
]; | ||
@@ -104,0 +120,0 @@ testing.run(tests, callback); |
@@ -136,17 +136,2 @@ 'use strict'; | ||
function testToArray(callback) | ||
{ | ||
var object = { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}; | ||
var converted = object.toArray(); | ||
var array = [1, 2, 3]; | ||
testing.assertEquals(converted, array, 'Invalid converted array', callback); | ||
converted = array.toArray(); | ||
testing.assertEquals(converted, array, 'Invalid reconverted array', callback); | ||
testing.success(callback); | ||
} | ||
/** | ||
@@ -163,3 +148,2 @@ * Run package tests. | ||
testForEach, | ||
testToArray, | ||
]; | ||
@@ -166,0 +150,0 @@ testing.run(tests, callback); |
38772
1039
408