Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule sliceChildren
10
*/
11
12
"use strict";
13
14
var flattenChildren = require('flattenChildren');
15
16
/**
17
* Slice children that are typically specified as `props.children`. This version
18
* of slice children ignores empty child components.
19
*
20
* @param {*} children The children set to filter.
21
* @param {number} start The first zero-based index to include in the subset.
22
* @param {?number} end The non-inclusive last index of the subset.
23
* @return {object} mirrored array with mapped children
24
*/
25
function sliceChildren(children, start, end) {
26
if (children == null) {
27
return children;
28
}
29
30
var slicedChildren = {};
31
var flattenedMap = flattenChildren(children);
32
var ii = 0;
33
for (var key in flattenedMap) {
34
if (!flattenedMap.hasOwnProperty(key)) {
35
continue;
36
}
37
var child = flattenedMap[key];
38
if (ii >= start) {
39
slicedChildren[key] = child;
40
}
41
ii++;
42
if (end != null && ii >= end) {
43
break;
44
}
45
}
46
return slicedChildren;
47
}
48
49
module.exports = sliceChildren;
50
51