React IO 描述
仅在测试环境执行并返回 IO 描述信息
// 获取IO描述
export function getIODescription(value: mixed): string {
if (!__DEV__) {
return '';
}
try {
switch (typeof value) {
case 'function':
return value.name || '';
case 'object':
// Test the object for a bunch of common property names that are useful identifiers.
// While we only have the return value here, it should ideally be a name that
// describes the arguments requested.
//
// 测试对象中一系列常用的属性名,这些属性名是有用的标识符。
// 虽然这里我们只有返回值,但理想情况下,它应该是一个能够描述所请求参数的名称。
if (value === null) {
return '';
} else if (value instanceof Error) {
return String(value.message);
} else if (typeof value.url === 'string') {
return value.url;
} else if (typeof value.href === 'string') {
return value.href;
} else if (typeof value.src === 'string') {
return value.src;
} else if (typeof value.currentSrc === 'string') {
return value.currentSrc;
} else if (typeof value.command === 'string') {
return value.command;
} else if (
typeof value.request === 'object' &&
value.request !== null &&
typeof value.request.url === 'string'
) {
return value.request.url;
} else if (
typeof value.response === 'object' &&
value.response !== null &&
typeof value.response.url === 'string'
) {
return value.response.url;
} else if (
typeof value.id === 'string' ||
typeof value.id === 'number' ||
typeof value.id === 'bigint'
) {
return String(value.id);
} else if (typeof value.name === 'string') {
return value.name;
} else {
const str = value.toString();
if (
str.startsWith('[object ') ||
str.length < 5 ||
str.length > 500
) {
// This is probably not a useful description.
// 这可能不是一个有用的描述。
return '';
}
return str;
}
case 'string':
if (value.length < 5 || value.length > 500) {
return '';
}
return value;
case 'number':
case 'bigint':
return String(value);
default:
// Not useful descriptors.
// 没有用的描述符。
return '';
}
} catch (x) {
return '';
}
}