2018-12-22 03:10:49 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { Iterable } from 'immutable';
|
|
|
|
import isIterable from '../utils/isIterable';
|
|
|
|
|
|
|
|
const IS_IMMUTABLE_KEY = '@@__IS_IMMUTABLE__@@';
|
|
|
|
|
|
|
|
function isImmutable(value) {
|
2019-01-10 21:51:14 +03:00
|
|
|
return (
|
|
|
|
Iterable.isKeyed(value) ||
|
|
|
|
Iterable.isIndexed(value) ||
|
|
|
|
Iterable.isIterable(value)
|
|
|
|
);
|
2018-12-22 03:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getShortTypeString(val, diff) {
|
|
|
|
if (diff && Array.isArray(val)) {
|
|
|
|
val = val[val.length === 2 ? 1 : 0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isIterable(val) && !isImmutable(val)) {
|
|
|
|
return '(…)';
|
|
|
|
} else if (Array.isArray(val)) {
|
|
|
|
return val.length > 0 ? '[…]' : '[]';
|
|
|
|
} else if (val === null) {
|
|
|
|
return 'null';
|
|
|
|
} else if (val === undefined) {
|
|
|
|
return 'undef';
|
|
|
|
} else if (typeof val === 'object') {
|
|
|
|
return Object.keys(val).length > 0 ? '{…}' : '{}';
|
|
|
|
} else if (typeof val === 'function') {
|
|
|
|
return 'fn';
|
|
|
|
} else if (typeof val === 'string') {
|
2019-01-10 21:51:14 +03:00
|
|
|
return `"${val.substr(0, 10) + (val.length > 10 ? '…' : '')}"`;
|
2018-12-22 03:10:49 +03:00
|
|
|
} else if (typeof val === 'symbol') {
|
2019-01-10 21:51:14 +03:00
|
|
|
return 'symbol';
|
2018-12-22 03:10:49 +03:00
|
|
|
} else {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getText(type, data, isWideLayout, isDiff) {
|
|
|
|
if (type === 'Object') {
|
|
|
|
const keys = Object.keys(data);
|
|
|
|
if (!isWideLayout) return keys.length ? '{…}' : '{}';
|
|
|
|
|
|
|
|
const str = keys
|
|
|
|
.slice(0, 3)
|
2020-08-08 23:26:39 +03:00
|
|
|
.map((key) => `${key}: ${getShortTypeString(data[key], isDiff)}`)
|
2018-12-22 03:10:49 +03:00
|
|
|
.concat(keys.length > 3 ? ['…'] : [])
|
|
|
|
.join(', ');
|
|
|
|
|
|
|
|
return `{ ${str} }`;
|
|
|
|
} else if (type === 'Array') {
|
|
|
|
if (!isWideLayout) return data.length ? '[…]' : '[]';
|
|
|
|
|
|
|
|
const str = data
|
|
|
|
.slice(0, 4)
|
2020-08-08 23:26:39 +03:00
|
|
|
.map((val) => getShortTypeString(val, isDiff))
|
2019-01-10 21:51:14 +03:00
|
|
|
.concat(data.length > 4 ? ['…'] : [])
|
|
|
|
.join(', ');
|
2018-12-22 03:10:49 +03:00
|
|
|
|
|
|
|
return `[${str}]`;
|
|
|
|
} else {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 21:51:14 +03:00
|
|
|
const getItemString = (
|
|
|
|
styling,
|
|
|
|
type,
|
|
|
|
data,
|
|
|
|
dataTypeKey,
|
|
|
|
isWideLayout,
|
|
|
|
isDiff
|
|
|
|
) => (
|
|
|
|
<span {...styling('treeItemHint')}>
|
2018-12-22 03:10:49 +03:00
|
|
|
{data[IS_IMMUTABLE_KEY] ? 'Immutable' : ''}
|
|
|
|
{dataTypeKey && data[dataTypeKey] ? data[dataTypeKey] + ' ' : ''}
|
|
|
|
{getText(type, data, isWideLayout, isDiff)}
|
2019-01-10 21:51:14 +03:00
|
|
|
</span>
|
|
|
|
);
|
2018-12-22 03:10:49 +03:00
|
|
|
|
|
|
|
export default getItemString;
|