UNPKG

554 BJavaScriptView Raw
1import { toChildArray } from 'preact';
2
3const mapFn = (children, fn) => {
4 if (children == null) return null;
5 return toChildArray(toChildArray(children).map(fn));
6};
7
8// This API is completely unnecessary for Preact, so it's basically passthrough.
9export 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};