• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alessiofrittoli / react-api / 15070927793

16 May 2025 02:34PM UTC coverage: 93.103% (-6.9%) from 100.0%
15070927793

Pull #8

github

alessiofrittoli
feat: improved `childrenFn`

it's now possible to render an array of children
Pull Request #8: feature/childrenFn

11 of 12 branches covered (91.67%)

Branch coverage included in aggregate %.

5 of 6 new or added lines in 1 file covered. (83.33%)

16 of 17 relevant lines covered (94.12%)

3.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.48
/src/utils.ts
1
import { isValidElement } from 'react'
1✔
2

3
/**
4
 * Check if the given `input` is a React ComponentType.
5
 * 
6
 * @template P The props the component accepts.
7
 * 
8
 * @param input The input to check.
9
 * @returns `true` if the given input is a React ComponentType, `false` otherwise.
10
 */
11
export const isComponentType = <
1✔
12
        P = unknown
13
>( input: unknown ): input is React.ComponentType<P> => (
14
        typeof input === 'function'
7✔
15
)
16

17

18
/**
19
 * Check if the given `input` is a React Node.
20
 * 
21
 * @param input The input to check.
22
 * @returns `true` if the given input is a React Node, `false` otherwise.
23
 */
24
export const isReactNode = ( input: unknown ): input is React.ReactNode => (
1✔
25
        typeof input === 'string'
17✔
26
        || typeof input === 'number'
27
        || typeof input === 'boolean'
28
        || input === null
29
        || input === undefined
30
        || isValidElement( input )
31
        || ( Array.isArray( input ) && input.every( isReactNode ) )
32
)
33

34

35
/**
36
 * Represent a `React.ReactNode` or a callable function that returns a `React.ReactNode`.
37
 * 
38
 * @template T An Array defining optional arguments passed to the `children` function.
39
 */
40
export type FunctionChildren<T extends unknown[]> = (
41
        | React.ReactNode
42
        | ( ( ...args: T ) => React.ReactNode )
43
        | FunctionChildren<T>[]
44
)
45

46

47
/**
48
 * Render `children` which could be possible a callable function.
49
 * 
50
 * @template T The `children` type which extends the `FunctionChildren<U>` interface.
51
 * @template U An Array defining optional arguments passed to the `children` function.
52
 * 
53
 * @param        children The `children` to render.
54
 * @param        args (Optional) Arguments passed to `children` if is a function.
55
 * 
56
 * @returns The rendered `children`. If `children` is a function, the result of that function is returned.
57
 */
58
export const childrenFn = <T extends FunctionChildren<U>, U extends unknown[]>( children: T, ...args: U ): React.ReactNode => {
1✔
59
        
60
        if ( Array.isArray( children ) ) {
7!
NEW
61
                return children.map( children => childrenFn( children, ...args ) )
×
62
        }
63

64
        if ( typeof children === 'function' ) {
7✔
65
                return children( ...args )
2✔
66
        }
67

68
        return children
5✔
69

70
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc