es6-array-extras
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "es6-array-extras", | ||
"description": "Correct implementations for Array.of and Array.from", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"homepage": "https://github.com/rwldrn/es6-array-extras", | ||
@@ -6,0 +6,0 @@ "author": { |
110
README.md
@@ -1,4 +0,4 @@ | ||
# es6-array-extras | ||
# ES6 Array Extras | ||
Correct implementations for Array.of and Array.from | ||
Correct implementations for Array.of and Array.from. [Original proposal](https://gist.github.com/1074126) | ||
@@ -8,13 +8,103 @@ ## Getting Started | ||
```javascript | ||
var es6_array_extras = require('es6-array-extras'); | ||
es6_array_extras.awesome(); // "awesome" | ||
## Documentation | ||
### Array.of( ...items ) | ||
[ES6 Spec (15.4.3.3)](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-15.4.3.3) | ||
Array.of provides a constructor that, unlike Array, does not have the special case for `new Array(42)`, which presets length (and hints to implementations to preallocate) but leaves holes in [0, length ). | ||
One of the main goals of ES6 is to become a better language for library writers and code generators. | ||
For compilation targets, ES/JS can't assume that implementations will always know what its factories are expected to construct: | ||
Imagine the following piece of code is used in a VM (think Dart->JS, LLJS->JS) | ||
```js | ||
var o = (function( construct, ...rest ) { | ||
return new construct( rest ); | ||
})( factory [, variable arity args] ); | ||
``` | ||
## Documentation | ||
_(Coming soon)_ | ||
If factory is Array and only one numeric arg is given, inline like this: | ||
## Examples | ||
_(Coming soon)_ | ||
```js | ||
var o = (function( construct, ...rest ) { | ||
return new construct( rest ); | ||
})( Array, 10 ); | ||
``` | ||
The result of `o` will be an array with 10 empty indexes, as if it were called like: | ||
```js | ||
new Array(10); | ||
``` | ||
If you replace that by using `Array.of()`, you avoid this "gotcha". | ||
```js | ||
Array.of( 1 ); | ||
// [ 1 ] | ||
Array.of( 1, 2, 3, 4 ); | ||
// [ 1, 2, 3, 4 ] | ||
``` | ||
### Array.from( arrayLike ) | ||
[ES6 Spec (15.4.3.4)](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-15.4.3.4) | ||
Converts a single argument that is an array-like object or list (eg. arguments, NodeList, DOMTokenList (used by classList), NamedNodeMap (used by attributes property)) into a new Array() and returns it; | ||
``` javascript | ||
var divs = document.querySelectorAll("div"); | ||
Array.from( divs ); | ||
// [ <div class="some classes" data-info="12"></div>, <div data-info="10"></div> ] | ||
Array.from( divs ).forEach(function( node ) { | ||
console.log( node ); | ||
}); | ||
// <div class="some classes" data-info="12"></div> | ||
// <div data-info="10"></div> | ||
Array.from( divs ).filter(function( node ) { | ||
return !!node.classList.length; | ||
}); | ||
// [ <div class="some classes" data-info="12"></div> ] | ||
Array.from( divs ).reduce(function( prev, current ) { | ||
return ( +prev.dataset.info ) + ( +current.dataset.info ); | ||
}); | ||
// 22 | ||
Array.from( divs[0].classList ) | ||
// ["some", "classes"] | ||
// Now shorter then [].foo.call :) | ||
var a = Array; | ||
a.from( divs ); | ||
// [ <div class="some classes" data-info="12"></div>, <div data-info="10"></div> ] | ||
``` | ||
## Contributing | ||
@@ -27,3 +117,3 @@ In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt](https://github.com/cowboy/grunt). | ||
## License | ||
Copyright (c) 2012 Rick Waldron | ||
Copyright (c) 2012 Rick Waldron | ||
Licensed under the MIT license. |
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
11887
118