compactArray.js.flow 597 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * Copyright 2015-present Facebook. All Rights Reserved.
  3. *
  4. * @providesModule compactArray
  5. * @typechecks
  6. * @flow
  7. */
  8. 'use strict';
  9. /**
  10. * Returns a new Array containing all the element of the source array except
  11. * `null` and `undefined` ones. This brings the benefit of strong typing over
  12. * `Array.prototype.filter`.
  13. */
  14. function compactArray<T>(array: Array<T | null | void>): Array<T> {
  15. var result = [];
  16. for (var i = 0; i < array.length; ++i) {
  17. var elem = array[i];
  18. if (elem != null) {
  19. result.push(elem);
  20. }
  21. }
  22. return result;
  23. }
  24. module.exports = compactArray;