1 | import { toChildArray } from 'preact';
|
2 |
|
3 | const mapFn = (children, fn) => {
|
4 | if (children == null) return null;
|
5 | return toChildArray(toChildArray(children).map(fn));
|
6 | };
|
7 |
|
8 |
|
9 | export const Children = {
|
10 | map: mapFn,
|
11 | forEach: mapFn,
|
12 | count(children) {
|
13 | return children ? toChildArray(children).length : 0;
|
14 | },
|
15 | only(children) {
|
16 | const normalized = toChildArray(children);
|
17 | if (normalized.length !== 1) throw 'Children.only';
|
18 | return normalized[0];
|
19 | },
|
20 | toArray: toChildArray
|
21 | };
|