UNPKG

1.86 kBJavaScriptView Raw
1import React from 'react';
2export var windowIsUndefined = !(typeof window !== 'undefined' && window.document && window.document.createElement);
3export function toArrayChildren(children) {
4 var ret = [];
5 React.Children.forEach(children, function (c) {
6 ret.push(c);
7 });
8 return ret;
9}
10export function findChildInChildrenByKey(children, key) {
11 var ret = null;
12
13 if (children) {
14 children.forEach(function (c) {
15 if (ret || !c) {
16 return;
17 }
18
19 if (c.key === key) {
20 ret = c;
21 }
22 });
23 }
24
25 return ret;
26}
27export function mergeChildren(prev, next) {
28 var ret = []; // For each key of `next`, the list of keys to insert before that key in
29 // the combined list
30
31 var nextChildrenPending = {};
32 var pendingChildren = [];
33 var followChildrenKey;
34 prev.forEach(function (c) {
35 if (!c) {
36 return;
37 }
38
39 if (findChildInChildrenByKey(next, c.key)) {
40 if (pendingChildren.length) {
41 nextChildrenPending[c.key] = pendingChildren;
42 pendingChildren = [];
43 }
44
45 followChildrenKey = c.key;
46 } else if (c.key) {
47 pendingChildren.push(c);
48 }
49 });
50
51 if (!followChildrenKey) {
52 ret = ret.concat(pendingChildren);
53 }
54
55 next.forEach(function (c) {
56 if (!c) {
57 return;
58 }
59
60 if (nextChildrenPending.hasOwnProperty(c.key)) {
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 if (Array.isArray(result)) {
85 if (result.length === 2) {
86 return result;
87 }
88
89 return [result[0], result[0]];
90 }
91
92 return [result, result];
93}
\No newline at end of file