mapWithSeparator.js 719 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow strict
  9. */
  10. 'use strict';
  11. function mapWithSeparator<TFrom, TTo>(
  12. items: Array<TFrom>,
  13. itemRenderer: (item: TFrom, index: number, items: Array<TFrom>) => TTo,
  14. spacerRenderer: (index: number) => TTo,
  15. ): Array<TTo> {
  16. const mapped = [];
  17. if (items.length > 0) {
  18. mapped.push(itemRenderer(items[0], 0, items));
  19. for (let ii = 1; ii < items.length; ii++) {
  20. mapped.push(spacerRenderer(ii - 1), itemRenderer(items[ii], ii, items));
  21. }
  22. }
  23. return mapped;
  24. }
  25. module.exports = mapWithSeparator;