prototypes
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -117,5 +117,46 @@ 'use strict'; | ||
{ | ||
return [].concat.apply([], this); | ||
var result = []; | ||
for (var i = 0; i < this.length; i++) | ||
{ | ||
var element = this[i]; | ||
if (Array.isArray(element)) | ||
{ | ||
for (var j = 0; j < element.length; j++) | ||
{ | ||
result.push(element[j]); | ||
} | ||
} | ||
else | ||
{ | ||
result.push(element); | ||
} | ||
} | ||
return result; | ||
}; | ||
/** | ||
* Returns the array flattened all the way. | ||
*/ | ||
newArray.flatten = function() | ||
{ | ||
var result = []; | ||
for (var i = 0; i < this.length; i++) | ||
{ | ||
var element = this[i]; | ||
if (Array.isArray(element)) | ||
{ | ||
var flattened = element.flatten(); | ||
for (var j = 0; j < flattened.length; j++) | ||
{ | ||
result.push(flattened[j]); | ||
} | ||
} | ||
else | ||
{ | ||
result.push(element); | ||
} | ||
} | ||
return result; | ||
}; | ||
// add new object functions as properties | ||
@@ -122,0 +163,0 @@ core.addProperties(Array.prototype, newArray); |
{ | ||
"name": "prototypes", | ||
"version": "1.0.0", | ||
"version": "1.1.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", |
@@ -349,2 +349,11 @@ [![Build Status](https://secure.travis-ci.org/alexfernandez/prototypes.png)](http://travis-ci.org/alexfernandez/prototypes) | ||
### array.flatten() | ||
Flattens all levels of nested arrays. Example: | ||
``` | ||
[1, 2, [3, 4, [5, 6]]].concatAll(); | ||
\=> [1, 2, 3, 4, 5, 6] | ||
``` | ||
### Array.toArray(object) | ||
@@ -351,0 +360,0 @@ |
@@ -89,2 +89,16 @@ 'use strict'; | ||
function testConcatAll(callback) | ||
{ | ||
var result = [1, 2, [3, 4, [5, 6]]].concatAll(); | ||
testing.assertEquals(result.length, 5, 'Should only flatten one level', callback); | ||
testing.success(callback); | ||
} | ||
function testFlatten(callback) | ||
{ | ||
var result = [1, 2, [3, 4, [5, 6]]].flatten(); | ||
testing.assertEquals(result.length, 6, 'Should flatten all the way', callback); | ||
testing.success(callback); | ||
} | ||
function testToArray(callback) | ||
@@ -104,9 +118,2 @@ { | ||
function testConcatAll(callback) | ||
{ | ||
var result = [1, 2, [3, 4, [5, 6]]].concatAll(); | ||
testing.assertEquals(result.length, 5, 'Should only flatten one level', callback); | ||
testing.success(callback); | ||
} | ||
/** | ||
@@ -124,4 +131,5 @@ * Run package tests. | ||
testDifference, | ||
testConcatAll, | ||
testFlatten, | ||
testToArray, | ||
testConcatAll, | ||
]; | ||
@@ -128,0 +136,0 @@ testing.run(tests, callback); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44079
1177
454