]> git.r.bdr.sh - rbdr/dotfiles/blob
a90ba2f9730c548970f71d5722ba811d8527706f
[rbdr/dotfiles] /
1 'use strict';
2
3 var iteratorSymbol = require('es6-symbol').iterator
4 , isArguments = require('../../function/is-arguments')
5 , isFunction = require('../../function/is-function')
6 , toPosInt = require('../../number/to-pos-integer')
7 , callable = require('../../object/valid-callable')
8 , validValue = require('../../object/valid-value')
9 , isString = require('../../string/is-string')
10
11 , isArray = Array.isArray, call = Function.prototype.call
12 , desc = { configurable: true, enumerable: true, writable: true, value: null }
13 , defineProperty = Object.defineProperty;
14
15 module.exports = function (arrayLike/*, mapFn, thisArg*/) {
16 var mapFn = arguments[1], thisArg = arguments[2], Constructor, i, j, arr, l, code, iterator
17 , result, getIterator, value;
18
19 arrayLike = Object(validValue(arrayLike));
20
21 if (mapFn != null) callable(mapFn);
22 if (!this || (this === Array) || !isFunction(this)) {
23 // Result: Plain array
24 if (!mapFn) {
25 if (isArguments(arrayLike)) {
26 // Source: Arguments
27 l = arrayLike.length;
28 if (l !== 1) return Array.apply(null, arrayLike);
29 arr = new Array(1);
30 arr[0] = arrayLike[0];
31 return arr;
32 }
33 if (isArray(arrayLike)) {
34 // Source: Array
35 arr = new Array(l = arrayLike.length);
36 for (i = 0; i < l; ++i) arr[i] = arrayLike[i];
37 return arr;
38 }
39 }
40 arr = [];
41 } else {
42 // Result: Non plain array
43 Constructor = this;
44 }
45
46 if (!isArray(arrayLike)) {
47 if ((getIterator = arrayLike[iteratorSymbol]) !== undefined) {
48 // Source: Iterator
49 iterator = callable(getIterator).call(arrayLike);
50 if (Constructor) arr = new Constructor();
51 result = iterator.next();
52 i = 0;
53 while (!result.done) {
54 value = mapFn ? call.call(mapFn, thisArg, result.value, i) : result.value;
55 if (!Constructor) {
56 arr[i] = value;
57 } else {
58 desc.value = value;
59 defineProperty(arr, i, desc);
60 }
61 result = iterator.next();
62 ++i;
63 }
64 l = i;
65 } else if (isString(arrayLike)) {
66 // Source: String
67 l = arrayLike.length;
68 if (Constructor) arr = new Constructor();
69 for (i = 0, j = 0; i < l; ++i) {
70 value = arrayLike[i];
71 if ((i + 1) < l) {
72 code = value.charCodeAt(0);
73 if ((code >= 0xD800) && (code <= 0xDBFF)) value += arrayLike[++i];
74 }
75 value = mapFn ? call.call(mapFn, thisArg, value, j) : value;
76 if (!Constructor) {
77 arr[j] = value;
78 } else {
79 desc.value = value;
80 defineProperty(arr, j, desc);
81 }
82 ++j;
83 }
84 l = j;
85 }
86 }
87 if (l === undefined) {
88 // Source: array or array-like
89 l = toPosInt(arrayLike.length);
90 if (Constructor) arr = new Constructor(l);
91 for (i = 0; i < l; ++i) {
92 value = mapFn ? call.call(mapFn, thisArg, arrayLike[i], i) : arrayLike[i];
93 if (!Constructor) {
94 arr[i] = value;
95 } else {
96 desc.value = value;
97 defineProperty(arr, i, desc);
98 }
99 }
100 }
101 if (Constructor) {
102 desc.value = null;
103 arr.length = l;
104 }
105 return arr;
106 };