UNPKG

2.32 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.toArrayChildren = toArrayChildren;
9exports.findChildInChildrenByKey = findChildInChildrenByKey;
10exports.mergeChildren = mergeChildren;
11exports.transformArguments = transformArguments;
12exports.windowIsUndefined = void 0;
13
14var _react = _interopRequireDefault(require("react"));
15
16var windowIsUndefined = !(typeof window !== 'undefined' && window.document && window.document.createElement);
17exports.windowIsUndefined = windowIsUndefined;
18
19function toArrayChildren(children) {
20 var ret = [];
21
22 _react.default.Children.forEach(children, function (c) {
23 ret.push(c);
24 });
25
26 return ret;
27}
28
29function findChildInChildrenByKey(children, key) {
30 var ret = null;
31
32 if (children) {
33 children.forEach(function (c) {
34 if (ret || !c) {
35 return;
36 }
37
38 if (c.key === key) {
39 ret = c;
40 }
41 });
42 }
43
44 return ret;
45}
46
47function mergeChildren(prev, next) {
48 var ret = []; // For each key of `next`, the list of keys to insert before that key in
49 // the combined list
50
51 var nextChildrenPending = {};
52 var pendingChildren = [];
53 var followChildrenKey;
54 prev.forEach(function (c) {
55 if (!c) {
56 return;
57 }
58
59 if (findChildInChildrenByKey(next, c.key)) {
60 if (pendingChildren.length) {
61 nextChildrenPending[c.key] = pendingChildren;
62 pendingChildren = [];
63 }
64
65 followChildrenKey = c.key;
66 } else if (c.key) {
67 pendingChildren.push(c);
68 }
69 });
70
71 if (!followChildrenKey) {
72 ret = ret.concat(pendingChildren);
73 }
74
75 next.forEach(function (c) {
76 if (!c) {
77 return;
78 }
79
80 if (nextChildrenPending.hasOwnProperty(c.key)) {
81 ret = ret.concat(nextChildrenPending[c.key]);
82 }
83
84 ret.push(c);
85
86 if (c.key === followChildrenKey) {
87 ret = ret.concat(pendingChildren);
88 }
89 });
90 return ret;
91}
92
93function transformArguments(arg, key, i) {
94 var result;
95
96 if (typeof arg === 'function') {
97 result = arg({
98 key: key,
99 index: i
100 });
101 } else {
102 result = arg;
103 }
104
105 if (Array.isArray(result)) {
106 if (result.length === 2) {
107 return result;
108 }
109
110 return [result[0], result[0]];
111 }
112
113 return [result, result];
114}
\No newline at end of file