2015-06-04 17:27:51 +00:00
|
|
|
/**
|
2016-05-26 19:22:30 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-06-04 17:27:51 +00:00
|
|
|
*
|
|
|
|
* @providesModule mapWithSeparator
|
2016-05-26 19:22:30 +00:00
|
|
|
* @flow
|
2015-06-04 17:27:51 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-05-26 19:22:30 +00:00
|
|
|
function mapWithSeparator<TFrom, TTo>(
|
|
|
|
items: Array<TFrom>,
|
|
|
|
itemRenderer: (item: TFrom, index: number, items: Array<TFrom>) => TTo,
|
|
|
|
spacerRenderer: (index: number) => TTo,
|
|
|
|
): Array<TTo> {
|
|
|
|
const mapped = [];
|
|
|
|
if (items.length > 0) {
|
|
|
|
mapped.push(itemRenderer(items[0], 0, items));
|
|
|
|
for (let ii = 1; ii < items.length; ii++) {
|
|
|
|
mapped.push(spacerRenderer(ii - 1), itemRenderer(items[ii], ii, items));
|
2015-06-04 17:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-26 19:22:30 +00:00
|
|
|
return mapped;
|
2015-06-04 17:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = mapWithSeparator;
|