Files
flipper/desktop/flipper-plugin/src/utils/textContent.tsx
Andres Suarez 79023ee190 Update copyright headers from Facebook to Meta
Reviewed By: bhamodi

Differential Revision: D33331422

fbshipit-source-id: 016e8dcc0c0c7f1fc353a348b54fda0d5e2ddc01
2021-12-27 14:31:45 -08:00

48 lines
1.3 KiB
TypeScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {ReactNode, ReactElement} from 'react';
function isReactElement(object: any) {
return (
typeof object === 'object' &&
object !== null &&
object.$$typeof === Symbol.for('react.element')
);
}
/**
* Recursively walks through all children of a React element and returns
* the string representation of the leafs concatenated.
*/
export function textContent(node: ReactNode): string {
let res = '';
const traverse = (node: ReactNode) => {
if (typeof node === 'string' || typeof node === 'number') {
// this is a leaf, add it to the result string
res += node;
} else if (Array.isArray(node)) {
// traverse all array members and recursively stringify them
node.forEach(traverse);
} else if (isReactElement(node)) {
// node is a react element access its children an recursively stringify them
const {children} = (node as ReactElement).props;
if (Array.isArray(children)) {
children.forEach(traverse);
} else {
traverse(children);
}
}
};
traverse(node);
return res;
}