MCPcopy
hub / github.com/facebook/react / formatDataForPreview

Function formatDataForPreview

packages/react-devtools-shared/src/utils.js:949–1220  ·  view source on GitHub ↗
(
  data: any,
  showFormattedValue: boolean,
)

Source from the content-addressed store, hash-verified

947// Would show a preview of...
948// [123, "abc", Array(2), {…}]
949export function formatDataForPreview(
950 data: any,
951 showFormattedValue: boolean,
952): string {
953 if (data != null && hasOwnProperty.call(data, meta.type)) {
954 return showFormattedValue
955 ? data[meta.preview_long]
956 : data[meta.preview_short];
957 }
958
959 const type = getDataType(data);
960
961 switch (type) {
962 case 'html_element':
963 return `<${truncateForDisplay(data.tagName.toLowerCase())} />`;
964 case 'function':
965 if (typeof data.name === 'function' || data.name === '') {
966 return '() => {}';
967 }
968 return `${truncateForDisplay(data.name)}() {}`;
969 case 'string':
970 return `"${data}"`;
971 case 'bigint':
972 return truncateForDisplay(data.toString() + 'n');
973 case 'regexp':
974 return truncateForDisplay(data.toString());
975 case 'symbol':
976 return truncateForDisplay(data.toString());
977 case 'react_element':
978 return `<${truncateForDisplay(
979 getDisplayNameForReactElement(data) || 'Unknown',
980 )} />`;
981 case 'react_lazy':
982 // To avoid actually initialize a lazy to cause a side-effect we make some assumptions
983 // about the structure of the payload even though that's not really part of the contract.
984 // In practice, this is really just coming from React.lazy helper or Flight.
985 const payload = data._payload;
986 if (payload !== null && typeof payload === 'object') {
987 if (payload._status === 0) {
988 // React.lazy constructor pending
989 return `pending lazy()`;
990 }
991 if (payload._status === 1 && payload._result != null) {
992 // React.lazy constructor fulfilled
993 if (showFormattedValue) {
994 const formatted = formatDataForPreview(
995 payload._result.default,
996 false,
997 );
998 return `fulfilled lazy() {${truncateForDisplay(formatted)}}`;
999 } else {
1000 return `fulfilled lazy() {…}`;
1001 }
1002 }
1003 if (payload._status === 2) {
1004 // React.lazy constructor rejected
1005 if (showFormattedValue) {
1006 const formatted = formatDataForPreview(payload._result, false);

Callers 3

createDehydratedFunction · 0.90
dehydrateFunction · 0.90
getMetaValueLabelFunction · 0.90

Calls 7

getDataTypeFunction · 0.85
truncateForDisplayFunction · 0.85
isArrayFunction · 0.85
isPlainObjectFunction · 0.85
getAllEnumerableKeysFunction · 0.85
toStringMethod · 0.65

Tested by

no test coverage detected