Skip to content
Myr

children.ts

This file

Path

src/jsx/children.ts

20 lines · 695 bytes · TypeScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–20why, one step back
1import type { Child } from './base'
23export const toArray = (children: Child): Child[] =>4 Array.isArray(children) ? children : [children]5export const Children = {6 map: (children: Child[], fn: (child: Child, index: number) => Child): Child[] =>7 toArray(children).map(fn),8 forEach: (children: Child[], fn: (child: Child, index: number) => void): void => {9 toArray(children).forEach(fn)10 },11 count: (children: Child[]): number => toArray(children).length,12 only: (_children: Child[]): Child => {13 const children = toArray(_children)14 if (children.length !== 1) {15 throw new Error('Children.only() expects only one child')16 }17 return children[0]18 },19 toArray,20}