safeArrayFrom.js.flow 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // @flow strict
  2. import { SYMBOL_ITERATOR } from '../polyfills/symbols';
  3. /**
  4. * Safer version of `Array.from` that return `null` if value isn't convertible to array.
  5. * Also protects against Array-like objects without items.
  6. *
  7. * @example
  8. *
  9. * safeArrayFrom([ 1, 2, 3 ]) // [1, 2, 3]
  10. * safeArrayFrom('ABC') // null
  11. * safeArrayFrom({ length: 1 }) // null
  12. * safeArrayFrom({ length: 1, 0: 'Alpha' }) // ['Alpha']
  13. * safeArrayFrom({ key: 'value' }) // null
  14. * safeArrayFrom(new Map()) // []
  15. *
  16. */
  17. export default function safeArrayFrom<T>(
  18. collection: mixed,
  19. mapFn: (elem: mixed, index: number) => T = (item) => ((item: any): T),
  20. ): Array<T> | null {
  21. if (collection == null || typeof collection !== 'object') {
  22. return null;
  23. }
  24. if (Array.isArray(collection)) {
  25. return collection.map(mapFn);
  26. }
  27. // Is Iterable?
  28. const iteratorMethod = collection[SYMBOL_ITERATOR];
  29. if (typeof iteratorMethod === 'function') {
  30. // $FlowFixMe[incompatible-use]
  31. const iterator = iteratorMethod.call(collection);
  32. const result = [];
  33. let step;
  34. for (let i = 0; !(step = iterator.next()).done; ++i) {
  35. result.push(mapFn(step.value, i));
  36. }
  37. return result;
  38. }
  39. // Is Array like?
  40. const length = collection.length;
  41. if (typeof length === 'number' && length >= 0 && length % 1 === 0) {
  42. const result = [];
  43. for (let i = 0; i < length; ++i) {
  44. if (!Object.prototype.hasOwnProperty.call(collection, i)) {
  45. return null;
  46. }
  47. result.push(mapFn(collection[String(i)], i));
  48. }
  49. return result;
  50. }
  51. return null;
  52. }