Projekt

Obecné

Profil

Stáhnout (1.89 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
var GetIntrinsic = require('../GetIntrinsic');
4

    
5
var $iterator = GetIntrinsic('%Symbol.iterator%', true);
6

    
7
var callBound = require('../helpers/callBound');
8

    
9
var $arrayJoin = callBound('Array.prototype.join');
10
var $arrayPush = callBound('Array.prototype.push');
11
var $stringSlice = callBound('String.prototype.slice');
12
var $stringSplit = callBound('String.prototype.split');
13

    
14
var AdvanceStringIndex = require('./AdvanceStringIndex');
15
var GetIterator = require('./GetIterator');
16
var GetMethod = require('./GetMethod');
17
var IsArray = require('./IsArray');
18
var IteratorStep = require('./IteratorStep');
19
var IteratorValue = require('./IteratorValue');
20
var ToObject = require('./ToObject');
21
var Type = require('./Type');
22

    
23
// https://www.ecma-international.org/ecma-262/7.0/#sec-iterabletoarraylike
24

    
25
module.exports = function IterableToArrayLike(items) {
26
	var usingIterator;
27
	if ($iterator) {
28
		usingIterator = GetMethod(items, $iterator);
29
	} else if (IsArray(items)) {
30
		usingIterator = function () {
31
			var i = -1;
32
			var arr = this; // eslint-disable-line no-invalid-this
33
			return {
34
				next: function () {
35
					i += 1;
36
					return {
37
						done: i >= arr.length,
38
						value: arr[i]
39
					};
40
				}
41
			};
42
		};
43
	} else if (Type(items) === 'String') {
44
		usingIterator = function () {
45
			var i = 0;
46
			return {
47
				next: function () {
48
					var nextIndex = AdvanceStringIndex(items, i, true);
49
					var value = $arrayJoin($stringSplit($stringSlice(items, i, nextIndex), ''), '');
50
					i = nextIndex;
51
					return {
52
						done: nextIndex > items.length,
53
						value: value
54
					};
55
				}
56
			};
57
		};
58
	}
59
	if (typeof usingIterator !== 'undefined') {
60
		var iterator = GetIterator(items, usingIterator);
61
		var values = [];
62
		var next = true;
63
		while (next) {
64
			next = IteratorStep(iterator);
65
			if (next) {
66
				var nextValue = IteratorValue(next);
67
				$arrayPush(values, nextValue);
68
			}
69
		}
70
		return values;
71
	}
72

    
73
	return ToObject(items);
74
};
(51-51/111)