[Pkg-javascript-commits] [node-array-from] 01/04: Import Upstream version 2.1.1
Paolo Greppi
paolog-guest at moszumanska.debian.org
Fri Dec 23 10:29:56 UTC 2016
This is an automated email from the git hooks/post-receive script.
paolog-guest pushed a commit to branch master
in repository node-array-from.
commit d87b6c9db02c5361effe9ad39e382e01b5a3714e
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date: Fri Dec 23 09:59:43 2016 +0000
Import Upstream version 2.1.1
---
.editorconfig | 36 ++++++++
.gitattributes | 112 +++++++++++++++++++++++
.gitignore | 18 ++++
.jshintignore | 2 +
.jshintrc | 69 +++++++++++++++
.travis.yml | 14 +++
License.md | 7 ++
Readme.md | 90 +++++++++++++++++++
index.js | 4 +
package.json | 51 +++++++++++
polyfill.js | 212 ++++++++++++++++++++++++++++++++++++++++++++
test.js | 275 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 files changed, 890 insertions(+)
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..f136268
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,36 @@
+#
+# Text editor configuration
+# =========================
+#
+# This file describes the end-of-line character, indentation and other rules for text files – in a
+# format compatible with most text editors. It really helps to have all that consistent, without
+# manual labor. You'll probably want a plugin for your editor. Go get one from
+# [editorconfig.org](http://editorconfig.org).
+#
+
+root = true
+
+
+# General rules
+# -------------
+# - Unicode
+# - Unix-style line endings
+# - Newline before EOF
+# - No sloppy trailing spaces
+# - Two-space indentation
+
+[*]
+end_of_line = lf
+charset = utf-8
+insert_final_newline = true
+trim_trailing_whitespace = true
+indent_style = space
+indent_size = 2
+
+
+# Files that need whitespace at the end of lines
+# ----------------------------------------------
+# - Markdown
+
+[*.md]
+trim_trailing_whitespace = false
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..7c21e20
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,112 @@
+#
+# File type specific Git settings
+# ===============================
+#
+
+
+# Default
+# -------
+# If a file doesn’t match anything below, fall back to autodetection.
+# <http://davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/>.
+* text=auto
+
+
+# Text files
+# ----------
+# Always perform CRLF → LF normalization on these.
+
+# * Source code
+*.js text
+*.sh text
+*.fish text
+
+# * Docs
+*.md text
+
+# * Config and data files
+*.json text
+*.{yml,yaml} text
+.editorconfig text
+.gitattributes text
+.gitignore text
+.jscsrc text
+.jshintignore text
+.jshintrc text
+.npmignore text
+
+# * Web stuff
+*.html text
+*.svg text
+*.mustache text
+
+# * Other text files
+*.csv text
+*.xml text
+
+
+# Smart diffs
+# -----------
+# Show approximate diffs for binary files by converting them to plain text.
+
+# * Show PDF and *MS Office* documents as text
+# – if you’re not on *Git Bash*, put this into your .gitconfig:
+# [diff "astextplain"]
+# textconv = pdf2txt
+# binary = true
+*.pdf diff=astextplain
+*.doc diff=astextplain
+*.docx diff=astextplain
+*.dot diff=astextplain
+*.rtf diff=astextplain
+
+# * Show ODF documents as text
+# – install *odt2txt* and put this into your .gitconfig:
+# [diff "odf"]
+# textconv=odt2txt
+# binary = true
+.odt diff=odf
+.ods diff=odf
+.odp diff=odf
+.odb diff=odf
+.odg diff=odf
+.odf diff=odf
+
+# * Show EXIF data from JPEG images
+# – install *exif* and put this into your .gitconfig:
+# [diff "jpg"]
+# textconv = exif
+# binary = true
+*.{jpg,jpeg} diff=exif
+
+
+# Plain binary files
+# ------------------
+# Just treat them as blobs.
+
+# * Binary image formats
+*.png binary
+*.gif binary
+*.xcf binary
+*.ico binary
+*.psd binary
+*.ai binary
+*.sketch binary
+
+# * Video and audio files
+*.mov binary
+*.mp4 binary
+*.mp3 binary
+*.flv binary
+*.fla binary
+*.swf binary
+
+# * Archives
+*.gz binary
+*.zip binary
+*.7z binary
+*.tar binary
+*.rar binary
+
+# * Fonts
+*.ttf binary
+*.otf binary
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8bba95f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,18 @@
+#
+# Files ignored by Git
+# ====================
+#
+
+# - All files and directories starting with "#"
+\#*
+
+# - NPM stuff
+/node_modules/
+/npm-debug.log
+
+# - Test coverage reports
+/coverage/
+
+# - Editor files
+*.sublime-project
+*.sublime-workspace
diff --git a/.jshintignore b/.jshintignore
new file mode 100644
index 0000000..e61051f
--- /dev/null
+++ b/.jshintignore
@@ -0,0 +1,2 @@
+/node_modules/
+/coverage/
diff --git a/.jshintrc b/.jshintrc
new file mode 100644
index 0000000..6cbd157
--- /dev/null
+++ b/.jshintrc
@@ -0,0 +1,69 @@
+{
+ /*
+ * ES6 additions
+ * =================
+ */
+
+ "esnext": true,
+
+ /*
+ * ENVIRONMENTS
+ * =================
+ */
+
+ // Define globals exposed by modern browsers.
+ "browser": true,
+
+ // Define globals exposed by jQuery.
+ "jquery": true,
+
+ // Define globals exposed by Node.js.
+ "node": true,
+
+ /*
+ * ENFORCING OPTIONS
+ * =================
+ */
+
+ // Force all variable names to use either camelCase style or UPPER_CASE
+ // with underscores.
+ "camelcase": true,
+
+ // Prohibit use of == and != in favor of === and !==.
+ "eqeqeq": true,
+
+ // Enforce tab width of 2 spaces.
+ "indent": 2,
+
+ // Prohibit use of a variable before it is defined.
+ "latedef": true,
+
+ // Enforce line length to 80 characters
+ "maxlen": 80,
+
+ // Require capitalized names for constructor functions.
+ "newcap": true,
+
+ // Enforce use of single quotation marks for strings.
+ "quotmark": "single",
+
+ // Enforce placing 'use strict' at the top function scope
+ // "strict": true,
+
+ // Prohibit use of explicitly undeclared variables.
+ "undef": true,
+
+ // Warn when variables are defined but never used.
+ "unused": true,
+
+ /*
+ * RELAXING OPTIONS
+ * =================
+ */
+
+ // Suppress warnings about == null comparisons.
+ "eqnull": true,
+
+ // Relax about comma-only lines. This is still valid airbnb style, BTW.
+ "laxcomma": true
+}
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..c603d10
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,14 @@
+branches:
+ only:
+ - master
+
+language:
+ node_js
+node_js:
+ - '0.12'
+ - '4.2'
+ - '5'
+ - '6'
+
+after_script:
+ npm run coveralls
diff --git a/License.md b/License.md
new file mode 100644
index 0000000..2125497
--- /dev/null
+++ b/License.md
@@ -0,0 +1,7 @@
+Copyright © 2015-2016 Studio B12 GmbH
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/Readme.md b/Readme.md
new file mode 100644
index 0000000..fc37f6f
--- /dev/null
+++ b/Readme.md
@@ -0,0 +1,90 @@
+[![Coveralls – test coverage
+](https://img.shields.io/coveralls/studio-b12/array-from.svg?style=flat-square
+)](https://coveralls.io/r/studio-b12/array-from
+) [![Travis – build status
+](https://img.shields.io/travis/studio-b12/array-from/master.svg?style=flat-square
+)](https://travis-ci.org/studio-b12/array-from
+) [![David – status of dependencies
+](https://img.shields.io/david/studio-b12/array-from.svg?style=flat-square
+)](https://david-dm.org/studio-b12/array-from
+) [![Code style: airbnb
+](https://img.shields.io/badge/code%20style-airbnb-777777.svg?style=flat-square
+)](https://github.com/airbnb/javascript)
+
+
+
+
+array-from
+==========
+
+**A ponyfill for the ES 2015 [`Array.from()`][].**
+
+*** Ponyfill**: A polyfill that doesn't overwrite the native method.
+*** ES 2015**: The new name for ES6 that [nobody expected][].
+
+Modeled after the final ES 2015 spec. Credits for the implementation go to the amazing folks of the MDN and the amazing guy [@barberboy](https://github.com/barberboy).
+
+
+
+[`Array.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from "Array.from()"
+[nobody expected]: http://webreflection.blogspot.de/2015/01/javascript-and-living-ecmascript.html "JavaScript and the living ECMAScript Standard"
+
+
+
+Installation
+------------
+
+```sh
+$ npm install array-from
+```
+
+
+
+
+Usage
+-----
+
+Recommended:
+
+```js
+var arrayFrom = require('array-from');
+ // You’ll get the native `Array.from` if it’s available.
+
+function () {console.log(
+ arrayFrom(arguments).map(require('1-liners/increment'))
+);}(1, 2, 3);
+//» [2, 3, 4]
+```
+
+You can also use it as a classical polyfill. It’s [not recommended][], but sometimes practical:
+
+```js
+if (!Array.from) Array.from = require('array-from');
+ // This will affect all loaded modules.
+
+function () {console.log(
+ Array.from(arguments).map(require('1-liners/increment'))
+);}(1, 2, 3);
+//» [2, 3, 4]
+```
+
+[not recommended]: https://github.com/sindresorhus/object-assign/issues/10#issuecomment-65065859 "Optionally shim native method?"
+
+
+
+
+Support note
+------------
+
+We support the _current_ and _active LTS_ release of Node.js. More info in [nodejs/LTS](https://github.com/nodejs/LTS#lts_schedule).
+
+
+
+
+License
+-------
+
+[MIT][] © [Studio B12 GmbH][]
+
+[MIT]: ./License.md
+[Studio B12 GmbH]: https://github.com/studio-b12
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..49e097c
--- /dev/null
+++ b/index.js
@@ -0,0 +1,4 @@
+module.exports = (typeof Array.from === 'function' ?
+ Array.from :
+ require('./polyfill')
+);
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..a489ec5
--- /dev/null
+++ b/package.json
@@ -0,0 +1,51 @@
+{
+ "name": "array-from",
+ "version": "2.1.1",
+ "description": "A ponyfill for the ES 2015 (ES6) `Array.from()`.",
+ "dependencies": {},
+ "scripts": {
+ "coveralls": "npm run coverage && cat ./coverage/lcov.info | coveralls",
+ "coverage": "istanbul cover test.js",
+ "develop": "nodangel --ignore node_modules --ignore coverage --exec 'npm run --silent test:lite'",
+ "test": "jshint . && npm run test:lite",
+ "test:lite": "node test.js | tap-spec",
+ "view-coverage": "echo 'Generating coverage reports...'; npm run coverage >/dev/null && echo '...done.' && xdg-open ./coverage/lcov-report/index.html >/dev/null"
+ },
+ "devDependencies": {
+ "1-liners": "0.2.2",
+ "core-js": "^1.0.0",
+ "coveralls": "2.11.2",
+ "istanbul": "0.3.14",
+ "jshint": "2.7.0",
+ "lodash.isnative": "^3.0.4",
+ "nodangel": "1.3.8",
+ "tap-spec": "2.2.2",
+ "tape": "4.2.2",
+ "tape-catch": "1.0.4"
+ },
+ "files": [
+ "/*.js",
+ "/Readme.md",
+ "/License.md"
+ ],
+ "license": "MIT",
+ "keywords": [
+ "Array.from",
+ "ponyfill",
+ "polyfill",
+ "convert",
+ "to",
+ "array",
+ "es-2015",
+ "es2015",
+ "es6"
+ ],
+ "contributors": [
+ "Tomek Wiszniewski <t.wiszniewski at gmail.com>",
+ "Benjamin Barber (http://github.com/barberboy)"
+ ],
+ "repository": {
+ "type": "git",
+ "url": "git at github.com:studio-b12/array-from.git"
+ }
+}
diff --git a/polyfill.js b/polyfill.js
new file mode 100644
index 0000000..51865f6
--- /dev/null
+++ b/polyfill.js
@@ -0,0 +1,212 @@
+// Production steps of ECMA-262, Edition 6, 22.1.2.1
+// Reference: http://www.ecma-international.org/ecma-262/6.0/#sec-array.from
+module.exports = (function() {
+ var isCallable = function(fn) {
+ return typeof fn === 'function';
+ };
+ var toInteger = function (value) {
+ var number = Number(value);
+ if (isNaN(number)) { return 0; }
+ if (number === 0 || !isFinite(number)) { return number; }
+ return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
+ };
+ var maxSafeInteger = Math.pow(2, 53) - 1;
+ var toLength = function (value) {
+ var len = toInteger(value);
+ return Math.min(Math.max(len, 0), maxSafeInteger);
+ };
+ var iteratorProp = function(value) {
+ if(value != null) {
+ if(['string','number','boolean','symbol'].indexOf(typeof value) > -1){
+ return Symbol.iterator;
+ } else if (
+ (typeof Symbol !== 'undefined') &&
+ ('iterator' in Symbol) &&
+ (Symbol.iterator in value)
+ ) {
+ return Symbol.iterator;
+ }
+ // Support "@@iterator" placeholder, Gecko 27 to Gecko 35
+ else if ('@@iterator' in value) {
+ return '@@iterator';
+ }
+ }
+ };
+ var getMethod = function(O, P) {
+ // Assert: IsPropertyKey(P) is true.
+ if (O != null && P != null) {
+ // Let func be GetV(O, P).
+ var func = O[P];
+ // ReturnIfAbrupt(func).
+ // If func is either undefined or null, return undefined.
+ if(func == null) {
+ return void 0;
+ }
+ // If IsCallable(func) is false, throw a TypeError exception.
+ if (!isCallable(func)) {
+ throw new TypeError(func + ' is not a function');
+ }
+ return func;
+ }
+ };
+ var iteratorStep = function(iterator) {
+ // Let result be IteratorNext(iterator).
+ // ReturnIfAbrupt(result).
+ var result = iterator.next();
+ // Let done be IteratorComplete(result).
+ // ReturnIfAbrupt(done).
+ var done = Boolean(result.done);
+ // If done is true, return false.
+ if(done) {
+ return false;
+ }
+ // Return result.
+ return result;
+ };
+
+ // The length property of the from method is 1.
+ return function from(items /*, mapFn, thisArg */ ) {
+ 'use strict';
+
+ // 1. Let C be the this value.
+ var C = this;
+
+ // 2. If mapfn is undefined, let mapping be false.
+ var mapFn = arguments.length > 1 ? arguments[1] : void 0;
+
+ var T;
+ if (typeof mapFn !== 'undefined') {
+ // 3. else
+ // a. If IsCallable(mapfn) is false, throw a TypeError exception.
+ if (!isCallable(mapFn)) {
+ throw new TypeError(
+ 'Array.from: when provided, the second argument must be a function'
+ );
+ }
+
+ // b. If thisArg was supplied, let T be thisArg; else let T
+ // be undefined.
+ if (arguments.length > 2) {
+ T = arguments[2];
+ }
+ // c. Let mapping be true (implied by mapFn)
+ }
+
+ var A, k;
+
+ // 4. Let usingIterator be GetMethod(items, @@iterator).
+ // 5. ReturnIfAbrupt(usingIterator).
+ var usingIterator = getMethod(items, iteratorProp(items));
+
+ // 6. If usingIterator is not undefined, then
+ if (usingIterator !== void 0) {
+ // a. If IsConstructor(C) is true, then
+ // i. Let A be the result of calling the [[Construct]]
+ // internal method of C with an empty argument list.
+ // b. Else,
+ // i. Let A be the result of the abstract operation ArrayCreate
+ // with argument 0.
+ // c. ReturnIfAbrupt(A).
+ A = isCallable(C) ? Object(new C()) : [];
+
+ // d. Let iterator be GetIterator(items, usingIterator).
+ var iterator = usingIterator.call(items);
+
+ // e. ReturnIfAbrupt(iterator).
+ if (iterator == null) {
+ throw new TypeError(
+ 'Array.from requires an array-like or iterable object'
+ );
+ }
+
+ // f. Let k be 0.
+ k = 0;
+
+ // g. Repeat
+ var next, nextValue;
+ while (true) {
+ // i. Let Pk be ToString(k).
+ // ii. Let next be IteratorStep(iterator).
+ // iii. ReturnIfAbrupt(next).
+ next = iteratorStep(iterator);
+
+ // iv. If next is false, then
+ if (!next) {
+
+ // 1. Let setStatus be Set(A, "length", k, true).
+ // 2. ReturnIfAbrupt(setStatus).
+ A.length = k;
+
+ // 3. Return A.
+ return A;
+ }
+ // v. Let nextValue be IteratorValue(next).
+ // vi. ReturnIfAbrupt(nextValue)
+ nextValue = next.value;
+
+ // vii. If mapping is true, then
+ // 1. Let mappedValue be Call(mapfn, T, «nextValue, k»).
+ // 2. If mappedValue is an abrupt completion, return
+ // IteratorClose(iterator, mappedValue).
+ // 3. Let mappedValue be mappedValue.[[value]].
+ // viii. Else, let mappedValue be nextValue.
+ // ix. Let defineStatus be the result of
+ // CreateDataPropertyOrThrow(A, Pk, mappedValue).
+ // x. [TODO] If defineStatus is an abrupt completion, return
+ // IteratorClose(iterator, defineStatus).
+ if (mapFn) {
+ A[k] = mapFn.call(T, nextValue, k);
+ }
+ else {
+ A[k] = nextValue;
+ }
+ // xi. Increase k by 1.
+ k++;
+ }
+ // 7. Assert: items is not an Iterable so assume it is
+ // an array-like object.
+ } else {
+
+ // 8. Let arrayLike be ToObject(items).
+ var arrayLike = Object(items);
+
+ // 9. ReturnIfAbrupt(items).
+ if (items == null) {
+ throw new TypeError(
+ 'Array.from requires an array-like object - not null or undefined'
+ );
+ }
+
+ // 10. Let len be ToLength(Get(arrayLike, "length")).
+ // 11. ReturnIfAbrupt(len).
+ var len = toLength(arrayLike.length);
+
+ // 12. If IsConstructor(C) is true, then
+ // a. Let A be Construct(C, «len»).
+ // 13. Else
+ // a. Let A be ArrayCreate(len).
+ // 14. ReturnIfAbrupt(A).
+ A = isCallable(C) ? Object(new C(len)) : new Array(len);
+
+ // 15. Let k be 0.
+ k = 0;
+ // 16. Repeat, while k < len… (also steps a - h)
+ var kValue;
+ while (k < len) {
+ kValue = arrayLike[k];
+ if (mapFn) {
+ A[k] = mapFn.call(T, kValue, k);
+ }
+ else {
+ A[k] = kValue;
+ }
+ k++;
+ }
+ // 17. Let setStatus be Set(A, "length", len, true).
+ // 18. ReturnIfAbrupt(setStatus).
+ A.length = len;
+ // 19. Return A.
+ }
+ return A;
+ };
+})();
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..81ba168
--- /dev/null
+++ b/test.js
@@ -0,0 +1,275 @@
+var test = require('tape-catch');
+var plus = require('1-liners/plus');
+var isNative = require('lodash.isnative');
+// Shim Symbol.iterator if it's not available
+require('core-js/es6/symbol');
+
+var arrayFrom = require('./polyfill');
+
+test('Works as expected', function(is) {
+ var mock = {
+ 0: 'a',
+ 1: 'b',
+ 2: 'c',
+ length: 3,
+ };
+
+ is.deepEqual(
+ arrayFrom(mock),
+ ['a', 'b', 'c'],
+ 'with a mock object'
+ );
+
+ is.ok(
+ arrayFrom(mock) instanceof Array,
+ '– returning an array'
+ );
+
+ is.deepEqual(
+ arrayFrom({
+ 0: 'a',
+ 1: 'b',
+ 2: 'c',
+ 'a': 'left out',
+ '-1': 'left out',
+ length: 3,
+ }),
+ ['a', 'b', 'c'],
+ '– ignoring illegal indices'
+ );
+
+ is.deepEqual(
+ arrayFrom({}),
+ [],
+ 'with an empty object'
+ );
+
+ is.deepEqual(
+ arrayFrom([]),
+ [],
+ 'with an empty array'
+ );
+
+ is.deepEqual(
+ (function() {return arrayFrom(arguments);})('a', 'b', 'c'),
+ ['a', 'b', 'c'],
+ 'with the `arguments` object'
+ );
+
+ is.deepEqual(
+ arrayFrom(['a', 'b', 'c']),
+ ['a', 'b', 'c'],
+ 'with an array'
+ );
+
+ is.deepEqual(
+ arrayFrom(mock, plus),
+ ['a0', 'b1', 'c2'],
+ 'when dealing with `mapFn`'
+ );
+
+ var context = {suffix: '+'};
+ is.deepEqual(
+ arrayFrom(mock,
+ function(item) {return (item + this.suffix);},
+ context
+ ),
+ ['a+', 'b+', 'c+'],
+ 'when dealing with `mapFn` and `thisArg`'
+ );
+
+ var Transferable = function(){};
+ Transferable.from = arrayFrom;
+
+ is.ok(
+ Transferable.from([1]) instanceof Transferable,
+ 'can be transferred to other constructor functions'
+ );
+
+ is.end();
+});
+
+test('Works for iterable objects', function(is) {
+
+ var SetPolyfill = require('core-js/library/fn/set');
+
+ is.deepEqual(
+ arrayFrom(new SetPolyfill(['a', 'b', 'c'])),
+ ['a', 'b', 'c'],
+ 'with Set (polyfill)'
+ );
+
+ is.deepEqual(
+ arrayFrom(new SetPolyfill(['a', 'b', 'c']).values(), plus),
+ ['a0', 'b1', 'c2'],
+ 'when dealing with `mapFn`'
+ );
+
+ var context = {suffix: '+'};
+ is.deepEqual(
+ arrayFrom(new SetPolyfill(['a', 'b', 'c']).keys(),
+ function(item) {return (item + this.suffix);},
+ context
+ ),
+ ['a+', 'b+', 'c+'],
+ 'when dealing with `mapFn` and `thisArg`'
+ );
+
+ if(typeof Set !== 'undefined' && isNative(Set)) {
+ is.deepEqual(
+ arrayFrom(new Set(['a', 'b', 'c'])),
+ ['a', 'b', 'c'],
+ 'with native Set'
+ );
+ }
+
+ if(typeof Map !== 'undefined' && isNative(Map)) {
+ is.deepEqual(
+ arrayFrom(new Map()
+ .set('key1', 'value1')
+ .set('key2', 'value2')
+ .set('key3', 'value3')
+ .keys()
+ ),
+ ['key1', 'key2', 'key3'],
+ 'with native Map'
+ );
+ }
+
+ var geckoIterator = {
+ value : 1,
+ '@@iterator' : function(){
+ var hasValue = true;
+ var value = this.value;
+ return {
+ next: function(){
+ if(hasValue) {
+ hasValue = false;
+ return { value: value, done: false };
+ } else {
+ return { done: true };
+ }
+ }
+ };
+ }
+ };
+
+ is.deepEqual(
+ arrayFrom(geckoIterator),
+ [1],
+ 'when using Gecko-based "@@iterator" property.'
+ );
+
+ geckoIterator['@@iterator'] = null;
+ is.deepEqual(
+ arrayFrom(geckoIterator),
+ [],
+ 'null iterator is like no iterator');
+
+ var Transferable = function(){};
+ Transferable.from = arrayFrom;
+
+ is.ok(Transferable.from(new SetPolyfill(['a'])) instanceof Transferable,
+ 'can be transferred to other constructor functions (iterable)'
+ );
+
+ is.end();
+});
+
+test('Throws when things go very wrong.', function(is) {
+ is.throws(
+ function() {
+ arrayFrom();
+ },
+ TypeError,
+ 'when the given object is invalid'
+ );
+
+ is.throws(
+ function() {
+ arrayFrom({length: 0}, /invalid/);
+ },
+ TypeError,
+ 'when `mapFn` is invalid'
+ );
+
+ var invalidIterator = {};
+ invalidIterator[Symbol.iterator] = {};
+
+ is.throws(
+ function() {
+ arrayFrom(invalidIterator);
+ },
+ TypeError,
+ 'when an iterable has an invalid iterator property'
+ );
+
+ var noIterator = {};
+ noIterator[Symbol.iterator] = function(){};
+
+ is.throws(
+ function() {
+ arrayFrom(noIterator);
+ },
+ TypeError,
+ '– no iterator returned');
+
+ var noNext = {};
+ noNext[Symbol.iterator] = function(){ return {}; };
+
+ is.throws(
+ function() {
+ arrayFrom(noNext);
+ },
+ TypeError,
+ '– no `next` function'
+ );
+
+ is.end();
+});
+
+test('Works for non-objects', function(is) {
+ is.deepEqual(
+ arrayFrom('a'),
+ ['a'],
+ 'string'
+ );
+
+ is.deepEqual(
+ arrayFrom('👺'),
+ ['👺'],
+ 'string(emoji)'
+ );
+
+ is.deepEqual(
+ arrayFrom('abc'),
+ ['a', 'b', 'c'],
+ 'string'
+ );
+
+ is.deepEqual(
+ arrayFrom('👺🍣🍻'),
+ ['👺', '🍣', '🍻'],
+ 'string(emoji)'
+ );
+
+ is.deepEqual(
+ arrayFrom(true),
+ [],
+ 'boolean'
+ );
+
+ is.deepEqual(
+ arrayFrom(1),
+ [],
+ 'number'
+ );
+
+ is.deepEqual(
+ arrayFrom(Symbol()),
+ [],
+ 'symbol'
+ );
+
+ is.end();
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-array-from.git
More information about the Pkg-javascript-commits
mailing list