UNPKG

1.76 kBJavaScriptView Raw
1import React from 'react';
2export function toArrayChildren(children) {
3 var ret = [];
4 React.Children.forEach(children, function (c) {
5 ret.push(c);
6 });
7 return ret;
8}
9export function findChildInChildrenByKey(children, key) {
10 var ret = null;
11
12 if (children) {
13 children.forEach(function (c) {
14 if (ret || !c) {
15 return;
16 }
17
18 if (c.key === key) {
19 ret = c;
20 }
21 });
22 }
23
24 return ret;
25}
26export function mergeChildren(prev, next) {
27 var ret = []; // For each key of `next`, the list of keys to insert before that key in
28 // the combined list
29
30 var nextChildrenPending = {};
31 var pendingChildren = [];
32 var followChildrenKey = null;
33 prev.forEach(function (c) {
34 if (!c) {
35 return;
36 }
37
38 if (c.key && findChildInChildrenByKey(next, c.key)) {
39 if (pendingChildren.length) {
40 nextChildrenPending[c.key] = pendingChildren;
41 pendingChildren = [];
42 }
43
44 followChildrenKey = c.key;
45 } else if (c.key) {
46 pendingChildren.push(c);
47 }
48 });
49
50 if (!followChildrenKey) {
51 ret = ret.concat(pendingChildren);
52 }
53
54 next.forEach(function (c) {
55 if (!c) {
56 return;
57 }
58
59 if (c.key && nextChildrenPending.hasOwnProperty(c.key)) {
60 // eslint-disable-line no-prototype-builtins
61 ret = ret.concat(nextChildrenPending[c.key]);
62 }
63
64 ret.push(c);
65
66 if (c.key === followChildrenKey) {
67 ret = ret.concat(pendingChildren);
68 }
69 });
70 return ret;
71}
72export function transformArguments(arg, key, i) {
73 var result;
74
75 if (typeof arg === 'function') {
76 result = arg({
77 key: key,
78 index: i
79 });
80 } else {
81 result = arg;
82 }
83
84 return result;
85}
86export function getChildrenFromProps(props) {
87 return props && props.children;
88}
\No newline at end of file