Skip to content

Commit

Permalink
gallery-2012.12.12-21-11 solmsted gallery-array-unnest
Browse files Browse the repository at this point in the history
  • Loading branch information
davglass committed Dec 12, 2012
1 parent 17931c0 commit 2c4b0ca
Show file tree
Hide file tree
Showing 19 changed files with 641 additions and 49 deletions.
6 changes: 6 additions & 0 deletions build/gallery-array-unnest/gallery-array-unnest-coverage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 15 additions & 17 deletions build/gallery-array-unnest/gallery-array-unnest-debug.js
@@ -1,38 +1,36 @@
YUI.add('gallery-array-unnest', function(Y) {
YUI.add('gallery-array-unnest', function (Y, NAME) {

/**
* Utility function that flattens nested arrays.
* @module gallery-array-unnest
*/
(function (Y) {
'use strict';

/**
* Utility function that flattens nested arrays.
* The original array is not modified. The returned array items are a
* shallow copy of the original array items.
* Utility function that flattens nested arrays. The original array is not
* modified. The returned array items are a shallow copy of the original
* array items.
* @for Array
* @method unnest
* @param {Array} array
* @param {Number} levels Optional. If defined, must be a non-negative
* integer. Defaults to 1.
* @returns {Array}
* @param {Number} [levels=1] If defined, must be a non-negative integer.
* @return {Array}
* @static
*/
var unnest = function (array, levels) {
var _unnest = function (array, levels) {
var empty = [];

array = empty.concat.apply(empty, array);

if (levels && levels - 1) {
return unnest(array, levels - 1);
return _unnest(array, levels - 1);
}

return array;
};

Y.Array.unnest = unnest;
}(Y));

Y.Array.unnest = _unnest;
}(Y));

}, 'gallery-2012.06.20-20-07' ,{requires:['yui'], skinnable:false});
}, 'gallery-2012.12.12-21-11', {"requires": ["yui-base"]});
2 changes: 1 addition & 1 deletion build/gallery-array-unnest/gallery-array-unnest-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 15 additions & 17 deletions build/gallery-array-unnest/gallery-array-unnest.js
@@ -1,38 +1,36 @@
YUI.add('gallery-array-unnest', function(Y) {
YUI.add('gallery-array-unnest', function (Y, NAME) {

/**
* Utility function that flattens nested arrays.
* @module gallery-array-unnest
*/
(function (Y) {
'use strict';

/**
* Utility function that flattens nested arrays.
* The original array is not modified. The returned array items are a
* shallow copy of the original array items.
* Utility function that flattens nested arrays. The original array is not
* modified. The returned array items are a shallow copy of the original
* array items.
* @for Array
* @method unnest
* @param {Array} array
* @param {Number} levels Optional. If defined, must be a non-negative
* integer. Defaults to 1.
* @returns {Array}
* @param {Number} [levels=1] If defined, must be a non-negative integer.
* @return {Array}
* @static
*/
var unnest = function (array, levels) {
var _unnest = function (array, levels) {
var empty = [];

array = empty.concat.apply(empty, array);

if (levels && levels - 1) {
return unnest(array, levels - 1);
return _unnest(array, levels - 1);
}

return array;
};

Y.Array.unnest = unnest;
}(Y));

Y.Array.unnest = _unnest;
}(Y));

}, 'gallery-2012.06.20-20-07' ,{requires:['yui'], skinnable:false});
}, 'gallery-2012.12.12-21-11', {"requires": ["yui-base"]});
Empty file.
10 changes: 10 additions & 0 deletions src/gallery-array-unnest/build.json
@@ -0,0 +1,10 @@
{
"builds": {
"gallery-array-unnest": {
"jsfiles": [
"array-unnest.js"
]
}
},
"name": "gallery-array-unnest"
}
Empty file.
20 changes: 20 additions & 0 deletions src/gallery-array-unnest/docs/component.json
@@ -0,0 +1,20 @@
{
"author": [
"solmsted"
],
"description": "Utility function that flattens nested arrays.",
"displayName": "Gallery: Array Unnest",
"name": "gallery-array-unnest",
"tags": [
"array",
"flatten",
"gallery",
"nest",
"nested",
"solmsted",
"unnest"
],
"use": [
"gallery-array-unnest"
]
}
51 changes: 51 additions & 0 deletions src/gallery-array-unnest/docs/index.mustache
@@ -0,0 +1,51 @@
<div class="intro">
<p>
Utility function that flattens nested arrays.
</p>
</div>

{{>getting-started}}

<h2>
Description
</h2>

<p>
`Y.Array.unnest` is a utility function that flattens nested arrays. The nesting depth can be specified. For example:
</p>

```
var array = [1, 2, [3, 4, 5], [6, [7, [8, 9]]]];

Y.Array.unnest(array, 3); // returns [1,2,3,4,5,6,7,8,9]
Y.Array.unnest(array, 2); // returns [1,2,3,4,5,6,7,[8,9]]
Y.Array.unnest(array, 1); // returns [1,2,3,4,5,6,[7,[8,9]]]
```

<p>
The original array is not modified. The returned array items are a shallow copy of the original array items.
</p>

<h2>
What about `Y.Array.flatten`?
</h2>

<p>
`Y.Array.flatten` does basically the same thing, is already part of `array-extras`, it can flatten to unknown depths, and it runs faster in the most common use cases. Please use `Y.Array.flatten`
instead of `Y.Array.unnest`. With that being said there are still some semi-valid reasons to use `Y.Array.unnest`.
</p>

<ul>
<li>
Use `Y.Array.unnest` if you have to stop flattening at a known depth.
</li>
<li>
`Y.Array.unnest` performs better than `Y.Array.flatten` on really really big arrays.
</li>
<li>
`Y.Array.unnest` performs better than `Y.Array.flatten` in some older browsers.
</li>
<li>
Use `Y.Array.unnest` if you enjoy being alternative.
</li>
</ul>
27 changes: 13 additions & 14 deletions src/gallery-array-unnest/js/array-unnest.js
Expand Up @@ -4,30 +4,29 @@
*/
(function (Y) {
'use strict';

/**
* Utility function that flattens nested arrays.
* The original array is not modified. The returned array items are a
* shallow copy of the original array items.
* Utility function that flattens nested arrays. The original array is not
* modified. The returned array items are a shallow copy of the original
* array items.
* @for Array
* @method unnest
* @param {Array} array
* @param {Number} levels Optional. If defined, must be a non-negative
* integer. Defaults to 1.
* @returns {Array}
* @param {Number} [levels=1] If defined, must be a non-negative integer.
* @return {Array}
* @static
*/
var unnest = function (array, levels) {
var _unnest = function (array, levels) {
var empty = [];

array = empty.concat.apply(empty, array);

if (levels && levels - 1) {
return unnest(array, levels - 1);
return _unnest(array, levels - 1);
}

return array;
};
Y.Array.unnest = unnest;

Y.Array.unnest = _unnest;
}(Y));
Empty file.
19 changes: 19 additions & 0 deletions src/gallery-array-unnest/logs/shifter.stdout.log
@@ -0,0 +1,19 @@
shifter [info] revving up
shifter [info] looking for build.json file
shifter [info] found build.json file, shifting
shifter [info] putting the hammer down, let's build this thing!
shifter [info] munging in loader meta data into build.json
shifter [info] putting the hammer down
shifter [info] shifting into gear for gallery-array-unnest
shifter [info] deleting build dir: /build/gallery-array-unnest
shifter [queu] writing RAW file
shifter [queu] compressing gallery-array-unnest/gallery-array-unnest.js with UglifyJS
shifter [queu] writing -min file
shifter [info] shifting for coverage
shifter [info] shifting assets for gallery-array-unnest
shifter [queu] coverage file read, starting coverage for: gallery-array-unnest/gallery-array-unnest.js
shifter [info] istanbul providing coverage
shifter [queu] instrumenting with istanbul
shifter [queu] writing coverage file to gallery-array-unnest/gallery-array-unnest-coverage.js
shifter [info] done racing, the gears are toast
shifter [info] finished in 0.039 seconds, pretty fast huh?

0 comments on commit 2c4b0ca

Please sign in to comment.