/
sdds
/
plasma
Обзор
Документация
Войти
/
sdds
/
plasma
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
dev
packages/plasma-core/src/utils/extractTextFrom.ts
21 строка
736 B
Vasiliy Loginevskiy
fix(plasma-core): improve type-coverage
24 ноя 2022, 16:49
24 ноя 2022, 16:49
b7ade6d
Код
Авторство
О чём код?
import { isValidElement, Children, ReactChild } from 'react'; import type { ReactNode } from 'react'; export const extractTextFrom = (textSource?: string | number | null | ReactNode): string => { switch (typeof textSource) { case 'string': return textSource; case 'number': return textSource.toString(); case 'object': { if (!isValidElement(textSource) || !textSource.props || !textSource.props.children) { return ''; } return Children.map<string, ReactChild>(textSource.props.children, (child) => { return extractTextFrom(child); }).join(''); } default: return ''; } };