1 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
2 |
|
3 | import { SYMBOL_ITERATOR } from "../polyfills/symbols.mjs";
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export default function safeArrayFrom(collection) {
|
20 | var mapFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (item) {
|
21 | return item;
|
22 | };
|
23 |
|
24 | if (collection == null || _typeof(collection) !== 'object') {
|
25 | return null;
|
26 | }
|
27 |
|
28 | if (Array.isArray(collection)) {
|
29 | return collection.map(mapFn);
|
30 | }
|
31 |
|
32 |
|
33 | var iteratorMethod = collection[SYMBOL_ITERATOR];
|
34 |
|
35 | if (typeof iteratorMethod === 'function') {
|
36 |
|
37 | var iterator = iteratorMethod.call(collection);
|
38 | var result = [];
|
39 | var step;
|
40 |
|
41 | for (var i = 0; !(step = iterator.next()).done; ++i) {
|
42 | result.push(mapFn(step.value, i));
|
43 | }
|
44 |
|
45 | return result;
|
46 | }
|
47 |
|
48 |
|
49 | var length = collection.length;
|
50 |
|
51 | if (typeof length === 'number' && length >= 0 && length % 1 === 0) {
|
52 | var _result = [];
|
53 |
|
54 | for (var _i = 0; _i < length; ++_i) {
|
55 | if (!Object.prototype.hasOwnProperty.call(collection, _i)) {
|
56 | return null;
|
57 | }
|
58 |
|
59 | _result.push(mapFn(collection[String(_i)], _i));
|
60 | }
|
61 |
|
62 | return _result;
|
63 | }
|
64 |
|
65 | return null;
|
66 | }
|