UNPKG

1.96 kBJavaScriptView Raw
1function _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
3import { SYMBOL_ITERATOR } from "../polyfills/symbols.mjs";
4/**
5 * Safer version of `Array.from` that return `null` if value isn't convertible to array.
6 * Also protects against Array-like objects without items.
7 *
8 * @example
9 *
10 * safeArrayFrom([ 1, 2, 3 ]) // [1, 2, 3]
11 * safeArrayFrom('ABC') // null
12 * safeArrayFrom({ length: 1 }) // null
13 * safeArrayFrom({ length: 1, 0: 'Alpha' }) // ['Alpha']
14 * safeArrayFrom({ key: 'value' }) // null
15 * safeArrayFrom(new Map()) // []
16 *
17 */
18
19export 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 } // Is Iterable?
31
32
33 var iteratorMethod = collection[SYMBOL_ITERATOR];
34
35 if (typeof iteratorMethod === 'function') {
36 // $FlowFixMe[incompatible-use]
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 } // Is Array like?
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}